public static bool TryParse(ReadOnlySpan <byte> source, out ushort value, out int bytesConsumed, char standardFormat = default) { FastPath: if (standardFormat == default) { return(TryParseUInt16D(source, out value, out bytesConsumed)); } // There's small but measurable overhead when entering the switch block below. // We optimize for the default case by hoisting it above the switch block. switch (standardFormat | 0x20) // convert to lowercase { case 'g': case 'd': case 'r': standardFormat = default; goto FastPath; case 'n': return(TryParseUInt16N(source, out value, out bytesConsumed)); case 'x': return(TryParseUInt16X(source, out value, out bytesConsumed)); default: return(ParserHelpers.TryParseThrowFormatException(source, out value, out bytesConsumed)); } }
/// <summary> /// Parses a Guid at the start of a Utf8 string. /// </summary> /// <param name="source">The Utf8 string to parse</param> /// <param name="value">Receives the parsed value</param> /// <param name="bytesConsumed">On a successful parse, receives the length in bytes of the substring that was parsed </param> /// <param name="standardFormat">Expected format of the Utf8 string</param> /// <returns> /// true for success. "bytesConsumed" contains the length in bytes of the substring that was parsed. /// false if the string was not syntactically valid or an overflow or underflow occurred. "bytesConsumed" is set to 0. /// </returns> /// <remarks> /// Formats supported: /// D (default) nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn /// B {nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn} /// P (nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn) /// N nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn /// </remarks> /// <exceptions> /// <cref>System.FormatException</cref> if the format is not valid for this data type. /// </exceptions> public static bool TryParse(ReadOnlySpan <byte> source, out Guid value, out int bytesConsumed, char standardFormat = default) { FastPath: if (standardFormat == default) { return(TryParseGuidCore(source, out value, out bytesConsumed, ends: 0)); } switch (standardFormat) { case 'D': standardFormat = default; goto FastPath; case 'B': return(TryParseGuidCore(source, out value, out bytesConsumed, ends: '{' | ('}' << 8))); case 'P': return(TryParseGuidCore(source, out value, out bytesConsumed, ends: '(' | (')' << 8))); case 'N': return(TryParseGuidN(source, out value, out bytesConsumed)); default: return(ParserHelpers.TryParseThrowFormatException(source, out value, out bytesConsumed)); } }
// // Attempt to parse the regular floating points (the ones without names like "Infinity" and "NaN") // private static bool TryParseNormalAsFloatingPoint(ReadOnlySpan <byte> source, ref Number.NumberBuffer number, out int bytesConsumed, char standardFormat) { ParseNumberOptions options; switch (standardFormat) { case default(char): case 'G': case 'g': case 'E': case 'e': options = ParseNumberOptions.AllowExponent; break; case 'F': case 'f': options = default; break; default: return(ParserHelpers.TryParseThrowFormatException(out bytesConsumed)); } if (!TryParseNumber(source, ref number, out bytesConsumed, options, out bool textUsedExponentNotation)) { return(false); } if ((!textUsedExponentNotation) && (standardFormat == 'E' || standardFormat == 'e')) { bytesConsumed = 0; return(false); } return(true); }
/// <summary> /// Parses a Decimal at the start of a Utf8 string. /// </summary> /// <param name="source">The Utf8 string to parse</param> /// <param name="value">Receives the parsed value</param> /// <param name="bytesConsumed">On a successful parse, receives the length in bytes of the substring that was parsed </param> /// <param name="standardFormat">Expected format of the Utf8 string</param> /// <returns> /// true for success. "bytesConsumed" contains the length in bytes of the substring that was parsed. /// false if the string was not syntactically valid or an overflow or underflow occurred. "bytesConsumed" is set to 0. /// </returns> /// <remarks> /// Formats supported: /// G/g (default) /// F/f 12.45 Fixed point /// E/e 1.245000e1 Exponential /// </remarks> /// <exceptions> /// <cref>System.FormatException</cref> if the format is not valid for this data type. /// </exceptions> public static unsafe bool TryParse(ReadOnlySpan <byte> source, out decimal value, out int bytesConsumed, char standardFormat = default) { ParseNumberOptions options; switch (standardFormat) { case default(char): case 'G': case 'g': case 'E': case 'e': options = ParseNumberOptions.AllowExponent; break; case 'F': case 'f': options = default; break; default: return(ParserHelpers.TryParseThrowFormatException(out value, out bytesConsumed)); } byte *pDigits = stackalloc byte[Number.DecimalNumberBufferLength]; Number.NumberBuffer number = new Number.NumberBuffer(Number.NumberBufferKind.Decimal, pDigits, Number.DecimalNumberBufferLength); if (!TryParseNumber(source, ref number, out bytesConsumed, options, out bool textUsedExponentNotation)) { value = default; return(false); } if ((!textUsedExponentNotation) && (standardFormat == 'E' || standardFormat == 'e')) { value = default; bytesConsumed = 0; return(false); } // More compat with .NET behavior - whether or not a 0 keeps the negative sign depends on whether it an "integer" 0 or a "fractional" 0 if (number.Digits[0] == 0 && number.Scale == 0) { number.IsNegative = false; } value = default; if (!Number.TryNumberToDecimal(ref number, ref value)) { value = default; bytesConsumed = 0; return(false); } return(true); }
public static unsafe bool TryParse(ReadOnlySpan <byte> source, out decimal value, out int bytesConsumed, char standardFormat = default) { ParseNumberOptions options; switch (standardFormat) { case default(char): case 'G': case 'g': case 'E': case 'e': options = ParseNumberOptions.AllowExponent; break; case 'F': case 'f': options = default; break; default: return(ParserHelpers.TryParseThrowFormatException(out value, out bytesConsumed)); } byte *pDigits = stackalloc byte[Number.DecimalNumberBufferLength]; Number.NumberBuffer number = new Number.NumberBuffer(Number.NumberBufferKind.Decimal, pDigits, Number.DecimalNumberBufferLength); if (!TryParseNumber(source, ref number, out bytesConsumed, options, out bool textUsedExponentNotation)) { value = default; return(false); } if ((!textUsedExponentNotation) && (standardFormat == 'E' || standardFormat == 'e')) { value = default; bytesConsumed = 0; return(false); } value = default; if (!Number.TryNumberToDecimal(ref number, ref value)) { value = default; bytesConsumed = 0; return(false); } return(true); }
public static bool TryParse(ReadOnlySpan <byte> source, out Guid value, out int bytesConsumed, char standardFormat = default) { switch (standardFormat) { case default(char): case 'D': return(TryParseGuidCore(source, false, ' ', ' ', out value, out bytesConsumed)); case 'B': return(TryParseGuidCore(source, true, '{', '}', out value, out bytesConsumed)); case 'P': return(TryParseGuidCore(source, true, '(', ')', out value, out bytesConsumed)); case 'N': return(TryParseGuidN(source, out value, out bytesConsumed)); default: return(ParserHelpers.TryParseThrowFormatException(out value, out bytesConsumed)); } }
public static bool TryParse(ReadOnlySpan <byte> source, out ushort value, out int bytesConsumed, char standardFormat = default) { switch (standardFormat) { case default(char): case 'g': case 'G': case 'd': case 'D': return(TryParseUInt16D(source, out value, out bytesConsumed)); case 'n': case 'N': return(TryParseUInt16N(source, out value, out bytesConsumed)); case 'x': case 'X': return(TryParseUInt16X(source, out value, out bytesConsumed)); default: return(ParserHelpers.TryParseThrowFormatException(out value, out bytesConsumed)); } }