Simplify Convert (#33659)
authorPent Ploompuu <kaalikas@gmail.com>
Tue, 17 Mar 2020 05:13:07 +0000 (07:13 +0200)
committerGitHub <noreply@github.com>
Tue, 17 Mar 2020 05:13:07 +0000 (22:13 -0700)
* Simplify Convert

* Optimize Number.UInt32ToDecStr

src/libraries/System.Private.CoreLib/src/System/Byte.cs
src/libraries/System.Private.CoreLib/src/System/Convert.cs
src/libraries/System.Private.CoreLib/src/System/Decimal.cs
src/libraries/System.Private.CoreLib/src/System/Int16.cs
src/libraries/System.Private.CoreLib/src/System/Int32.cs
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
src/libraries/System.Private.CoreLib/src/System/SByte.cs
src/libraries/System.Private.CoreLib/src/System/UInt16.cs
src/libraries/System.Private.CoreLib/src/System/UInt32.cs
src/libraries/System.Private.CoreLib/src/System/UInt64.cs

index 88d349a..77b10c4 100644 (file)
@@ -166,7 +166,7 @@ namespace System
 
         public override string ToString()
         {
-            return Number.UInt32ToDecStr(m_value, -1);
+            return Number.UInt32ToDecStr(m_value);
         }
 
         public string ToString(string? format)
@@ -176,7 +176,7 @@ namespace System
 
         public string ToString(IFormatProvider? provider)
         {
-            return Number.FormatUInt32(m_value, null, provider);
+            return Number.UInt32ToDecStr(m_value);
         }
 
         public string ToString(string? format, IFormatProvider? provider)
index 50ee261..ec6b92a 100644 (file)
@@ -528,11 +528,7 @@ namespace System
             return (char)value;
         }
 
-        public static char ToChar(int value)
-        {
-            if (value < 0 || value > char.MaxValue) ThrowCharOverflowException();
-            return (char)value;
-        }
+        public static char ToChar(int value) => ToChar((uint)value);
 
         [CLSCompliant(false)]
         public static char ToChar(uint value)
@@ -541,11 +537,7 @@ namespace System
             return (char)value;
         }
 
-        public static char ToChar(long value)
-        {
-            if (value < 0 || value > char.MaxValue) ThrowCharOverflowException();
-            return (char)value;
-        }
+        public static char ToChar(long value) => ToChar((ulong)value);
 
         [CLSCompliant(false)]
         public static char ToChar(ulong value)
@@ -667,7 +659,7 @@ namespace System
         [CLSCompliant(false)]
         public static sbyte ToSByte(uint value)
         {
-            if (value > sbyte.MaxValue) ThrowSByteOverflowException();
+            if (value > (uint)sbyte.MaxValue) ThrowSByteOverflowException();
             return (sbyte)value;
         }
 
@@ -708,13 +700,13 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return sbyte.Parse(value, CultureInfo.CurrentCulture);
+            return sbyte.Parse(value);
         }
 
         [CLSCompliant(false)]
         public static sbyte ToSByte(string value, IFormatProvider? provider)
         {
-            return sbyte.Parse(value, NumberStyles.Integer, provider);
+            return sbyte.Parse(value, provider);
         }
 
         [CLSCompliant(false)]
@@ -757,13 +749,13 @@ namespace System
         [CLSCompliant(false)]
         public static byte ToByte(sbyte value)
         {
-            if (value < byte.MinValue) ThrowByteOverflowException();
+            if (value < 0) ThrowByteOverflowException();
             return (byte)value;
         }
 
         public static byte ToByte(short value)
         {
-            if (value < byte.MinValue || value > byte.MaxValue) ThrowByteOverflowException();
+            if ((uint)value > byte.MaxValue) ThrowByteOverflowException();
             return (byte)value;
         }
 
@@ -774,11 +766,7 @@ namespace System
             return (byte)value;
         }
 
-        public static byte ToByte(int value)
-        {
-            if (value < byte.MinValue || value > byte.MaxValue) ThrowByteOverflowException();
-            return (byte)value;
-        }
+        public static byte ToByte(int value) => ToByte((uint)value);
 
         [CLSCompliant(false)]
         public static byte ToByte(uint value)
@@ -787,11 +775,7 @@ namespace System
             return (byte)value;
         }
 
-        public static byte ToByte(long value)
-        {
-            if (value < byte.MinValue || value > byte.MaxValue) ThrowByteOverflowException();
-            return (byte)value;
-        }
+        public static byte ToByte(long value) => ToByte((ulong)value);
 
         [CLSCompliant(false)]
         public static byte ToByte(ulong value)
@@ -819,14 +803,14 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return byte.Parse(value, CultureInfo.CurrentCulture);
+            return byte.Parse(value);
         }
 
         public static byte ToByte(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0;
-            return byte.Parse(value, NumberStyles.Integer, provider);
+            return byte.Parse(value, provider);
         }
 
         public static byte ToByte(DateTime value)
@@ -887,7 +871,7 @@ namespace System
         [CLSCompliant(false)]
         public static short ToInt16(uint value)
         {
-            if (value > short.MaxValue) ThrowInt16OverflowException();
+            if (value > (uint)short.MaxValue) ThrowInt16OverflowException();
             return (short)value;
         }
 
@@ -928,14 +912,14 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return short.Parse(value, CultureInfo.CurrentCulture);
+            return short.Parse(value);
         }
 
         public static short ToInt16(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0;
-            return short.Parse(value, NumberStyles.Integer, provider);
+            return short.Parse(value, provider);
         }
 
         public static short ToInt16(DateTime value)
@@ -993,11 +977,7 @@ namespace System
         }
 
         [CLSCompliant(false)]
-        public static ushort ToUInt16(int value)
-        {
-            if (value < 0 || value > ushort.MaxValue) ThrowUInt16OverflowException();
-            return (ushort)value;
-        }
+        public static ushort ToUInt16(int value) => ToUInt16((uint)value);
 
         [CLSCompliant(false)]
         public static ushort ToUInt16(ushort value)
@@ -1013,11 +993,7 @@ namespace System
         }
 
         [CLSCompliant(false)]
-        public static ushort ToUInt16(long value)
-        {
-            if (value < 0 || value > ushort.MaxValue) ThrowUInt16OverflowException();
-            return (ushort)value;
-        }
+        public static ushort ToUInt16(long value) => ToUInt16((ulong)value);
 
         [CLSCompliant(false)]
         public static ushort ToUInt16(ulong value)
@@ -1049,7 +1025,7 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return ushort.Parse(value, CultureInfo.CurrentCulture);
+            return ushort.Parse(value);
         }
 
         [CLSCompliant(false)]
@@ -1057,7 +1033,7 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return ushort.Parse(value, NumberStyles.Integer, provider);
+            return ushort.Parse(value, provider);
         }
 
         [CLSCompliant(false)]
@@ -1116,7 +1092,7 @@ namespace System
         [CLSCompliant(false)]
         public static int ToInt32(uint value)
         {
-            if (value > int.MaxValue) ThrowInt32OverflowException();
+            if ((int)value < 0) ThrowInt32OverflowException();
             return (int)value;
         }
 
@@ -1177,14 +1153,14 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return int.Parse(value, CultureInfo.CurrentCulture);
+            return int.Parse(value);
         }
 
         public static int ToInt32(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0;
-            return int.Parse(value, NumberStyles.Integer, provider);
+            return int.Parse(value, provider);
         }
 
         public static int ToInt32(DateTime value)
@@ -1261,11 +1237,7 @@ namespace System
         }
 
         [CLSCompliant(false)]
-        public static uint ToUInt32(long value)
-        {
-            if (value < 0 || value > uint.MaxValue) ThrowUInt32OverflowException();
-            return (uint)value;
-        }
+        public static uint ToUInt32(long value) => ToUInt32((ulong)value);
 
         [CLSCompliant(false)]
         public static uint ToUInt32(ulong value)
@@ -1304,7 +1276,7 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return uint.Parse(value, CultureInfo.CurrentCulture);
+            return uint.Parse(value);
         }
 
         [CLSCompliant(false)]
@@ -1312,7 +1284,7 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return uint.Parse(value, NumberStyles.Integer, provider);
+            return uint.Parse(value, provider);
         }
 
         [CLSCompliant(false)]
@@ -1382,7 +1354,7 @@ namespace System
         [CLSCompliant(false)]
         public static long ToInt64(ulong value)
         {
-            if (value > long.MaxValue) ThrowInt64OverflowException();
+            if ((long)value < 0) ThrowInt64OverflowException();
             return (long)value;
         }
 
@@ -1410,14 +1382,14 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return long.Parse(value, CultureInfo.CurrentCulture);
+            return long.Parse(value);
         }
 
         public static long ToInt64(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0;
-            return long.Parse(value, NumberStyles.Integer, provider);
+            return long.Parse(value, provider);
         }
 
         public static long ToInt64(DateTime value)
@@ -1529,7 +1501,7 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return ulong.Parse(value, CultureInfo.CurrentCulture);
+            return ulong.Parse(value);
         }
 
         [CLSCompliant(false)]
@@ -1537,7 +1509,7 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return ulong.Parse(value, NumberStyles.Integer, provider);
+            return ulong.Parse(value, provider);
         }
 
         [CLSCompliant(false)]
@@ -1629,14 +1601,14 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return float.Parse(value, CultureInfo.CurrentCulture);
+            return float.Parse(value);
         }
 
         public static float ToSingle(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0;
-            return float.Parse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider);
+            return float.Parse(value, provider);
         }
 
         public static float ToSingle(bool value)
@@ -1732,14 +1704,14 @@ namespace System
         {
             if (value == null)
                 return 0;
-            return double.Parse(value, CultureInfo.CurrentCulture);
+            return double.Parse(value);
         }
 
         public static double ToDouble(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0;
-            return double.Parse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider);
+            return double.Parse(value, provider);
         }
 
         public static double ToDouble(bool value)
@@ -1830,14 +1802,14 @@ namespace System
         {
             if (value == null)
                 return 0m;
-            return decimal.Parse(value, CultureInfo.CurrentCulture);
+            return decimal.Parse(value);
         }
 
         public static decimal ToDecimal(string? value, IFormatProvider? provider)
         {
             if (value == null)
                 return 0m;
-            return decimal.Parse(value, NumberStyles.Number, provider);
+            return decimal.Parse(value, provider);
         }
 
         public static decimal ToDecimal(decimal value)
@@ -1879,7 +1851,7 @@ namespace System
         {
             if (value == null)
                 return new DateTime(0);
-            return DateTime.Parse(value, CultureInfo.CurrentCulture);
+            return DateTime.Parse(value);
         }
 
         public static DateTime ToDateTime(string? value, IFormatProvider? provider)
@@ -2000,7 +1972,7 @@ namespace System
         [CLSCompliant(false)]
         public static string ToString(sbyte value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         [CLSCompliant(false)]
@@ -2011,7 +1983,7 @@ namespace System
 
         public static string ToString(byte value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(byte value, IFormatProvider? provider)
@@ -2021,7 +1993,7 @@ namespace System
 
         public static string ToString(short value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(short value, IFormatProvider? provider)
@@ -2032,7 +2004,7 @@ namespace System
         [CLSCompliant(false)]
         public static string ToString(ushort value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         [CLSCompliant(false)]
@@ -2043,7 +2015,7 @@ namespace System
 
         public static string ToString(int value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(int value, IFormatProvider? provider)
@@ -2054,7 +2026,7 @@ namespace System
         [CLSCompliant(false)]
         public static string ToString(uint value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         [CLSCompliant(false)]
@@ -2065,7 +2037,7 @@ namespace System
 
         public static string ToString(long value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(long value, IFormatProvider? provider)
@@ -2076,7 +2048,7 @@ namespace System
         [CLSCompliant(false)]
         public static string ToString(ulong value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         [CLSCompliant(false)]
@@ -2087,7 +2059,7 @@ namespace System
 
         public static string ToString(float value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(float value, IFormatProvider? provider)
@@ -2097,7 +2069,7 @@ namespace System
 
         public static string ToString(double value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(double value, IFormatProvider? provider)
@@ -2107,7 +2079,7 @@ namespace System
 
         public static string ToString(decimal value)
         {
-            return value.ToString(CultureInfo.CurrentCulture);
+            return value.ToString();
         }
 
         public static string ToString(decimal value, IFormatProvider? provider)
@@ -2157,7 +2129,7 @@ namespace System
             }
 
             int r = ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight | ParseNumbers.TreatAsUnsigned);
-            if (r < byte.MinValue || r > byte.MaxValue)
+            if ((uint)r > byte.MaxValue)
                 ThrowByteOverflowException();
             return (byte)r;
         }
@@ -2231,7 +2203,7 @@ namespace System
             }
 
             int r = ParseNumbers.StringToInt(value.AsSpan(), fromBase, ParseNumbers.IsTight | ParseNumbers.TreatAsUnsigned);
-            if (r < ushort.MinValue || r > ushort.MaxValue)
+            if ((uint)r > ushort.MaxValue)
                 ThrowUInt16OverflowException();
             return (ushort)r;
         }
index da61628..72b85bf 100644 (file)
@@ -936,9 +936,9 @@ namespace System
         [CLSCompliant(false)]
         public static explicit operator ulong(decimal value) => ToUInt64(value);
 
-        public static explicit operator float(decimal value) => ToSingle(value);
+        public static explicit operator float(decimal value) => DecCalc.VarR4FromDec(in value);
 
-        public static explicit operator double(decimal value) => ToDouble(value);
+        public static explicit operator double(decimal value) => DecCalc.VarR8FromDec(in value);
 
         public static decimal operator +(decimal d) => d;
 
@@ -1051,12 +1051,12 @@ namespace System
 
         float IConvertible.ToSingle(IFormatProvider? provider)
         {
-            return Convert.ToSingle(this);
+            return DecCalc.VarR4FromDec(in this);
         }
 
         double IConvertible.ToDouble(IFormatProvider? provider)
         {
-            return Convert.ToDouble(this);
+            return DecCalc.VarR8FromDec(in this);
         }
 
         decimal IConvertible.ToDecimal(IFormatProvider? provider)
index 88bd953..8a45dbe 100644 (file)
@@ -74,7 +74,7 @@ namespace System
 
         public string ToString(IFormatProvider? provider)
         {
-            return ToString(null, provider);
+            return Number.FormatInt32(m_value, 0, null, provider);
         }
 
         public string ToString(string? format)
index 2e71773..9ecd926 100644 (file)
@@ -89,7 +89,7 @@ namespace System
 
         public string ToString(IFormatProvider? provider)
         {
-            return ToString(null, provider);
+            return Number.FormatInt32(m_value, 0, null, provider);
         }
 
         public string ToString(string? format, IFormatProvider? provider)
index 2317048..8b0868d 100644 (file)
@@ -698,7 +698,7 @@ namespace System
             if (string.IsNullOrEmpty(format))
             {
                 return value >= 0 ?
-                    UInt32ToDecStr((uint)value, digits: -1) :
+                    UInt32ToDecStr((uint)value) :
                     NegativeInt32ToDecStr(value, digits: -1, NumberFormatInfo.GetInstance(provider).NegativeSign);
             }
 
@@ -800,7 +800,7 @@ namespace System
             // Fast path for default format
             if (string.IsNullOrEmpty(format))
             {
-                return UInt32ToDecStr(value, digits: -1);
+                return UInt32ToDecStr(value);
             }
 
             return FormatUInt32Slow(value, format, provider);
@@ -1122,7 +1122,7 @@ namespace System
         public static string Int32ToDecStr(int value)
         {
             return value >= 0 ?
-                UInt32ToDecStr((uint)value, -1) :
+                UInt32ToDecStr((uint)value) :
                 NegativeInt32ToDecStr(value, -1, NumberFormatInfo.CurrentInfo.NegativeSign);
         }
 
@@ -1267,9 +1267,9 @@ namespace System
             return bufferEnd;
         }
 
-        internal static unsafe string UInt32ToDecStr(uint value, int digits)
+        internal static unsafe string UInt32ToDecStr(uint value)
         {
-            int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
+            int bufferLength = FormattingHelpers.CountDigits(value);
 
             // For single-digit values that are very common, especially 0 and 1, just return cached strings.
             if (bufferLength == 1)
@@ -1281,19 +1281,28 @@ namespace System
             fixed (char* buffer = result)
             {
                 char* p = buffer + bufferLength;
-                if (digits <= 1)
-                {
-                    do
-                    {
-                        value = Math.DivRem(value, 10, out uint remainder);
-                        *(--p) = (char)(remainder + '0');
-                    }
-                    while (value != 0);
-                }
-                else
+                do
                 {
-                    p = UInt32ToDecChars(p, value, digits);
+                    value = Math.DivRem(value, 10, out uint remainder);
+                    *(--p) = (char)(remainder + '0');
                 }
+                while (value != 0);
+                Debug.Assert(p == buffer);
+            }
+            return result;
+        }
+
+        private static unsafe string UInt32ToDecStr(uint value, int digits)
+        {
+            if (digits <= 1)
+                return UInt32ToDecStr(value);
+
+            int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
+            string result = string.FastAllocateString(bufferLength);
+            fixed (char* buffer = result)
+            {
+                char* p = buffer + bufferLength;
+                p = UInt32ToDecChars(p, value, digits);
                 Debug.Assert(p == buffer);
             }
             return result;
index 4cba7c6..effc14d 100644 (file)
@@ -84,7 +84,7 @@ namespace System
 
         public string ToString(IFormatProvider? provider)
         {
-            return ToString(null, provider);
+            return Number.FormatInt32(m_value, 0, null, provider);
         }
 
         public string ToString(string? format, IFormatProvider? provider)
index c99cf64..a0041be 100644 (file)
@@ -68,12 +68,12 @@ namespace System
         // Converts the current value to a String in base-10 with no extra padding.
         public override string ToString()
         {
-            return Number.UInt32ToDecStr(m_value, -1);
+            return Number.UInt32ToDecStr(m_value);
         }
 
         public string ToString(IFormatProvider? provider)
         {
-            return Number.FormatUInt32(m_value, null, provider);
+            return Number.UInt32ToDecStr(m_value);
         }
 
         public string ToString(string? format)
index 00c4e55..aebd3e5 100644 (file)
@@ -78,12 +78,12 @@ namespace System
         // The base 10 representation of the number with no extra padding.
         public override string ToString()
         {
-            return Number.UInt32ToDecStr(m_value, -1);
+            return Number.UInt32ToDecStr(m_value);
         }
 
         public string ToString(IFormatProvider? provider)
         {
-            return Number.FormatUInt32(m_value, null, provider);
+            return Number.UInt32ToDecStr(m_value);
         }
 
         public string ToString(string? format)
index 050c256..dcb4141 100644 (file)
@@ -82,7 +82,7 @@ namespace System
 
         public string ToString(IFormatProvider? provider)
         {
-            return Number.FormatUInt64(m_value, null, provider);
+            return Number.UInt64ToDecStr(m_value, -1);
         }
 
         public string ToString(string? format)