@@ -7,26 +7,41 @@ namespace Open.Text;
77public static partial class TextExtensions
88{
99 private const uint BYTE_RED = 1024 ;
10- [ SuppressMessage ( "Style" ,
11- "IDE0300:Simplify collection initialization" ,
12- Justification = "Can cause NullReferenceException when initializing a static class." ) ]
13- private static readonly string [ ] _byte_labels = new [ ] { "KB" , "MB" , "GB" , "TB" , "PB" } ;
14- [ SuppressMessage ( "Style" ,
15- "IDE0300:Simplify collection initialization" ,
16- Justification = "Can cause NullReferenceException when initializing a static class." ) ]
17- private static readonly string [ ] _number_labels = new [ ] { "K" , "M" , "B" } ;
1810
19- /// <summary>
20- /// Compiled pattern for finding alpha-numeric sequences.
21- /// </summary>
22- public static readonly Regex ValidAlphaNumericOnlyPattern
23- = new ( @"^\w+$" , RegexOptions . Compiled ) ;
11+ private static IEnumerable < string > ByteLabels {
12+ get {
13+ yield return "KB" ;
14+ yield return "MB" ;
15+ yield return "GB" ;
16+ yield return "TB" ;
17+ yield return "PB" ;
18+ }
19+ }
2420
25- /// <summary>
26- /// Compiled pattern for finding alpha-numeric sequences and possible surrounding white-space.
27- /// </summary>
28- public static readonly Regex ValidAlphaNumericOnlyUntrimmedPattern
29- = new ( @"^\s*\w+\s*$" , RegexOptions . Compiled ) ;
21+ private static IEnumerable < string > NumberLabels
22+ {
23+ get
24+ {
25+ yield return "K" ;
26+ yield return "M" ;
27+ yield return "G" ;
28+ }
29+ }
30+
31+ /// <inheritdoc cref="RegexPatterns.ValidAlphaNumericOnlyPattern"/>
32+ [ Obsolete ( "Use RegexPatterns.ValidAlphaNumericOnlyPattern." ) ]
33+ public static Regex ValidAlphaNumericOnlyPattern
34+ => RegexPatterns . ValidAlphaNumericOnlyPattern ;
35+
36+ /// <inheritdoc cref="RegexPatterns.ValidAlphaNumericOnlyUntrimmedPattern"/>
37+ [ Obsolete ( "Use RegexPatterns.ValidAlphaNumericOnlyUntrimmedPattern." ) ]
38+ public static Regex ValidAlphaNumericOnlyUntrimmedPattern
39+ => RegexPatterns . ValidAlphaNumericOnlyUntrimmedPattern ;
40+
41+ /// <inheritdoc cref="RegexPatterns.WhiteSpacePattern"/>
42+ [ Obsolete ( "Use RegexPatterns.WhiteSpacePattern." ) ]
43+ public static Regex WhiteSpacePattern
44+ => RegexPatterns . WhiteSpacePattern ;
3045
3146 /// <summary>
3247 /// Converts a string to title-case.
@@ -121,60 +136,7 @@ public static string ToFormat<T>(this T? value, string? format = null, CultureIn
121136 [ ExcludeFromCodeCoverage ]
122137 public static bool IsAlphaNumeric ( this string source , bool trim = false )
123138 => ! string . IsNullOrWhiteSpace ( source )
124- && ( trim ? ValidAlphaNumericOnlyUntrimmedPattern : ValidAlphaNumericOnlyPattern ) . IsMatch ( source ) ;
125-
126- #region Regex helper methods.
127- private static readonly Func < Capture , string > _textDelegate = ( Func < Capture , string > )
128- typeof ( Capture ) . GetProperty ( "Text" , BindingFlags . Instance | BindingFlags . NonPublic ) !
129- . GetGetMethod ( nonPublic : true ) !
130- . CreateDelegate ( typeof ( Func < Capture , string > ) ) ;
131-
132- /// <summary>
133- /// Returns a ReadOnlySpan of the capture without creating a new string.
134- /// </summary>
135- /// <remarks>This is a stop-gap until .NET 6 releases the .ValueSpan property.</remarks>
136- /// <param name="capture">The capture to get the span from.</param>
137- public static ReadOnlySpan < char > AsSpan ( this Capture capture )
138- => capture is null
139- ? throw new ArgumentNullException ( nameof ( capture ) )
140- : _textDelegate . Invoke ( capture ) . AsSpan ( capture . Index , capture . Length ) ;
141-
142- /// <summary>
143- /// Gets a group by name.
144- /// </summary>
145- /// <param name="groups">The group collection to get the group from.</param>
146- /// <param name="groupName">The declared name of the group.</param>
147- /// <returns>The value of the requested group or null if not found.</returns>
148- /// <exception cref="ArgumentNullException">Groups or groupName is null.</exception>
149- public static string ? GetValue ( this GroupCollection groups , string groupName )
150- {
151- if ( groups is null )
152- throw new ArgumentNullException ( nameof ( groups ) ) ;
153- if ( groupName is null )
154- throw new ArgumentNullException ( nameof ( groupName ) ) ;
155- Contract . EndContractBlock ( ) ;
156-
157- var group = groups [ groupName ] ;
158- return group . Success
159- ? group . Value
160- : null ;
161- }
162-
163- /// <returns>The value of the requested group or an empty span if not found.</returns>
164- /// <inheritdoc cref="GetValue(GroupCollection, string)" />
165- public static ReadOnlySpan < char > GetValueSpan ( this GroupCollection groups , string groupName )
166- {
167- if ( groups is null )
168- throw new ArgumentNullException ( nameof ( groups ) ) ;
169- if ( groupName is null )
170- throw new ArgumentNullException ( nameof ( groupName ) ) ;
171- Contract . EndContractBlock ( ) ;
172-
173- var group = groups [ groupName ] ;
174- return group . Success
175- ? group . AsSpan ( )
176- : [ ] ;
177- }
139+ && ( trim ? RegexPatterns . ValidAlphaNumericOnlyUntrimmedPattern : RegexPatterns . ValidAlphaNumericOnlyPattern ) . IsMatch ( source ) ;
178140
179141 /// <summary>
180142 /// Returns the available matches as StringSegments.
@@ -203,7 +165,6 @@ static IEnumerable<StringSegment> AsSegmentsCore(Regex pattern, string input)
203165 }
204166 }
205167 }
206- #endregion
207168
208169 #region Numeric string formatting.
209170 /// <summary>
@@ -251,7 +212,7 @@ public static string ToByteString(this double bytes, string decimalFormat = "N1"
251212 return string . Format ( cultureInfo , bytes == 1 ? BYTE : BYTES , bytes ) ;
252213
253214 var label = string . Empty ;
254- foreach ( var s in _byte_labels )
215+ foreach ( var s in ByteLabels )
255216 {
256217 label = s ;
257218 bytes /= BYTE_RED ;
@@ -284,7 +245,7 @@ public static string ToMetricString(this double number, string decimalFormat = "
284245 return number . ToString ( decimalFormat , cultureInfo ?? CultureInfo . InvariantCulture ) ;
285246
286247 var label = string . Empty ;
287- foreach ( var s in _number_labels )
248+ foreach ( var s in NumberLabels )
288249 {
289250 label = s ;
290251 number /= 1000 ;
@@ -309,11 +270,6 @@ public static string ToMetricString(this int number, string decimalFormat = "N1"
309270 => ToMetricString ( ( double ) number , decimalFormat , cultureInfo ) ;
310271 #endregion
311272
312- /// <summary>
313- /// Compiled Regex for finding white-space.
314- /// </summary>
315- public static readonly Regex WhiteSpacePattern = new ( @"\s+" , RegexOptions . Compiled ) ;
316-
317273 /// <summary>
318274 /// Replaces any white-space with the specified string.
319275 /// Collapses multiple white-space characters to a single space if no replacement specified.
@@ -328,7 +284,7 @@ public static string ReplaceWhiteSpace(this string source, string replace = " ")
328284 if ( replace is null ) throw new ArgumentNullException ( nameof ( replace ) ) ;
329285 Contract . EndContractBlock ( ) ;
330286
331- return WhiteSpacePattern . Replace ( source , replace ) ;
287+ return RegexPatterns . WhiteSpacePattern . Replace ( source , replace ) ;
332288 }
333289
334290 /// <summary>
0 commit comments