Procházet zdrojové kódy

* fixed TextToFloat() in case ThousandSeparator = DecimalSeparator, based
on patch by Bart Broersma (mantis #13307)

git-svn-id: trunk@12885 -

Jonas Maebe před 16 roky
rodič
revize
8d2f3946e0
3 změnil soubory, kde provedl 184 přidání a 15 odebrání
  1. 1 0
      .gitattributes
  2. 28 15
      rtl/objpas/sysutils/sysstr.inc
  3. 155 0
      tests/webtbs/tw13307.pp

+ 1 - 0
.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

+ 28 - 15
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
+      Result := False;
+      Exit;
+    end;
+  if (FormatSettings.DecimalSeparator <> '.') and
+     (Pos('.', S) <>0) then
     begin
-    Delete(S,P,1);
-    P:=Pos(FormatSettings.ThousandSeparator,S);
+      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

+ 155 - 0
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.
+