2002-07-10 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System / IntegerFormatter.cs
index a717ea157da3e545f639b15bf14ee710b24891b1..b53ad1f46b68c991ba634fc258b9d8631664c220 100644 (file)
@@ -122,8 +122,8 @@ namespace System {
                                digitUpperTable[i] = (char) ('0' + i);
                        }
 
-                       char lc = 'a';
-                       char uc = 'A';
+                       char lc = (char ) ('a' - i);
+                       char uc = (char ) ('A' - i);
                        while (i < 16){
                                digitLowerTable[i] = (char) (lc + i);
                                digitUpperTable[i] = (char) (uc + i);
@@ -141,10 +141,11 @@ namespace System {
                        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 
                }
  
-               private static bool ParseFormat (string format, out char specifier,  out int precision)
+               private static bool ParseFormat (string format, out char specifier,  out int precision, out bool custom)
                {                                
                        precision = -1;
                        specifier = '\0';
+                       custom = false;
                        
                        int length = format.Length;
                        // TODO: Could an empty string be a custom format string?
@@ -177,7 +178,7 @@ namespace System {
                        }
                        
                        // We've got a custom format string.
-                       specifier = 'z';
+                       custom = true;
                        return true;
                }        
 
@@ -187,9 +188,14 @@ namespace System {
                {
                        char specifier;
                        int precision;
-                       
-                       if (!ParseFormat (format, out specifier, out precision))
+                       bool custom;
+
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
+                       
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
 
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
@@ -210,7 +216,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -220,9 +225,14 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
+                       
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
 
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
@@ -243,7 +253,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this insance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -253,10 +262,15 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
+
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);        
                        case 'C': return FormatCurrency (value, precision, nfi);        
@@ -276,7 +290,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -286,10 +299,15 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
+
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
                        case 'C': return FormatCurrency (value, precision, nfi);
@@ -309,7 +327,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }                       
@@ -320,10 +337,15 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
+                       
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
                        case 'C': return FormatCurrency (value, precision, nfi);
@@ -343,7 +365,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -354,10 +375,15 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
+                       
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
                        case 'C': return FormatCurrency (value, precision, nfi);
@@ -377,7 +403,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -388,10 +413,15 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
+                       
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
                        case 'C': return FormatCurrency (value, precision, nfi);
@@ -411,7 +441,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -422,10 +451,15 @@ namespace System {
                {
                        char specifier;
                        int precision;
+                       bool custom;
                        
-                       if (!ParseFormat (format, out specifier, out precision))
+                       if (!ParseFormat (format, out specifier, out precision, out custom))
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        
+                       if (custom){
+                               return FormatCustom (format, value, nfi);
+                       }
+                       
                        switch(specifier) {
                        case 'c': return FormatCurrency (value, precision, nfi);
                        case 'C': return FormatCurrency (value, precision, nfi);
@@ -445,7 +479,6 @@ namespace System {
                        case 'R': throw new FormatException (Locale.GetText ("The specified format cannot be used in this instance"));
                        case 'x': return FormatHexadecimal (value, precision, false);
                        case 'X': return FormatHexadecimal (value, precision, true);
-                       case 'z': return FormatCustom (format, value, nfi);
                        default: 
                                throw new FormatException (Locale.GetText ("The specified format '" + format + "' is invalid"));
                        }
@@ -1918,15 +1951,15 @@ namespace System {
                // discarded.
                //
 
-               private static string FormatGeneral (byte value, int precision, NumberFormatInfo nfi, bool upper) {
+               internal static string FormatGeneral (byte value, int precision, NumberFormatInfo nfi, bool upper) {
                        return FormatGeneral ((uint)value, precision, nfi, upper);
                }
 
-               private static string FormatGeneral (short value, int precision, NumberFormatInfo nfi, bool upper) {
+               internal static string FormatGeneral (short value, int precision, NumberFormatInfo nfi, bool upper) {
                        return FormatGeneral ((int)value, precision, nfi, upper);
                }
 
-               private static string FormatGeneral (int value, int precision, NumberFormatInfo nfi, bool upper) 
+               internal static string FormatGeneral (int value, int precision, NumberFormatInfo nfi, bool upper) 
                {
                        bool negative = (value < 0);
                        char[] tmp = new char [maxIntLength];
@@ -2033,7 +2066,7 @@ namespace System {
                        return new string (buffy, 0, position);
                }
 
-               private static string FormatGeneral (long value, int precision, NumberFormatInfo nfi, bool upper) 
+               internal static string FormatGeneral (long value, int precision, NumberFormatInfo nfi, bool upper) 
                {
                        bool negative = (value < 0);
                        char[] tmp = new char [maxLongLength];
@@ -2139,15 +2172,15 @@ namespace System {
                        return new string (buffy, 0, position);
                }
 
-               private static string FormatGeneral (sbyte value, int precision, NumberFormatInfo nfi, bool upper) {
+               internal static string FormatGeneral (sbyte value, int precision, NumberFormatInfo nfi, bool upper) {
                        return FormatGeneral ((int)value, precision, nfi, upper);
                }
 
-               private static string FormatGeneral (ushort value, int precision, NumberFormatInfo nfi, bool upper) {
+               internal static string FormatGeneral (ushort value, int precision, NumberFormatInfo nfi, bool upper) {
                        return FormatGeneral ((uint)value, precision, nfi, upper);
                }
 
-               private static string FormatGeneral (uint value, int precision, NumberFormatInfo nfi, bool upper) 
+               internal static string FormatGeneral (uint value, int precision, NumberFormatInfo nfi, bool upper) 
                {
                        char[] tmp = new char [maxIntLength];
                        int exponent = 0;
@@ -2237,7 +2270,7 @@ namespace System {
                        return new string (buffy, 0, position);
                }
 
-               private static string FormatGeneral (ulong value, int precision, NumberFormatInfo nfi, bool upper) 
+               internal static string FormatGeneral (ulong value, int precision, NumberFormatInfo nfi, bool upper) 
                {
                        char[] tmp = new char [maxLongLength];
                        int exponent = 0;
@@ -3661,7 +3694,7 @@ class FormatParse {
                        type = sec.TokenTypes [tokidx];
                        if (type == PH_0 || type == PH_NUMBER) {
                                //FIXME: PH_NUMBER should also check for significant digits
-                               Console.WriteLine ("group : {0}", group);
+                               // Console.WriteLine ("group : {0}", group);
                                int i = sec.tokens [tokidx].Length - 1;
                                while (i >= 0) {
                                        if (insertComma && group == 3) {
@@ -3751,6 +3784,9 @@ class FormatParse {
                if (signValue == 0 && nsections > 1)
                        section = 2;
 
+               if (number [0] == '-')
+                       number = number.Substring (1);
+
                FormatSection sec = sections [section];
                digits = AdjustDigits (number.ToString (), sec);
                if (digits.Length == 1 && digits [0] == '0')