Browse Source

--- Merging r15764 into '.':
U rtl/objpas/sysutils/sysstr.inc
A tests/webtbs/tw16848.pp

# revisions: 15764
------------------------------------------------------------------------
r15764 | jonas | 2010-08-10 15:06:22 +0200 (Tue, 10 Aug 2010) | 2 lines
Changed paths:
M /trunk/rtl/objpas/sysutils/sysstr.inc
A /trunk/tests/webtbs/tw16848.pp

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

------------------------------------------------------------------------

git-svn-id: branches/fixes_2_4@16788 -

marco 14 years ago
parent
commit
9a319251ff
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

@@ -9747,6 +9747,7 @@ tests/webtbs/tw16622.pp svneol=native#text/pascal
 tests/webtbs/tw1677.pp svneol=native#text/plain
 tests/webtbs/tw16787.pp svneol=native#text/plain
 tests/webtbs/tw1681.pp svneol=native#text/plain
+tests/webtbs/tw16848.pp svneol=native#text/plain
 tests/webtbs/tw16863.pp svneol=native#text/plain
 tests/webtbs/tw1696.pp svneol=native#text/plain
 tests/webtbs/tw1699.pp svneol=native#text/plain

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

@@ -1771,9 +1771,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
@@ -1787,10 +1785,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
@@ -1818,6 +1818,7 @@ end;
 function TryStrToBool(const S: string; out Value: Boolean): Boolean;
 Var
   Temp : String;
+  I    : Longint;
 {$ifdef FPUNONE}
   D : Longint;
 {$else}
@@ -1834,12 +1835,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.