Advertisement
Guest User

utf8decode delphi 10

a guest
May 19th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. type
  3.     //UTF8String = string;
  4.     UTF8String = AnsiString;
  5.    
  6. function Utf8Decode(const S: UTF8String): WideString;
  7. var
  8.   L: Integer;
  9.   Temp: WideString;
  10. begin
  11.   Result := '';
  12.   if S = '' then Exit;
  13.   SetLength(Temp, Length(S));
  14.  
  15.   L := Utf8ToUnicode(PWideChar(Temp), Length(Temp)+1, PChar(S), Length(S));
  16.   if L > 0 then
  17.     SetLength(Temp, L-1)
  18.   else
  19.     Temp := '';
  20.   Result := Temp;
  21. end;
  22.  
  23. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: Integer): Integer;
  24. var
  25.   len: Cardinal;
  26. begin
  27.   len := 0;
  28.   if Source <> nil then
  29.     while Source[len] <> #0 do
  30.       Inc(len);
  31.   Result := Utf8ToUnicode4(Dest, MaxChars, Source, len);
  32. end;
  33.  
  34. function Utf8ToUnicode4(Dest: PWideChar; MaxDestChars: Cardinal; Source: PChar; SourceBytes: Cardinal): Cardinal;
  35. var
  36.   i, count: Cardinal;
  37.   c: Byte;
  38.   wc: Cardinal;
  39. begin
  40.   if Source = nil then
  41.   begin
  42.     Result := 0;
  43.     Exit;
  44.   end;
  45.   Result := Cardinal(-1);
  46.   count := 0;
  47.   i := 0;
  48.   if Dest <> nil then
  49.   begin
  50.     while (i < SourceBytes) and (count < MaxDestChars) do
  51.     begin
  52.       wc := Cardinal(Source[i]);
  53.       Inc(i);
  54.       if (wc and $80) <> 0 then
  55.       begin
  56.         if i >= SourceBytes then Exit;          // incomplete multibyte char
  57.         wc := wc and $3F;
  58.         if (wc and $20) <> 0 then
  59.         begin
  60.           c := Byte(Source[i]);
  61.           Inc(i);
  62.           if (c and $C0) <> $80 then Exit;      // malformed trail byte or out of range char
  63.           if i >= SourceBytes then Exit;        // incomplete multibyte char
  64.           wc := (wc shl 6) or (c and $3F);
  65.         end;
  66.         c := Byte(Source[i]);
  67.         Inc(i);
  68.         if (c and $C0) <> $80 then Exit;       // malformed trail byte
  69.  
  70.         Dest[count] := WideChar((wc shl 6) or (c and $3F));
  71.       end
  72.       else
  73.         Dest[count] := WideChar(wc);
  74.       Inc(count);
  75.     end;
  76.     if count >= MaxDestChars then count := MaxDestChars-1;
  77.     Dest[count] := #0;
  78.   end
  79.   else
  80.   begin
  81.     while (i < SourceBytes) do
  82.     begin
  83.       c := Byte(Source[i]);
  84.       Inc(i);
  85.       if (c and $80) <> 0 then
  86.       begin
  87.         if i >= SourceBytes then Exit;          // incomplete multibyte char
  88.         c := c and $3F;
  89.         if (c and $20) <> 0 then
  90.         begin
  91.           c := Byte(Source[i]);
  92.           Inc(i);
  93.           if (c and $C0) <> $80 then Exit;      // malformed trail byte or out of range char
  94.           if i >= SourceBytes then Exit;        // incomplete multibyte char
  95.         end;
  96.         c := Byte(Source[i]);
  97.         Inc(i);
  98.         if (c and $C0) <> $80 then Exit;       // malformed trail byte
  99.       end;
  100.       Inc(count);
  101.     end;
  102.   end;
  103.   Result := count+1;
  104. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement