anchor.diff:    {$MODE DELPHI}
   {$ENDIF}
   
  -uses SysUtils, Variants;
  +uses Classes, SysUtils, Variants;
   
   const
     TheDayWhenTimeStarted = 730120; //01/01/2000 (starting from 01/01/01)
   
  +type
  +  TListSortCompare = function (Item1, Item2: Pointer): Integer of object; // cloned from LCLProc but with 'of object'
  +
   function GenerateRandomIdString : string; overload;
   function GenerateRandomIdString(aLength : integer): string; overload;
   
  
positive.code: function VarRecToVariant (AValue : TVarRec) : Variant;
  
  function CompareVariants (aVal1, aVal2 : variant) : integer;
  
  {$IFDEF FPC}
  function CharInSet(C: Char; const CharSet: TSysCharSet): Boolean;
  
negative.code:     Result := 1;
  end;
  
  // http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/mergesort.html
  procedure _MergeSort(List: TFPList; StartIndex, EndIndex: integer; const OnCompare: TListSortCompare);
  // sort so that for each i is OnCompare(List[i],List[i+1])<=0
  var
    MergeList: PPointer;
  
    procedure SmallSort(StartPos, EndPos: PtrInt);
    // use insertion sort for small lists
    var
      i: PtrInt;
      Best: PtrInt;
      j: PtrInt;
      Item: Pointer;
    begin
      for i:=StartPos to EndPos-1 do begin
        Best:=i;
        for j:=i+1 to EndPos do
          if OnCompare(List[Best],List[j])>0 then
            Best:=j;
        if Best>i then begin
          Item:=List[i];
          List[i]:=List[Best];
          List[Best]:=Item;
        end;
      end;
    end;
  
    procedure Merge(Pos1, Pos2, Pos3: PtrInt);
    // merge two sorted arrays
    // the first array ranges Pos1..Pos2-1, the second ranges Pos2..Pos3
    var Src1Pos,Src2Pos,DestPos,cmp,a:PtrInt;
    begin
      while (Pos3>=Pos2) and (OnCompare(List[Pos2-1],List[Pos3])<=0) do
        dec(Pos3);
      if (Pos1>=Pos2) or (Pos2>Pos3) then exit;
      Src1Pos:=Pos2-1;
      Src2Pos:=Pos3;
      DestPos:=Pos3;
      while (Src2Pos>=Pos2) and (Src1Pos>=Pos1) do begin
        cmp:=OnCompare(List[Src1Pos],List[Src2Pos]);
        if cmp>0 then begin
          MergeList[DestPos]:=List[Src1Pos];
          dec(Src1Pos);
        end else begin
          MergeList[DestPos]:=List[Src2Pos];
          dec(Src2Pos);
        end;
        dec(DestPos);
      end;
      while Src2Pos>=Pos2 do begin
        MergeList[DestPos]:=List[Src2Pos];
        dec(Src2Pos);
        dec(DestPos);
      end;
      for a:=DestPos+1 to Pos3 do
        List[a]:=MergeList[a];
    end;
  
    procedure Sort(StartPos, EndPos: PtrInt);
    // sort an interval in List. Use MergeList as work space.
    var
      mid: integer;
    begin
      if EndPos-StartPos<6 then begin
        SmallSort(StartPos,EndPos);
      end else begin
        mid:=(StartPos+EndPos) shr 1;
        Sort(StartPos,mid);
        Sort(mid+1,EndPos);
        Merge(StartPos,mid+1,EndPos);
      end;
    end;
  
  var
    Cnt: Integer;
  begin
    if (List=nil) then exit;
    Cnt:=List.Count;
    if StartIndex<0 then StartIndex:=0;
    if EndIndex>=Cnt then EndIndex:=Cnt-1;
    if StartIndex>=EndIndex then exit;
    MergeList:=GetMem(List.Count*SizeOf(Pointer));
    Sort(StartIndex,EndIndex);
    Freemem(MergeList);
  end;
  
  // http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/mergesort.html
  procedure MergeSort(List: TFPList; const OnCompare: TListSortCompare);
  begin
    if List=nil then exit;
    _MergeSort(List,0,List.Count-1,OnCompare);
  end;
  
  
  initialization
    Randomize;
  