Browse Source

* only allow decimalseparator to be used as decimal separator in
texttofloat() and friends, rather than both '.' and decimalseparator
(mantis #9126)

git-svn-id: trunk@11069 -

Jonas Maebe 17 years ago
parent
commit
e60e078eb5
3 changed files with 52 additions and 3 deletions
  1. 1 0
      .gitattributes
  2. 13 3
      rtl/objpas/sysutils/sysstr.inc
  3. 38 0
      tests/webtbs/tw9126.pp

+ 1 - 0
.gitattributes

@@ -9133,6 +9133,7 @@ tests/webtbs/tw9098.pp svneol=native#text/plain
 tests/webtbs/tw9107.pp svneol=native#text/plain
 tests/webtbs/tw9108.pp svneol=native#text/plain
 tests/webtbs/tw9113.pp svneol=native#text/plain
+tests/webtbs/tw9126.pp svneol=native#text/plain
 tests/webtbs/tw9128.pp svneol=native#text/plain
 tests/webtbs/tw9139.pp svneol=native#text/plain
 tests/webtbs/tw9139a.pp svneol=native#text/plain

+ 13 - 3
rtl/objpas/sysutils/sysstr.inc

@@ -1024,9 +1024,19 @@ Var
 
 Begin
   S:=StrPas(Buffer);
-  P:=Pos(FormatSettings.DecimalSeparator,S);
-  If (P<>0) Then
-    S[P] := '.';
+  if (FormatSettings.DecimalSeparator<>'.') 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] := '.';
+    end;
   Val(trim(S),Value,E);
   Result:=(E=0);
 End;

+ 38 - 0
tests/webtbs/tw9126.pp

@@ -0,0 +1,38 @@
+{$ifdef fpc}
+{$mode delphi}
+{$endif}
+
+uses sysutils;
+
+var
+  failed : boolean;
+
+procedure testconvert(s : string; shouldsucceed: boolean);
+var
+  succeeded: boolean;
+begin
+  succeeded:=true;
+  try
+    writeln(strtofloat(s));
+  except 
+    on EConvertError do begin
+      writeln('Failed to convert ', s, ' to a float value');
+      succeeded := false;
+    end;
+  end;
+  failed:=failed or (succeeded<>shouldsucceed);
+end;
+
+begin
+  failed := false;
+  
+  thousandseparator := '.';
+  decimalseparator := ',';
+  
+  testconvert('1.200',false); // fails
+  testconvert('1,200',true); // working
+  testconvert('1.200,23',false); // fails
+  testconvert('1.200.300',false); // fails
+  
+  if (failed) then halt(1);
+end.