Make Int32.Parse respect current culture.
authorRodrigo Kumpera <kumpera@gmail.com>
Sun, 13 Feb 2011 18:33:35 +0000 (19:33 +0100)
committerRodrigo Kumpera <kumpera@gmail.com>
Sun, 13 Feb 2011 18:46:10 +0000 (19:46 +0100)
* Int32.cs (Parse): Repect current culture for
positive and negative signs.

Fixes #586085

mcs/class/corlib/System/Int32.cs
mcs/class/corlib/Test/System/Int32Test.cs

index dcb76d75e59959ada2f4a3c6b7917b3f7301cc6c..f0f6a11e0a32a4e3d00cab68781107a65b41b927 100644 (file)
@@ -113,6 +113,7 @@ namespace System {
 
                        result = 0;
                        exc = null;
+                       NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
 
                        if (s == null) {
                                if (!tryParse)
@@ -135,12 +136,11 @@ namespace System {
                                return false;
                        }
 
-                       c = s [i];
-                       if (c == '+')
-                               i++;
-                       else if (c == '-'){
+                       if (String.Compare (s, i, nfi.PositiveSign, 0, nfi.PositiveSign.Length) == 0)
+                               i += nfi.PositiveSign.Length;
+                       else if (String.Compare (s, i, nfi.NegativeSign, 0, nfi.NegativeSign.Length) == 0) {
                                sign = -1;
-                               i++;
+                               i += nfi.NegativeSign.Length;
                        }
                        
                        for (; i < len; i++){
index ff5e055a56acc1a0168901379c769239b1423938..0fd4e1a10fd46ed9e45d59f29321eaadd616abcc 100644 (file)
@@ -431,6 +431,31 @@ public class Int32Test
 
                Assert.AreEqual ("254", def, "ToString(G)");
        }
+
+       [Test]
+       public void ParseRespectCurrentCulture ()
+       {
+               var old = Thread.CurrentThread.CurrentCulture;
+               var cur = (CultureInfo)old.Clone ();
+
+               NumberFormatInfo ninfo = new NumberFormatInfo ();
+               ninfo.NegativeSign = ">";
+               ninfo.PositiveSign = "%";
+               cur.NumberFormat = ninfo;
+
+               Thread.CurrentThread.CurrentCulture = cur;
+
+               int val = 0;
+
+               try {
+                       Assert.IsTrue (int.TryParse (">11", out val), "#1");
+                       Assert.AreEqual (-11, val, "#2");
+                       Assert.IsTrue (int.TryParse ("%11", out val), "#3");
+                       Assert.AreEqual (11, val, "#4");
+               } finally {
+                       Thread.CurrentThread.CurrentCulture = old;
+               }
+       }
 }
 
 }