X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem%2FUInt64.cs;h=70f6bc97f130075c9576c0495d550434fbed4e65;hb=c39145af2464b19374fac41b252e07480ae1a197;hp=69845c42335ba34f4220e43c821aae362acf2f3f;hpb=b26f3e034aa5d31e0afc1acb3a68e2b41caa35a5;p=mono.git diff --git a/mcs/class/corlib/System/UInt64.cs b/mcs/class/corlib/System/UInt64.cs index 69845c42335..70f6bc97f13 100644 --- a/mcs/class/corlib/System/UInt64.cs +++ b/mcs/class/corlib/System/UInt64.cs @@ -111,14 +111,21 @@ namespace System return Parse (s, style, null); } - [CLSCompliant (false)] - public static ulong Parse (string s, NumberStyles style, IFormatProvider provider) + internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out ulong result) { + result = 0; + if (s == null) - throw new ArgumentNullException ("s"); + if (tryParse) + return false; + else + throw new ArgumentNullException ("s"); if (s.Length == 0) - throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); + if (tryParse) + return false; + else + throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); NumberFormatInfo nfi; if (provider != null) { @@ -131,7 +138,6 @@ namespace System Int32.CheckStyle (style); bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0; - bool AllowExponent = (style & NumberStyles.AllowExponent) != 0; bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0; bool AllowThousands = (style & NumberStyles.AllowThousands) != 0; bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0; @@ -162,9 +168,15 @@ namespace System pos = Int32.JumpOverWhite (pos, s, true); if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) - throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); + if (tryParse) + return false; + else + throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) - throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); + if (tryParse) + return false; + else + throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); } if (AllowLeadingSign && !foundSign) { @@ -237,7 +249,10 @@ namespace System // Allows decimal point as long as it's only // followed by zeroes. if (s [pos++] != '0') - throw new OverflowException (Locale.GetText ("Value too large or too small.")); + if (tryParse) + return false; + else + throw new OverflowException (Locale.GetText ("Value too large or too small.")); } else { nDigits++; @@ -246,14 +261,20 @@ namespace System number = checked (number * 10 + (ulong) (s [pos++] - '0')); } catch (OverflowException) { - throw new OverflowException (Locale.GetText ("Value too large or too small.")); + if (tryParse) + return false; + else + throw new OverflowException (Locale.GetText ("Value too large or too small.")); } } } while (pos < s.Length); // Post number stuff if (nDigits == 0) - throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); + if (tryParse) + return false; + else + throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); if (AllowTrailingSign && !foundSign) { // Sign + Currency @@ -282,23 +303,67 @@ namespace System if (foundOpenParentheses) { if (pos >= s.Length || s [pos++] != ')') - throw new FormatException (Locale.GetText - ("Input string was not in the correct format.")); + if (tryParse) + return false; + else + throw new FormatException (Locale.GetText + ("Input string was not in the correct format.")); if (AllowTrailingWhite && pos < s.Length) pos = Int32.JumpOverWhite (pos, s, false); } if (pos < s.Length && s [pos] != '\u0000') - throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); + if (tryParse) + return false; + else + throw new FormatException (Locale.GetText ("Input string was not in the correct format.")); // -0 is legal but other negative values are not if (negative && (number > 0)) { - throw new OverflowException ( - Locale.GetText ("Negative number")); + if (tryParse) + return false; + else + throw new OverflowException ( + Locale.GetText ("Negative number")); + } + + result = number; + return true; + } + + [CLSCompliant (false)] + public static ulong Parse (string s, NumberStyles style, IFormatProvider fp) { + ulong res; + + Parse (s, style, fp, false, out res); + + return res; + } + + +#if NET_2_0 + [CLSCompliant (false)] + public static bool TryParse (string s, out ulong result) { + try { + return Parse (s, NumberStyles.Integer, null, true, out result); } + catch (Exception) { + result = 0; + return false; + } + } - return number; + [CLSCompliant (false)] + public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ulong result) { + try { + return Parse (s, style, provider, true, out result); + } + catch (Exception) { + result = 0; + return false; + } } +#endif public override string ToString () {