Skip to content

Instantly share code, notes, and snippets.

@prajaybasu
Last active September 24, 2018 16:10
Show Gist options
  • Save prajaybasu/99513fd5ac2c6f9374a2bec1d63c802e to your computer and use it in GitHub Desktop.
Save prajaybasu/99513fd5ac2c6f9374a2bec1d63c802e to your computer and use it in GitHub Desktop.
Int32 Parsing differences
public static int Convert.ToInt32(string value)
{
if (value == null) return 0;
return Int32.Parse(value, CultureInfo.CurrentCulture);
}
public static int Int32.Parse(string s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
public static bool Int32.TryParse(string s, out int result)
{
if (s == null)
{
result = 0;
return false;
}
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
internal static int ParseInt32(ReadOnlySpan<char> value, NumberStyles styles, NumberFormatInfo info)
{
if ((styles & ~NumberStyles.Integer) == 0)
{
// Optimized path for the common case of anything that's allowed for integer style.
bool overflow = false;
if (!TryParseInt32IntegerStyle(value, styles, info, out int intResult, ref overflow))
{
ThrowOverflowOrFormatException(overflow, nameof(SR.Overflow_Int32));
}
return intResult;
}
// SOME CODE REMOVED FOR READABILITY
NumberBuffer number = default;
int result = 0;
StringToNumber(value, styles, ref number, info, false);
if (!NumberToInt32(ref number, ref result))
{
ThrowOverflowOrFormatException(overflow: true, nameof(SR.Overflow_Int32));
}
return result;
}
internal static bool TryParseInt32(ReadOnlySpan<char> value, NumberStyles styles, NumberFormatInfo info, out int result)
{
if ((styles & ~NumberStyles.Integer) == 0)
{
// Optimized path for the common case of anything that's allowed for integer style.
bool overflow = false;
return TryParseInt32IntegerStyle(value, styles, info, out result, ref overflow);
}
// SOME CODE REMOVED FOR READABILITY
NumberBuffer number = default;
result = 0;
return
TryStringToNumber(value, styles, ref number, info, false) &&
NumberToInt32(ref number, ref result);
}
private static unsafe void StringToNumber(ReadOnlySpan<char> value, NumberStyles styles, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal)
{
// SOME CODE REMOVED FOR READABILITY
fixed (char* stringPointer = &MemoryMarshal.GetReference(value))
{
char* p = stringPointer;
if (!ParseNumber(ref p, p + value.Length, styles, ref number, info, parseDecimal)
|| (p - stringPointer < value.Length && !TrailingZeros(value, (int)(p - stringPointer))))
{
ThrowOverflowOrFormatException(overflow: false, null);
}
}
}
internal static unsafe bool TryStringToNumber(ReadOnlySpan<char> value, NumberStyles styles, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal)
{
// SOME CODE REMOVED FOR READABILITY
fixed (char* stringPointer = &MemoryMarshal.GetReference(value))
{
char* p = stringPointer;
if (!ParseNumber(ref p, p + value.Length, styles, ref number, info, parseDecimal)
|| (p - stringPointer < value.Length && !TrailingZeros(value, (int)(p - stringPointer))))
{
return false;
}
}
return true;
}
private static bool TryParseInt32IntegerStyle(ReadOnlySpan<char> value, NumberStyles styles, NumberFormatInfo info, out int result, ref bool failureIsOverflow) => // SOME CODE REMOVED FOR READABILITY
private static unsafe bool ParseNumber(ref char* str, char* strEnd, NumberStyles styles, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal) => // SOME CODE REMOVED FOR READABILITY
private static bool TrailingZeros(ReadOnlySpan<char> value, int index) => // SOME CODE REMOVED FOR READABILITY
private static unsafe char* MatchChars(char* p, char* pEnd, string value) => // SOME CODE REMOVED FOR READABILITY
private static bool IsWhite(int ch) => // SOME CODE REMOVED FOR READABILITY
private static bool IsDigit(int ch) => // SOME CODE REMOVED FOR READABILITY
private static void ThrowOverflowOrFormatException(bool overflow, string overflowResourceKey) => // SOME CODE REMOVED FOR READABILITY;
private static unsafe bool ParseNumber(ref char* str, char* strEnd, NumberStyles styles, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal) => // SOME CODE REMOVED FOR READABILITY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment