@@ -10,25 +10,34 @@ namespace Open.Text;
1010/// Allows for easy conversion from a string to a <see cref="StringBuilder"/> by declaring the type as <see cref="StringBuilderHelper"/>.
1111/// </summary>
1212[ SuppressMessage ( "Usage" , "CA2225:Operator overloads have named alternates" , Justification = "<Pending>" ) ]
13- public class StringBuilderHelper ( StringBuilder ? sb = default )
13+ public readonly record struct StringBuilderHelper
1414{
1515 /// <summary>
1616 /// The underlying <see cref="Builder"/>.
1717 /// </summary>
18- public StringBuilder Builder { get ; } = sb ?? new ( ) ;
19-
20- /// <summary>
21- /// Constructs a new <see cref="StringBuilderHelper"/> with a <see cref="StringBuilder"/> of the <paramref name="initialCapacity"/>.
22- /// </summary>
23- public StringBuilderHelper ( int initialCapacity )
18+ public StringBuilder Builder { get ; }
19+
20+ /// <inheritdoc cref="StringBuilder.ToString()"/>
21+ public override string ToString ( )
22+ => Builder . ToString ( ) ;
23+
24+ /// <summary>
25+ /// Constructs a new <see cref="StringBuilderHelper"/> with the specified <see cref="StringBuilder"/>.
26+ /// </summary>
27+ public StringBuilderHelper ( StringBuilder ? sb = default )
28+ => Builder = sb ?? new ( ) ;
29+
30+ /// <summary>
31+ /// Constructs a new <see cref="StringBuilderHelper"/> with a <see cref="StringBuilder"/> of the <paramref name="initialCapacity"/>.
32+ /// </summary>
33+ public StringBuilderHelper ( int initialCapacity )
2434 : this ( new StringBuilder ( initialCapacity ) ) { }
2535
2636 /// <summary>
2737 /// Appends the characters to the underlying <see cref="StringBuilder"/>.
2838 /// </summary>
2939 public static StringBuilderHelper Add ( StringBuilderHelper helper , string characters )
3040 {
31- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
3241 helper . Builder . Append ( characters ) ;
3342 return helper ;
3443 }
@@ -40,31 +49,27 @@ public static StringBuilderHelper Add(StringBuilderHelper helper, string charact
4049 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
4150 public static StringBuilderHelper operator + ( StringBuilderHelper helper , StringSegment characters )
4251 {
43- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
4452 helper . Builder . AppendSegment ( characters ) ;
4553 return helper ;
4654 }
4755
4856 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
4957 public static StringBuilderHelper operator + ( StringBuilderHelper helper , ReadOnlySpan < char > characters )
5058 {
51- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
5259 helper . Builder . Append ( characters ) ;
5360 return helper ;
5461 }
5562
5663 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
5764 public static StringBuilderHelper operator + ( StringBuilderHelper helper , Span < char > characters )
5865 {
59- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
6066 helper . Builder . Append ( characters ) ;
6167 return helper ;
6268 }
6369
6470 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
6571 public static StringBuilderHelper operator + ( StringBuilderHelper helper , char [ ] characters )
6672 {
67- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
6873 if ( characters is null ) return helper ;
6974 helper . Builder . Append ( characters . AsSpan ( ) ) ;
7075 return helper ;
@@ -73,31 +78,27 @@ public static StringBuilderHelper Add(StringBuilderHelper helper, string charact
7378 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
7479 public static StringBuilderHelper operator + ( StringBuilderHelper helper , ReadOnlyMemory < char > characters )
7580 {
76- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
7781 helper . Builder . Append ( characters . Span ) ;
7882 return helper ;
7983 }
8084
8185 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
8286 public static StringBuilderHelper operator + ( StringBuilderHelper helper , Memory < char > characters )
8387 {
84- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
8588 helper . Builder . Append ( characters . Span ) ;
8689 return helper ;
8790 }
8891
8992 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
9093 public static StringBuilderHelper operator + ( StringBuilderHelper helper , ArraySegment < char > characters )
9194 {
92- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
9395 helper . Builder . Append ( characters . AsSpan ( ) ) ;
9496 return helper ;
9597 }
9698
9799 /// <inheritdoc cref="Add(StringBuilderHelper, string)"/>
98100 public static StringBuilderHelper operator + ( StringBuilderHelper helper , IEnumerable < char > characters )
99101 {
100- if ( helper is null ) throw new ArgumentNullException ( nameof ( helper ) ) ;
101102 if ( characters is null ) return helper ;
102103 var sb = helper . Builder ;
103104 foreach ( var c in characters )
@@ -151,10 +152,6 @@ public static implicit operator StringBuilderHelper(StringBuilder value)
151152 /// <summary>
152153 /// Converts the <see cref="StringBuilderHelper"/> instance to a string.
153154 /// </summary>
154- #if NETSTANDARD2_0
155- #else
156- [ return : NotNullIfNotNull ( nameof ( value ) ) ]
157- #endif
158- public static implicit operator string ? ( StringBuilderHelper ? value )
159- => value ? . Builder . ToString ( ) ;
155+ public static implicit operator string ( StringBuilderHelper value )
156+ => value . Builder . ToString ( ) ;
160157}
0 commit comments