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

neon-sunset / U8String / 5903348006

18 Aug 2023 01:41PM UTC coverage: 21.619% (-0.03%) from 21.646%
5903348006

push

github

neon-sunset
feat: better conversion overloads

115 of 776 branches covered (14.82%)

Branch coverage included in aggregate %.

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

411 of 1657 relevant lines covered (24.8%)

17208.32 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 System.Diagnostics.CodeAnalysis;
3
using U8Primitives;
4

5
namespace System;
6

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

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

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

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

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

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

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

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

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

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

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

94
        return u8str;
×
95
    }
96

97
    // TODO: Move impls off U8StringExtensions and change strategy into
98
    // FormatExact (for Ts of exact max length, with other calls optimized away) -> FormatOpporunistic -> FormatUnsized
99
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
100
    static bool FormatExact<T>(
101
        ReadOnlySpan<char> format,
102
        T value,
103
        IFormatProvider? provider,
104
        int length,
105
        out U8String result) where T : IUtf8SpanFormattable
106
    {
107
        result = default;
×
108
        var buffer = new byte[length];
×
109
        var success = value.TryFormat(buffer, out length, format, provider);
×
110
        if (success)
×
111
        {
112
            result = new(buffer, 0, length);
×
113
        }
114

115
        return success;
×
116
    }
117

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

130
        // Limits???? Check what CoreLib does
131
        buffer = new byte[buffer.Length * 2];
×
132
        goto Retry;
×
133
    }
134
}
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