diff options
author | Marek Safar <marek.safar@gmail.com> | 2015-01-20 14:22:55 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2015-01-20 14:23:29 +0300 |
commit | 429fd07df8e1320b1729c4085a5a6359e957fd57 (patch) | |
tree | 5149398a38d6c0cf75eb3268319570a5e693344b | |
parent | 7ef5dfed95c9ea014abbd39ed2af3c12f7210230 (diff) |
[corlib] Primitive types from reference sources
m--------- | external/referencesource | 0 | ||||
-rw-r--r-- | mcs/class/corlib/System.Globalization/NumberFormatInfo.cs | 36 | ||||
-rw-r--r-- | mcs/class/corlib/System.Security.Permissions/IsolatedStoragePermission.cs | 3 | ||||
-rw-r--r-- | mcs/class/corlib/System/Byte.cs | 225 | ||||
-rw-r--r-- | mcs/class/corlib/System/Double.cs | 9 | ||||
-rw-r--r-- | mcs/class/corlib/System/Int16.cs | 328 | ||||
-rw-r--r-- | mcs/class/corlib/System/Int32.cs | 764 | ||||
-rw-r--r-- | mcs/class/corlib/System/Int64.cs | 600 | ||||
-rw-r--r-- | mcs/class/corlib/System/SByte.cs | 331 | ||||
-rw-r--r-- | mcs/class/corlib/System/UInt16.cs | 229 | ||||
-rw-r--r-- | mcs/class/corlib/System/UInt32.cs | 588 | ||||
-rw-r--r-- | mcs/class/corlib/System/UInt64.cs | 590 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/ByteTest.cs | 16 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/Int16Test.cs | 12 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/Int32Test.cs | 12 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/Int64Test.cs | 14 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/UInt32Test.cs | 15 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/UInt64Test.cs | 12 | ||||
-rw-r--r-- | mcs/class/corlib/corlib.dll.sources | 19 |
19 files changed, 117 insertions, 3686 deletions
diff --git a/external/referencesource b/external/referencesource -Subproject cbec67c761ed7a6ccd994ee141718b3fd2f7a0e +Subproject fd7d7704b92bd375d9a91fe3254baf7a8584389 diff --git a/mcs/class/corlib/System.Globalization/NumberFormatInfo.cs b/mcs/class/corlib/System.Globalization/NumberFormatInfo.cs index 4d7559fe684..6305d30d46f 100644 --- a/mcs/class/corlib/System.Globalization/NumberFormatInfo.cs +++ b/mcs/class/corlib/System.Globalization/NumberFormatInfo.cs @@ -45,6 +45,7 @@ // Other than that this is totally ECMA compliant. // +using System.Diagnostics.Contracts; using System.Runtime.InteropServices; namespace System.Globalization { @@ -100,7 +101,7 @@ namespace System.Globalization { #pragma warning restore 649 #pragma warning disable 169 - string ansiCurrencySymbol; // TODO, MS.NET serializes this. + internal string ansiCurrencySymbol; // TODO, MS.NET serializes this. int m_dataItem; // Unused, but MS.NET serializes this. bool m_useUserOverride; // Unused, but MS.NET serializes this. bool validForParseAsNumber; // Unused, but MS.NET serializes this. @@ -961,12 +962,43 @@ namespace System.Globalization { { if (formatProvider != null) { NumberFormatInfo nfi; - nfi = (NumberFormatInfo)formatProvider.GetFormat(typeof(NumberFormatInfo)); + nfi = formatProvider.GetFormat (typeof (NumberFormatInfo)) as NumberFormatInfo; if (nfi != null) return nfi; } return CurrentInfo; } + + // private const NumberStyles InvalidNumberStyles = unchecked((NumberStyles) 0xFFFFFC00); + private const NumberStyles InvalidNumberStyles = ~(NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite + | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign + | NumberStyles.AllowParentheses | NumberStyles.AllowDecimalPoint + | NumberStyles.AllowThousands | NumberStyles.AllowExponent + | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowHexSpecifier); + + internal static void ValidateParseStyleInteger(NumberStyles style) { + // Check for undefined flags + if ((style & InvalidNumberStyles) != 0) { + throw new ArgumentException(Environment.GetResourceString("Argument_InvalidNumberStyles"), "style"); + } + Contract.EndContractBlock(); + if ((style & NumberStyles.AllowHexSpecifier) != 0) { // Check for hex number + if ((style & ~NumberStyles.HexNumber) != 0) { + throw new ArgumentException(Environment.GetResourceString("Arg_InvalidHexStyle")); + } + } + } + + internal static void ValidateParseStyleFloatingPoint(NumberStyles style) { + // Check for undefined flags + if ((style & InvalidNumberStyles) != 0) { + throw new ArgumentException(Environment.GetResourceString("Argument_InvalidNumberStyles"), "style"); + } + Contract.EndContractBlock(); + if ((style & NumberStyles.AllowHexSpecifier) != 0) { // Check for hex number + throw new ArgumentException(Environment.GetResourceString("Arg_HexStyleNotSupported")); + } + } } } diff --git a/mcs/class/corlib/System.Security.Permissions/IsolatedStoragePermission.cs b/mcs/class/corlib/System.Security.Permissions/IsolatedStoragePermission.cs index 3e5d958ddc5..d4450eb3ec7 100644 --- a/mcs/class/corlib/System.Security.Permissions/IsolatedStoragePermission.cs +++ b/mcs/class/corlib/System.Security.Permissions/IsolatedStoragePermission.cs @@ -119,8 +119,7 @@ namespace System.Security.Permissions { } a = esd.Attribute ("UserQuota"); if (a != null) { - Exception exc; - Int64.Parse (a, true, out m_userQuota, out exc); + m_userQuota = Int64.Parse (a, CultureInfo.InvariantCulture); } } } diff --git a/mcs/class/corlib/System/Byte.cs b/mcs/class/corlib/System/Byte.cs deleted file mode 100644 index 53ac1f2e926..00000000000 --- a/mcs/class/corlib/System/Byte.cs +++ /dev/null @@ -1,225 +0,0 @@ -// -// System.Byte.cs -// -// Author: -// Miguel de Icaza (miguel@ximian.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// - -// -// Copyright (C) 2004 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; - -namespace System -{ - [Serializable] - [System.Runtime.InteropServices.ComVisible (true)] - public struct Byte : IFormattable, IConvertible, IComparable, IComparable<Byte>, IEquatable <Byte> - { - public const byte MinValue = 0; - public const byte MaxValue = 255; - - internal byte m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if (!(value is System.Byte)) - throw new ArgumentException (Locale.GetText ("Value is not a System.Byte.")); - - return CompareTo ((byte) value); - } - - public override bool Equals (object obj) - { - if (!(obj is System.Byte)) - return false; - - return ((byte) obj) == m_value; - } - - public override int GetHashCode () - { - return m_value; - } - - public int CompareTo (byte value) - { - return m_value - value; - } - - public bool Equals (byte obj) - { - return m_value == obj; - } - - public static byte Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - public static byte Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - public static byte Parse (string s, NumberStyles style, IFormatProvider provider) - { - uint tmpResult = UInt32.Parse (s, style, provider); - if (tmpResult > Byte.MaxValue) - throw new OverflowException (Locale.GetText ("Value too large.")); - - return (byte) tmpResult; - } - - public static byte Parse (string s) - { - return Parse (s, NumberStyles.Integer, null); - } - - public static bool TryParse (string s, out byte result) - { - return TryParse (s, NumberStyles.Integer, null, out result); - } - - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) - { - uint tmpResult; - result = 0; - - if (!UInt32.TryParse (s, style, provider, out tmpResult)) - return false; - - if (tmpResult > Byte.MaxValue) - return false; - - result = (byte)tmpResult; - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - public TypeCode GetTypeCode () - { - return TypeCode.Byte; - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return m_value; - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException (); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/Double.cs b/mcs/class/corlib/System/Double.cs index 64ca3650e80..8eed93da056 100644 --- a/mcs/class/corlib/System/Double.cs +++ b/mcs/class/corlib/System/Double.cs @@ -275,7 +275,7 @@ namespace System { if (sidx == len) { if (!tryParse) - exc = Int32.GetFormatException (); + exc = GetFormatException (); return false; } } @@ -530,7 +530,7 @@ namespace System { double retVal; if (!ParseImpl (p, out retVal)) { if (!tryParse) - exc = Int32.GetFormatException (); + exc = GetFormatException (); return false; } if (IsPositiveInfinity(retVal) || IsNegativeInfinity(retVal)) { @@ -592,6 +592,11 @@ namespace System { return NumberFormatter.NumberToString (format, m_value, provider); } + static Exception GetFormatException () + { + return new FormatException ("Input string was not in the correct format"); + } + // =========== IConvertible Methods =========== // public TypeCode GetTypeCode () diff --git a/mcs/class/corlib/System/Int16.cs b/mcs/class/corlib/System/Int16.cs deleted file mode 100644 index 0a36558f1b9..00000000000 --- a/mcs/class/corlib/System/Int16.cs +++ /dev/null @@ -1,328 +0,0 @@ -// -// System.Int16.cs -// -// Author: -// Miguel de Icaza (miguel@ximian.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; - -namespace System { - - [Serializable] - [System.Runtime.InteropServices.ComVisible (true)] - public struct Int16 : IFormattable, IConvertible, IComparable, IComparable<Int16>, IEquatable <Int16> - { - - public const short MaxValue = 32767; - public const short MinValue = -32768; - - internal short m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if (!(value is System.Int16)) - throw new ArgumentException (Locale.GetText ("Value is not a System.Int16")); - - return CompareTo ((short) value); - } - - public override bool Equals (object obj) - { - if (!(obj is System.Int16)) - return false; - - return ((short) obj) == m_value; - } - - public override int GetHashCode () - { - return m_value; - } - - public int CompareTo (short value) - { - return m_value - value; - } - - public bool Equals (short obj) - { - return obj == m_value; - } - - internal static bool Parse (string s, bool tryParse, out short result, out Exception exc) - { - short val = 0; - int len; - int i, sign = 1; - bool digits_seen = false; - - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - len = s.Length; - - char c; - for (i = 0; i < len; i++){ - c = s [i]; - if (!Char.IsWhiteSpace (c)) - break; - } - - if (i == len) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - c = s [i]; - if (c == '+') - i++; - else if (c == '-'){ - sign = -1; - i++; - } - - for (; i < len; i++){ - c = s [i]; - if (c >= '0' && c <= '9'){ - byte d = (byte) (c - '0'); - - if (val > (MaxValue/10)) - goto overflow; - - if (val == (MaxValue/10)){ - if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1)))) - goto overflow; - if (sign == -1) - val = (short) ((val * sign * 10) - d); - else - val = (short) ((val * 10) + d); - - if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){ - result = val; - return true; - } - goto overflow; - } else - val = (short) (val * 10 + d); - - - digits_seen = true; - } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)) - return false; - - } - if (!digits_seen) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (sign == -1) - result = (short) (val * sign); - else - result = val; - - return true; - - overflow: - if (!tryParse) - exc = new OverflowException ("Value is too large"); - return false; - } - - public static short Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - public static short Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - public static short Parse (string s, NumberStyles style, IFormatProvider provider) - { - int tmpResult = Int32.Parse (s, style, provider); - if ((style & NumberStyles.AllowHexSpecifier) != 0) { - if (tmpResult >= 0 && tmpResult <= ushort.MaxValue) - return (short) tmpResult; - } else if (tmpResult <= MaxValue && tmpResult >= MinValue) { - return (short) tmpResult; - } - - throw new OverflowException ("Value too large or too small."); - } - - public static short Parse (string s) - { - Exception exc; - short res; - - if (!Parse (s, false, out res, out exc)) - throw exc; - - return res; - } - - public static bool TryParse (string s, out short result) - { - Exception exc; - if (!Parse (s, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) - { - int tmpResult; - result = 0; - - if (!Int32.TryParse (s, style, provider, out tmpResult)) - return false; - - if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue) - return false; - - result = (short)tmpResult; - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString(format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - - public TypeCode GetTypeCode () - { - return TypeCode.Int16; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Int16", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/Int32.cs b/mcs/class/corlib/System/Int32.cs deleted file mode 100644 index 7e2b0174752..00000000000 --- a/mcs/class/corlib/System/Int32.cs +++ /dev/null @@ -1,764 +0,0 @@ -// -// System.Int32.cs -// -// Authors: -// Miguel de Icaza (miguel@ximian.com) -// Marek Safar (marek.safar@gmail.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell, Inc (http://www.novell.com) -// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; -using System.Threading; -using System.Runtime.CompilerServices; - -namespace System { - - [Serializable] - [System.Runtime.InteropServices.ComVisible (true)] - public struct Int32 : IFormattable, IConvertible, IComparable, IComparable<Int32>, IEquatable <Int32> - { - - public const int MaxValue = 0x7fffffff; - public const int MinValue = -2147483648; - - // This field is looked up by name in the runtime - internal int m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if (!(value is System.Int32)) - throw new ArgumentException (Locale.GetText ("Value is not a System.Int32")); - - int xv = (int) value; - if (m_value == xv) - return 0; - if (m_value > xv) - return 1; - else - return -1; - } - - public override bool Equals (object obj) - { - if (!(obj is System.Int32)) - return false; - - return ((int) obj) == m_value; - } - - public override int GetHashCode () - { - return m_value; - } - - public int CompareTo (int value) - { - return - m_value == value ? 0 : - m_value > value ? 1 : - -1; - } - - public bool Equals (int obj) - { - return obj == m_value; - } - - internal static bool ProcessTrailingWhitespace (bool tryParse, string s, int position, ref Exception exc) - { - int len = s.Length; - - for (int i = position; i < len; i++){ - char c = s [i]; - - if (c != 0 && !Char.IsWhiteSpace (c)){ - if (!tryParse) - exc = GetFormatException (); - return false; - } - } - return true; - } - - internal static bool Parse (string s, bool tryParse, out int result, out Exception exc) - { - int val = 0; - int len; - int i, sign = 1; - bool digits_seen = false; - - result = 0; - exc = null; - NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - len = s.Length; - - char c; - for (i = 0; i < len; i++){ - c = s [i]; - if (!Char.IsWhiteSpace (c)) - break; - } - - if (i == len) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - var ps_length = nfi.PositiveSign.Length; - var ns_length = nfi.NegativeSign.Length; - if (len > ps_length && string.CompareOrdinalUnchecked (s, i, ns_length, nfi.PositiveSign, 0, ps_length) == 0) - i += ps_length; - else if (len > ns_length && string.CompareOrdinalUnchecked (s, i, ns_length, nfi.NegativeSign, 0, ns_length) == 0) { - sign = -1; - i += ns_length; - } - - for (; i < len; i++){ - c = s [i]; - - if (c == '\0') { - i = len; - continue; - } - - if (c >= '0' && c <= '9'){ - byte d = (byte) (c - '0'); - - if (val > (MaxValue/10)) - goto overflow; - - if (val == (MaxValue/10)){ - if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1)))) - goto overflow; - if (sign == -1) - val = (val * sign * 10) - d; - else - val = (val * 10) + d; - - if (ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){ - result = val; - return true; - } - goto overflow; - } else - val = val * 10 + d; - - digits_seen = true; - } else if (!ProcessTrailingWhitespace (tryParse, s, i, ref exc)) - return false; - } - if (!digits_seen) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - if (sign == -1) - result = val * sign; - else - result = val; - - return true; - - overflow: - if (!tryParse) - exc = new OverflowException ("Value is too large"); - return false; - } - - public static int Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - public static int Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - internal static bool CheckStyle (NumberStyles style, bool tryParse, ref Exception exc) - { - if ((style & NumberStyles.AllowHexSpecifier) != 0) { - NumberStyles ne = style ^ NumberStyles.AllowHexSpecifier; - if ((ne & NumberStyles.AllowLeadingWhite) != 0) - ne ^= NumberStyles.AllowLeadingWhite; - if ((ne & NumberStyles.AllowTrailingWhite) != 0) - ne ^= NumberStyles.AllowTrailingWhite; - if (ne != 0) { - if (!tryParse) - exc = new ArgumentException ( - "With AllowHexSpecifier only " + - "AllowLeadingWhite and AllowTrailingWhite " + - "are permitted."); - return false; - } - } else if ((uint) style > (uint) NumberStyles.Any){ - if (!tryParse) - exc = new ArgumentException ("Not a valid number style"); - return false; - } - - return true; - } - - internal static bool JumpOverWhite (ref int pos, string s, bool reportError, bool tryParse, ref Exception exc) - { - while (pos < s.Length && Char.IsWhiteSpace (s [pos])) - pos++; - - if (reportError && pos >= s.Length) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - return true; - } - - internal static void FindSign (ref int pos, string s, NumberFormatInfo nfi, - ref bool foundSign, ref bool negative) - { - if ((pos + nfi.NegativeSign.Length) <= s.Length && - s.IndexOfOrdinalUnchecked (nfi.NegativeSign, pos, nfi.NegativeSign.Length) == pos) { - negative = true; - foundSign = true; - pos += nfi.NegativeSign.Length; - } else if ((pos + nfi.PositiveSign.Length) <= s.Length && - s.IndexOfOrdinalUnchecked (nfi.PositiveSign, pos, nfi.PositiveSign.Length) == pos) { - negative = false; - pos += nfi.PositiveSign.Length; - foundSign = true; - } - } - - internal static void FindCurrency (ref int pos, - string s, - NumberFormatInfo nfi, - ref bool foundCurrency) - { - if ((pos + nfi.CurrencySymbol.Length) <= s.Length && - s.Substring (pos, nfi.CurrencySymbol.Length) == nfi.CurrencySymbol) { - foundCurrency = true; - pos += nfi.CurrencySymbol.Length; - } - } - - internal static bool FindExponent (ref int pos, string s, ref int exponent, bool tryParse, ref Exception exc) - { - exponent = 0; - bool neg; - - if (pos >= s.Length || (s [pos] != 'e' && s[pos] != 'E')) { - exc = null; - return false; - } - - var i = pos + 1; - if (i == s.Length) { - exc = tryParse ? null : GetFormatException (); - return true; - } - - neg = (s [i] == '-'); - if (neg) - i++; - - if (s [i] == '+' && ++i == s.Length) { - exc = tryParse ? null : GetFormatException (); - return true; - } - - long exp = 0; // temp long value - for (; i < s.Length; i++) { - if (!Char.IsDigit (s [i])) { - exc = tryParse ? null : GetFormatException (); - return true; - } - - // Reduce the risk of throwing an overflow exc - exp = checked (exp * 10 - (int) (s [i] - '0')); - if (exp < Int32.MinValue || exp > Int32.MaxValue) { - exc = tryParse ? null : new OverflowException ("Value too large or too small."); - return true; - } - } - - // exp value saved as negative, and neg tracks whether we had a negative - if (!neg) - exp = -exp; - - exc = null; - exponent = (int)exp; - pos = i; - return true; - } - - internal static bool FindOther (ref int pos, - string s, - string other) - { - if ((pos + other.Length) <= s.Length && - s.Substring (pos, other.Length) == other) { - pos += other.Length; - return true; - } - - return false; - } - - internal static bool ValidDigit (char e, bool allowHex) - { - return (e >= '0' && e <= '9') || (allowHex && ((e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f'))); - } - - internal static Exception GetFormatException () - { - return new FormatException ("Input string was not in the correct format"); - } - - internal static bool Parse (string s, NumberStyles style, IFormatProvider fp, bool tryParse, out int result, out Exception exc) - { - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - if (s.Length == 0) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - NumberFormatInfo nfi = null; - if (fp != null) { - Type typeNFI = typeof (System.Globalization.NumberFormatInfo); - nfi = fp.GetFormat (typeNFI) as NumberFormatInfo; - } - if (nfi == null) - nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; - - if (!CheckStyle (style, tryParse, ref exc)) - return false; - - bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0; - bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0; - bool AllowThousands = (style & NumberStyles.AllowThousands) != 0; - bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0; - bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0; - bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0; - bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0; - bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0; - bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0; - bool AllowExponent = (style & NumberStyles.AllowExponent) != 0; - - int pos = 0; - - if (AllowLeadingWhite && !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - bool foundOpenParentheses = false; - bool negative = false; - bool foundSign = false; - bool foundCurrency = false; - - // Pre-number stuff - if (AllowParentheses && s [pos] == '(') { - foundOpenParentheses = true; - foundSign = true; - negative = true; // MS always make the number negative when there parentheses - // even when NumberFormatInfo.NumberNegativePattern != 0!!! - pos++; - if (AllowLeadingWhite && !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - } - - if (AllowLeadingSign && !foundSign) { - // Sign + Currency - FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign) { - if (AllowLeadingWhite && !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (AllowCurrencySymbol) { - FindCurrency (ref pos, s, nfi, - ref foundCurrency); - if (foundCurrency && AllowLeadingWhite && - !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - // Currency + sign - FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency) { - if (AllowLeadingWhite && !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (foundCurrency) { - if (!foundSign && AllowLeadingSign) { - FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - if (foundSign && AllowLeadingWhite && - !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - } - - int number = 0; - int nDigits = 0; - int decimalPointPos = -1; - int digitValue; - char hexDigit; - - // Number stuff - while (pos < s.Length) { - - if (!ValidDigit (s [pos], AllowHexSpecifier)) { - if (AllowThousands && - (FindOther (ref pos, s, nfi.NumberGroupSeparator) - || FindOther (ref pos, s, nfi.CurrencyGroupSeparator))) - continue; - - if (AllowDecimalPoint && decimalPointPos < 0 && - (FindOther (ref pos, s, nfi.NumberDecimalSeparator) - || FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) { - decimalPointPos = nDigits; - continue; - } - - break; - } - - nDigits++; - - if (AllowHexSpecifier) { - hexDigit = s [pos++]; - if (Char.IsDigit (hexDigit)) - digitValue = (int) (hexDigit - '0'); - else if (Char.IsLower (hexDigit)) - digitValue = (int) (hexDigit - 'a' + 10); - else - digitValue = (int) (hexDigit - 'A' + 10); - - uint unumber = (uint)number; - if (tryParse){ - if ((unumber & 0xf0000000) != 0) - return false; - - number = (int) (unumber * 16u + (uint) digitValue); - } else { - number = (int)checked (unumber * 16u + (uint)digitValue); - } - - continue; - } - - try { - // Calculations done as negative - // (abs (MinValue) > abs (MaxValue)) - number = checked (number * 10 - (int) (s [pos++] - '0')); - } catch (OverflowException) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - } - - // Post number stuff - if (nDigits == 0) { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - int exponent = 0; - if (AllowExponent) - if (FindExponent (ref pos, s, ref exponent, tryParse, ref exc) && exc != null) - return false; - - if (AllowTrailingSign && !foundSign) { - // Sign + Currency - FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign && pos < s.Length) { - if (AllowTrailingWhite && !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - if (AllowTrailingWhite && pos < s.Length && !JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - // Currency + sign - FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency && pos < s.Length) { - if (AllowTrailingWhite && !JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (!foundSign && AllowTrailingSign) - FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - } - } - - if (AllowTrailingWhite && pos < s.Length && !JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - if (foundOpenParentheses) { - if (pos >= s.Length || s [pos++] != ')') { - if (!tryParse) - exc = GetFormatException (); - return false; - } - if (AllowTrailingWhite && pos < s.Length && !JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - } - - if (pos < s.Length && s [pos] != '\u0000') { - if (!tryParse) - exc = GetFormatException (); - return false; - } - - if (!negative && !AllowHexSpecifier){ - if (tryParse){ - long lval = -((long)number); - - if (lval < MinValue || lval > MaxValue) - return false; - number = (int) lval; - } else - number = checked (-number); - } - - if (decimalPointPos >= 0) - exponent = exponent - nDigits + decimalPointPos; - - if (exponent < 0) { - // - // Any non-zero values after decimal point are not allowed - // - int remainder; - number = Math.DivRem (number, (int) Math.Pow (10, -exponent), out remainder); - if (remainder != 0) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - } else if (exponent > 0) { - // - // result *= 10^exponent - // - // Reduce the risk of throwing an overflow exc - // - double res = checked (Math.Pow (10, exponent) * number); - if (res < MinValue || res > MaxValue) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - - number = (int)res; - } - - result = number; - return true; - } - - public static int Parse (string s) - { - Exception exc; - int res; - - if (!Parse (s, false, out res, out exc)) - throw exc; - - return res; - } - - public static int Parse (string s, NumberStyles style, IFormatProvider provider) - { - Exception exc; - int res; - - if (!Parse (s, style, provider, false, out res, out exc)) - throw exc; - - return res; - } - - public static bool TryParse (string s, out int result) - { - Exception exc; - if (!Parse (s, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out int result) - { - Exception exc; - if (!Parse (s, style, provider, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - - public TypeCode GetTypeCode () - { - return TypeCode.Int32; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Int32", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return m_value; - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/Int64.cs b/mcs/class/corlib/System/Int64.cs deleted file mode 100644 index 885f604e6bb..00000000000 --- a/mcs/class/corlib/System/Int64.cs +++ /dev/null @@ -1,600 +0,0 @@ -// -// System.Int64.cs -// -// Authors: -// Miguel de Icaza (miguel@ximian.com) -// Marek Safar (marek.safar@gmail.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell, Inc (http://www.novell.com) -// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; -using System.Threading; - -namespace System { - - [Serializable] - [System.Runtime.InteropServices.ComVisible (true)] - public struct Int64 : IFormattable, IConvertible, IComparable, IComparable<Int64>, IEquatable <Int64> - { - - public const long MaxValue = 0x7fffffffffffffff; - public const long MinValue = -9223372036854775808; - - internal long m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if (!(value is System.Int64)) - throw new ArgumentException (Locale.GetText ("Value is not a System.Int64")); - - long lValue = (long) value; - - if (m_value == lValue) - return 0; - - return (m_value < lValue) ? -1 : 1; - } - - public override bool Equals (object obj) - { - if (!(obj is System.Int64)) - return false; - - return ((long) obj) == m_value; - } - - public override int GetHashCode () - { - return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32); - } - - public int CompareTo (long value) - { - if (m_value == value) - return 0; - if (m_value > value) - return 1; - else - return -1; - } - - public bool Equals (long obj) - { - return obj == m_value; - } - - internal static bool Parse (string s, bool tryParse, out long result, out Exception exc) - { - long val = 0; - int len; - int i, sign = 1; - bool digits_seen = false; - - result = 0; - exc = null; - NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - len = s.Length; - - char c; - for (i = 0; i < len; i++){ - c = s [i]; - if (!Char.IsWhiteSpace (c)) - break; - } - - if (i == len) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (String.Compare (s, i, nfi.PositiveSign, 0, nfi.PositiveSign.Length) == 0) - i += nfi.PositiveSign.Length; - else if (String.Compare (s, i, nfi.NegativeSign, 0, nfi.NegativeSign.Length) == 0) { - sign = -1; - i += nfi.NegativeSign.Length; - } - - for (; i < len; i++){ - c = s [i]; - - if (c >= '0' && c <= '9'){ - byte d = (byte) (c - '0'); - - if (val > (MaxValue/10)) - goto overflow; - - if (val == (MaxValue/10)){ - if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1)))) - goto overflow; - if (sign == -1) - val = (val * sign * 10) - d; - else - val = (val * 10) + d; - - if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){ - result = val; - return true; - } - goto overflow; - } else - val = val * 10 + d; - - digits_seen = true; - } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)) - return false; - } - if (!digits_seen) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (sign == -1) - result = val * sign; - else - result = val; - - return true; - - overflow: - if (!tryParse) - exc = new OverflowException ("Value is too large"); - return false; - } - - public static long Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - public static long Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - internal static bool Parse (string s, NumberStyles style, IFormatProvider fp, bool tryParse, out long result, out Exception exc) - { - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - if (s.Length == 0) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - NumberFormatInfo nfi = null; - if (fp != null) { - Type typeNFI = typeof (System.Globalization.NumberFormatInfo); - nfi = fp.GetFormat (typeNFI) as NumberFormatInfo; - } - if (nfi == null) - nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; - - if (!Int32.CheckStyle (style, tryParse, ref exc)) - return false; - - bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0; - bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0; - bool AllowThousands = (style & NumberStyles.AllowThousands) != 0; - bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0; - bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0; - bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0; - bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0; - bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0; - bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0; - bool AllowExponent = (style & NumberStyles.AllowExponent) != 0; - - int pos = 0; - - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - bool foundOpenParentheses = false; - bool negative = false; - bool foundSign = false; - bool foundCurrency = false; - - // Pre-number stuff - if (AllowParentheses && s [pos] == '(') { - foundOpenParentheses = true; - foundSign = true; - negative = true; // MS always make the number negative when there parentheses - // even when NumberFormatInfo.NumberNegativePattern != 0!!! - pos++; - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - } - - if (AllowLeadingSign && !foundSign) { - // Sign + Currency - Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign) { - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (AllowCurrencySymbol) { - Int32.FindCurrency (ref pos, s, nfi, - ref foundCurrency); - if (foundCurrency && AllowLeadingWhite && - !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - // Currency + sign - Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency) { - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (foundCurrency) { - if (!foundSign && AllowLeadingSign) { - Int32.FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - if (foundSign && AllowLeadingWhite && - !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - } - - long number = 0; - int nDigits = 0; - int decimalPointPos = -1; - int digitValue; - char hexDigit; - - // Number stuff - while (pos < s.Length) { - - if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) { - if (AllowThousands && - (Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator) - || Int32.FindOther (ref pos, s, nfi.CurrencyGroupSeparator))) - continue; - - if (AllowDecimalPoint && decimalPointPos < 0 && - (Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator) - || Int32.FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) { - decimalPointPos = nDigits; - continue; - } - - break; - } - - nDigits++; - - if (AllowHexSpecifier) { - hexDigit = s [pos++]; - if (Char.IsDigit (hexDigit)) - digitValue = (int) (hexDigit - '0'); - else if (Char.IsLower (hexDigit)) - digitValue = (int) (hexDigit - 'a' + 10); - else - digitValue = (int) (hexDigit - 'A' + 10); - - ulong unumber = (ulong)number; - - // IMPROVME: We could avoid catching OverflowException - try { - number = (long)checked(unumber * 16ul + (ulong)digitValue); - } catch (OverflowException e){ - if (!tryParse) - exc = e; - return false; - } - - continue; - } - - try { - // Calculations done as negative - // (abs (MinValue) > abs (MaxValue)) - number = checked (number * 10 - (long) (s [pos++] - '0')); - } catch (OverflowException) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - } - - // Post number stuff - if (nDigits == 0) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - int exponent = 0; - if (AllowExponent) - if (Int32.FindExponent (ref pos, s, ref exponent, tryParse, ref exc) && exc != null) - return false; - - if (AllowTrailingSign && !foundSign) { - // Sign + Currency - Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign && pos < s.Length) { - if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - // Currency + sign - Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency && pos < s.Length) { - if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (!foundSign && AllowTrailingSign) - Int32.FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - } - } - - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - if (foundOpenParentheses) { - if (pos >= s.Length || s [pos++] != ')') { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - } - - if (pos < s.Length && s [pos] != '\u0000') { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (!negative && !AllowHexSpecifier){ - try { - number = checked (-number); - } catch (OverflowException e){ - if (!tryParse) - exc = e; - return false; - } - } - - if (decimalPointPos >= 0) - exponent = exponent - nDigits + decimalPointPos; - - if (exponent < 0) { - // - // Any non-zero values after decimal point are not allowed - // - long remainder; - number = Math.DivRem (number, (long) Math.Pow (10, -exponent), out remainder); - if (remainder != 0) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - } else if (exponent > 0) { - // - // result *= 10^exponent - // - // Reduce the risk of throwing an overflow exc - // - double res = checked (Math.Pow (10, exponent) * number); - if (res < MinValue || res > MaxValue) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - - number = (long)res; - } - - result = number; - return true; - } - - public static long Parse (string s) - { - Exception exc; - long res; - - if (!Parse (s, false, out res, out exc)) - throw exc; - - return res; - } - - public static long Parse (string s, NumberStyles style, IFormatProvider provider) - { - Exception exc; - long res; - - if (!Parse (s, style, provider, false, out res, out exc)) - throw exc; - - return res; - } - - public static bool TryParse (string s, out long result) - { - Exception exc; - if (!Parse (s, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out long result) - { - Exception exc; - if (!Parse (s, style, provider, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - - public TypeCode GetTypeCode () - { - return TypeCode.Int64; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Int64", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return m_value; - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/SByte.cs b/mcs/class/corlib/System/SByte.cs deleted file mode 100644 index 2df5f4e9119..00000000000 --- a/mcs/class/corlib/System/SByte.cs +++ /dev/null @@ -1,331 +0,0 @@ -// -// System.SByte.cs -// -// Author: -// Miguel de Icaza (miguel@ximian.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; - -namespace System -{ - [CLSCompliant(false)] - [Serializable] - [System.Runtime.InteropServices.ComVisible (true)] - public struct SByte : IFormattable, IConvertible, IComparable, IComparable<SByte>, IEquatable <SByte> - { - public const sbyte MinValue = -128; - public const sbyte MaxValue = 127; - - internal sbyte m_value; - - public int CompareTo (object obj) - { - if (obj == null) - return 1; - - if (!(obj is System.SByte)) - throw new ArgumentException (Locale.GetText ("Value is not a System.SByte.")); - - return CompareTo ((sbyte) obj); - } - - public override bool Equals (object obj) - { - if (!(obj is System.SByte)) - return false; - - return ((sbyte) obj) == m_value; - } - - public override int GetHashCode () - { - return m_value; - } - - public int CompareTo (sbyte value) - { - return m_value - value; - } - - public bool Equals (sbyte obj) - { - return obj == m_value; - } - - internal static bool Parse (string s, bool tryParse, out sbyte result, out Exception exc) - { - int ival = 0; - int len; - int i; - bool neg = false; - bool digits_seen = false; - - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - len = s.Length; - - char c; - for (i = 0; i < len; i++) { - c = s [i]; - if (!Char.IsWhiteSpace (c)) - break; - } - - if (i == len) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - c = s [i]; - if (c == '+') - i++; - else if (c == '-') { - neg = true; - i++; - } - - for (; i < len; i++) { - c = s [i]; - - if (c >= '0' && c <= '9') { - if (tryParse){ - int intval = ival * 10 - (int) (c - '0'); - - if (intval < MinValue) - return false; - ival = (sbyte) intval; - } else - ival = checked (ival * 10 - (int) (c - '0')); - digits_seen = true; - } else { - if (Char.IsWhiteSpace (c)) { - for (i++; i < len; i++) { - if (!Char.IsWhiteSpace (s [i])) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - } - break; - } else { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - } - } - if (!digits_seen) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - ival = neg ? ival : -ival; - if (ival < SByte.MinValue || ival > SByte.MaxValue) { - if (!tryParse) - exc = new OverflowException (); - return false; - } - - result = (sbyte)ival; - return true; - } - - [CLSCompliant(false)] - public static sbyte Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - [CLSCompliant(false)] - public static sbyte Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - [CLSCompliant(false)] - public static sbyte Parse (string s, NumberStyles style, IFormatProvider provider) - { - int tmpResult = Int32.Parse (s, style, provider); - if ((style & NumberStyles.AllowHexSpecifier) != 0) { - if (tmpResult >= 0 && tmpResult <= byte.MaxValue) - return (sbyte) tmpResult; - } else if (tmpResult <= MaxValue && tmpResult >= MinValue) { - return (sbyte) tmpResult; - } - - throw new OverflowException (Locale.GetText ("Value too large or too small.")); - } - - [CLSCompliant(false)] - public static sbyte Parse (string s) - { - Exception exc; - sbyte res; - - if (!Parse (s, false, out res, out exc)) - throw exc; - - return res; - } - - [CLSCompliant(false)] - public static bool TryParse (string s, out sbyte result) - { - Exception exc; - if (!Parse (s, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - [CLSCompliant(false)] - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out sbyte result) - { - int tmpResult; - result = 0; - - if (!Int32.TryParse (s, style, provider, out tmpResult)) - return false; - if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue) - return false; - - result = (sbyte)tmpResult; - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== ICovnertible Methods =========== // - public TypeCode GetTypeCode () - { - return TypeCode.SByte; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "SByte", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return m_value; - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/UInt16.cs b/mcs/class/corlib/System/UInt16.cs deleted file mode 100644 index 10f03176945..00000000000 --- a/mcs/class/corlib/System/UInt16.cs +++ /dev/null @@ -1,229 +0,0 @@ -// -// System.UInt16.cs -// -// Author: -// Miguel de Icaza (miguel@ximian.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; - -namespace System -{ - [Serializable] - [CLSCompliant (false)] - [System.Runtime.InteropServices.ComVisible (true)] - public struct UInt16 : IFormattable, IConvertible, IComparable, IComparable<UInt16>, IEquatable <UInt16> - { - public const ushort MaxValue = 0xffff; - public const ushort MinValue = 0; - - internal ushort m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if(!(value is System.UInt16)) - throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16.")); - - return CompareTo ((ushort) value); - } - - public override bool Equals (object obj) - { - if (!(obj is System.UInt16)) - return false; - - return ((ushort) obj) == m_value; - } - - public override int GetHashCode () - { - return m_value; - } - - public int CompareTo (ushort value) - { - return m_value - value; - } - - public bool Equals (ushort obj) - { - return obj == m_value; - } - - [CLSCompliant (false)] - public static ushort Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - [CLSCompliant (false)] - public static ushort Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - [CLSCompliant (false)] - public static ushort Parse (string s, NumberStyles style, IFormatProvider provider) - { - uint tmpResult = UInt32.Parse (s, style, provider); - if (tmpResult > UInt16.MaxValue) - throw new OverflowException (Locale.GetText ("Value too large.")); - - return (ushort) tmpResult; - } - - [CLSCompliant(false)] - public static ushort Parse (string s) - { - return Parse (s, NumberStyles.Number, null); - } - - [CLSCompliant(false)] - public static bool TryParse (string s, out ushort result) - { - return TryParse (s, NumberStyles.Integer, null, out result); - } - - [CLSCompliant(false)] - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result) - { - uint tmpResult; - result = 0; - - if (!UInt32.TryParse (s, style, provider, out tmpResult)) - return false; - - if (tmpResult > UInt16.MaxValue) - return false; - - result = (ushort)tmpResult; - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - public TypeCode GetTypeCode () - { - return TypeCode.UInt16; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "UInt16", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return m_value; - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/UInt32.cs b/mcs/class/corlib/System/UInt32.cs deleted file mode 100644 index 1aa5ce0d4a1..00000000000 --- a/mcs/class/corlib/System/UInt32.cs +++ /dev/null @@ -1,588 +0,0 @@ -// -// System.UInt32.cs -// -// Authors: -// Miguel de Icaza (miguel@ximian.com) -// Marek Safar (marek.safar@gmail.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell (http://www.novell.com) -// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; -using System.Threading; - -namespace System -{ - [Serializable] - [CLSCompliant (false)] - [System.Runtime.InteropServices.ComVisible (true)] - public struct UInt32 : IFormattable, IConvertible, IComparable, IComparable<UInt32>, IEquatable <UInt32> - { - public const uint MaxValue = 0xffffffff; - public const uint MinValue = 0; - - internal uint m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if (!(value is System.UInt32)) - throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32.")); - - uint val = (uint) value; - - if (m_value == val) - return 0; - - return (m_value < val) ? -1 : 1; - } - - public override bool Equals (object obj) - { - if (!(obj is System.UInt32)) - return false; - - return ((uint) obj) == m_value; - } - - public override int GetHashCode () - { - return (int) m_value; - } - - public int CompareTo (uint value) - { - if (m_value == value) - return 0; - if (m_value > value) - return 1; - else - return -1; - } - - public bool Equals (uint obj) - { - return obj == m_value; - } - - internal static bool Parse (string s, bool tryParse, out uint result, out Exception exc) - { - uint val = 0; - int len; - int i; - bool digits_seen = false; - bool has_negative_sign = false; - - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - len = s.Length; - - char c; - for (i = 0; i < len; i++) { - c = s [i]; - if (!Char.IsWhiteSpace (c)) - break; - } - - if (i == len) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (s [i] == '+') - i++; - else - if (s[i] == '-') { - i++; - has_negative_sign = true; - } - - for (; i < len; i++) { - c = s [i]; - - if (c >= '0' && c <= '9') { - uint d = (uint) (c - '0'); - - if ((val > MaxValue/10) || (val == (MaxValue / 10) && d > (MaxValue % 10))){ - if (!tryParse) - exc = new OverflowException (Locale.GetText ("Value is too large")); - return false; - } - val = (val * 10) + d; - digits_seen = true; - } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)){ - return false; - } - } - if (!digits_seen) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - // -0 is legal but other negative values are not - if (has_negative_sign && (val > 0)) { - if (!tryParse) - exc = new OverflowException ( - Locale.GetText ("Negative number")); - return false; - } - - result = val; - return true; - } - - internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out uint result, out Exception exc) - { - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - if (s.Length == 0) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - NumberFormatInfo nfi = null; - if (provider != null) { - Type typeNFI = typeof (NumberFormatInfo); - nfi = provider.GetFormat (typeNFI) as NumberFormatInfo; - } - if (nfi == null) - nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; - - if (!Int32.CheckStyle (style, tryParse, ref exc)) - return false; - - bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0; - bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0; - bool AllowThousands = (style & NumberStyles.AllowThousands) != 0; - bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0; - bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0; - bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0; - bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0; - bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0; - bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0; - bool AllowExponent = (style & NumberStyles.AllowExponent) != 0; - - int pos = 0; - - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - bool foundOpenParentheses = false; - bool negative = false; - bool foundSign = false; - bool foundCurrency = false; - - // Pre-number stuff - if (AllowParentheses && s [pos] == '(') { - foundOpenParentheses = true; - foundSign = true; - negative = true; // MS always make the number negative when there parentheses - // even when NumberFormatInfo.NumberNegativePattern != 0!!! - pos++; - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - } - - if (AllowLeadingSign && !foundSign) { - // Sign + Currency - Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign) { - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (AllowCurrencySymbol) { - Int32.FindCurrency (ref pos, s, nfi, - ref foundCurrency); - if (foundCurrency && AllowLeadingWhite && - !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - // Currency + sign - Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency) { - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (foundCurrency) { - if (!foundSign && AllowLeadingSign) { - Int32.FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - if (foundSign && AllowLeadingWhite && - !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - } - - uint number = 0; - int nDigits = 0; - int decimalPointPos = -1; - uint digitValue; - char hexDigit; - - // Number stuff - // Just the same as Int32, but this one adds instead of substract - while (pos < s.Length) { - - if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) { - if (AllowThousands && - (Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator) - || Int32.FindOther (ref pos, s, nfi.CurrencyGroupSeparator))) - continue; - - if (AllowDecimalPoint && decimalPointPos < 0 && - (Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator) - || Int32.FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) { - decimalPointPos = nDigits; - continue; - } - - break; - } - - nDigits++; - - if (AllowHexSpecifier) { - hexDigit = s [pos++]; - if (Char.IsDigit (hexDigit)) - digitValue = (uint) (hexDigit - '0'); - else if (Char.IsLower (hexDigit)) - digitValue = (uint) (hexDigit - 'a' + 10); - else - digitValue = (uint) (hexDigit - 'A' + 10); - - if (tryParse){ - ulong l = number * 16 + digitValue; - - if (l > MaxValue) - return false; - number = (uint) l; - } else - number = checked (number * 16 + digitValue); - - continue; - } - - try { - number = checked (number * 10 + (uint) (s [pos++] - '0')); - } catch (OverflowException) { - if (!tryParse) - exc = new OverflowException (Locale.GetText ("Value too large or too small.")); - return false; - } - } - - // Post number stuff - if (nDigits == 0) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - int exponent = 0; - if (AllowExponent) - if (Int32.FindExponent (ref pos, s, ref exponent, tryParse, ref exc) && exc != null) - return false; - - if (AllowTrailingSign && !foundSign) { - // Sign + Currency - Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign && pos < s.Length) { - if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - // Currency + sign - Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency && pos < s.Length) { - if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (!foundSign && AllowTrailingSign) - Int32.FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - } - } - - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - if (foundOpenParentheses) { - if (pos >= s.Length || s [pos++] != ')') { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - } - - if (pos < s.Length && s [pos] != '\u0000') { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - // -0 is legal but other negative values are not - if (negative && (number > 0)) { - if (!tryParse) - exc = new OverflowException ( - Locale.GetText ("Negative number")); - return false; - } - - if (decimalPointPos >= 0) - exponent = exponent - nDigits + decimalPointPos; - - if (exponent < 0) { - // - // Any non-zero values after decimal point are not allowed - // - long remainder; - number = (uint) Math.DivRem (number, (int) Math.Pow (10, -exponent), out remainder); - if (remainder != 0) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - } else if (exponent > 0) { - // - // result *= 10^exponent - // - // Reduce the risk of throwing an overflow exc - // - double res = checked (Math.Pow (10, exponent) * number); - if (res < MinValue || res > MaxValue) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - - number = (uint)res; - } - - result = number; - return true; - } - - [CLSCompliant (false)] - public static uint Parse (string s) - { - Exception exc; - uint res; - - if (!Parse (s, false, out res, out exc)) - throw exc; - - return res; - } - - [CLSCompliant (false)] - public static uint Parse (string s, NumberStyles style, IFormatProvider provider) - { - Exception exc; - uint res; - - if (!Parse (s, style, provider, false, out res, out exc)) - throw exc; - - return res; - } - - [CLSCompliant (false)] - public static uint Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - [CLSCompliant (false)] - public static uint Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - [CLSCompliant (false)] - public static bool TryParse (string s, out uint result) - { - Exception exc; - if (!Parse (s, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - [CLSCompliant (false)] - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out uint result) - { - Exception exc; - if (!Parse (s, style, provider, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - public TypeCode GetTypeCode () - { - return TypeCode.UInt32; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Uint32", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte (IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return m_value; - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return System.Convert.ToUInt64 (m_value); - } - } -} diff --git a/mcs/class/corlib/System/UInt64.cs b/mcs/class/corlib/System/UInt64.cs deleted file mode 100644 index d99fdf4650f..00000000000 --- a/mcs/class/corlib/System/UInt64.cs +++ /dev/null @@ -1,590 +0,0 @@ -// -// System.UInt64.cs -// -// Authors: -// Miguel de Icaza (miguel@ximian.com) -// Marek Safar (marek.safar@gmail.com) -// -// (C) Ximian, Inc. http://www.ximian.com -// Copyright (C) 2004 Novell, Inc (http://www.novell.com) -// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Globalization; -using System.Threading; - -namespace System -{ - [Serializable] - [CLSCompliant (false)] - [System.Runtime.InteropServices.ComVisible (true)] - public struct UInt64 : IFormattable, IConvertible, IComparable, IComparable<UInt64>, IEquatable <UInt64> - { - public const ulong MaxValue = 0xffffffffffffffff; - public const ulong MinValue = 0; - - internal ulong m_value; - - public int CompareTo (object value) - { - if (value == null) - return 1; - - if (!(value is System.UInt64)) - throw new ArgumentException (Locale.GetText ("Value is not a System.UInt64.")); - - ulong int64 = (ulong) value; - - if (m_value == int64) - return 0; - - return (m_value < int64) ? -1 : 1; - } - - public override bool Equals (object obj) - { - if (!(obj is System.UInt64)) - return false; - - return ((ulong) obj) == m_value; - } - - public override int GetHashCode () - { - return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32); - } - - public int CompareTo (ulong value) - { - if (m_value == value) - return 0; - if (m_value > value) - return 1; - else - return -1; - } - - public bool Equals (ulong obj) - { - return obj == m_value; - } - - [CLSCompliant (false)] - public static ulong Parse (string s) - { - Exception exc; - ulong result; - - if (!Parse (s, false, out result, out exc)) - throw exc; - - return result; - } - - internal static bool Parse (string s, bool tryParse, out ulong result, out Exception exc) - { - ulong val = 0; - int len; - int i; - bool digits_seen = false; - bool has_negative_sign = false; - - exc = null; - result = 0; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - len = s.Length; - - char c; - for (i = 0; i < len; i++) { - c = s [i]; - if (!Char.IsWhiteSpace (c)) - break; - } - - if (i == len) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (s [i] == '+') - i++; - else if (s [i] == '-') { - i++; - has_negative_sign = true; - } - - // Actual number stuff - for (; i < len; i++) { - c = s [i]; - - if (c >= '0' && c <= '9') { - uint d = (uint) (c - '0'); - - if (val > MaxValue / 10 || (val == MaxValue / 10 && d > MaxValue % 10)) { - if (!tryParse) - exc = new OverflowException ("Value is too large."); - return false; - } - - val = (val * 10) + d; - digits_seen = true; - } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)) - return false; - } - - if (!digits_seen) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (has_negative_sign && val > 0) { - if (!tryParse) - exc = new OverflowException ("Negative number."); - return false; - } - - result = val; - return true; - } - - [CLSCompliant (false)] - public static ulong Parse (string s, IFormatProvider provider) - { - return Parse (s, NumberStyles.Integer, provider); - } - - [CLSCompliant (false)] - public static ulong Parse (string s, NumberStyles style) - { - return Parse (s, style, null); - } - - internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out ulong result, out Exception exc) - { - result = 0; - exc = null; - - if (s == null) { - if (!tryParse) - exc = new ArgumentNullException ("s"); - return false; - } - - if (s.Length == 0) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - NumberFormatInfo nfi = null; - if (provider != null) { - Type typeNFI = typeof (NumberFormatInfo); - nfi = provider.GetFormat (typeNFI) as NumberFormatInfo; - } - if (nfi == null) - nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; - - if (!Int32.CheckStyle (style, tryParse, ref exc)) - return false; - - bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0; - bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0; - bool AllowThousands = (style & NumberStyles.AllowThousands) != 0; - bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0; - bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0; - bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0; - bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0; - bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0; - bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0; - bool AllowExponent = (style & NumberStyles.AllowExponent) != 0; - - int pos = 0; - - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - bool foundOpenParentheses = false; - bool negative = false; - bool foundSign = false; - bool foundCurrency = false; - - // Pre-number stuff - if (AllowParentheses && s [pos] == '(') { - foundOpenParentheses = true; - foundSign = true; - negative = true; // MS always make the number negative when there parentheses - // even when NumberFormatInfo.NumberNegativePattern != 0!!! - pos++; - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - - if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - } - - if (AllowLeadingSign && !foundSign) { - // Sign + Currency - Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign) { - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (AllowCurrencySymbol) { - Int32.FindCurrency (ref pos, s, nfi, - ref foundCurrency); - if (foundCurrency && AllowLeadingWhite && - !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - // Currency + sign - Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency) { - if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (foundCurrency) { - if (!foundSign && AllowLeadingSign) { - Int32.FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - if (foundSign && AllowLeadingWhite && - !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - } - } - - ulong number = 0; - int nDigits = 0; - int decimalPointPos = -1; - ulong digitValue; - char hexDigit; - - // Number stuff - // Just the same as Int32, but this one adds instead of substract - while (pos < s.Length) { - - if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) { - if (AllowThousands && - (Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator) - || Int32.FindOther (ref pos, s, nfi.CurrencyGroupSeparator))) - continue; - - if (AllowDecimalPoint && decimalPointPos < 0 && - (Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator) - || Int32.FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) { - decimalPointPos = nDigits; - continue; - } - - break; - } - - nDigits++; - - if (AllowHexSpecifier) { - hexDigit = s [pos++]; - if (Char.IsDigit (hexDigit)) - digitValue = (ulong) (hexDigit - '0'); - else if (Char.IsLower (hexDigit)) - digitValue = (ulong) (hexDigit - 'a' + 10); - else - digitValue = (ulong) (hexDigit - 'A' + 10); - - if (tryParse){ - // Any number above 32 will do - bool can_overflow = number > 0xffff; - - number = number * 16 + digitValue; - - if (can_overflow && number < 16) - return false; - } else - number = checked (number * 16 + digitValue); - - continue; - } - - try { - number = checked (number * 10 + (ulong) (s [pos++] - '0')); - } catch (OverflowException) { - if (!tryParse) - exc = new OverflowException (Locale.GetText ("Value too large or too small.")); - return false; - } - } - - // Post number stuff - if (nDigits == 0) { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - int exponent = 0; - if (AllowExponent) - if (Int32.FindExponent (ref pos, s, ref exponent, tryParse, ref exc) && exc != null) - return false; - - if (AllowTrailingSign && !foundSign) { - // Sign + Currency - Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative); - if (foundSign && pos < s.Length) { - if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - } - } - - if (AllowCurrencySymbol && !foundCurrency) { - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - // Currency + sign - Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency); - if (foundCurrency && pos < s.Length) { - if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc)) - return false; - if (!foundSign && AllowTrailingSign) - Int32.FindSign (ref pos, s, nfi, ref foundSign, - ref negative); - } - } - - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - - if (foundOpenParentheses) { - if (pos >= s.Length || s [pos++] != ')') { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc)) - return false; - } - - if (pos < s.Length && s [pos] != '\u0000') { - if (!tryParse) - exc = Int32.GetFormatException (); - return false; - } - - // -0 is legal but other negative values are not - if (negative && (number > 0)) { - if (!tryParse) - exc = new OverflowException ( - Locale.GetText ("Negative number")); - return false; - } - - if (decimalPointPos >= 0) - exponent = exponent - nDigits + decimalPointPos; - - if (exponent < 0) { - // - // Any non-zero values after decimal point are not allowed - // - long remainder; - number = (ulong) Math.DivRem ((long) number, (long) Math.Pow (10, -exponent), out remainder); - if (remainder != 0) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - } else if (exponent > 0) { - // - // result *= 10^exponent - // - // Reduce the risk of throwing an overflow exc - // - double res = checked (Math.Pow (10, exponent) * number); - if (res < MinValue || res > MaxValue) { - if (!tryParse) - exc = new OverflowException ("Value too large or too small."); - return false; - } - - number = (ulong)res; - } - - result = number; - return true; - } - - [CLSCompliant (false)] - public static ulong Parse (string s, NumberStyles style, IFormatProvider provider) - { - Exception exc; - ulong res; - - if (!Parse (s, style, provider, false, out res, out exc)) - throw exc; - - return res; - } - - - [CLSCompliant (false)] - public static bool TryParse (string s, out ulong result) - { - Exception exc; - if (!Parse (s, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - [CLSCompliant (false)] - public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ulong result) - { - Exception exc; - if (!Parse (s, style, provider, true, out result, out exc)) { - result = 0; - return false; - } - - return true; - } - - public override string ToString () - { - return NumberFormatter.NumberToString (m_value, null); - } - - public string ToString (IFormatProvider provider) - { - return NumberFormatter.NumberToString (m_value, provider); - } - - public string ToString (string format) - { - return ToString (format, null); - } - - public string ToString (string format, IFormatProvider provider) - { - return NumberFormatter.NumberToString (format, m_value, provider); - } - - // =========== IConvertible Methods =========== // - public TypeCode GetTypeCode () - { - return TypeCode.UInt64; - } - - bool IConvertible.ToBoolean (IFormatProvider provider) - { - return System.Convert.ToBoolean (m_value); - } - - byte IConvertible.ToByte (IFormatProvider provider) - { - return System.Convert.ToByte (m_value); - } - - char IConvertible.ToChar (IFormatProvider provider) - { - return System.Convert.ToChar (m_value); - } - - DateTime IConvertible.ToDateTime (IFormatProvider provider) - { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Uint64", "DateTime")); - } - - decimal IConvertible.ToDecimal (IFormatProvider provider) - { - return System.Convert.ToDecimal (m_value); - } - - double IConvertible.ToDouble (IFormatProvider provider) - { - return System.Convert.ToDouble (m_value); - } - - short IConvertible.ToInt16 (IFormatProvider provider) - { - return System.Convert.ToInt16 (m_value); - } - - int IConvertible.ToInt32 (IFormatProvider provider) - { - return System.Convert.ToInt32 (m_value); - } - - long IConvertible.ToInt64 (IFormatProvider provider) - { - return System.Convert.ToInt64 (m_value); - } - - sbyte IConvertible.ToSByte(IFormatProvider provider) - { - return System.Convert.ToSByte (m_value); - } - - float IConvertible.ToSingle (IFormatProvider provider) - { - return System.Convert.ToSingle (m_value); - } - - object IConvertible.ToType (Type type, IFormatProvider provider) - { - return Convert.DefaultToType ((IConvertible)this, type, provider); - } - - ushort IConvertible.ToUInt16 (IFormatProvider provider) - { - return System.Convert.ToUInt16 (m_value); - } - - uint IConvertible.ToUInt32 (IFormatProvider provider) - { - return System.Convert.ToUInt32 (m_value); - } - - ulong IConvertible.ToUInt64 (IFormatProvider provider) - { - return m_value; - } - } -} diff --git a/mcs/class/corlib/Test/System/ByteTest.cs b/mcs/class/corlib/Test/System/ByteTest.cs index 43a14920553..8146db90639 100644 --- a/mcs/class/corlib/Test/System/ByteTest.cs +++ b/mcs/class/corlib/Test/System/ByteTest.cs @@ -190,10 +190,18 @@ public class ByteTest Assert.IsTrue (typeof(FormatException) == e.GetType(), NumberFormatInfo.CurrentInfo.CurrencySymbol+"42, NumberStyles.Integer, Nfi"); } - Assert.AreEqual (734, Int64.Parse ("734\0"), "#1"); - Assert.AreEqual (734, Int64.Parse ("734\0\0\0 \0"), "#2"); - Assert.AreEqual (734, Int64.Parse ("734\0\0\0 "), "#3"); - Assert.AreEqual (734, Int64.Parse ("734\0\0\0"), "#4"); + Assert.AreEqual (34, Byte.Parse ("34\0"), "#1"); + try { + Byte.Parse ("34\0\0\0 \0"); + Assert.Fail ("#2"); + } catch (FormatException) {} + + try { + Byte.Parse ("34\0\0\0 "); + Assert.Fail ("#3"); + } catch (FormatException) {} + + Assert.AreEqual (34, Byte.Parse ("34\0\0\0"), "#4"); } [Test] diff --git a/mcs/class/corlib/Test/System/Int16Test.cs b/mcs/class/corlib/Test/System/Int16Test.cs index 93317046963..66c132d055e 100644 --- a/mcs/class/corlib/Test/System/Int16Test.cs +++ b/mcs/class/corlib/Test/System/Int16Test.cs @@ -162,8 +162,16 @@ public class Int16Test } Assert.AreEqual (7345, Int16.Parse ("7345\0"), "#1"); - Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0 \0"), "#2"); - Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0 "), "#3"); + try { + Int16.Parse ("7345\0\0\0 \0"); + Assert.Fail ("#2"); + } catch (FormatException) {} + + try { + Int16.Parse ("7345\0\0\0 "); + Assert.Fail ("#3"); + } catch (FormatException) {} + Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4"); Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5"); diff --git a/mcs/class/corlib/Test/System/Int32Test.cs b/mcs/class/corlib/Test/System/Int32Test.cs index 506a6cbf86f..8d9a7fb03a0 100644 --- a/mcs/class/corlib/Test/System/Int32Test.cs +++ b/mcs/class/corlib/Test/System/Int32Test.cs @@ -270,8 +270,16 @@ public class Int32Test Int32.Parse ("123", new DateTimeFormatInfo ()); Assert.AreEqual (734561, Int32.Parse ("734561\0"), "C#43"); - Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0 \0"), "C#44"); - Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0 "), "C#45"); + try { + Int32.Parse ("734561\0\0\0 \0"); + Assert.Fail ("C#44"); + } catch (FormatException) {} + + try { + Int32.Parse ("734561\0\0\0 "); + Assert.Fail ("C#45"); + } catch (FormatException) {} + Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0"), "C#46"); Assert.AreEqual (0, Int32.Parse ("0+", NumberStyles.Any), "#50"); diff --git a/mcs/class/corlib/Test/System/Int64Test.cs b/mcs/class/corlib/Test/System/Int64Test.cs index b12395ea0c0..3bcc28c5c33 100644 --- a/mcs/class/corlib/Test/System/Int64Test.cs +++ b/mcs/class/corlib/Test/System/Int64Test.cs @@ -320,8 +320,18 @@ public class Int64Test Int64.Parse ("123", new DateTimeFormatInfo ()); Assert.AreEqual (734561, Int64.Parse ("734561\0"), "#21"); - Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0 \0"), "#22"); - Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0 "), "#23"); + try { + Int64.Parse ("734561\0\0\0 \0"); + Assert.Fail ("#22"); + } catch (FormatException) { + } + + try { + Int64.Parse ("734561\0\0\0 "); + Assert.Fail ("#23"); + } catch (FormatException) { + } + Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0"), "#24"); Assert.AreEqual (0, Int64.Parse ("0+", NumberStyles.Any), "#30"); diff --git a/mcs/class/corlib/Test/System/UInt32Test.cs b/mcs/class/corlib/Test/System/UInt32Test.cs index 2ae0d55a3de..9726e614afc 100644 --- a/mcs/class/corlib/Test/System/UInt32Test.cs +++ b/mcs/class/corlib/Test/System/UInt32Test.cs @@ -202,8 +202,16 @@ public class UInt32Test UInt32.Parse ("123", new DateTimeFormatInfo ()); Assert.AreEqual (734561, UInt32.Parse ("734561\0"), "C#43"); - Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0 \0"), "C#44"); - Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0 "), "C#45"); + try { + UInt32.Parse ("734561\0\0\0 \0"); + Assert.Fail ("C#44"); + } catch (FormatException) {} + + try { + UInt32.Parse ("734561\0\0\0 "); + Assert.Fail ("C#45"); + } catch (FormatException) {} + Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0"), "C#46"); Assert.AreEqual (0, UInt32.Parse ("0+", NumberStyles.Any), "#50"); @@ -321,7 +329,7 @@ public class UInt32Test Assert.AreEqual (false, UInt32.TryParse ("$42", NumberStyles.Integer, Nfi, out result)); Assert.AreEqual (false, UInt32.TryParse (" - 1 ", out result)); Assert.AreEqual (false, UInt32.TryParse (" - ", out result)); - Assert.AreEqual (true, UInt32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result)); + Assert.AreEqual (false, UInt32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result)); Assert.AreEqual (false, UInt32.TryParse ("10000000000", out result)); Assert.AreEqual (false, UInt32.TryParse ("-10000000000", out result)); Assert.AreEqual (true, UInt32.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result)); @@ -330,7 +338,6 @@ public class UInt32Test Assert.AreEqual (Int32.MaxValue + (uint)1, result); Assert.AreEqual (true, UInt32.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result)); Assert.AreEqual (uint.MaxValue, result); - Assert.AreEqual (true, UInt32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result)); Assert.IsFalse (uint.TryParse ("-", NumberStyles.AllowLeadingSign, Nfi, out result)); Assert.IsFalse (uint.TryParse (Nfi.CurrencySymbol + "-", NumberStyles.AllowLeadingSign | NumberStyles.AllowCurrencySymbol, Nfi, out result)); } diff --git a/mcs/class/corlib/Test/System/UInt64Test.cs b/mcs/class/corlib/Test/System/UInt64Test.cs index 7633fea270f..7542a3aefab 100644 --- a/mcs/class/corlib/Test/System/UInt64Test.cs +++ b/mcs/class/corlib/Test/System/UInt64Test.cs @@ -190,8 +190,16 @@ public class UInt64Test UInt64.Parse ("123", new DateTimeFormatInfo ()); Assert.AreEqual (734561, UInt64.Parse ("734561\0"), "C#43"); - Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0 \0"), "C#44"); - Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0 "), "C#45"); + try { + UInt64.Parse ("734561\0\0\0 \0"); + Assert.Fail ("C#44"); + } catch (FormatException) {} + + try { + UInt64.Parse ("734561\0\0\0 "); + Assert.Fail ("C#45"); + } catch (FormatException) {} + Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0"), "C#46"); Assert.AreEqual (0, UInt64.Parse ("0+", NumberStyles.Any), "#50"); diff --git a/mcs/class/corlib/corlib.dll.sources b/mcs/class/corlib/corlib.dll.sources index 3c5245b14ff..a81144f886f 100644 --- a/mcs/class/corlib/corlib.dll.sources +++ b/mcs/class/corlib/corlib.dll.sources @@ -111,7 +111,6 @@ System/BadImageFormatException.cs System/BitConverter.cs System/Boolean.cs System/Buffer.cs -System/Byte.cs System/CannotUnloadAppDomainException.cs System/CharEnumerator.cs System/CLSCompliantAttribute.cs @@ -180,9 +179,6 @@ System/IFormattable.cs System/IndexOutOfRangeException.cs System/InsufficientExecutionStackException.cs System/InsufficientMemoryException.cs -System/Int16.cs -System/Int32.cs -System/Int64.cs System/IntPtr.cs System/InvalidCastException.cs System/InvalidOperationException.cs @@ -238,7 +234,6 @@ System/RuntimeFieldHandle.cs System/RuntimeMethodHandle.cs System/RuntimeTypeHandle.cs System/ModuleHandle.cs -System/SByte.cs System/SerializableAttribute.cs System/Single.cs System/StackOverflowException.cs @@ -272,9 +267,6 @@ System/TypedReference.cs System/TypeInitializationException.cs System/TypeLoadException.cs System/TypeUnloadedException.cs -System/UInt16.cs -System/UInt32.cs -System/UInt64.cs System/UIntPtr.cs System/UnauthorizedAccessException.cs System/UnhandledExceptionEventArgs.cs @@ -1576,14 +1568,23 @@ ReferenceSources/JitHelpers.cs ../../../external/referencesource/mscorlib/system/__hresults.cs ../../../external/referencesource/mscorlib/system/AggregateException.cs ../../../external/referencesource/mscorlib/system/arraysegment.cs +../../../external/referencesource/mscorlib/system/byte.cs ../../../external/referencesource/mscorlib/system/char.cs ../../../external/referencesource/mscorlib/system/convert.cs -../../../external/referencesource/mscorlib/system/progress.cs +../../../external/referencesource/mscorlib/system/int16.cs +../../../external/referencesource/mscorlib/system/int32.cs +../../../external/referencesource/mscorlib/system/int64.cs ../../../external/referencesource/mscorlib/system/Lazy.cs +../../../external/referencesource/mscorlib/system/number.cs +../../../external/referencesource/mscorlib/system/progress.cs ../../../external/referencesource/mscorlib/system/resid.cs +../../../external/referencesource/mscorlib/system/sbyte.cs ../../../external/referencesource/mscorlib/system/stringcomparer.cs ../../../external/referencesource/mscorlib/system/throwhelper.cs ../../../external/referencesource/mscorlib/system/tuple.cs +../../../external/referencesource/mscorlib/system/uint16.cs +../../../external/referencesource/mscorlib/system/uint32.cs +../../../external/referencesource/mscorlib/system/uint64.cs ../../../external/referencesource/mscorlib/system/collections/arraylist.cs ../../../external/referencesource/mscorlib/system/collections/bitarray.cs |