Advertisement
abouchez

FastNewRawUTF8

May 24th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.96 KB | None | 0 0
  1. procedure FastNewRawUTF8(var s: RawUTF8; len: integer);
  2. {$ifdef FPC} inline;
  3. begin
  4.   SetString(s,nil,len);
  5. end;
  6. {$else}
  7. {$ifdef PUREPASCAL} {$ifdef HASINLINE}inline;{$endif}
  8. begin
  9.   if len<>0 then
  10.     if (PtrUInt(s)=0) or                   // s=''
  11.        (PInteger(PtrUInt(s)-8)^<>1) or     // s.refcount<>1
  12.        (PInteger(PtrUInt(s)-4)^<>len) then // s.length<>len
  13.       SetString(s,nil,len) else
  14.       exit else
  15.     if s='' then
  16.       exit else
  17.       s := '';
  18. end;
  19. {$else}
  20. asm // eax=s edx=len
  21.      test edx,edx
  22.      mov ecx,[eax]
  23.      jz System.@LStrClr
  24.      test ecx,ecx
  25.      jz @set
  26.      cmp dword ptr [ecx-8],1
  27.      jne @set
  28.      cmp dword ptr [ecx-4],edx
  29.      je @out
  30. @set:mov ecx,edx
  31.      xor edx,edx
  32. {$ifdef UNICODE}
  33.      push CP_UTF8 // UTF-8 code page for Delphi 2009+
  34.      call  System.@LStrFromPCharLen // we need a call, not a jmp here
  35. {$else}
  36.      jmp System.@LStrFromPCharLen
  37. {$endif}
  38. @out:
  39. end;
  40. {$endif PUREPASCAL}
  41. {$endif FPC}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement