< Summary

Class:Towel_Testing.SStringBuilder
Assembly:Towel
File(s):File 1: /home/runner/work/Towel/Towel/Sources/Towel/SStringBuilder.cs
Covered lines:50
Uncovered lines:0
Coverable lines:50
Total lines:94
Line coverage:100% (50 of 50)
Covered branches:12
Total branches:12
Branch coverage:100% (12 of 12)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
File 1: .ctor(...)100%1100%
File 1: Append(...)100%4100%
File 1: Append(...)100%4100%
File 1: AppendLine(...)100%1100%
File 1: AppendLine(...)100%1100%
File 1: AppendLine()100%1100%
File 1: op_Implicit(...)100%1100%
File 1: op_Implicit(...)100%2100%
File 1: ToString()100%2100%

File(s)

/home/runner/work/Towel/Towel/Sources/Towel/SStringBuilder.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace Towel_Testing;
 4
 5/// <summary>Represents a <see cref="string"/> in the process of being built.</summary>
 6public ref struct SStringBuilder
 7{
 8  internal SpanBuilder<char> _spanBuilder;
 9  internal StringBuilder? _stringBuilder;
 10
 11  /// <summary>Constructs a new <see cref="SpanBuilder{T}"/>.</summary>
 12  /// <param name="span">The <see cref="Span{T}"/> to initialize.</param>
 13  public SStringBuilder(Span<char> span)
 1114  {
 1115    _spanBuilder = span;
 1116    _stringBuilder = null;
 1117  }
 18
 19  /// <summary>Appends a <see cref="char"/> to this <see cref="SpanBuilder{T}"/>.</summary>
 20  /// <param name="value">The <see cref="char"/> to append to this <see cref="SpanBuilder{T}"/>.</param>
 21  public void Append(char value)
 1022  {
 1023    if (_stringBuilder is not null)
 124    {
 125      _stringBuilder.Append(value);
 126    }
 927    else if (_spanBuilder._i + 1 > _spanBuilder._span.Length)
 428    {
 429      _stringBuilder = new(_spanBuilder._i + 1);
 430      _stringBuilder.Append(_spanBuilder);
 431      _stringBuilder.Append(value);
 432    }
 33    else
 534    {
 535      _spanBuilder.Append(value);
 536    }
 1037  }
 38
 39  /// <summary>Appends a <see cref="char"/> span to this <see cref="SpanBuilder{T}"/>.</summary>
 40  /// <param name="span">The <see cref="char"/> span to append to this <see cref="SpanBuilder{T}"/>.</param>
 41  public void Append(ReadOnlySpan<char> span)
 1142  {
 1143    if (_stringBuilder is not null)
 244    {
 245      _stringBuilder.Append(span);
 246    }
 947    else if (_spanBuilder._i + span.Length > _spanBuilder._span.Length)
 548    {
 549      _stringBuilder = new(_spanBuilder._i + span.Length);
 550      _stringBuilder.Append(_spanBuilder);
 551      _stringBuilder.Append(span);
 552    }
 53    else
 454    {
 455      _spanBuilder.Append(span);
 456    }
 1157  }
 58
 59  /// <summary>Appends a <see cref="char"/> followed by <see cref="Environment.NewLine"/>.</summary>
 60  /// <param name="value">The <see cref="char"/> to append.</param>
 61  public void AppendLine(char value)
 162  {
 163    Append(value);
 164    Append(Environment.NewLine);
 165  }
 66
 67  /// <summary>Appends a <see cref="char"/> span followed by <see cref="Environment.NewLine"/>.</summary>
 68  /// <param name="span">The <see cref="char"/> span to append.</param>
 69  public void AppendLine(ReadOnlySpan<char> span)
 170  {
 171    Append(span);
 172    Append(Environment.NewLine);
 173  }
 74
 75  /// <summary>Appends a <see cref="Environment.NewLine"/>.</summary>
 76  public void AppendLine()
 177  {
 178    Append(Environment.NewLine);
 179  }
 80
 81  /// <summary>Converts a <see cref="Span{T}"/> to a <see cref="SpanBuilder{T}"/>.</summary>
 82  /// <param name="span">The <see cref="Span{T}"/> to convert to a <see cref="SpanBuilder{T}"/>.</param>
 1183  public static implicit operator SStringBuilder(Span<char> span) => new(span);
 84
 85  /// <summary>Converts a <see cref="SStringBuilder"/> to a <see cref="string"/>.</summary>
 86  /// <param name="sStringBuilder">The <see cref="SStringBuilder"/> to convert to a <see cref="string"/>.</param>
 87  public static implicit operator string(SStringBuilder sStringBuilder) =>
 988    sStringBuilder._stringBuilder is null
 989      ? new(sStringBuilder._spanBuilder)
 990      : sStringBuilder._stringBuilder.ToString();
 91
 92  /// <inheritdoc cref="StringBuilder.ToString()"/>
 493  public override string ToString() => _stringBuilder is null ? new(_spanBuilder) : _stringBuilder.ToString();
 94}