* Enum.cs: Fixed Enum.Format so that the "x" format specifier would work
authorJonathan Pryor <jpryor@novell.com>
Tue, 3 Sep 2002 17:28:30 +0000 (17:28 -0000)
committerJonathan Pryor <jpryor@novell.com>
Tue, 3 Sep 2002 17:28:30 +0000 (17:28 -0000)
           properly.

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

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

index 3a9bbb03ef061048b378cc6fbb47ec63be4bd813..124ffa2b33c0772e8c7081308bcd58d053b7c783 100644 (file)
@@ -1,3 +1,6 @@
+2002-09-03  Jonathan Pryor <jonpryor@vt.edu>
+       * Enum.cs: Fixed Enum.Format so that the "x" format specifier would work
+                  properly.
 
 2002-08-28  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
index 8f7d67fec042c36bb5129df1f99cca39a5de7e97..603480c2e4a13648695aec7c3110fdf519faa2dd 100644 (file)
@@ -402,6 +402,40 @@ namespace System {
                        return v.GetHashCode ();
                }
 
+               private static string FormatSpecifier_X (Type enumType, object value)
+               {
+                       // FIXME: Not sure if padding should always be with precision
+                       // 8, if it's culture specific, or what.  This works for me.
+                       const string format = "x8";
+
+                       switch (Type.GetTypeCode(enumType)) {
+                               case TypeCode.Char:
+                                       // Char doesn't support ToString(format), so convert to an int and
+                                       // use that...
+                                       char v = (char) value;
+                                       return Convert.ToInt32(v).ToString(format);
+                               case TypeCode.SByte:
+                                       return ((sbyte)value).ToString(format);
+                               case TypeCode.Byte:
+                                       return ((byte)value).ToString(format);
+                               case TypeCode.Int16:
+                                       return ((short)value).ToString(format);
+                               case TypeCode.UInt16:
+                                       return ((ushort)value).ToString(format);
+                               case TypeCode.Int32:
+                                       return ((int)value).ToString(format);
+                               case TypeCode.UInt32:
+                                       return ((uint)value).ToString(format);
+                               case TypeCode.Int64:
+                                       return ((long)value).ToString(format);
+                               case TypeCode.UInt64:
+                                       return ((ulong)value).ToString(format);
+                               default:
+                                       throw new Exception ("invalid type code for enumeration");
+                                       break;
+                       }
+               }
+
                [MonoTODO]
                public static string Format (Type enumType, object value, string format)
                {
@@ -439,11 +473,7 @@ namespace System {
                                break;
                            case 'X':
                            case 'x':
-                               retVal = value.ToString();
-                               long xValue = Int64.Parse(retVal);
-                               // FIXME: Not sure if padding should always be with precision
-                               // 8, if it's culture specific, or what.  This works for me.
-                               retVal = xValue.ToString("x8");
+                               retVal = FormatSpecifier_X (enumType, value);
                                break;
                            case 'D':
                            case 'd':