unit {*htns*}UTF8StringUtils;
(*
Copyright (c) 2010 HREF Tools Corp.

Permission is hereby granted, on 11-Jun-2010, free of charge, to any person
obtaining a copy of this file (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Author of original version of this file: Vseslav Suvorov
*)

interface

{$IFDEF CLR}
  {$UNSAFECODE ON}
{$ENDIF}

uses
{$IFDEF CLR}
  System.Text,
  System.Reflection,
  WinUtils,
{$ENDIF}
{$IFNDEF LINUX}
  Windows,
{$ENDIF}
  SysUtils;

function UTF8CompareText(const S1, S2: UTF8String):integer;
function UTF8Trim(const Value: UTF8String): UTF8String;
function UTF8Copy(const Value: UTF8String; const FromIndex,
  CountSymbols: Integer): UTF8String;
function UTF8Length(const Value:UTF8String): integer; overload;
function UTF8Length(p: PAnsiChar; ByteCount: integer): integer;overload;

implementation

function UTF8CharacterLength(p: PAnsiChar): integer;
begin
  // FreePascal uses % before numbers, example %11000000
  if p<>nil then begin
    if ord(p^) < $C0 {%11000000} then begin
      // regular single byte character (#0 is a character, this is pascal ;)
      Result:=1;
    end
    else if ((ord(p^) and $E0 {%11100000}) = $C0 {%11000000}) then begin
      // could be 2 byte character
      if (ord(p[1]) and $C0) = $80 {%10000000} then
        Result:=2
      else
        Result:=1;
    end
    else if ((ord(p^) and $F0 {11110000}) = $E0 {11100000}) then begin
      // could be 3 byte character
      if ((ord(p[1]) and $C0{11000000}) = $80 {10000000})
      and ((ord(p[2]) and $C0 {11000000}) = $80 {10000000}) then
        Result:=3
      else
        Result:=1;
    end
    else if ((ord(p^) and $F8 {11111000}) = $F0 {11110000}) then begin
      // could be 4 byte character
      if ((ord(p[1]) and $C0 {11000000}) = $80 {10000000})
      and ((ord(p[2]) and $C0 {11000000}) = $80 {10000000})
      and ((ord(p[3]) and $C0 {11000000}) = $80 {10000000}) then
        Result:=4
      else
        Result:=1;
    end
    else
      Result:=1
  end else
    Result:=0;
end;

function UTF8CharStart(UTF8Str: PAnsiChar; Len: Integer; CharIndex: Integer): PAnsiChar;
var
  CharLen: LongInt;
begin
  Result:=UTF8Str;
  if Result<>nil then begin
    while (CharIndex>0) and (Len>0) do begin
      CharLen:=UTF8CharacterLength(Result);
      dec(Len,CharLen);
      dec(CharIndex);
      inc(Result,CharLen);
    end;
    if (CharIndex>0) or (Len<0) then
      Result:=nil;
  end;
end;

function UTF8UpperCase(const s: UTF8String): UTF8String;
begin
//{$IFDEF Delphi12UP}
{$IFDEF UNICODE}
  Result :=UTF8String(WideUpperCase(UTF8ToString(s)));
{$ELSE}
  Result := UTF8Encode(WideUpperCase(UTF8Decode(s)));
{$ENDIF}
end;

function UTF8CompareText(const S1, S2: UTF8String):integer;
begin
  Result := CompareStr(String(UTF8UpperCase(S1)),String(UTF8UpperCase(S2)));
end;

function UTF8Trim(const Value: UTF8String): UTF8String;
begin
  Result := UTF8String(Trim(String(Value)));
end;

function UTF8Copy(const Value: UTF8String; const FromIndex,
  CountSymbols: Integer): UTF8String;
var
  StartBytePos: PAnsiChar;
  EndBytePos: PAnsiChar;
  MaxBytes: Integer;
begin
  StartBytePos:=UTF8CharStart(PAnsiChar(Value),length(Value), FromIndex - 1);
  if StartBytePos=nil then
    Result:=''
  else begin
    MaxBytes := (PAnsiChar(Value)+length(Value)-StartBytePos);
    EndBytePos := UTF8CharStart(StartBytePos, MaxBytes, CountSymbols);
    if EndBytePos=nil then
      Result:=copy(Value, StartBytePos-PAnsiChar(Value)+1,MaxBytes)
    else
      Result:=copy(Value, StartBytePos-PAnsiChar(Value)+1,EndBytePos-StartBytePos);
  end;
end;

function UTF8Length(const Value:UTF8String): integer;
begin
  Result := UTF8Length(PAnsiChar(Value), Length(Value));
end;

function UTF8Length(p: PAnsiChar; ByteCount: integer): integer;
var
  CharLen: LongInt;
begin
  Result:=0;
  while (ByteCount>0) do begin
    inc(Result);
    CharLen:=UTF8CharacterLength(p);
    inc(p,CharLen);
    dec(ByteCount,CharLen);
  end;
end;


end.
