1- using System ;
1+ /*!
2+ * @author electricessence / https://github.com/electricessence/
3+ * Licensing: MIT
4+ */
5+
6+ using System ;
27using System . Collections . Generic ;
38using System . Diagnostics . Contracts ;
49using System . Text ;
@@ -7,6 +12,14 @@ namespace Open.Text
712{
813 public static class StringBuilderExtensions
914 {
15+ private const string ParametersMissing = "Parameters missing." ;
16+
17+ /// <summary>
18+ /// Adds every entry to a StringBuilder.
19+ /// </summary>
20+ /// <typeparam name="T">The type of the source.</typeparam>
21+ /// <param name="source">The source span.</param>
22+ /// <returns>The resultant StringBuilder.</returns>
1023 public static StringBuilder ToStringBuilder < T > ( this in ReadOnlySpan < T > source )
1124 {
1225 var len = source . Length ;
@@ -18,15 +31,29 @@ public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source)
1831 return sb ;
1932 }
2033
34+ /// <summary>
35+ /// Adds every entry to a StringBuilder.
36+ /// </summary>
37+ /// <typeparam name="T">The type of the source.</typeparam>
38+ /// <param name="source">The source span.</param>
39+ /// <returns>The resultant StringBuilder.</returns>
2140 public static StringBuilder ToStringBuilder < T > ( this IEnumerable < T > source )
2241 {
42+ if ( source is null ) throw new NullReferenceException ( ) ;
2343 var sb = new StringBuilder ( ) ;
2444 foreach ( var s in source )
2545 sb . Append ( s ) ;
2646
2747 return sb ;
2848 }
2949
50+ /// <summary>
51+ /// Adds every entry to a StringBuilder separated by the specified sequence.
52+ /// </summary>
53+ /// <typeparam name="T">The type of the source.</typeparam>
54+ /// <param name="source">The source span.</param>
55+ /// <param name="separator">The separator sequence.</param>
56+ /// <returns>The resultant StringBuilder.</returns>
3057 public static StringBuilder ToStringBuilder < T > ( this in ReadOnlySpan < T > source , string separator )
3158 {
3259 var len = source . Length ;
@@ -45,11 +72,17 @@ public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source, s
4572 return sb ;
4673 }
4774
75+ /// <summary>
76+ /// Adds every entry to a StringBuilder separated by the specified character.
77+ /// </summary>
78+ /// <typeparam name="T">The type of the source.</typeparam>
79+ /// <param name="source">The source span.</param>
80+ /// <param name="separator">The separator character.</param>
81+ /// <returns>The resultant StringBuilder.</returns>
4882 public static StringBuilder ToStringBuilder < T > ( this in ReadOnlySpan < T > source , in char separator )
4983 {
5084 var len = source . Length ;
51- if ( len < 2 )
52- return ToStringBuilder ( source ) ;
85+ if ( len < 2 ) return ToStringBuilder ( source ) ;
5386
5487 var sb = new StringBuilder ( 2 * len - 1 ) ;
5588
@@ -63,10 +96,23 @@ public static StringBuilder ToStringBuilder<T>(this in ReadOnlySpan<T> source, i
6396 return sb ;
6497 }
6598
99+ /// <summary>
100+ /// Adds every entry to a StringBuilder separated by the specified sequence.
101+ /// </summary>
102+ /// <typeparam name="T">The type of the source.</typeparam>
103+ /// <param name="source">The source enumerable.</param>
104+ /// <param name="separator">The separator sequence.</param>
105+ /// <returns>The resultant StringBuilder.</returns>
66106 public static StringBuilder ToStringBuilder < T > ( this IEnumerable < T > source , string separator )
67107 {
68- var sb = new StringBuilder ( ) ;
108+ if ( source is null ) throw new NullReferenceException ( ) ;
109+ Contract . EndContractBlock ( ) ;
110+
111+ if ( string . IsNullOrEmpty ( separator ) )
112+ return ToStringBuilder ( source ) ;
113+
69114 var first = true ;
115+ var sb = new StringBuilder ( ) ;
70116 foreach ( var s in source )
71117 {
72118 if ( first ) first = false ;
@@ -76,8 +122,18 @@ public static StringBuilder ToStringBuilder<T>(this IEnumerable<T> source, strin
76122 return sb ;
77123 }
78124
125+ /// <summary>
126+ /// Adds every entry to a StringBuilder separated by the specified character.
127+ /// </summary>
128+ /// <typeparam name="T">The type of the source.</typeparam>
129+ /// <param name="source">The source enumerable.</param>
130+ /// <param name="separator">The separator character.</param>
131+ /// <returns>The resultant StringBuilder.</returns>
79132 public static StringBuilder ToStringBuilder < T > ( this IEnumerable < T > source , in char separator )
80133 {
134+ if ( source is null ) throw new NullReferenceException ( ) ;
135+ Contract . EndContractBlock ( ) ;
136+
81137 var sb = new StringBuilder ( ) ;
82138 var first = true ;
83139 foreach ( var s in source )
@@ -139,7 +195,7 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, st
139195 if ( target is null )
140196 throw new NullReferenceException ( ) ;
141197 if ( values is null || values . Length == 0 )
142- throw new ArgumentException ( "Parameters missing." ) ;
198+ throw new ArgumentException ( ParametersMissing ) ;
143199 Contract . EndContractBlock ( ) ;
144200
145201 if ( ! string . IsNullOrEmpty ( separator ) && target . Length != 0 )
@@ -157,7 +213,7 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, in
157213 if ( target is null )
158214 throw new NullReferenceException ( ) ;
159215 if ( values is null || values . Length == 0 )
160- throw new ArgumentException ( "Parameters missing." ) ;
216+ throw new ArgumentException ( ParametersMissing ) ;
161217 Contract . EndContractBlock ( ) ;
162218
163219 if ( target . Length != 0 )
0 commit comments