[Mono-list] System.Enum patch for "x" format specifier

Jonathan Pryor jonpryor@vt.edu
03 Sep 2002 12:34:55 -0400


--=-EAH0lzx0Fmgllz6qW/n3
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Using System.Enum.Format with the "x" format specifier occasionally
generates a FormatException from Int64.Parse.  This is because
Enum.Format assumes that the result of ``enumerationValue.ToString()''
will return a number, which isn't always true.

See http://bugzilla.ximian.com/show_bug.cgi?id=29811 for more
information.

The attached patch addresses the issue, by introducing an intermediate
cast to the enumerations base type, and using the intermediate object to
create the output.

After applying the patch I'm able to recompile mcs and the class
library.

Should I commit in CVS?

Thanks,
 - Jon



--=-EAH0lzx0Fmgllz6qW/n3
Content-Disposition: attachment; filename=Enum-x-specifier.diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=Enum-x-specifier.diff; charset=UTF-8

Index: mcs/class/corlib/System/Enum.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/mcs/class/corlib/System/Enum.cs,v
retrieving revision 1.29
diff -u -r1.29 Enum.cs
--- mcs/class/corlib/System/Enum.cs	8 Jun 2002 12:41:54 -0000	1.29
+++ mcs/class/corlib/System/Enum.cs	3 Sep 2002 16:28:17 -0000
@@ -402,6 +402,40 @@
 			return v.GetHashCode ();
 		}
=20
+		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 =3D "x8";
+
+			switch (Type.GetTypeCode(enumType)) {
+				case TypeCode.Char:
+					// Char doesn't support ToString(format), so convert to an int and
+					// use that...
+					char v =3D (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 @@
 				break;
 			    case 'X':
 			    case 'x':
-				retVal =3D value.ToString();
-				long xValue =3D 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 =3D xValue.ToString("x8");
+				retVal =3D FormatSpecifier_X (enumType, value);
 				break;
 			    case 'D':
 			    case 'd':

--=-EAH0lzx0Fmgllz6qW/n3--