| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | namespace Towel_Testing; |
| | | 4 | | |
| | | 5 | | /// <summary>Represents a <see cref="string"/> in the process of being built.</summary> |
| | | 6 | | public 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) |
| | 11 | 14 | | { |
| | 11 | 15 | | _spanBuilder = span; |
| | 11 | 16 | | _stringBuilder = null; |
| | 11 | 17 | | } |
| | | 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) |
| | 10 | 22 | | { |
| | 10 | 23 | | if (_stringBuilder is not null) |
| | 1 | 24 | | { |
| | 1 | 25 | | _stringBuilder.Append(value); |
| | 1 | 26 | | } |
| | 9 | 27 | | else if (_spanBuilder._i + 1 > _spanBuilder._span.Length) |
| | 4 | 28 | | { |
| | 4 | 29 | | _stringBuilder = new(_spanBuilder._i + 1); |
| | 4 | 30 | | _stringBuilder.Append(_spanBuilder); |
| | 4 | 31 | | _stringBuilder.Append(value); |
| | 4 | 32 | | } |
| | | 33 | | else |
| | 5 | 34 | | { |
| | 5 | 35 | | _spanBuilder.Append(value); |
| | 5 | 36 | | } |
| | 10 | 37 | | } |
| | | 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) |
| | 11 | 42 | | { |
| | 11 | 43 | | if (_stringBuilder is not null) |
| | 2 | 44 | | { |
| | 2 | 45 | | _stringBuilder.Append(span); |
| | 2 | 46 | | } |
| | 9 | 47 | | else if (_spanBuilder._i + span.Length > _spanBuilder._span.Length) |
| | 5 | 48 | | { |
| | 5 | 49 | | _stringBuilder = new(_spanBuilder._i + span.Length); |
| | 5 | 50 | | _stringBuilder.Append(_spanBuilder); |
| | 5 | 51 | | _stringBuilder.Append(span); |
| | 5 | 52 | | } |
| | | 53 | | else |
| | 4 | 54 | | { |
| | 4 | 55 | | _spanBuilder.Append(span); |
| | 4 | 56 | | } |
| | 11 | 57 | | } |
| | | 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) |
| | 1 | 62 | | { |
| | 1 | 63 | | Append(value); |
| | 1 | 64 | | Append(Environment.NewLine); |
| | 1 | 65 | | } |
| | | 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) |
| | 1 | 70 | | { |
| | 1 | 71 | | Append(span); |
| | 1 | 72 | | Append(Environment.NewLine); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary>Appends a <see cref="Environment.NewLine"/>.</summary> |
| | | 76 | | public void AppendLine() |
| | 1 | 77 | | { |
| | 1 | 78 | | Append(Environment.NewLine); |
| | 1 | 79 | | } |
| | | 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> |
| | 11 | 83 | | 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) => |
| | 9 | 88 | | sStringBuilder._stringBuilder is null |
| | 9 | 89 | | ? new(sStringBuilder._spanBuilder) |
| | 9 | 90 | | : sStringBuilder._stringBuilder.ToString(); |
| | | 91 | | |
| | | 92 | | /// <inheritdoc cref="StringBuilder.ToString()"/> |
| | 4 | 93 | | public override string ToString() => _stringBuilder is null ? new(_spanBuilder) : _stringBuilder.ToString(); |
| | | 94 | | } |