unit Chilkat.Utils;

{
  Shared string conversion helpers for all Chilkat Pascal wrapper units.

  The C bridge always exchanges strings as UTF-8 PAnsiChar.

  Compatible with:
    Lazarus/FPC  -- string = AnsiString, UTF-8 bytes pass through unchanged.
    Delphi XE2+ -- string = UnicodeString, converted via UTF8ToString / UTF8Encode.

  $IFDEF UNICODE is defined by Delphi 2009+ when string = UnicodeString.
  Lazarus/FPC does not define it.
}

interface

{ Convert a UTF-8 PAnsiChar from the C bridge to a Pascal string. }
function CkToStr(p: PAnsiChar): string; inline;

{ Convert a Pascal string to a UTF-8 AnsiString suitable for PAnsiChar
  casts when passing strings to the C bridge. }
function StrToCk(const s: string): UTF8String; inline;

implementation

function CkToStr(p: PAnsiChar): string; inline;
begin
{$IFDEF UNICODE}
  Result := UTF8ToString(p);   // UTF-8 bytes -> UTF-16 UnicodeString
{$ELSE}
  Result := string(p);         // UTF-8 bytes -> AnsiString (no conversion)
{$ENDIF}
end;

function StrToCk(const s: string): UTF8String; inline;
begin
{$IFDEF UNICODE}
  Result := UTF8Encode(s);     // UTF-16 UnicodeString -> UTF-8 bytes
{$ELSE}
  Result := s;                 // AnsiString already holds UTF-8 bytes
{$ENDIF}
end;

end.
