我使用这个函数,因为D2007我在网上得到它,不记得它在哪里。
但是现在在XE7中,它返回一个编译错误:
"E2107操作数大小不匹配“
function FastCharPos(const aSource : string; const C: Char; StartPos : Integer) : Integer;
var
L : Integer;
begin
//If this assert failed, it is because you passed 0 for StartPos, lowest value is 1 !!
Assert(StartPos > 0);
Result := 0;
L := Length(aSource);
if L = 0 then exit;
if StartPos > L then exit;
Dec(StartPos);
asm
PUSH EDI //Preserve this register
mov EDI, aSource //Point EDI at aSource
add EDI, StartPos
mov ECX, L //Make a note of how many chars to search through
sub ECX, StartPos
mov AL, C //and which char we want :Error -"E2107 Operand size mismatch"
@Loop:
cmp Al, [EDI] //compare it against the SourceString
jz @Found
inc EDI
dec ECX
jnz @Loop
jmp @NotFound
@Found:
sub EDI, aSource //EDI has been incremented, so EDI-OrigAdress = Char pos !
inc EDI
mov Result, EDI
@NotFound:
POP EDI
end;
end;
function FastCharPosNoCase(const aSource : string; C: Char; StartPos : Integer) : Integer;
var
L : Integer;
begin
Result := 0;
L := Length(aSource);
if L = 0 then exit;
if StartPos > L then exit;
Dec(StartPos);
if StartPos < 0 then StartPos := 0;
asm
PUSH EDI //Preserve this register
PUSH EBX
mov EDX, GUpcaseLUT
mov EDI, aSource //Point EDI at aSource
add EDI, StartPos
mov ECX, L //Make a note of how many chars to search through
sub ECX, StartPos
xor EBX, EBX
mov BL, C //:Error -"E2107 Operand size mismatch"
mov AL, [EDX+EBX]
@Loop:
mov BL, [EDI]
inc EDI
cmp Al, [EDX+EBX]
jz @Found
dec ECX
jnz @Loop
jmp @NotFound
@Found:
sub EDI, aSource //EDI has been incremented, so EDI-OrigAdress = Char pos !
mov Result, EDI
@NotFound:
POP EBX
POP EDI
end;
end;
我需要什么才能将这两个函数更新为XE7 win32?
我该怎么做?
谢谢。
发布于 2015-02-03 12:12:39
这段代码是为前Unicode编写的,其中Char
是AnsiChar
(8位字符类型)的别名。在Delphi2009及更高版本中,Char
是WideChar
( 16位字符类型)的别名。
错误消息的原因是代码打算对8位字符元素进行操作,但是您提供了16位操作数。运算符期望8位操作数,但您提供了16位操作数。
将Char
更改为AnsiChar
,使此代码在所有版本的Delphi上都能编译和运行。
话虽如此,我建议您停止使用此代码。相反,请使用Pos
。一般来说,最好使用内置的库函数。
发布于 2015-02-03 13:03:49
您应该停止对字符串例程使用旧的汇编程序版本,并使用内置的库函数。
如果您想匆忙地继续前进,您可以重新实现这样的函数:
function FastCharPos(const aSource: string; const C: Char; StartPos: Integer): Integer; inline;
begin
Result := Pos(C, aSource, StartPos);
end;
function FastCharPosNoCase(const aSource: string; C: Char; StartPos: Integer): Integer; inline;
begin
Result := Pos(AnsiUppercase(C), AnsiUppercase(aSource), StartPos);
end;
https://stackoverflow.com/questions/28298529
复制相似问题