internal static void ValidateNumber(ReadOnlySpan <byte> utf8FormattedNumber) { // This is a simplified version of the number reader from Utf8JsonReader.TryGetNumber, // because it doesn't need to deal with "NeedsMoreData", or remembering the format. // // The Debug.Asserts in this method should change to validated ArgumentExceptions if/when // writing a formatted number becomes public API. Debug.Assert(!utf8FormattedNumber.IsEmpty); int i = 0; if (utf8FormattedNumber[i] == '-') { i++; if (utf8FormattedNumber.Length <= i) { throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } } if (utf8FormattedNumber[i] == '0') { i++; } else { while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i])) { i++; } } if (i == utf8FormattedNumber.Length) { return; } // The non digit character inside the number byte val = utf8FormattedNumber[i]; if (val == '.') { i++; if (utf8FormattedNumber.Length <= i) { throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i])) { i++; } if (i == utf8FormattedNumber.Length) { return; } Debug.Assert(i < utf8FormattedNumber.Length); val = utf8FormattedNumber[i]; } if (val == 'e' || val == 'E') { i++; if (utf8FormattedNumber.Length <= i) { throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } val = utf8FormattedNumber[i]; if (val == '+' || val == '-') { i++; } } else { throw new ArgumentException( SR.Format(SR.ExpectedEndOfDigitNotFound, ThrowHelper.GetPrintableString(val)), nameof(utf8FormattedNumber)); } if (utf8FormattedNumber.Length <= i) { throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i])) { i++; } if (i != utf8FormattedNumber.Length) { throw new ArgumentException( SR.Format(SR.ExpectedEndOfDigitNotFound, ThrowHelper.GetPrintableString(utf8FormattedNumber[i])), nameof(utf8FormattedNumber)); } }
internal static void ValidateNumber(ReadOnlySpan <byte> utf8FormattedNumber) { // This is a simplified version of the number reader from Utf8JsonReader.TryGetNumber, // because it doesn't need to deal with "NeedsMoreData", or remembering the format. if (utf8FormattedNumber.IsEmpty) { throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } int i = 0; if (utf8FormattedNumber[i] == '-') { i++; if (utf8FormattedNumber.Length <= i) { throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } } if (utf8FormattedNumber[i] == '0') { i++; } else { while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i])) { i++; } } if (i == utf8FormattedNumber.Length) { return; } byte val = utf8FormattedNumber[i]; if (val == '.') { i++; } else if (val == 'e' || val == 'E') { i++; if (i >= utf8FormattedNumber.Length) { throw new ArgumentException( SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } val = utf8FormattedNumber[i]; if (val == '+' || val == '-') { i++; } } else { throw new ArgumentException( SR.Format(SR.ExpectedEndOfDigitNotFound, ThrowHelper.GetPrintableString(val)), nameof(utf8FormattedNumber)); } if (i >= utf8FormattedNumber.Length) { throw new ArgumentException( SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber)); } while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i])) { i++; } if (i != utf8FormattedNumber.Length) { throw new ArgumentException( SR.Format(SR.ExpectedEndOfDigitNotFound, ThrowHelper.GetPrintableString(utf8FormattedNumber[i])), nameof(utf8FormattedNumber)); } }