From 8d2f3946e0e8e0a9a9d081c5743854fa883d0d21 Mon Sep 17 00:00:00 2001 From: Jonas Maebe <jonas@freepascal.org> Date: Sat, 14 Mar 2009 15:20:46 +0000 Subject: [PATCH] * fixed TextToFloat() in case ThousandSeparator = DecimalSeparator, based on patch by Bart Broersma (mantis #13307) git-svn-id: trunk@12885 - --- .gitattributes | 1 + rtl/objpas/sysutils/sysstr.inc | 43 +++++---- tests/webtbs/tw13307.pp | 155 +++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 15 deletions(-) create mode 100644 tests/webtbs/tw13307.pp diff --git a/.gitattributes b/.gitattributes index 0478a76d1b..345b01a475 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8795,6 +8795,7 @@ tests/webtbs/tw13187.pp svneol=native#text/plain tests/webtbs/tw13210.pp svneol=native#text/plain tests/webtbs/tw1323.pp svneol=native#text/plain tests/webtbs/tw1327.pp svneol=native#text/plain +tests/webtbs/tw13307.pp svneol=native#text/plain tests/webtbs/tw1331.pp svneol=native#text/plain tests/webtbs/tw13313.pp svneol=native#text/plain tests/webtbs/tw13313a.pp svneol=native#text/plain diff --git a/rtl/objpas/sysutils/sysstr.inc b/rtl/objpas/sysutils/sysstr.inc index 3ab830776a..6438b5adee 100644 --- a/rtl/objpas/sysutils/sysstr.inc +++ b/rtl/objpas/sysutils/sysstr.inc @@ -1046,23 +1046,27 @@ Var Begin S:=StrPas(Buffer); - if (FormatSettings.DecimalSeparator<>'.') then + //ThousandSeparator not allowed as by Delphi specs + if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and + (Pos(FormatSettings.ThousandSeparator, S) <> 0) then begin - { only decimalseparator may appear in the string } - P:=Pos('.',S); - if (P<>0) then - begin - result:=false; - exit; - end; - P:=Pos(FormatSettings.DecimalSeparator,S); - If (P<>0) Then - S[P] := '.'; + Result := False; + Exit; end; + if (FormatSettings.DecimalSeparator <> '.') and + (Pos('.', S) <>0) then + begin + Result := False; + Exit; + end; + P:=Pos(FormatSettings.DecimalSeparator,S); + If (P<>0) Then + S[P] := '.'; Val(trim(S),Value,E); Result:=(E=0); End; + Function TextToFloat(Buffer: PChar; Out Value: Extended): Boolean; begin @@ -1086,12 +1090,20 @@ Var Begin S:=StrPas(Buffer); - P:=Pos(FormatSettings.ThousandSeparator,S); - While (P<>0) do + //ThousandSeparator not allowed as by Delphi specs + if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and + (Pos(FormatSettings.ThousandSeparator, S) <> 0) then begin - Delete(S,P,1); - P:=Pos(FormatSettings.ThousandSeparator,S); + Result := False; + Exit; end; + if (FormatSettings.DecimalSeparator <> '.') and + (Pos('.', S) <>0) then + begin + Result := False; + Exit; + end; + P:=Pos(FormatSettings.DecimalSeparator,S); If (P<>0) Then S[P] := '.'; @@ -1120,6 +1132,7 @@ Begin Result:=(E=0); End; + Function TryStrToFloat(Const S : String; Out Value: Single): Boolean; begin diff --git a/tests/webtbs/tw13307.pp b/tests/webtbs/tw13307.pp new file mode 100644 index 0000000000..a8778accaa --- /dev/null +++ b/tests/webtbs/tw13307.pp @@ -0,0 +1,155 @@ +program stf2; + +{$IFDEF FPC} +{$mode objfpc}{$H+} +{$ENDIF} + +uses SysUtils; + + +var S: String; + E: Extended; + B: Boolean; + Code: Integer; +begin + DecimalSeparator := '.'; + ThousandSeparator := ','; + writeln('DecimalSeparator = ',DecimalSeparator); + writeln('ThousandSeparator = ',ThousandSeparator); + E := -1.0; + S := '123.456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then writeln(Format('(1) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(1) "%s" -> Conversion Error',[S])); + halt(1); + end; + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then writeln(Format('(2) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(2) "%s" -> Conversion Error',[S])); + halt(1); + end; + S := '123,456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then writeln(Format('(1) "%s" -> %.3f',[S,E])) + else writeln(Format('(1) "%s" -> Conversion Error',[S])); + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then writeln(Format('(2) "%s" -> %.3f',[S,E])) + else writeln(Format('(2) "%s" -> Conversion Error',[S])); + + + + DecimalSeparator := ','; + ThousandSeparator := '.'; + writeln('DecimalSeparator = ',DecimalSeparator); + writeln('ThousandSeparator = ',ThousandSeparator); + E := -1.0; + S := '123.456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then + begin + writeln(Format('(1) "%s" -> %.3f',[S,E])); + halt(1); + end + else writeln(Format('(1) "%s" -> Conversion Error',[S])); + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then + begin + writeln(Format('(2) "%s" -> %.3f',[S,E])); + halt(1); + end + else writeln(Format('(2) "%s" -> Conversion Error',[S])); + S := '123,456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then writeln(Format('(1) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(1) "%s" -> Conversion Error',[S])); + halt(1); + end; + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then writeln(Format('(2) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(2) "%s" -> Conversion Error',[S])); + halt(1); + end; + + DecimalSeparator := ','; + ThousandSeparator := ','; + writeln('DecimalSeparator = ',DecimalSeparator); + writeln('ThousandSeparator = ',ThousandSeparator); + E := -1.0; + S := '123.456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then + begin + writeln(Format('(1) "%s" -> %.3f',[S,E])); + halt(1); + end + else writeln(Format('(1) "%s" -> Conversion Error',[S])); + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then + begin + writeln(Format('(12 "%s" -> %.3f',[S,E])); + halt(1); + end + else writeln(Format('(2) "%s" -> Conversion Error',[S])); + S := '123,456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then writeln(Format('(1) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(1) "%s" -> Conversion Error',[S])); + halt(1); + end; + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then writeln(Format('(2) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(2) "%s" -> Conversion Error',[S])); + halt(1); + end; + + DecimalSeparator := '.'; + ThousandSeparator := '.'; + writeln('DecimalSeparator = ',DecimalSeparator); + writeln('ThousandSeparator = ',ThousandSeparator); + E := -1.0; + S := '123.456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then writeln(Format('(1) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(1) "%s" -> Conversion Error',[S])); + halt(1); + end; + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then writeln(Format('(2) "%s" -> %.3f',[S,E])) + else + begin + writeln(Format('(2) "%s" -> Conversion Error',[S])); + halt(1); + end; + S := '123,456'; + B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings); + if B then + begin + writeln(Format('(1) "%s" -> %.3f',[S,E])); + halt(1); + end + else writeln(Format('(1) "%s" -> Conversion Error',[S])); + B := TextToFloat(PChar(S), E, DefaultFormatSettings); + if B then + begin + writeln(Format('(2) "%s" -> %.3f',[S,E])); + halt(1); + end + else writeln(Format('(2) "%s" -> Conversion Error',[S])); + + +end. +