Browse Source

* use TrueBoolStrs/FalseBoolStrs for *StrToBool* (mantis #16848)

git-svn-id: trunk@15764 -
Jonas Maebe 15 years ago
parent
commit
89be8d45e3
3 changed files with 73 additions and 9 deletions
  1. 1 0
      .gitattributes
  2. 21 9
      rtl/objpas/sysutils/sysstr.inc
  3. 51 0
      tests/webtbs/tw16848.pp

+ 1 - 0
.gitattributes

@@ -10577,6 +10577,7 @@ tests/webtbs/tw16787.pp svneol=native#text/plain
 tests/webtbs/tw16803.pp svneol=native#text/plain
 tests/webtbs/tw1681.pp svneol=native#text/plain
 tests/webtbs/tw16820.pp svneol=native#text/plain
+tests/webtbs/tw16848.pp svneol=native#text/plain
 tests/webtbs/tw16861.pp svneol=native#text/plain
 tests/webtbs/tw16863.pp svneol=native#text/plain
 tests/webtbs/tw16874.pp svneol=native#text/plain

+ 21 - 9
rtl/objpas/sysutils/sysstr.inc

@@ -1732,9 +1732,7 @@ begin
     Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
 end;
 
-function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
-
-procedure CheckStrs;
+procedure CheckBoolStrs;
 begin
     If Length(TrueBoolStrs)=0 then
       begin
@@ -1748,10 +1746,12 @@ begin
       end;
 end;
 
+
+function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
 begin
  if UseBoolStrs Then
   begin
-    CheckStrs;
+    CheckBoolStrs;
     if B then
       Result:=TrueBoolStrs[0]
     else
@@ -1779,6 +1779,7 @@ end;
 function TryStrToBool(const S: string; out Value: Boolean): Boolean;
 Var
   Temp : String;
+  I    : Longint;
 {$ifdef FPUNONE}
   D : Longint;
 {$else}
@@ -1795,12 +1796,23 @@ begin
 {$else}
     Value:=(D<>0.0)
 {$endif}
-  else If Temp='TRUE' then
-    Value:=true
-  else if Temp='FALSE' then
-    Value:=false
   else
-    Result:=false;
+    begin
+      CheckBoolStrs;
+      for I:=low(TrueBoolStrs) to High(TrueBoolStrs) do
+        if Temp=upcase(TrueBoolStrs[I]) then
+          begin
+            Value:=true;
+            exit;
+          end;
+      for I:=low(FalseBoolStrs) to High(FalseBoolStrs) do
+        if Temp=upcase(FalseBoolStrs[I]) then
+          begin
+            Value:=false;
+            exit;
+          end;
+      Result:=false;
+    end;
 end;
 
 {$ifndef FPUNONE}

+ 51 - 0
tests/webtbs/tw16848.pp

@@ -0,0 +1,51 @@
+{$ifdef fpc}
+{$mode objfpc}{$H+}
+{$endif}
+uses
+  Classes, SysUtils;
+
+var
+  B: Boolean;
+  S: String;
+begin
+  WriteLn('BoolToStr(False, True): ' + BoolToStr(False, True));
+  if BoolToStr(False, True)<>'False' then
+    halt(1);
+  WriteLn('BoolToStr(True, True): ' + BoolToStr(True, True));
+  if BoolToStr(True, True)<>'True' then
+    halt(2);
+
+  SetLength(TrueBoolStrs, 1);
+  SetLength(FalseBoolStrs, 1);
+  TrueBoolStrs[0] := 'Sim';
+  FalseBoolStrs[0] := 'Não';
+
+  WriteLn('BoolStrs = Não;Sim');
+
+  WriteLn('BoolToStr(False, True): ' + BoolToStr(False, True));
+  WriteLn('BoolToStr(True, True): ' + BoolToStr(True, True));
+
+  S := BoolToStr(False, True);
+  if S<>'Não' then
+    halt(3);
+  B := StrToBool(S);
+  if b<>false then
+    halt(4);
+
+  WriteLn('StrToBool(' + S +') = ' + BoolToStr(B, True));
+  if BoolToStr(B, True)<>'Não' then
+    halt(5);
+  S := BoolToStr(True, True);
+  if s<>'Sim' then
+    halt(6);
+  B := StrToBool(S);
+  if b<>true then
+    halt(7);
+  WriteLn('StrToBool(' + S +') = ' + BoolToStr(B, True));
+  if BoolToStr(B, True)<>'Sim' then
+    halt(8);
+
+  { should give exception }
+  if TryStrToBool('True',B) then
+    halt(9);
+end.