| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace System.Buffers.Text |
| | | 9 | | { |
| | | 10 | | internal static class ParserHelpers |
| | | 11 | | { |
| | | 12 | | public const int ByteOverflowLength = 3; |
| | | 13 | | public const int ByteOverflowLengthHex = 2; |
| | | 14 | | public const int UInt16OverflowLength = 5; |
| | | 15 | | public const int UInt16OverflowLengthHex = 4; |
| | | 16 | | public const int UInt32OverflowLength = 10; |
| | | 17 | | public const int UInt32OverflowLengthHex = 8; |
| | | 18 | | public const int UInt64OverflowLength = 20; |
| | | 19 | | public const int UInt64OverflowLengthHex = 16; |
| | | 20 | | |
| | | 21 | | public const int SByteOverflowLength = 3; |
| | | 22 | | public const int SByteOverflowLengthHex = 2; |
| | | 23 | | public const int Int16OverflowLength = 5; |
| | | 24 | | public const int Int16OverflowLengthHex = 4; |
| | | 25 | | public const int Int32OverflowLength = 10; |
| | | 26 | | public const int Int32OverflowLengthHex = 8; |
| | | 27 | | public const int Int64OverflowLength = 19; |
| | | 28 | | public const int Int64OverflowLengthHex = 16; |
| | | 29 | | |
| | | 30 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 31 | | public static bool IsDigit(int i) |
| | | 32 | | { |
| | 0 | 33 | | return (uint)(i - '0') <= ('9' - '0'); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // |
| | | 37 | | // Enable use of ThrowHelper from TryParse() routines without introducing dozens of non-code-coveraged "value= d |
| | | 38 | | // |
| | | 39 | | public static bool TryParseThrowFormatException(out int bytesConsumed) |
| | | 40 | | { |
| | 0 | 41 | | bytesConsumed = 0; |
| | 0 | 42 | | ThrowHelper.ThrowFormatException_BadFormatSpecifier(); |
| | | 43 | | return false; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // |
| | | 47 | | // Enable use of ThrowHelper from TryParse() routines without introducing dozens of non-code-coveraged "value= d |
| | | 48 | | // |
| | | 49 | | public static bool TryParseThrowFormatException<T>(out T value, out int bytesConsumed) where T : struct |
| | | 50 | | { |
| | 0 | 51 | | value = default; |
| | 0 | 52 | | return TryParseThrowFormatException(out bytesConsumed); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // |
| | | 56 | | // Enable use of ThrowHelper from TryParse() routines without introducing dozens of non-code-coveraged "value= d |
| | | 57 | | // |
| | | 58 | | [DoesNotReturn] |
| | | 59 | | [StackTraceHidden] |
| | | 60 | | public static bool TryParseThrowFormatException<T>(ReadOnlySpan<byte> _, out T value, out int bytesConsumed) whe |
| | | 61 | | { |
| | | 62 | | // The parameters to this method are ordered the same as our callers' parameters |
| | | 63 | | // allowing the JIT to avoid unnecessary register swapping or spilling. |
| | | 64 | | |
| | 0 | 65 | | Unsafe.SkipInit(out value); // bypass language initialization rules since we're about to throw |
| | 0 | 66 | | Unsafe.SkipInit(out bytesConsumed); |
| | 0 | 67 | | ThrowHelper.ThrowFormatException_BadFormatSpecifier(); |
| | | 68 | | |
| | | 69 | | Debug.Fail("Control should never reach this point."); |
| | | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |