< Summary

Information
Class: FixedMathSharp.FixedDiagnosticsFormatter
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Diagnostics/FixedDiagnosticsFormatter.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 93
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryFormat(...)100%22100%
ToString(...)100%44100%
Append(...)100%22100%
Append(...)100%22100%
Append(...)100%22100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Diagnostics/FixedDiagnosticsFormatter.cs

#LineLine coverage
 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
 8using System;
 9using System.Buffers;
 10using System.Globalization;
 11using System.Runtime.CompilerServices;
 12
 13namespace FixedMathSharp;
 14
 15internal 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    {
 13040127        return ((double)value).TryFormat(
 13040128            destination,
 13040129            out charsWritten,
 13040130            format,
 13040131            provider ?? CultureInfo.InvariantCulture);
 32    }
 33
 34    internal static string ToString(TryFormatHandler handler)
 35    {
 513236        Span<char> initialBuffer = stackalloc char[256];
 513237        if (handler(initialBuffer, out int charsWritten))
 513038            return new string(initialBuffer[..charsWritten]);
 39
 240        int length = 512;
 141        while (true)
 42        {
 343            char[] rented = ArrayPool<char>.Shared.Rent(length);
 44            try
 45            {
 346                if (handler(rented.AsSpan(0, length), out charsWritten))
 247                    return new string(rented, 0, charsWritten);
 148            }
 49            finally
 50            {
 351                ArrayPool<char>.Shared.Return(rented);
 352            }
 53
 154            length *= 2;
 55        }
 256    }
 57
 58    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 59    internal static bool Append(char value, Span<char> destination, ref int charsWritten)
 60    {
 3065761        if ((uint)charsWritten >= (uint)destination.Length)
 1762            return false;
 63
 3064064        destination[charsWritten++] = value;
 3064065        return true;
 66    }
 67
 68    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 69    internal static bool Append(ReadOnlySpan<char> value, Span<char> destination, ref int charsWritten)
 70    {
 2947271        if (value.Length > destination.Length - charsWritten)
 8872            return false;
 73
 2938474        value.CopyTo(destination[charsWritten..]);
 2938475        charsWritten += value.Length;
 2938476        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    {
 4481587        if (!TryFormat(value, destination[charsWritten..], out int written, format, provider))
 20488            return false;
 89
 4461190        charsWritten += written;
 4461191        return true;
 92    }
 93}