- CultureInfo implements IFormatProvider
authorDaniel Stodden <stodden@mono-cvs.ximian.com>
Thu, 21 Feb 2002 06:56:17 +0000 (06:56 -0000)
committerDaniel Stodden <stodden@mono-cvs.ximian.com>
Thu, 21 Feb 2002 06:56:17 +0000 (06:56 -0000)
- DateTime/NumberformatInfo _FomatInfo ecma-lamespec corrections
- integer type cleanup accordingly

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

mcs/class/corlib/System.Globalization/CultureInfo.cs
mcs/class/corlib/System.Globalization/DateTimeFormatInfo.cs
mcs/class/corlib/System.Globalization/NumberFormatInfo.cs
mcs/class/corlib/System/Byte.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 d6f9c2bd5fcbad844635fe483ed7d1aee54f1c59..b065280bd0fb8e0b9445444026c50cafd8d3caef 100644 (file)
@@ -10,7 +10,7 @@ using System.Threading;
 
 namespace System.Globalization
 {
-       public class CultureInfo
+       public class CultureInfo : IFormatProvider
        {
                static CultureInfo invariant_culture_info;
                bool is_read_only;
@@ -806,6 +806,21 @@ namespace System.Globalization
                        }
                }
 
+               // 
+               // IFormatProvider implementation
+               //
+               public virtual object GetFormat( Type formatType )
+               {
+                       object format = null;
+
+                       if ( formatType == typeof(NumberFormatInfo) )
+                               format = NumberFormat;
+                       else if ( formatType == typeof(DateTimeFormatInfo) )
+                               format = DateTimeFormat;
+                       
+                       return format;
+               }
+
                //
                // Constructors
                //
index 48f91d199759e500ea63be97bbccea829a65d08c..937527dbefa8f41285786c654e2c6402ea2bf533 100644 (file)
@@ -81,21 +81,19 @@ namespace System.Globalization
                        _MonthNames = INVARIANT_MONTH_NAMES;
                }
                                
-               // LAMESPEC: this is not in ECMA specs\r
-               public static DateTimeFormatInfo GetInstance(IFormatProvider provider)\r
-               {\r
-                       if (provider != null) \r
-                       {\r
-                               if (provider is DateTimeFormatInfo) return (DateTimeFormatInfo) provider;\r
-                               if (provider is CultureInfo) return ((CultureInfo) provider).DateTimeFormat;\r
-                               return null;\r
-                       }\r
-                       else \r
-                       {\r
-                               return CurrentInfo;\r
-                       }\r
-               }\r
-\r
+               // LAMESPEC: this is not in ECMA specs
+               public static DateTimeFormatInfo GetInstance(IFormatProvider provider)
+               {
+                       if (provider != null) {
+                               DateTimeFormatInfo dtfi;
+                               dtfi = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
+                               if (dtfi != null)
+                                       return dtfi;
+                       }
+                       
+                       return CurrentInfo;
+               }
+
                public bool IsReadOnly {
                        get {
                                return readOnly;
index 4b2eb5e897b3642f5884ec5df7b65d8243e8d9c5..e3062012f81e05caeee82f4fec9618d39d2aa535 100644 (file)
@@ -636,22 +636,7 @@ namespace System.Globalization {
 
                public object GetFormat (Type formatType) 
                {
-                       // LAMESPEC: ECMA says we implement IFormatProvider, but doesn't define this
-                       //
-                       // From the .NET Framework SDK
-                       //
-                       // Parameters: formatType The Type of the formatting service required. 
-                       //
-                       // Return Value: The current instance of the NumberFormatInfo class, if formatType 
-                       // is the same as the type of the current instance; otherwise, a null reference
-                       //
-                       // Remarks: This method is invoked by the Format(String, IFormatProvider) method                        
-                       // supported by the base data types when this instance is passed as the 
-                       // IFormatProvider parameter. It implements IFormatProvider.GetFormat.
-
-                       if (formatType.Equals(this)) // LAMESPEC: Should this be IsInstanceOfType?
-                               return this;
-                       else return null;
+                       return (formatType == GetType()) ? this : null;
                }
                
                public object Clone () 
@@ -669,12 +654,16 @@ namespace System.Globalization {
                        return copy;
                }                       
 
-               public static NumberFormatInfo GetInstance(IFormatProvider formatProvider)
+               public static NumberFormatInfo GetInstance(IFormatProvider provider)
                {
-                       if (formatProvider == null) return NumberFormatInfo.CurrentInfo;
-                       NumberFormatInfo retval = (NumberFormatInfo)formatProvider.GetFormat(Type.GetType("System.Globalization.NumberFormatInfo"));
-                       if (retval == null) return NumberFormatInfo.CurrentInfo;
-                       return retval;
+                       if (provider != null) {
+                               NumberFormatInfo nfi;
+                               nfi = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo));
+                               if (nfi != null)
+                                       return nfi;
+                       }
+                       
+                       return CurrentInfo;
                }
        }
 }
index ebceb44251ef4fd5dba8de95c4a1a662c8e0ab8b..2da7a42234f75b61e1c322763f0dfcf923424134 100644 (file)
@@ -12,7 +12,6 @@ using System.Globalization;
 namespace System {
        
        public struct Byte : IComparable, IFormattable, IConvertible {
-               private static Type Type = typeof (byte);
                
                public const byte MinValue = 0;
                public const byte MaxValue = 255;
@@ -133,7 +132,7 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (string format)
@@ -143,26 +142,17 @@ namespace System {
 
                public string ToString (IFormatProvider provider)
                {
-                       return ToString ("G", provider);
+                       return ToString (null, provider);
                }
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString(format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //
index b85f45a1d9d4fdbccbb00980a2bfabcf1c92fa7d..2d28b8eba61a56c77e8b92297a339620ff7b5ccc 100644 (file)
@@ -12,7 +12,6 @@ using System.Globalization;
 namespace System {
        
        public struct Int16 : IComparable, IFormattable { //, IConvertible {
-               private static Type Type = typeof (short);
 
                public const short MaxValue =  32767;
                public const short MinValue = -32768;
@@ -125,12 +124,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -140,21 +139,12 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString(format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //
index ae4d5e870d80301dc8d70442ac9a904e97b08fd4..660af49be1a58570740682b93dabc62bca705baf 100644 (file)
@@ -12,8 +12,7 @@ using System.Globalization;
 namespace System {
        
        public struct Int32 : IComparable, IFormattable, IConvertible {
-               private static Type Type = typeof (int);
-               
+
                public const int MaxValue = 0x7fffffff;
                public const int MinValue = -2147483648;
                
@@ -126,12 +125,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -139,23 +138,14 @@ namespace System {
                        return ToString (format, null);
                }
 
-               public string ToString (string format, IFormatProvider fp)
+               public string ToString (string format, IFormatProvider fp )
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
-                       
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
+                       if ( format == null ) 
+                               format = "G";
 
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString (format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //
index 5abd24972602f429a9a3712056a5ca876cbd663a..c7d3d47776e9251c9ab3ec54ad6072e4c261a0df 100644 (file)
@@ -12,7 +12,6 @@ using System.Globalization;
 namespace System {
        
        public struct Int64 : IComparable, IFormattable { //, IConvertible {
-               private static Type Type = typeof (long);
 
                public const long MaxValue = 0x7fffffffffffffff;
                public const long MinValue = -9223372036854775808;
@@ -127,12 +126,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -142,21 +141,12 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString (format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //
index e1473817abd88ece3e2d6283d324700220d3e3b1..23323ea676c48c018f1f41eb15c1d3d30932bec1 100644 (file)
@@ -13,7 +13,6 @@ namespace System {
 
        [CLSCompliant(false)]
        public struct SByte : IComparable, IFormattable { //, IConvertible {
-               public static Type Type = typeof (sbyte);
 
                public const sbyte MinValue = -128;
                public const sbyte MaxValue = 127;
@@ -128,12 +127,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -143,21 +142,12 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString(format, nfi, value);
                }
 
                // =========== ICovnertible Methods =========== //
index b757cf5a78d59b547a57d2df60623ebe154456e3..c3ee009dc76e5d65dad22121928aba711daa38ba 100644 (file)
@@ -13,7 +13,6 @@ namespace System {
 
        [CLSCompliant(false)]
        public struct UInt16 : IComparable, IFormattable { //, IConvertible {
-               private static Type Type = typeof (ushort);
 
                public const ushort MaxValue = 0xffff;
                public const ushort MinValue = 0;
@@ -114,12 +113,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -129,21 +128,12 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString(format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //
index a08f51a86fe271cf18d8aee1643e5d194fbf3bba..a188e24afdb2519a153ec2a9ef0bfcc96714a13f 100644 (file)
@@ -13,7 +13,6 @@ namespace System {
 
        [CLSCompliant(false)]
        public struct UInt32 : IComparable, IFormattable { //, IConvertible {
-               public static Type Type = typeof (uint);
 
                public const uint MaxValue = 0xffffffff;
                public const uint MinValue = 0;
@@ -120,12 +119,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -135,21 +134,12 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString(format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //
index e13923d94c2388b61c817bda65cc28e6e817875f..6d96f722f8ddb43d753dd28fdbb3a9bcc9417c60 100644 (file)
@@ -13,8 +13,6 @@ namespace System {
 
        [CLSCompliant(false)]
        public struct UInt64 : IComparable, IFormattable { //, IConvertible {
-               private static Type Type = typeof (ulong);
-
                public const ulong MaxValue = 0xffffffffffffffff;
                public const ulong MinValue = 0;
                
@@ -126,12 +124,12 @@ namespace System {
 
                public override string ToString ()
                {
-                       return ToString ("G", null);
+                       return ToString (null, null);
                }
 
                public string ToString (IFormatProvider fp)
                {
-                       return ToString ("G", fp);
+                       return ToString (null, fp);
                }
 
                public string ToString (string format)
@@ -141,21 +139,12 @@ namespace System {
 
                public string ToString (string format, IFormatProvider fp)
                {
-                       string fmt;
-                       NumberFormatInfo nfi;
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
                        
-                       fmt = (format == null) ? "G" : format;
+                       if ( format == null )
+                               format = "G";
                        
-                       if (fp == null)
-                               nfi = NumberFormatInfo.CurrentInfo;
-                       else {
-                               nfi = (NumberFormatInfo) fp.GetFormat (Type);
-                               
-                               if (nfi == null)
-                                       nfi = NumberFormatInfo.CurrentInfo;
-                       }
-
-                       return IntegerFormatter.NumberToString (fmt, nfi, value);
+                       return IntegerFormatter.NumberToString(format, nfi, value);
                }
 
                // =========== IConvertible Methods =========== //