Copy 有 ANSI 版本吗?

发布于 2024-11-08 17:23:55 字数 60 浏览 0 评论 0原文

Delphi XE下,Copy有ANSI版本吗? 我经常使用 Copy 来复制 ANSI 字符串的片段。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄离 2024-11-15 17:23:55

Delphi 中的 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]

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]
树深时见影 2024-11-15 17:23:55

Copy 是一个“编译器魔法”例程,它由编译器根据您传递给它的参数(ANSI 字符串、字符串或动态数组)进行本质上的处理。您可以只使用复制;它可以正确地处理 ANSI 字符串。

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.

じ违心 2024-11-15 17:23:55

我有同样的问题,请参阅以下代码:

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 必须保存到文件内的 Char 数组中。因此必须进行混合类型...因此,将会出现一些松散的 Unicode 字符,这是无法避免的。

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

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

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

解决方案 1:

MyFileField[MyIndex]:=Copy(UTF8Encode(TheLiteral),MyIndex,1)[1]; // 注意:Unicode 字符不会丢失,而是会转换,因此请注意口音等...

MyFileField[MyIndex]:=Copy(String(TheLiteral),MyIndex,1)[ 1]; // 注意:Unicode 字符会丢失,它们将被转换为 '?'

解决方案 2:

MyFileField[MyIndex]:=Char(Copy(TheLiteral,MyIndex,1)[1]); // 注意:Unicode 字符会丢失,它们将被转换为 '?'

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

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

示例: Length(String('BlaBlaBlá')) -> 9
示例: Length(UTF8Encode('BlaBlaBlá')) ->自最后一个“á”以来超过 9 个被转换为多个字符等...

希望这可以帮助某人!

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!

画骨成沙 2024-11-15 17:23:55

我做了我自己的职责。它可能很有用,并且可以在 Linux 平台上获得正确的结果,而不是 Copy(string(ansistr), i, l):

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文