|
| 1 | +using Microsoft.Extensions.Primitives; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace Open.Text; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Allows for easy conversion from a string to a <see cref="StringBuilder"/> by declaring the type as <see cref="StringBuilderHelper"/>. |
| 11 | +/// </summary> |
| 12 | + |
| 13 | +[SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Justification = "<Pending>")] |
| 14 | +public class StringBuilderHelper(StringBuilder? sb = default) |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// The underlying <see cref="Builder"/>. |
| 18 | + /// </summary> |
| 19 | + public StringBuilder Builder { get; } = sb ?? new(); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Constructs a new <see cref="StringBuilderHelper"/> with a <see cref="StringBuilder"/> of the <paramref name="initialCapacity"/>. |
| 23 | + /// </summary> |
| 24 | + public StringBuilderHelper(int initialCapacity) |
| 25 | + : this(new StringBuilder(initialCapacity)) { } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Appends the characters to the underlying <see cref="StringBuilder"/>. |
| 29 | + /// </summary> |
| 30 | + public static StringBuilderHelper Add(StringBuilderHelper helper, string characters) |
| 31 | + { |
| 32 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 33 | + helper.Builder.Append(characters); |
| 34 | + return helper; |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 38 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, string characters) |
| 39 | + => Add(helper, characters); |
| 40 | + |
| 41 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 42 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, StringSegment characters) |
| 43 | + { |
| 44 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 45 | + helper.Builder.AppendSegment(characters); |
| 46 | + return helper; |
| 47 | + } |
| 48 | + |
| 49 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 50 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, ReadOnlySpan<char> characters) |
| 51 | + { |
| 52 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 53 | + helper.Builder.Append(characters); |
| 54 | + return helper; |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 58 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, Span<char> characters) |
| 59 | + { |
| 60 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 61 | + helper.Builder.Append(characters); |
| 62 | + return helper; |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 66 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, char[] characters) |
| 67 | + { |
| 68 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 69 | + if (characters is null) return helper; |
| 70 | + helper.Builder.Append(characters.AsSpan()); |
| 71 | + return helper; |
| 72 | + } |
| 73 | + |
| 74 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 75 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, ReadOnlyMemory<char> characters) |
| 76 | + { |
| 77 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 78 | + helper.Builder.Append(characters.Span); |
| 79 | + return helper; |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 83 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, Memory<char> characters) |
| 84 | + { |
| 85 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 86 | + helper.Builder.Append(characters.Span); |
| 87 | + return helper; |
| 88 | + } |
| 89 | + |
| 90 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 91 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, ArraySegment<char> characters) |
| 92 | + { |
| 93 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 94 | + helper.Builder.Append(characters.AsSpan()); |
| 95 | + return helper; |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc cref="Add(StringBuilderHelper, string)"/> |
| 99 | + public static StringBuilderHelper operator +(StringBuilderHelper helper, IEnumerable<char> characters) |
| 100 | + { |
| 101 | + if (helper is null) throw new ArgumentNullException(nameof(helper)); |
| 102 | + if (characters is null) return helper; |
| 103 | + var sb = helper.Builder; |
| 104 | + foreach(var c in characters) |
| 105 | + sb.Append(c); |
| 106 | + return helper; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Creates a new <see cref="StringBuilderHelper"/> instance beginning with the specified <paramref name="sequence"/>. |
| 112 | + /// </summary> |
| 113 | + internal static StringBuilderHelper NewFrom<T>(T sequence) |
| 114 | + => throw new NotImplementedException(); |
| 115 | + |
| 116 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 117 | + [ExcludeFromCodeCoverage] |
| 118 | + public static implicit operator StringBuilderHelper(string sequence) |
| 119 | + => string.IsNullOrEmpty(sequence) ? new() : new(new StringBuilder(sequence)); |
| 120 | + |
| 121 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 122 | + public static implicit operator StringBuilderHelper(char value) |
| 123 | + => new(new StringBuilder(1).Append(value)); |
| 124 | + |
| 125 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 126 | + public static implicit operator StringBuilderHelper(StringSegment value) |
| 127 | + => new(new StringBuilder(value.Length).AppendSegment(value)); |
| 128 | + |
| 129 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 130 | + public static implicit operator StringBuilderHelper(ReadOnlySpan<char> value) |
| 131 | + => new(new StringBuilder(value.Length).Append(value)); |
| 132 | + |
| 133 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 134 | + public static implicit operator StringBuilderHelper(ReadOnlyMemory<char> value) |
| 135 | + => new(new StringBuilder(value.Length).Append(value.Span)); |
| 136 | + |
| 137 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 138 | + public static implicit operator StringBuilderHelper(Memory<char> value) |
| 139 | + => new(new StringBuilder(value.Length).Append(value.Span)); |
| 140 | + |
| 141 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 142 | + public static implicit operator StringBuilderHelper(ArraySegment<char> value) |
| 143 | + => new(new StringBuilder(value.Count).Append(value.AsSpan())); |
| 144 | + |
| 145 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 146 | + public static implicit operator StringBuilderHelper(char[] value) |
| 147 | + => value is null ? new() : new(new StringBuilder(value.Length).Append(value.AsSpan())); |
| 148 | + |
| 149 | + /// <inheritdoc cref="NewFrom{T}(T)"/> |
| 150 | + public static implicit operator StringBuilderHelper(StringBuilder value) |
| 151 | + => value is null ? new() : new(new StringBuilder(value.Length).Append(value)); |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Converts the <see cref="StringConcat"/> instance to a string. |
| 155 | + /// </summary> |
| 156 | +#if NETSTANDARD2_0 |
| 157 | +#else |
| 158 | + [return:NotNullIfNotNull(nameof(value))] |
| 159 | +#endif |
| 160 | + public static implicit operator string?(StringBuilderHelper? value) |
| 161 | + => value?.Builder.ToString(); |
| 162 | +} |
0 commit comments