2002-05-09 Daniel Morgan <danmorg@sc.rr.com>
authorDaniel Morgan <monodanmorg@yahoo.com>
Thu, 9 May 2002 19:12:00 +0000 (19:12 -0000)
committerDaniel Morgan <monodanmorg@yahoo.com>
Thu, 9 May 2002 19:12:00 +0000 (19:12 -0000)
* Single.cs: copied ToString() and Parse() methods from
Double to Single and modified a tiny bit for Single.
There is still a FIXME for Double and Single about
passing the format and provider info to the icall too.

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

mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/Single.cs

index d6e35b277cf5a4777c342dc82d42f33b48499199..ff1a78c1d4f775677c79877b1b5edb3db846c39e 100644 (file)
@@ -1,3 +1,10 @@
+2002-05-09  Daniel Morgan <danmorg@sc.rr.com>
+
+       * Single.cs: copied ToString() and Parse() methods from 
+       Double to Single and modified a tiny bit for Single.  
+       There is still a FIXME for Double and Single about
+       passing the format and provider info to the icall too.\r
+
 2002-05-02  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * Int32.cs:
index afe4cf2d82d8a01897bee50bdb8722917b17a9f3..cefe405533dc7e47f2af53d5d4f7b69a04fcd751 100644 (file)
@@ -8,6 +8,7 @@
 //
 
 using System.Globalization;
+using System.Runtime.CompilerServices;
 
 namespace System {
        
@@ -84,10 +85,68 @@ namespace System {
                }
 
                [MonoTODO]
-               public static float Parse (string s, NumberStyles style, IFormatProvider fp)
+               public static float Parse (string s, NumberStyles style, IFormatProvider fp) 
                {
-                       // TODO: Implement me
-                       throw new NotImplementedException ();
+                       // FIXME: I copied this method from System.Double
+                       //        fix it for System.Single
+                       
+                       if (s == null) throw new ArgumentNullException();
+                       if (style > NumberStyles.Any) {
+                               throw new ArgumentException();
+                       }
+                       NumberFormatInfo format = NumberFormatInfo.GetInstance(fp);
+                       if (format == null) throw new Exception("How did this happen?");
+                       if (s == format.NaNSymbol) return Single.NaN;
+                       if (s == format.PositiveInfinitySymbol) return Single.PositiveInfinity;
+                       if (s == format.NegativeInfinitySymbol) return Single.NegativeInfinity;
+                       string[] sl;
+                       long integral = 0;
+                       long fraction = 0;
+                       long exponent = 1;
+                       float retval = 0;
+                       if ((style & NumberStyles.AllowLeadingWhite) != 0) {
+                               s.TrimStart(null);
+                       }
+                       if ((style & NumberStyles.AllowTrailingWhite) != 0) {
+                               s.TrimEnd(null);
+                       }
+                       sl = s.Split(new Char[] {'e', 'E'}, 2);
+                       if (sl.Length > 1) {
+                               if ((style & NumberStyles.AllowExponent) == 0) {
+                                       throw new FormatException();
+                               }
+                               exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
+                       }
+                       s = sl[0];
+                       sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
+                       if (sl.Length > 1) {
+                               if ((style & NumberStyles.AllowDecimalPoint) == 0) {
+                                       throw new FormatException();
+                               }
+                               fraction = long.Parse(sl[1], NumberStyles.None, format);
+                       }
+                       NumberStyles tempstyle = NumberStyles.None;
+                       if ((style & NumberStyles.AllowLeadingSign) != 0){
+                               tempstyle = NumberStyles.AllowLeadingSign;
+                       }
+
+                       if (sl[0].Length > 0)
+                               integral = long.Parse(sl[0], tempstyle, format);
+                       else
+                               integral = 0;
+
+                       retval = fraction;
+
+                       // FIXME: what about the zeros between the decimal point 
+                       // and the first non-zero digit?
+                       while (retval >1) retval /= 10;
+                       if (integral < 0){
+                               retval -= integral;
+                               retval = -retval;
+                       }
+                       else retval += integral;
+                       if (exponent != 1) retval *= (float) Math.Pow(10, exponent);
+                       return retval;
                }
 
                public override string ToString ()
@@ -108,10 +167,13 @@ namespace System {
                [MonoTODO]
                public string ToString (string format, IFormatProvider fp)
                {
-                       // TODO: Implement me.
-                       throw new NotImplementedException ();
+                       // FIXME: Need to pass format and provider info to this call too.
+                       return ToStringImpl(value);
                }
 
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               private static extern string ToStringImpl (float value);
+
                // ============= IConvertible Methods ============ //
 
                public TypeCode GetTypeCode ()