< 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    {
 304327        return ((double)value).TryFormat(
 304328            destination,
 304329            out charsWritten,
 304330            format,
 304331            provider ?? CultureInfo.InvariantCulture);
 32    }
 33
 34    internal static string ToString(TryFormatHandler handler)
 35    {
 27336        Span<char> initialBuffer = stackalloc char[256];
 27337        if (handler(initialBuffer, out int charsWritten))
 27238            return new string(initialBuffer[..charsWritten]);
 39
 140        int length = 512;
 141        while (true)
 42        {
 243            char[] rented = ArrayPool<char>.Shared.Rent(length);
 44            try
 45            {
 246                if (handler(rented.AsSpan(0, length), out charsWritten))
 147                    return new string(rented, 0, charsWritten);
 148            }
 49            finally
 50            {
 251                ArrayPool<char>.Shared.Return(rented);
 252            }
 53
 154            length *= 2;
 55        }
 156    }
 57
 58    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 59    internal static bool Append(char value, Span<char> destination, ref int charsWritten)
 60    {
 82461        if ((uint)charsWritten >= (uint)destination.Length)
 162            return false;
 63
 82364        destination[charsWritten++] = value;
 82365        return true;
 66    }
 67
 68    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 69    internal static bool Append(ReadOnlySpan<char> value, Span<char> destination, ref int charsWritten)
 70    {
 240371        if (value.Length > destination.Length - charsWritten)
 272            return false;
 73
 240174        value.CopyTo(destination[charsWritten..]);
 240175        charsWritten += value.Length;
 240176        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    {
 281487        if (!TryFormat(value, destination[charsWritten..], out int written, format, provider))
 788            return false;
 89
 280790        charsWritten += written;
 280791        return true;
 92    }
 93}