< Summary

Information
Class: SwiftCollections.Diagnostics.DiagnosticInterpolatedStringHandler
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/Diagnostics/DiagnosticInterpolatedStringHandler.cs
Line coverage
100%
Covered lines: 50
Uncovered lines: 0
Coverable lines: 50
Total lines: 316
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
get_IsEnabled()100%11100%
AppendLiteral(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
GetFormattedText()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/Diagnostics/DiagnosticInterpolatedStringHandler.cs

#LineLine coverage
 1namespace SwiftCollections.Diagnostics;
 2
 3using System;
 4using System.Runtime.CompilerServices;
 5using System.Text;
 6
 7/// <summary>
 8/// Builds diagnostic messages for enabled diagnostic levels while allowing disabled levels
 9/// to skip formatted interpolation expression evaluation.
 10/// </summary>
 11[InterpolatedStringHandler]
 12public ref struct DiagnosticInterpolatedStringHandler
 13{
 14    private readonly bool _isEnabled;
 15#if NET6_0_OR_GREATER
 16    private DefaultInterpolatedStringHandler _builder;
 17#else
 18    private StringBuilder? _builder;
 19#endif
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="DiagnosticInterpolatedStringHandler"/> struct.
 23    /// </summary>
 24    /// <param name="literalLength">The combined length of literal portions in the interpolated string.</param>
 25    /// <param name="formattedCount">The number of formatted expressions in the interpolated string.</param>
 26    /// <param name="channel">The diagnostic channel that will receive the message.</param>
 27    /// <param name="level">The severity level being evaluated.</param>
 28    /// <param name="isEnabled">
 29    /// Set to <c>true</c> when formatted expressions should be evaluated; otherwise, <c>false</c>.
 30    /// </param>
 31    public DiagnosticInterpolatedStringHandler(
 32        int literalLength,
 33        int formattedCount,
 34        DiagnosticChannel channel,
 35        DiagnosticLevel level,
 36        out bool isEnabled)
 37    {
 1538        isEnabled = channel != null && channel.IsEnabled(level);
 1539        _isEnabled = isEnabled;
 40#if NET6_0_OR_GREATER
 1541        _builder = isEnabled ? new DefaultInterpolatedStringHandler(literalLength, formattedCount) : default;
 42#else
 43        _ = formattedCount;
 44        _builder = isEnabled ? new StringBuilder(literalLength) : null;
 45#endif
 1546    }
 47
 48    /// <summary>
 49    /// Gets a value indicating whether this handler is actively building a diagnostic message.
 50    /// </summary>
 1851    public bool IsEnabled => _isEnabled;
 52
 53    /// <summary>
 54    /// Appends a literal string segment.
 55    /// </summary>
 56    /// <param name="value">The literal string segment.</param>
 57    public void AppendLiteral(string value)
 58    {
 2559        if (!_isEnabled)
 260            return;
 61
 62#if NET6_0_OR_GREATER
 2363        _builder.AppendLiteral(value);
 64#else
 65        _builder!.Append(value);
 66#endif
 2367    }
 68
 69    /// <summary>
 70    /// Appends a formatted value.
 71    /// </summary>
 72    /// <typeparam name="T">The type of value to append.</typeparam>
 73    /// <param name="value">The value to append.</param>
 74    public void AppendFormatted<T>(T value)
 75    {
 676        if (!_isEnabled)
 177            return;
 78
 79#if NET6_0_OR_GREATER
 580        _builder.AppendFormatted(value);
 81#else
 82        _builder!.Append(value);
 83#endif
 584    }
 85
 86    /// <summary>
 87    /// Appends a formatted value using the specified format string.
 88    /// </summary>
 89    /// <typeparam name="T">The type of value to append.</typeparam>
 90    /// <param name="value">The value to append.</param>
 91    /// <param name="format">The format string to apply.</param>
 92    public void AppendFormatted<T>(T value, string? format)
 93    {
 394        if (!_isEnabled)
 195            return;
 96
 97#if NET6_0_OR_GREATER
 298        _builder.AppendFormatted(value, format);
 99#else
 100        AppendFormattedValue(value, 0, format);
 101#endif
 2102    }
 103
 104    /// <summary>
 105    /// Appends a formatted value with the specified alignment.
 106    /// </summary>
 107    /// <typeparam name="T">The type of value to append.</typeparam>
 108    /// <param name="value">The value to append.</param>
 109    /// <param name="alignment">The minimum width for the formatted value.</param>
 110    public void AppendFormatted<T>(T value, int alignment)
 111    {
 3112        if (!_isEnabled)
 1113            return;
 114
 115#if NET6_0_OR_GREATER
 2116        _builder.AppendFormatted(value, alignment);
 117#else
 118        AppendFormattedValue(value, alignment, null);
 119#endif
 2120    }
 121
 122    /// <summary>
 123    /// Appends a formatted value with the specified alignment and format string.
 124    /// </summary>
 125    /// <typeparam name="T">The type of value to append.</typeparam>
 126    /// <param name="value">The value to append.</param>
 127    /// <param name="alignment">The minimum width for the formatted value.</param>
 128    /// <param name="format">The format string to apply.</param>
 129    public void AppendFormatted<T>(T value, int alignment, string? format)
 130    {
 3131        if (!_isEnabled)
 1132            return;
 133
 134#if NET6_0_OR_GREATER
 2135        _builder.AppendFormatted(value, alignment, format);
 136#else
 137        AppendFormattedValue(value, alignment, format);
 138#endif
 2139    }
 140
 141    /// <summary>
 142    /// Appends a string value.
 143    /// </summary>
 144    /// <param name="value">The value to append.</param>
 145    public void AppendFormatted(string? value)
 146    {
 3147        if (!_isEnabled)
 1148            return;
 149
 150#if NET6_0_OR_GREATER
 2151        _builder.AppendFormatted(value);
 152#else
 153        _builder!.Append(value);
 154#endif
 2155    }
 156
 157    /// <summary>
 158    /// Appends a string value with the specified alignment.
 159    /// </summary>
 160    /// <param name="value">The value to append.</param>
 161    /// <param name="alignment">The minimum width for the formatted value.</param>
 162    public void AppendFormatted(string? value, int alignment)
 163    {
 2164        if (!_isEnabled)
 1165            return;
 166
 167#if NET6_0_OR_GREATER
 1168        _builder.AppendFormatted(value, alignment);
 169#else
 170        AppendAligned(value, alignment);
 171#endif
 1172    }
 173
 174    /// <summary>
 175    /// Appends a string value with the specified alignment and format string.
 176    /// </summary>
 177    /// <param name="value">The value to append.</param>
 178    /// <param name="alignment">The minimum width for the formatted value.</param>
 179    /// <param name="format">The format string to apply.</param>
 180    public void AppendFormatted(string? value, int alignment, string? format)
 181    {
 2182        if (!_isEnabled)
 1183            return;
 184
 185#if NET6_0_OR_GREATER
 1186        _builder.AppendFormatted(value, alignment, format);
 187#else
 188        AppendAligned(value, alignment);
 189#endif
 1190    }
 191
 192    /// <summary>
 193    /// Appends a character span.
 194    /// </summary>
 195    /// <param name="value">The span to append.</param>
 196    public void AppendFormatted(ReadOnlySpan<char> value)
 197    {
 2198        if (!_isEnabled)
 1199            return;
 200
 201#if NET6_0_OR_GREATER
 1202        _builder.AppendFormatted(value);
 203#else
 204        _builder!.Append(value);
 205#endif
 1206    }
 207
 208    /// <summary>
 209    /// Appends a character span with the specified alignment.
 210    /// </summary>
 211    /// <param name="value">The span to append.</param>
 212    /// <param name="alignment">The minimum width for the formatted value.</param>
 213    public void AppendFormatted(ReadOnlySpan<char> value, int alignment)
 214    {
 2215        if (!_isEnabled)
 1216            return;
 217
 218#if NET6_0_OR_GREATER
 1219        _builder.AppendFormatted(value, alignment);
 220#else
 221        AppendAligned(value, alignment);
 222#endif
 1223    }
 224
 225    /// <summary>
 226    /// Appends a character span with the specified alignment and format string.
 227    /// </summary>
 228    /// <param name="value">The span to append.</param>
 229    /// <param name="alignment">The minimum width for the formatted value.</param>
 230    /// <param name="format">The format string to apply.</param>
 231    public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format)
 232    {
 2233        if (!_isEnabled)
 1234            return;
 235
 236#if NET6_0_OR_GREATER
 1237        _builder.AppendFormatted(value, alignment, format);
 238#else
 239        AppendAligned(value, alignment);
 240#endif
 1241    }
 242
 243    internal string GetFormattedText()
 244    {
 245#if NET6_0_OR_GREATER
 10246        return _builder.ToStringAndClear();
 247#else
 248        return _builder?.ToString() ?? string.Empty;
 249#endif
 250    }
 251
 252#if !NET6_0_OR_GREATER
 253    private void AppendFormattedValue<T>(T value, int alignment, string? format)
 254    {
 255        string? text = value is IFormattable formattable
 256            ? formattable.ToString(format, null)
 257            : value?.ToString();
 258
 259        AppendAligned(text, alignment);
 260    }
 261
 262    private void AppendAligned(string? value, int alignment)
 263    {
 264        value ??= string.Empty;
 265
 266        if (alignment == 0)
 267        {
 268            _builder!.Append(value);
 269            return;
 270        }
 271
 272        int width = alignment < 0 ? -alignment : alignment;
 273        int padding = width - value.Length;
 274
 275        if (padding <= 0)
 276        {
 277            _builder!.Append(value);
 278            return;
 279        }
 280
 281        if (alignment > 0)
 282            _builder!.Append(' ', padding);
 283
 284        _builder!.Append(value);
 285
 286        if (alignment < 0)
 287            _builder!.Append(' ', padding);
 288    }
 289
 290    private void AppendAligned(ReadOnlySpan<char> value, int alignment)
 291    {
 292        if (alignment == 0)
 293        {
 294            _builder!.Append(value);
 295            return;
 296        }
 297
 298        int width = alignment < 0 ? -alignment : alignment;
 299        int padding = width - value.Length;
 300
 301        if (padding <= 0)
 302        {
 303            _builder!.Append(value);
 304            return;
 305        }
 306
 307        if (alignment > 0)
 308            _builder!.Append(' ', padding);
 309
 310        _builder!.Append(value);
 311
 312        if (alignment < 0)
 313            _builder!.Append(' ', padding);
 314    }
 315#endif
 316}