|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | + |
| 3 | +namespace LinkDotNet.StringBuilder; |
| 4 | + |
| 5 | +public ref partial struct ValueStringBuilder |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Appends an interpolated string to the builder. |
| 9 | + /// </summary> |
| 10 | + /// <param name="handler">The interpolated string handler.</param> |
| 11 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 12 | + public void Append([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedStringHandler handler) |
| 13 | + { |
| 14 | + this = handler.Builder; |
| 15 | + } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Appends an interpolated string followed by a new line to the builder. |
| 19 | + /// </summary> |
| 20 | + /// <param name="handler">The interpolated string handler.</param> |
| 21 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 22 | + public void AppendLine([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedStringHandler handler) |
| 23 | + { |
| 24 | + this = handler.Builder; |
| 25 | + Append(Environment.NewLine); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Nested struct which handles interpolated strings for <see cref="ValueStringBuilder"/>. |
| 30 | + /// </summary> |
| 31 | + [InterpolatedStringHandler] |
| 32 | + public ref struct AppendInterpolatedStringHandler |
| 33 | + { |
| 34 | + internal ValueStringBuilder Builder; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Initializes a new instance of the <see cref="AppendInterpolatedStringHandler"/> struct. |
| 38 | + /// </summary> |
| 39 | + /// <param name="literalLength">The length of the literal part of the interpolated string.</param> |
| 40 | + /// <param name="formattedCount">The number of formatted segments in the interpolated string.</param> |
| 41 | + /// <param name="builder">The builder to append to.</param> |
| 42 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 43 | + public AppendInterpolatedStringHandler(int literalLength, int formattedCount, ValueStringBuilder builder) |
| 44 | + { |
| 45 | + this.Builder = builder; |
| 46 | + |
| 47 | + // A conservative guess for the capacity. |
| 48 | + this.Builder.EnsureCapacity(this.Builder.Length + literalLength + (formattedCount * 11)); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Appends a literal string to the handler. |
| 53 | + /// </summary> |
| 54 | + /// <param name="value">The literal string.</param> |
| 55 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 56 | + public void AppendLiteral(string value) => Builder.Append(value); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Appends a formatted value to the handler. |
| 60 | + /// </summary> |
| 61 | + /// <param name="value">The value to format.</param> |
| 62 | + /// <typeparam name="T">The type of the value.</typeparam> |
| 63 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 64 | + public void AppendFormatted<T>(T value) |
| 65 | + { |
| 66 | + if (value is ISpanFormattable formattable) |
| 67 | + { |
| 68 | + Builder.Append(formattable); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + Builder.Append(value?.ToString()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Appends a formatted value with a given format. |
| 78 | + /// </summary> |
| 79 | + /// <param name="value">The value to format.</param> |
| 80 | + /// <param name="format">The format string.</param> |
| 81 | + /// <typeparam name="T">The type of the value.</typeparam> |
| 82 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 83 | + public void AppendFormatted<T>(T value, string? format) |
| 84 | + { |
| 85 | + if (value is ISpanFormattable formattable) |
| 86 | + { |
| 87 | + Builder.Append(formattable, format); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + Builder.Append(value?.ToString()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Appends a character span to the handler. |
| 97 | + /// </summary> |
| 98 | + /// <param name="value">The character span.</param> |
| 99 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 100 | + public void AppendFormatted(ReadOnlySpan<char> value) => Builder.Append(value); |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Appends a string to the handler. |
| 104 | + /// </summary> |
| 105 | + /// <param name="value">The string value.</param> |
| 106 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 107 | + public void AppendFormatted(string? value) => Builder.Append(value); |
| 108 | + } |
| 109 | +} |
0 commit comments