是否有复制的ANSI版本?

时间:2011-05-19 09:01:07

标签: delphi delphi-unicode

在Delphi XE下,是否有ANSI版本的Copy? 我正在使用Copy来复制ANSI字符串。

3 个答案:

答案 0 :(得分:19)

Alphi中的Copy函数是一个内在函数,这意味着它由编译器而不是运行时库处理。根据传递此参数的参数,调用LStrCopyUStrCopy内部函数

检查此样本:

{$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.

现在检查汇编代码

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]

答案 1 :(得分:5)

复制是一个“编译魔术”例程,它由编译器根据您传递的参数(ANSI字符串,字符串或动态数组)进行内在处理。你可以使用Copy;它将与ANSI字符串一起正常工作。

答案 2 :(得分:0)

我遇到同样的问题,请看这段代码:

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;

问题是必须将WideString保存到文件中的am Array of Char中。因此必须完成混合类型......因此,会出现一些松散的Unicode字符,无法避免它。

想要的:编译器可以编译它。

Solution1:在调用Copy之前或在Copy中将WideString转换为String。 解决方案2:在分配之前将WideChar转换为Char。

这两个解决方案(请记住一些unicode字符可能会丢失)...

解决方法1:

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

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

溶液2:

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

如果有人知道更好,我会很高兴知道。

我个人使用Copy(String(Literal),Start,NumberOfChars),因为正常的重音字母是守恒的,更重要的是,长度......

示例:Length(String('BlaBlaBlá')) - > 9 示例:长度(UTF8Encode('BlaBlaBlá')) - >自上次'á'转换为多个字符等以来超过9个......

希望这可以帮助别人!