| | | 1 | | //======================================================================= |
| | | 2 | | // FixedDiagnosticsFormatter.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Buffers; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace FixedMathSharp; |
| | | 14 | | |
| | | 15 | | internal static class FixedDiagnosticsFormatter |
| | | 16 | | { |
| | | 17 | | internal delegate bool TryFormatHandler(Span<char> destination, out int charsWritten); |
| | | 18 | | |
| | | 19 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 20 | | internal static bool TryFormat( |
| | | 21 | | Fixed64 value, |
| | | 22 | | Span<char> destination, |
| | | 23 | | out int charsWritten, |
| | | 24 | | ReadOnlySpan<char> format, |
| | | 25 | | IFormatProvider? provider) |
| | | 26 | | { |
| | 3043 | 27 | | return ((double)value).TryFormat( |
| | 3043 | 28 | | destination, |
| | 3043 | 29 | | out charsWritten, |
| | 3043 | 30 | | format, |
| | 3043 | 31 | | provider ?? CultureInfo.InvariantCulture); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static string ToString(TryFormatHandler handler) |
| | | 35 | | { |
| | 273 | 36 | | Span<char> initialBuffer = stackalloc char[256]; |
| | 273 | 37 | | if (handler(initialBuffer, out int charsWritten)) |
| | 272 | 38 | | return new string(initialBuffer[..charsWritten]); |
| | | 39 | | |
| | 1 | 40 | | int length = 512; |
| | 1 | 41 | | while (true) |
| | | 42 | | { |
| | 2 | 43 | | char[] rented = ArrayPool<char>.Shared.Rent(length); |
| | | 44 | | try |
| | | 45 | | { |
| | 2 | 46 | | if (handler(rented.AsSpan(0, length), out charsWritten)) |
| | 1 | 47 | | return new string(rented, 0, charsWritten); |
| | 1 | 48 | | } |
| | | 49 | | finally |
| | | 50 | | { |
| | 2 | 51 | | ArrayPool<char>.Shared.Return(rented); |
| | 2 | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | length *= 2; |
| | | 55 | | } |
| | 1 | 56 | | } |
| | | 57 | | |
| | | 58 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 59 | | internal static bool Append(char value, Span<char> destination, ref int charsWritten) |
| | | 60 | | { |
| | 824 | 61 | | if ((uint)charsWritten >= (uint)destination.Length) |
| | 1 | 62 | | return false; |
| | | 63 | | |
| | 823 | 64 | | destination[charsWritten++] = value; |
| | 823 | 65 | | return true; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 69 | | internal static bool Append(ReadOnlySpan<char> value, Span<char> destination, ref int charsWritten) |
| | | 70 | | { |
| | 2403 | 71 | | if (value.Length > destination.Length - charsWritten) |
| | 2 | 72 | | return false; |
| | | 73 | | |
| | 2401 | 74 | | value.CopyTo(destination[charsWritten..]); |
| | 2401 | 75 | | charsWritten += value.Length; |
| | 2401 | 76 | | return true; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 80 | | internal static bool Append( |
| | | 81 | | Fixed64 value, |
| | | 82 | | Span<char> destination, |
| | | 83 | | ref int charsWritten, |
| | | 84 | | ReadOnlySpan<char> format, |
| | | 85 | | IFormatProvider? provider) |
| | | 86 | | { |
| | 2814 | 87 | | if (!TryFormat(value, destination[charsWritten..], out int written, format, provider)) |
| | 7 | 88 | | return false; |
| | | 89 | | |
| | 2807 | 90 | | charsWritten += written; |
| | 2807 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | } |