2001-12-21 Miguel de Icaza <miguel@ximian.com>
authorMiguel de Icaza <miguel@gnome.org>
Sat, 22 Dec 2001 03:46:42 +0000 (03:46 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Sat, 22 Dec 2001 03:46:42 +0000 (03:46 -0000)
* Char.cs (Parse): Implement.

* Byte.cs (Parse): Implement a fast parser.

* SByte.cs (Parse): Implement a fast parser.

* UInt16.cs (Parse): Implement a fast parser.

* Int16.cs (Parse): Implement a fast parser.

* UInt32.cs (Parse): Implement a fast parser.

* Int32.cs (Parse): Implement a fast parser.

svn path=/trunk/mcs/; revision=1681

mcs/class/corlib/System/Byte.cs
mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/Char.cs
mcs/class/corlib/System/Int16.cs
mcs/class/corlib/System/Int32.cs
mcs/class/corlib/System/Int64.cs
mcs/class/corlib/System/SByte.cs
mcs/class/corlib/System/UInt16.cs
mcs/class/corlib/System/UInt32.cs
mcs/class/corlib/System/UInt64.cs

index 0dd7b228f5770ab032ef3d9aad973d9c77c88dae..96615405bdde865ec1c90ff7fa61f1d49131ae55 100644 (file)
@@ -54,7 +54,52 @@ namespace System {
 
                public static byte Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       byte val = 0;
+                       int len;
+                       int i;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       if (s [i] == '+')
+                               i++;
+
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       byte d = (byte) (c - '0');
+                                       
+                                       val = checked ((byte) (val * 10 + d));
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       return val;
                }
 
                public static byte Parse (string s, IFormatProvider fp)
index 2ab48af8793ce609529ee1eecfd2d8a0d8fda74e..044003802c60a8a854b24d24be752cd4af0f8ddb 100644 (file)
@@ -1,3 +1,18 @@
+2001-12-21  Miguel de Icaza  <miguel@ximian.com>
+
+       * Char.cs (Parse): Implement.
+
+       * Byte.cs (Parse): Implement a fast parser.
+       
+       * SByte.cs (Parse): Implement a fast parser.
+
+       * UInt16.cs (Parse): Implement a fast parser.
+       
+       * Int16.cs (Parse): Implement a fast parser.
+
+       * UInt32.cs (Parse): Implement a fast parser.
+
+       * Int32.cs (Parse): Implement a fast parser.
 
 Fri Dec 21 15:14:52 CET 2001 Paolo Molaro <lupus@ximian.com>
 
index 5396c00f0dff01af18adbad7c5df3b2d65f0d9cf..bed23135a6d24e568487b97f5ed4b4243fca9bd5 100644 (file)
@@ -317,8 +317,17 @@ namespace System {
 
                public static char Parse (string str)
                {
-                       // TODO: Implement me
-                       return (char) 0;
+                       if (str == null)
+                               throw new ArgumentNullException (Locale.GetText ("str is a null reference"));
+
+                       int len = str.Length;
+                       if (len != 1){
+                               if (len < 1)
+                                       throw new ArgumentNullException ();
+                               else
+                                       throw new FormatException ();
+                       }
+                       return str [0];
                }
 
                public static char ToLower (char c)
index c415e10fe621df35ac8d43c7a15f8dbdcd30a55b..b85f45a1d9d4fdbccbb00980a2bfabcf1c92fa7d 100644 (file)
@@ -53,7 +53,59 @@ namespace System {
 
                public static short Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       short val = 0;
+                       int len;
+                       int i;
+                       bool neg = false;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       c = s [i];
+                       if (c == '+')
+                               i++;
+                       else if (c == '-'){
+                               neg = true;
+                               i++;
+                       }
+                       
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       val = checked ((short) (val * 10 + (c - '0')));
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       if (neg)
+                               val = checked ((short) -val);
+
+                       return val;
                }
 
                public static short Parse (string s, IFormatProvider fp)
index 7d1a919ca0f3090c7466c20b35ce474747ea3fb7..215c0a43ea7e363f63cb8446c6f5d55fcaf6d043 100644 (file)
@@ -51,7 +51,59 @@ namespace System {
 
                public static int Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       int val = 0;
+                       int len;
+                       int i;
+                       bool neg = false;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       c = s [i];
+                       if (c == '+')
+                               i++;
+                       else if (c == '-'){
+                               neg = true;
+                               i++;
+                       }
+                       
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       val = checked (val * 10 + (c - '0'));
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       if (neg)
+                               val = -val;
+
+                       return val;
                }
 
                public static int Parse (string s, IFormatProvider fp)
@@ -66,16 +118,8 @@ namespace System {
                
                public static int Parse (string s, NumberStyles style, IFormatProvider fp)
                {
-                       // TODO: Implement me
-                       int val = 0;
-                       int j;
-                       for (j = 0; j < s.Length; ++j) {
-                               if (s [j] >= '0' && s [j] <= '9')
-                                       val = val * 10 + s [j] - '0';
-                               else
-                                       break;
-                       }
-                       return val;
+                       // FIXME: Better than nothing ;-)
+                       return Parse (s);
                        //throw new NotImplementedException ();
                }
 
index ea1ceb2273516cff70281ea599af60c44751ec0a..80d3cac4af247e03adc66bb925779c72b5ab3471 100644 (file)
@@ -51,7 +51,59 @@ namespace System {
 
                public static long Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       long val = 0;
+                       int len;
+                       int i;
+                       bool neg = false;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       c = s [i];
+                       if (c == '+')
+                               i++;
+                       else if (c == '-'){
+                               neg = true;
+                               i++;
+                       }
+                       
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       val = checked (val * 10 + (c - '0'));
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       if (neg)
+                               val = -val;
+
+                       return val;
                }
 
                public static long Parse (string s, IFormatProvider fp)
index dc58709f017fc461e71a757f136cbd0da22f885c..272a840d7cb59b949e00a949436123d587ffe660 100644 (file)
@@ -54,7 +54,59 @@ namespace System {
 
                public static sbyte Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       sbyte val = 0;
+                       int len;
+                       int i;
+                       bool neg = false;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       c = s [i];
+                       if (c == '+')
+                               i++;
+                       else if (c == '-'){
+                               neg = true;
+                               i++;
+                       }
+                       
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       val = checked ((sbyte) (val * 10 + (c - '0')));
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       if (neg)
+                               val = checked ((sbyte) -val);
+
+                       return val;
                }
 
                public static sbyte Parse (string s, IFormatProvider fp)
index a3ad8d9abb3e8459d9b48d258275af7d18a5fc4c..c19b856674e8a62bd397830d361aa4e8057369e3 100644 (file)
@@ -46,7 +46,53 @@ namespace System {
 
                public static ushort Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       ushort val = 0;
+                       int len;
+                       int i;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       if (s [i] == '+')
+                               i++;
+
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       ushort d = (ushort) (c - '0');
+                                       
+                                       val = checked ((ushort) (val * 10 + d));
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       return val;
+
                }
 
                public static ushort Parse (string s, IFormatProvider fp)
index a935dcd4c30064be1ea820b7be83f0ed315ccddc..d3bb1b2ee87010b2049b74c044cf65569d90c2d1 100644 (file)
@@ -52,7 +52,53 @@ namespace System {
 
                public static uint Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       uint val = 0;
+                       int len;
+                       int i;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       if (s [i] == '+')
+                               i++;
+
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       uint d = (uint) (c - '0');
+                                       
+                                       val = checked (val * 10 + d);
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       return val;
+
                }
 
                public static uint Parse (string s, IFormatProvider fp)
index c77b67429bec4a1e86c12605127d7fd986a20731..e13923d94c2388b61c817bda65cc28e6e817875f 100644 (file)
@@ -52,7 +52,53 @@ namespace System {
 
                public static ulong Parse (string s)
                {
-                       return Parse (s, NumberStyles.Integer, null);
+                       ulong val = 0;
+                       int len;
+                       int i;
+                       bool digits_seen = false;
+                       
+                       if (s == null)
+                               throw new ArgumentNullException (Locale.GetText ("s is null"));
+
+                       len = s.Length;
+
+                       char c;
+                       for (i = 0; i < len; i++){
+                               c = s [i];
+                               if (!Char.IsWhiteSpace (c))
+                                       break;
+                       }
+                       
+                       if (i == len)
+                               throw new FormatException ();
+
+                       if (s [i] == '+')
+                               i++;
+
+                       for (; i < len; i++){
+                               c = s [i];
+
+                               if (c >= '0' && c <= '9'){
+                                       uint d = (uint) (c - '0');
+                                       
+                                       val = checked (val * 10 + d);
+                                       digits_seen = true;
+                               } else {
+                                       if (Char.IsWhiteSpace (c)){
+                                               for (i++; i < len; i++){
+                                                       if (!Char.IsWhiteSpace (s [i]))
+                                                               throw new FormatException ();
+                                               }
+                                               break;
+                                       } else
+                                               throw new FormatException ();
+                               }
+                       }
+                       if (!digits_seen)
+                               throw new FormatException ();
+                       
+                       return val;
+
                }
 
                public static ulong Parse (string s, IFormatProvider fp)