Prechádzať zdrojové kódy

* StrToBool friends, fixes #7222

git-svn-id: trunk@4378 -
florian 19 rokov pred
rodič
commit
215b870f5e

+ 1 - 0
.gitattributes

@@ -6244,6 +6244,7 @@ tests/test/units/sysutils/execansi.pp svneol=native#text/plain
 tests/test/units/sysutils/execedbya.pp svneol=native#text/plain
 tests/test/units/sysutils/extractquote.pp svneol=native#text/plain
 tests/test/units/sysutils/tsscanf.pp svneol=native#text/plain
+tests/test/units/sysutils/tstrtobool.pp svneol=native#text/plain
 tests/test/uprocext1.pp svneol=native#text/plain
 tests/test/uprocext2.pp svneol=native#text/plain
 tests/test/utasout.pp svneol=native#text/plain

+ 28 - 12
rtl/objpas/sysutils/sysstr.inc

@@ -1345,33 +1345,49 @@ begin
     Result:=Default;
 end;
 
+
 function StrToBool(const S: string): Boolean;
+begin
+  if not(TryStrToBool(S,Result)) then
+    Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
+end;
+
+
+function BoolToStr(B: Boolean): string;
+begin
+  If B then
+    Result:='TRUE'
+  else
+    Result:='FALSE';
+end;
 
+
+function StrToBoolDef(const S: string; Default: Boolean): Boolean;
+begin
+  if not(TryStrToBool(S,Result)) then
+    Result:=Default;
+end;
+  
+  
+function TryStrToBool(const S: string; out Value: Boolean): Boolean;
 Var
   Temp : String;
   D : Double;
   Code: word;
-
 begin
   Temp:=upcase(S);
   Val(temp,D,code);
+  Result:=true;
   If Code=0 then
-    Result:=(D<>0.0)
+    Value:=(D<>0.0)
   else If Temp='TRUE' then
-    result:=true
+    Value:=true
   else if Temp='FALSE' then
-    result:=false
+    Value:=false
   else
-    Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
+    Result:=false;
 end;
 
-function BoolToStr(B: Boolean): string;
-begin
-  If B then
-    Result:='TRUE'
-  else
-    Result:='FALSE';
-end;
 
 Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
 

+ 5 - 0
rtl/objpas/sysutils/sysstrh.inc

@@ -153,12 +153,17 @@ Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Preci
 Function FloatToDateTime (Const Value : Extended) : TDateTime;
 Function FloattoCurr (Const Value : Extended) : Currency;
 function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
+
 Function CurrToStr(Value: Currency): string;
 function StrToCurr(const S: string): Currency;
 function TryStrToCurr(const S: string;Var Value : Currency): Boolean;
 function StrToCurrDef(const S: string; Default : Currency): Currency;
+
 function StrToBool(const S: string): Boolean;
 function BoolToStr(B: Boolean): string;
+function StrToBoolDef(const S: string; Default: Boolean): Boolean;
+function TryStrToBool(const S: string; out Value: Boolean): Boolean;
+
 function LastDelimiter(const Delimiters, S: string): Integer;
 function StringReplace(const S, OldPattern, NewPattern: string;  Flags: TReplaceFlags): string;
 Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;

+ 71 - 0
tests/test/units/sysutils/tstrtobool.pp

@@ -0,0 +1,71 @@
+uses
+  sysutils;
+var
+  b : boolean;
+begin
+  if not TryStrToBool('true',b) then
+    halt(1);
+  if not b then
+    halt(1);
+  if not TryStrToBool('false',b) then
+    halt(1);
+  if b then
+    halt(1);
+
+  if not TryStrToBool('True',b) then
+    halt(1);
+  if not b then
+    halt(1);
+  if not TryStrToBool('False',b) then
+    halt(1);
+  if b then
+    halt(1);
+
+  if not TryStrToBool('truE',b) then
+    halt(1);
+  if not b then
+    halt(1);
+  if not TryStrToBool('falsE',b) then
+    halt(1);
+  if b then
+    halt(1);
+
+  if not TryStrToBool('TRUE',b) then
+    halt(1);
+  if not b then
+    halt(1);
+  if not TryStrToBool('FALSE',b) then
+    halt(1);
+  if b then
+    halt(1);
+
+  if not TryStrToBool('3.1415',b) then
+    halt(1);
+  if not b then
+    halt(1);
+  if not TryStrToBool('0.0',b) then
+    halt(1);
+  if b then
+    halt(1);
+
+  if TryStrToBool('',b) then
+    halt(1);
+
+  if TryStrToBool('asdf',b) then
+    halt(1);
+
+  b:=StrToBool('truE');
+  if not b then
+    halt(1);
+  b:=StrToBool('falsE');
+  if b then
+    halt(1);
+
+  if not(StrToBoolDef('',true)) then
+    halt(1);
+
+  if StrToBoolDef('asdf',false) then
+    halt(1);
+
+  writeln('ok');
+end.