瀏覽代碼

Patch from Petr Kristan for AnsiStrComp/AnsiStrIComp to fix comparison
when both strings are empty and differ after the null character + test.

git-svn-id: trunk@11277 -

giulio 17 年之前
父節點
當前提交
cc08543f57
共有 3 個文件被更改,包括 35 次插入4 次删除
  1. 1 0
      .gitattributes
  2. 4 4
      rtl/objpas/sysutils/sysstr.inc
  3. 30 0
      tests/test/units/sysutils/tastrcmp1.pp

+ 1 - 0
.gitattributes

@@ -7855,6 +7855,7 @@ tests/test/units/system/tval4.pp -text
 tests/test/units/system/tval5.pp svneol=native#text/plain
 tests/test/units/system/tvalc.pp -text
 tests/test/units/sysutils/tastrcmp.pp svneol=native#text/plain
+tests/test/units/sysutils/tastrcmp1.pp svneol=native#text/plain
 tests/test/units/sysutils/texec1.pp svneol=native#text/plain
 tests/test/units/sysutils/texec2.pp svneol=native#text/plain
 tests/test/units/sysutils/textractquote.pp svneol=native#text/plain

+ 4 - 4
rtl/objpas/sysutils/sysstr.inc

@@ -307,11 +307,11 @@ begin
       Result:=1;
       exit;
     end;
-  Repeat
+  While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
     Result:=Ord(S1^)-Ord(S2^); //!! Must be replaced by ansi characters !!
     Inc(S1);
     Inc(S2);
-  Until (Result<>0) or (S1^=#0) or (S2^=#0);
+  end;  
   if (Result=0) and (S1^<>S2^) then // loop ended because exactly one has #0
     if S1^=#0 then // shorter string is smaller
       result:=-1
@@ -335,11 +335,11 @@ begin
     Result:=1;
     exit;
     end;
-  Repeat
+  While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
     Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
     Inc(S1);
     Inc(S2);
-  Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0));
+  end;
   if (Result=0) and (s1[0]<>s2[0]) then //length(s1)<>length(s2)
     if s1[0]=#0 then
       Result:=-1 //s1 shorter than s2

+ 30 - 0
tests/test/units/sysutils/tastrcmp1.pp

@@ -0,0 +1,30 @@
+program comp;
+uses
+   SysUtils;
+
+var
+  error : boolean;
+
+procedure check(ok : boolean; func : string; value : longint);
+begin
+  if not ok then
+  begin
+    error:=true;
+    writeln(func,' failed, result = ',value);
+  end;
+end;
+
+var
+  a, b: array[0..1] of char;
+  tmp : longint;
+begin
+  error:=false;
+  a[0] := #0; a[1] := #1;      //Empty string
+  b[0] := #0; b[1] := #0;      //Empty string with different char after end
+  tmp:=AnsiStrComp(a, b);      //should be zero because a=b
+  check(tmp=0,'AnsiStrComp',tmp);
+  tmp:=AnsiStrIComp(a, b);     //should be zero because a=b
+  check(tmp=0,'AnsiStrIComp',tmp);
+  if error then
+    halt(1);
+end.