* fixed AnsiStrLComp and AnsiStrLIComp: they also have to ignore

embedded #0's apparently + adapted test

git-svn-id: trunk@9471 -
This commit is contained in:
Jonas Maebe 2007-12-16 10:41:18 +00:00
parent 72f6ec3c8f
commit 2ffc310ebb
2 changed files with 29 additions and 25 deletions

View File

@ -549,19 +549,26 @@ function CharLengthPChar(const Str: PChar): PtrInt;
end; end;
function StrCompAnsiIntern(const s1,s2 : PChar; len1, len2: PtrInt): PtrInt; function StrCompAnsiIntern(s1,s2 : PChar; len1, len2: PtrInt; canmodifys1, canmodifys2: boolean): PtrInt;
var var
a,b: pchar; a,b: pchar;
i: PtrInt; i: PtrInt;
begin begin
getmem(a,len1+1); if not(canmodifys1) then
getmem(b,len2+1); getmem(a,len1+1)
else
a:=s1;
for i:=0 to len1-1 do for i:=0 to len1-1 do
if s1[i]<>#0 then if s1[i]<>#0 then
a[i]:=s1[i] a[i]:=s1[i]
else else
a[i]:=#32; a[i]:=#32;
a[len1]:=#0; a[len1]:=#0;
if not(canmodifys2) then
getmem(b,len2+1)
else
b:=s2;
for i:=0 to len2-1 do for i:=0 to len2-1 do
if s2[i]<>#0 then if s2[i]<>#0 then
b[i]:=s2[i] b[i]:=s2[i]
@ -569,14 +576,16 @@ function StrCompAnsiIntern(const s1,s2 : PChar; len1, len2: PtrInt): PtrInt;
b[i]:=#32; b[i]:=#32;
b[len2]:=#0; b[len2]:=#0;
result:=strcoll(a,b); result:=strcoll(a,b);
freemem(a); if not(canmodifys1) then
freemem(b); freemem(a);
if not(canmodifys2) then
freemem(b);
end; end;
function CompareStrAnsiString(const s1, s2: ansistring): PtrInt; function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
begin begin
result:=StrCompAnsiIntern(pchar(s1),pchar(s2),length(s1),length(s2)); result:=StrCompAnsiIntern(pchar(s1),pchar(s2),length(s1),length(s2),false,false);
end; end;
@ -592,7 +601,7 @@ function AnsiCompareText(const S1, S2: ansistring): PtrInt;
begin begin
a:=UpperAnsistring(s1); a:=UpperAnsistring(s1);
b:=UpperAnsistring(s2); b:=UpperAnsistring(s2);
result:=StrCompAnsiIntern(pchar(a),pchar(b),length(a),length(b)); result:=StrCompAnsiIntern(pchar(a),pchar(b),length(a),length(b),true,true);
end; end;
@ -606,7 +615,9 @@ function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
var var
a, b: pchar; a, b: pchar;
begin begin
if (IndexChar(s1^,maxlen,#0)<0) then if (maxlen=0) then
exit(0);
if (s1[maxlen]<>#0) then
begin begin
getmem(a,maxlen+1); getmem(a,maxlen+1);
move(s1^,a^,maxlen); move(s1^,a^,maxlen);
@ -614,7 +625,7 @@ begin
end end
else else
a:=s1; a:=s1;
if (IndexChar(s2^,maxlen,#0)<0) then if (s2[maxlen]<>#0) then
begin begin
getmem(b,maxlen+1); getmem(b,maxlen+1);
move(s2^,b^,maxlen); move(s2^,b^,maxlen);
@ -622,7 +633,7 @@ begin
end end
else else
b:=s2; b:=s2;
result:=strcoll(a,b); result:=StrCompAnsiIntern(a,b,maxlen,maxlen,a<>s1,b<>s2);
if (a<>s1) then if (a<>s1) then
freemem(a); freemem(a);
if (b<>s2) then if (b<>s2) then
@ -633,20 +644,13 @@ end;
function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt; function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
var var
a, b: ansistring; a, b: ansistring;
len1,len2: SizeInt;
begin begin
len1:=IndexChar(s1^,maxlen,#0); if (maxlen=0) then
if (len1<0) then exit(0);
len1:=maxlen; setlength(a,maxlen);
setlength(a,len1); move(s1^,a[1],maxlen);
if (len1<>0) then setlength(b,maxlen);
move(s1^,a[1],len1); move(s2^,b[1],maxlen);
len2:=IndexChar(s2^,maxlen,#0);
if (len2<0) then
len2:=maxlen;
setlength(b,len2);
if (len2<>0) then
move(s2^,b[1],len2);
result:=AnsiCompareText(a,b); result:=AnsiCompareText(a,b);
end; end;

View File

@ -95,7 +95,7 @@ begin
check (ansistrlcomp ('abce', 'abc', 3) = 0, 11); { Count = length. } check (ansistrlcomp ('abce', 'abc', 3) = 0, 11); { Count = length. }
check (ansistrlcomp ('abcd', 'abce', 4) < 0, 12); { Nudging limit. } check (ansistrlcomp ('abcd', 'abce', 4) < 0, 12); { Nudging limit. }
check (ansistrlcomp ('abc', 'def', 0) = 0, 13); { Zero count. } check (ansistrlcomp ('abc', 'def', 0) = 0, 13); { Zero count. }
check (ansistrlcomp ('abc'#0'e', 'abc'#0'd', 99) = 0, 14); check (ansistrlcomp ('abc'#0'e', 'abc'#0'd', 99) > 0, 14);
end; end;
@ -150,7 +150,7 @@ begin
check(ansistrlicomp('AbC', 'abcd', 4) < 0, 18); check(ansistrlicomp('AbC', 'abcd', 4) < 0, 18);
check(ansistrlicomp('ADC', 'abcd', 1) = 0, 19); check(ansistrlicomp('ADC', 'abcd', 1) = 0, 19);
check(ansistrlicomp('ADC', 'abcd', 2) > 0, 20); check(ansistrlicomp('ADC', 'abcd', 2) > 0, 20);
check(ansistrlicomp('abc'#0'e', 'abc'#0'd', 99) = 0, 21); check(ansistrlicomp('abc'#0'e', 'abc'#0'd', 99) > 0, 21);
end; end;