Advertisement
abouchez

SetRawUTF8

May 24th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.85 KB | None | 0 0
  1. procedure SetRawUTF8(var Dest: RawUTF8; text: pointer; len: integer);
  2. {$ifdef FPC}inline;
  3. begin
  4.   if (len>128) or (len=0) or (text<>pointer(Dest)) then
  5.     SetString(Dest,PAnsiChar(text),len) else
  6.     SetLength(Dest,len);
  7. end;
  8. {$else}
  9. {$ifdef PUREPASCAL}
  10. var P: PStrRec;
  11. begin
  12.   if (len>128) or (len=0) or (PtrInt(Dest)=0) or     // Dest=''
  13.     (PStrRec(PtrInt(Dest)-STRRECSIZE)^.refCnt<>1) then
  14.     SetString(Dest,PAnsiChar(text),len) else begin
  15.     if PStrRec(Pointer(PtrInt(Dest)-STRRECSIZE))^.length<>len then begin
  16.       P := Pointer(PtrInt(Dest)-STRRECSIZE);
  17.       ReallocMem(P,len+(STRRECSIZE+1));
  18.       P^.length := len;
  19.       pointer(Dest) := pointer(PAnsiChar(P)+STRRECSIZE);
  20.       PByteArray(Dest)[len] := 0;
  21.     end;
  22.     Move(pointer(text)^,pointer(Dest)^,len);
  23.   end;
  24. end;
  25. {$else}
  26. asm // eax=@Dest text=edx len=ecx
  27.     cmp ecx,128 // avoid huge move() in ReallocMem()
  28. {$ifdef UNICODE}
  29.     ja @3
  30. {$else}
  31.     ja System.@LStrFromPCharLen
  32. {$endif}
  33.     or ecx,ecx // len=0
  34. {$ifdef UNICODE}
  35.     jz @3
  36. {$else}
  37.     jz System.@LStrFromPCharLen
  38. {$endif}
  39.     push ebx
  40.     mov ebx,[eax]
  41.     test ebx,ebx
  42.     jnz @2
  43. @0: pop ebx
  44. {$ifdef UNICODE}
  45. @3: push CP_UTF8 // UTF-8 code page for Delphi 2009+
  46.     call System.@LStrFromPCharLen // we need a call, not a jmp here
  47.     ret
  48. {$else}
  49.     jmp System.@LStrFromPCharLen
  50. {$endif}
  51. @2: cmp dword ptr [ebx-8],1
  52.     jne @0
  53.     cmp dword ptr [ebx-4],ecx
  54.     je @1
  55.     sub ebx,STRRECSIZE
  56.     push edx
  57.     push eax
  58.     push ecx
  59.     push ebx
  60.     mov eax,esp // ReallocMem() over ebx pointer on stack
  61.     lea edx,ecx+STRRECSIZE+1
  62.     call System.@ReallocMem
  63.     pop ebx
  64.     pop ecx
  65.     add ebx,STRRECSIZE
  66.     pop eax
  67.     pop edx
  68.     mov [eax],ebx
  69.     mov dword ptr [ebx-4],ecx
  70.     mov byte ptr [ebx+ecx],0
  71. @1: mov eax,edx
  72.     mov edx,ebx
  73.     call Move
  74.     pop ebx
  75. end;
  76. {$endif}
  77. {$endif}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement