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)
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)
[CLSCompliant(false)]
public static sbyte ToSByte(uint value)
{
- if (value > sbyte.MaxValue) ThrowSByteOverflowException();
+ if (value > (uint)sbyte.MaxValue) ThrowSByteOverflowException();
return (sbyte)value;
}
{
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)]
[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;
}
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)
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)
{
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)
[CLSCompliant(false)]
public static short ToInt16(uint value)
{
- if (value > short.MaxValue) ThrowInt16OverflowException();
+ if (value > (uint)short.MaxValue) ThrowInt16OverflowException();
return (short)value;
}
{
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)
}
[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)
}
[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)
{
if (value == null)
return 0;
- return ushort.Parse(value, CultureInfo.CurrentCulture);
+ return ushort.Parse(value);
}
[CLSCompliant(false)]
{
if (value == null)
return 0;
- return ushort.Parse(value, NumberStyles.Integer, provider);
+ return ushort.Parse(value, provider);
}
[CLSCompliant(false)]
[CLSCompliant(false)]
public static int ToInt32(uint value)
{
- if (value > int.MaxValue) ThrowInt32OverflowException();
+ if ((int)value < 0) ThrowInt32OverflowException();
return (int)value;
}
{
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)
}
[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)
{
if (value == null)
return 0;
- return uint.Parse(value, CultureInfo.CurrentCulture);
+ return uint.Parse(value);
}
[CLSCompliant(false)]
{
if (value == null)
return 0;
- return uint.Parse(value, NumberStyles.Integer, provider);
+ return uint.Parse(value, provider);
}
[CLSCompliant(false)]
[CLSCompliant(false)]
public static long ToInt64(ulong value)
{
- if (value > long.MaxValue) ThrowInt64OverflowException();
+ if ((long)value < 0) ThrowInt64OverflowException();
return (long)value;
}
{
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)
{
if (value == null)
return 0;
- return ulong.Parse(value, CultureInfo.CurrentCulture);
+ return ulong.Parse(value);
}
[CLSCompliant(false)]
{
if (value == null)
return 0;
- return ulong.Parse(value, NumberStyles.Integer, provider);
+ return ulong.Parse(value, provider);
}
[CLSCompliant(false)]
{
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)
{
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)
{
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)
{
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)
[CLSCompliant(false)]
public static string ToString(sbyte value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
[CLSCompliant(false)]
public static string ToString(byte value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(byte value, IFormatProvider? provider)
public static string ToString(short value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(short value, IFormatProvider? provider)
[CLSCompliant(false)]
public static string ToString(ushort value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
[CLSCompliant(false)]
public static string ToString(int value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(int value, IFormatProvider? provider)
[CLSCompliant(false)]
public static string ToString(uint value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
[CLSCompliant(false)]
public static string ToString(long value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(long value, IFormatProvider? provider)
[CLSCompliant(false)]
public static string ToString(ulong value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
[CLSCompliant(false)]
public static string ToString(float value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(float value, IFormatProvider? provider)
public static string ToString(double value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(double value, IFormatProvider? provider)
public static string ToString(decimal value)
{
- return value.ToString(CultureInfo.CurrentCulture);
+ return value.ToString();
}
public static string ToString(decimal value, IFormatProvider? provider)
}
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;
}
}
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;
}
if (string.IsNullOrEmpty(format))
{
return value >= 0 ?
- UInt32ToDecStr((uint)value, digits: -1) :
+ UInt32ToDecStr((uint)value) :
NegativeInt32ToDecStr(value, digits: -1, NumberFormatInfo.GetInstance(provider).NegativeSign);
}
// Fast path for default format
if (string.IsNullOrEmpty(format))
{
- return UInt32ToDecStr(value, digits: -1);
+ return UInt32ToDecStr(value);
}
return FormatUInt32Slow(value, format, provider);
public static string Int32ToDecStr(int value)
{
return value >= 0 ?
- UInt32ToDecStr((uint)value, -1) :
+ UInt32ToDecStr((uint)value) :
NegativeInt32ToDecStr(value, -1, NumberFormatInfo.CurrentInfo.NegativeSign);
}
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)
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;