diff --git a/.gitattributes b/.gitattributes index 361bf18862..47af3d7d40 100644 --- a/.gitattributes +++ b/.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 diff --git a/rtl/objpas/sysutils/sysstr.inc b/rtl/objpas/sysutils/sysstr.inc index 9f8a6e5191..3414caccf2 100644 --- a/rtl/objpas/sysutils/sysstr.inc +++ b/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} diff --git a/tests/webtbs/tw16848.pp b/tests/webtbs/tw16848.pp new file mode 100644 index 0000000000..f19ce0321b --- /dev/null +++ b/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.