Search code examples
delphiunicode

Is there an ANSI version for Copy?


Under Delphi XE, is there an ANSI version for Copy? I am using Copy a lot to copy pieces of a ANSI strings.


Solution

  • Altar the Copy function in Delphi is a intrinsic function this means which is handled by the compiler rather than the run-time library. depending of the parameters passed this function call the LStrCopy or a UStrCopy internal functions

    check this sample :

    {$APPTYPE CONSOLE}
    
    uses
      SysUtils;
    Var
       s : AnsiString;
       u : string;
    begin
      try
       s:='this is a ansi string';
       s:= Copy(s,1,5);
       Writeln(s);
       u:='this is a unicode string';
       u:= Copy(u,1,5);
       Writeln(u);
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      Readln;
    end.
    

    Now check the assembly code

    Project91.dpr.12: s:='this is a ansi string';
    004111DC B8787E4100       mov eax,$00417e78
    004111E1 BA04134100       mov edx,$00411304
    004111E6 E8314FFFFF       call @LStrAsg
    Project91.dpr.13: s:= Copy(s,1,5);
    004111EB 68787E4100       push $00417e78
    004111F0 B905000000       mov ecx,$00000005
    004111F5 BA01000000       mov edx,$00000001
    004111FA A1787E4100       mov eax,[$00417e78]
    004111FF E8A050FFFF       call @LStrCopy //call the ansi version of copy
    Project91.dpr.14: Writeln(s);
    00411204 A1EC2C4100       mov eax,[$00412cec]
    00411209 8B15787E4100     mov edx,[$00417e78]
    0041120F E84033FFFF       call @Write0LString
    00411214 E8DF33FFFF       call @WriteLn
    00411219 E8D22AFFFF       call @_IOTest
    Project91.dpr.15: u:='this is a unicode string';
    0041121E B87C7E4100       mov eax,$00417e7c
    00411223 BA28134100       mov edx,$00411328
    00411228 E8534EFFFF       call @UStrAsg
    Project91.dpr.16: u:= Copy(u,1,5);
    0041122D 687C7E4100       push $00417e7c
    00411232 B905000000       mov ecx,$00000005
    00411237 BA01000000       mov edx,$00000001
    0041123C A17C7E4100       mov eax,[$00417e7c]
    00411241 E8C654FFFF       call @UStrCopy //call the unicode version of copy
    Project91.dpr.17: Writeln(u);
    00411246 A1EC2C4100       mov eax,[$00412cec]