IceCold
IceCold

Reputation: 21154

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.

Upvotes: 15

Views: 6144

Answers (4)

Dmitry Belkeivch
Dmitry Belkeivch

Reputation: 33

I did my own function. It could be useful and get right results instead of Copy(string(ansistr), i, l) on the Linux platform:

function AnsiCopy(const s: ansistring; StartIndex, Lenght: integer): ansistring;
begin
 SetLength(Result, Lenght);
 Move(s[StartIndex], Result[1], Lenght);
end;

Upvotes: 0

z666z666z
z666z666z

Reputation: 1

I have the same problem, see this code:

const
     TheStart=13;
     TheEnd=69;
type
    TMyFileField: Array[TheStart..TheEnd] of Char; // This is a simplification of a field type on a file
procedure WriteWideStringToArrayOfChars(TheLiteral:WideString);
var
   MyFileField:TMyFileField; // This is a simplification, it is really a Field inside a File
   MyIndex:Integer;
begin
     for MyIndex:=1 to Max(Length(TheLiteral),1+TheEnd-TheStart)
     do begin // Will copy as many charactes as possible from TheLiteral to MyFileField
             MyFileField[MyIndex]:=Copy(TheLiteral,MyIndex,1)[1]; // This gives Copile Error: Incompatible types 'Char' and 'WideChar'
        end;
end;

The problem is that the WideString must be saved onto am Array of Char inside a file. So mix types must be done... and so, some loose of Unicode chars will occur, no way to avoid it.

The wanted: The compiler can compile it.

Solution1: Convert WideString to String prior to call Copy, or inside Copy. Solution2: Convert WideChar to Char prior to assing.

Here are both solutions (remember some unicode chars could get lost)...

Solution1:

MyFileField[MyIndex]:=Copy(UTF8Encode(TheLiteral),MyIndex,1)[1]; // Note: Unicode chars will not get lost, but converted, so beware of accent vocals, etc...

or

MyFileField[MyIndex]:=Copy(String(TheLiteral),MyIndex,1)[1]; // Note: Unicode chars will get lost, they will be converted to '?'

Solution2:

MyFileField[MyIndex]:=Char(Copy(TheLiteral,MyIndex,1)[1]); // Note: Unicode chars will get lost, they will be converted to '?'

If anyone knows anthing better i would be glad to know.

I personally use Copy(String(Literal),Start,NumberOfChars) since normal accent letters are conserved and more important, length...

Example: Length(String('BlaBlaBlá')) -> 9 Example: Length(UTF8Encode('BlaBlaBlá')) -> More than 9 since the last 'á' is converted to multiple chars, etc...

Hope this can help someone!

Upvotes: 0

RRUZ
RRUZ

Reputation: 136401

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]

Upvotes: 20

Ondrej Kelle
Ondrej Kelle

Reputation: 37211

Copy is a "compiler magic" routine, it is handled intrinsically by the compiler depending on what parameters you pass it (ANSI string, string, or dynamic array). You can just use Copy; it will work correctly with ANSI strings.

Upvotes: 6

Related Questions