unit dansistring_manager;

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

type
TAnsiStringManager = object
public
  procedure Init;
  procedure Done; inline;
  procedure Initialize(var S); inline;
  procedure Finalize(var S); inline;
  function Compare(const A, B: AnsiString): PtrInt; inline;
  function Assign(out Dst: AnsiString; const Src: AnsiString): Boolean; inline;
  function FirstOf(const S: AnsiString): PAnsiChar; inline;
  function LastOf(const S: AnsiString): PAnsiChar; inline;
  function LengthOf(const S: AnsiString): SizeUInt; inline;
end;

implementation

procedure TAnsiStringManager.Init;
begin
end;

procedure TAnsiStringManager.Done;
begin
end;

procedure TAnsiStringManager.Initialize(var S);
begin
  //Pointer(S) := nil;
  System.Initialize(AnsiString(S));
end;

procedure TAnsiStringManager.Finalize(var S);
begin
  System.Finalize(AnsiString(S));
  Pointer(S) := nil;
end;

function TAnsiStringManager.Compare(const A, B: AnsiString): PtrInt;
begin
  if Length(A) < Length(B) then begin
    Result := CompareByte(A[1], B[1], Length(A));
    if Result = 0 then
      Exit(-1);
    Exit;
  end else if Length(A) > Length(B) then begin
    Result := CompareByte(A[1], B[1], Length(B));
    if Result = 0 then
      Exit(1);
    Exit;
  end else
    Exit(CompareByte(A[1], B[1], Length(A)));
end;

function TAnsiStringManager.Assign(out Dst: AnsiString; const Src: AnsiString): Boolean;
begin
  Dst := Src;
  Exit(True);
end;

function TAnsiStringManager.FirstOf(const S: AnsiString): PAnsiChar;
begin
  Exit(@S[1]);
end;

function TAnsiStringManager.LastOf(const S: AnsiString): PAnsiChar;
begin
  Exit(@S[Length(S)]);
end;

function TAnsiStringManager.LengthOf(const S: AnsiString): SizeUInt;
begin
  Exit(Length(S));
end;

end.
