• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

neon-sunset / U8String / 5951223859

23 Aug 2023 12:22PM UTC coverage: 23.214% (-0.04%) from 23.255%
5951223859

push

github

neon-sunset
perf: tune formatting

154 of 858 branches covered (17.95%)

Branch coverage included in aggregate %.

23 of 23 new or added lines in 2 files covered. (100.0%)

499 of 1955 relevant lines covered (25.52%)

42168.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/U8StringExtensions.cs
1
using System.Collections.Immutable;
2
using U8Primitives;
3

4
namespace System;
5

6
#pragma warning disable CA1305 // Specify IFormatProvider
7
/// <summary>
8
/// Provides extension methods to integrate <see cref="U8String"/> with the .NET type system.
9
/// </summary>
10
public static class U8StringExtensions
11
{
12
    /// <inheritdoc cref="U8String(ImmutableArray{byte})"/>
13
    public static U8String AsU8String(this ImmutableArray<byte> value) => new(value);
×
14

15
    /// <inheritdoc cref="U8String(string?)"/>
16
    public static U8String ToU8String(this string? value) => new(value);
×
17

18
    /// <inheritdoc cref="U8String(ReadOnlySpan{byte})"/>
19
    public static U8String ToU8String(this byte[] value) => new(value);
×
20

21
    /// <inheritdoc cref="U8String(ReadOnlySpan{byte})"/>
22
    public static U8String ToU8String(this Span<byte> value) => new(value);
×
23

24
    /// <inheritdoc cref="U8String(ReadOnlySpan{char})"/>
25
    public static U8String ToU8String(this Span<char> value) => new(value);
×
26

27
    /// <inheritdoc cref="U8String(ReadOnlySpan{byte})"/>
28
    public static U8String ToU8String(this ReadOnlySpan<byte> value) => new(value);
×
29

30
    /// <inheritdoc cref="U8String(ReadOnlySpan{char})"/>
31
    public static U8String ToU8String(this ReadOnlySpan<char> value) => new(value);
×
32

33
    /// <summary>
34
    /// Converts the <see paramref="value"/> to a <see cref="U8String"/> using the default format.
35
    /// </summary>
36
    /// <typeparam name="T">The type of the value to convert.</typeparam>
37
    /// <param name="value">The value to convert.</param>
38
    /// <returns>U8String representation of the value.</returns>
39
    public static U8String ToU8String<T>(this T value)
40
        where T : IUtf8SpanFormattable
41
    {
42
        return ToU8String(value, default, null);
×
43
    }
44

45
    /// <summary>
46
    /// Converts the <see paramref="value"/> to a <see cref="U8String"/> using the specified format.
47
    /// </summary>
48
    /// <typeparam name="T">The type of the value to convert.</typeparam>
49
    /// <param name="value">The value to convert.</param>
50
    /// <param name="format">The format to use.</param>
51
    /// <returns>U8String representation of the value.</returns>
52
    public static U8String ToU8String<T>(this T value, ReadOnlySpan<char> format)
53
        where T : IUtf8SpanFormattable
54
    {
55
        return ToU8String(value, format, null);
×
56
    }
57

58
    /// <summary>
59
    /// Converts the <see paramref="value"/> to a <see cref="U8String"/> using the default format and a provider, if specified.
60
    /// </summary>
61
    /// <typeparam name="T">The type of the value to convert.</typeparam>
62
    /// <param name="value">The value to convert.</param>
63
    /// <param name="provider">The format provider to use.</param>
64
    /// <returns>U8String representation of the value.</returns>
65
    public static U8String ToU8String<T>(this T value, IFormatProvider? provider)
66
        where T : IUtf8SpanFormattable
67
    {
68
        return ToU8String(value, default, provider);
×
69
    }
70

71
    /// <summary>
72
    /// Converts the <see paramref="value"/> to a <see cref="U8String"/> using the specified format and provider.
73
    /// </summary>
74
    /// <typeparam name="T">The type of the value to convert.</typeparam>
75
    /// <param name="value">The value to convert.</param>
76
    /// <param name="format">The format to use.</param>
77
    /// <param name="provider">The format provider to use.</param>
78
    /// <returns>U8String representation of the value.</returns>
79
    // [MethodImpl(MethodImplOptions.AggressiveInlining)] // We might this need after all...
80
    public static U8String ToU8String<T>(
81
        this T value,
82
        ReadOnlySpan<char> format,
83
        IFormatProvider? provider) where T : IUtf8SpanFormattable
84
    {
85
        if (value is not U8String u8str)
×
86
        {
87
            var length = U8Constants.GetFormattedLength(value);
×
88
            return FormatPresized(format, value, provider, length, out var result)
×
89
                ? result
×
90
                : FormatUnsized(format, value, provider);
×
91
        }
92

93
        return u8str;
×
94
    }
95

96
    // TODO:
97
    // - Really, this should have been moved to U8Conversions or U8String long ago
98
    // - Use inline-array based or sequence-like builder when FormatExact can fail or
99
    // when calling into FormatUnsized
100
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
101
    static bool FormatPresized<T>(
102
        ReadOnlySpan<char> format,
103
        T value,
104
        IFormatProvider? provider,
105
        int length,
106
        out U8String result) where T : IUtf8SpanFormattable
107
    {
108
        result = default;
×
109
        var buffer = new byte[length];
×
110
        var success = value.TryFormat(buffer, out length, format, provider);
×
111
        if (success)
×
112
        {
113
            result = new(buffer, 0, length);
×
114
        }
115

116
        return success;
×
117
    }
118

119
    static U8String FormatUnsized<T>(
120
        ReadOnlySpan<char> format, T value, IFormatProvider? provider)
121
            where T : IUtf8SpanFormattable
122
    {
123
        // TODO: Additional length-resolving heuristics or a stack-allocated into arraypool buffer
124
        int length;
125
        var buffer = new byte[64];
×
126
        while (!value.TryFormat(buffer, out length, format, provider))
×
127
        {
128
            buffer = new byte[buffer.Length * 2];
×
129
        }
130

131
        return new(buffer, 0, length);
×
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc