1- using System ;
1+ using JetBrains . Annotations ;
2+ using System ;
23using System . Collections . Concurrent ;
34using System . Collections . Generic ;
45using System . ComponentModel ;
5-
66using System . Globalization ;
77using System . IO ;
88using System . Linq ;
@@ -2228,18 +2228,15 @@ public static bool CoinToss(this Random @this)
22282228 public static bool IsMatch ( this string input , string pattern , RegexOptions options ) => Regex . IsMatch ( input , pattern , options ) ;
22292229
22302230 /// <summary>An IEnumerable<string> extension method that concatenates the given this.</summary>
2231- /// <param name="this ">The @this to act on.</param>
2231+ /// <param name="stringCollection ">The string collection to act on.</param>
22322232 /// <returns>A string.</returns>
2233- public static string Concatenate ( this IEnumerable < string > @this )
2233+ public static string Concatenate ( [ NotNull ] this IEnumerable < string > stringCollection )
22342234 {
2235- var sb = new StringBuilder ( ) ;
2236-
2237- foreach ( var s in @this )
2235+ if ( stringCollection is null )
22382236 {
2239- sb . Append ( s ) ;
2237+ throw new ArgumentNullException ( nameof ( stringCollection ) ) ;
22402238 }
2241-
2242- return sb . ToString ( ) ;
2239+ return string . Join ( string . Empty , stringCollection ) ;
22432240 }
22442241
22452242 /// <summary>An IEnumerable<T> extension method that concatenates.</summary>
@@ -2249,13 +2246,11 @@ public static string Concatenate(this IEnumerable<string> @this)
22492246 /// <returns>A string.</returns>
22502247 public static string Concatenate < T > ( this IEnumerable < T > source , Func < T , string > func )
22512248 {
2252- var sb = new StringBuilder ( ) ;
2253- foreach ( var item in source )
2249+ if ( source is null )
22542250 {
2255- sb . Append ( func ( item ) ) ;
2251+ throw new ArgumentNullException ( nameof ( source ) ) ;
22562252 }
2257-
2258- return sb . ToString ( ) ;
2253+ return string . Join ( string . Empty , source . Select ( func ) ) ;
22592254 }
22602255
22612256 /// <summary>
@@ -2264,7 +2259,7 @@ public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string>
22642259 /// <param name="this">The @this to act on.</param>
22652260 /// <param name="value">The value.</param>
22662261 /// <returns>true if the value is in the string, false if not.</returns>
2267- public static bool Contains ( this string @this , string value ) => @this . IndexOf ( value , StringComparison . Ordinal ) != - 1 ;
2262+ public static bool Contains ( this string @this , string value ) => Guard . NotNull ( @this , nameof ( @this ) ) . IndexOf ( value , StringComparison . Ordinal ) != - 1 ;
22682263
22692264 /// <summary>
22702265 /// A string extension method that query if this object contains the given value.
@@ -2273,23 +2268,23 @@ public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string>
22732268 /// <param name="value">The value.</param>
22742269 /// <param name="comparisonType">Type of the comparison.</param>
22752270 /// <returns>true if the value is in the string, false if not.</returns>
2276- public static bool Contains ( this string @this , string value , StringComparison comparisonType ) => @this . IndexOf ( value , comparisonType ) != - 1 ;
2271+ public static bool Contains ( this string @this , string value , StringComparison comparisonType ) => Guard . NotNull ( @this , nameof ( @this ) ) . IndexOf ( value , comparisonType ) != - 1 ;
22772272
22782273 /// <summary>
22792274 /// A string extension method that extracts this object.
22802275 /// </summary>
22812276 /// <param name="this">The @this to act on.</param>
22822277 /// <param name="predicate">The predicate.</param>
22832278 /// <returns>A string.</returns>
2284- public static string Extract ( this string @this , Func < char , bool > predicate ) => new ( @this . ToCharArray ( ) . Where ( predicate ) . ToArray ( ) ) ;
2279+ public static string Extract ( this string @this , Func < char , bool > predicate ) => new ( Guard . NotNull ( @this , nameof ( @this ) ) . ToCharArray ( ) . Where ( predicate ) . ToArray ( ) ) ;
22852280
22862281 /// <summary>
22872282 /// A string extension method that removes the letter.
22882283 /// </summary>
22892284 /// <param name="this">The @this to act on.</param>
22902285 /// <param name="predicate">The predicate.</param>
22912286 /// <returns>A string.</returns>
2292- public static string RemoveWhere ( this string @this , Func < char , bool > predicate ) => new ( @this . ToCharArray ( ) . Where ( x => ! predicate ( x ) ) . ToArray ( ) ) ;
2287+ public static string RemoveWhere ( this string @this , Func < char , bool > predicate ) => new ( Guard . NotNull ( @this , nameof ( @this ) ) . ToCharArray ( ) . Where ( x => ! predicate ( x ) ) . ToArray ( ) ) ;
22932288
22942289 /// <summary>
22952290 /// Replaces the format item in a specified String with the text equivalent of the value of a corresponding
@@ -2301,7 +2296,7 @@ public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string>
23012296 /// A copy of format in which the format items have been replaced by the String equivalent of the corresponding
23022297 /// instances of Object in args.
23032298 /// </returns>
2304- public static string FormatWith ( this string @this , params object [ ] values ) => string . Format ( @this , values ) ;
2299+ public static string FormatWith ( this string @this , params object [ ] values ) => string . Format ( Guard . NotNull ( @this , nameof ( @this ) ) , values ) ;
23052300
23062301 /// <summary>
23072302 /// A string extension method that query if '@this' satisfy the specified pattern.
@@ -2322,7 +2317,7 @@ public static bool IsLike(this string @this, string pattern)
23222317 . Replace ( @"\*" , ".*" )
23232318 . Replace ( @"\#" , @"\d" ) ;
23242319
2325- return Regex . IsMatch ( @this , regexPattern ) ;
2320+ return Regex . IsMatch ( Guard . NotNull ( @this , nameof ( @this ) ) , regexPattern ) ;
23262321 }
23272322
23282323 /// <summary>
@@ -2349,6 +2344,7 @@ public static string SafeSubstring(this string @this, int startIndex)
23492344 /// <returns></returns>
23502345 public static string SafeSubstring ( this string str , int startIndex , int length )
23512346 {
2347+ Guard . NotNull ( str , nameof ( str ) ) ;
23522348 if ( startIndex < 0 || startIndex >= str . Length || length < 0 )
23532349 {
23542350 return string . Empty ;
@@ -2364,6 +2360,7 @@ public static string SafeSubstring(this string str, int startIndex, int length)
23642360 /// <returns>substring</returns>
23652361 public static string Sub ( this string @this , int startIndex )
23662362 {
2363+ Guard . NotNull ( @this , nameof ( @this ) ) ;
23672364 if ( startIndex >= 0 )
23682365 {
23692366 return @this . SafeSubstring ( startIndex ) ;
@@ -2383,6 +2380,7 @@ public static string Sub(this string @this, int startIndex)
23832380 /// <returns>The repeated string.</returns>
23842381 public static string Repeat ( this string @this , int repeatCount )
23852382 {
2383+ Guard . NotNull ( @this , nameof ( @this ) ) ;
23862384 if ( @this . Length == 1 )
23872385 {
23882386 return new string ( @this [ 0 ] , repeatCount ) ;
@@ -2402,14 +2400,14 @@ public static string Repeat(this string @this, int repeatCount)
24022400 /// </summary>
24032401 /// <param name="this">The @this to act on.</param>
24042402 /// <returns>The string reversed.</returns>
2405- public static string Reverse ( this string @this )
2403+ public static string Reverse ( this string ? @this )
24062404 {
2407- if ( @this . Length <= 1 )
2405+ if ( string . IsNullOrWhiteSpace ( @this ) )
24082406 {
2409- return @this ;
2407+ return @this ?? string . Empty ;
24102408 }
24112409
2412- var chars = @this . ToCharArray ( ) ;
2410+ var chars = @this ! . ToCharArray ( ) ;
24132411 Array . Reverse ( chars ) ;
24142412 return new string ( chars ) ;
24152413 }
@@ -2427,57 +2425,57 @@ public static string Reverse(this string @this)
24272425 /// <returns>
24282426 /// An array whose elements contain the substrings in this string that are delimited by the separator.
24292427 /// </returns>
2430- public static string [ ] Split ( this string @this , string separator , StringSplitOptions option = StringSplitOptions . None ) => @this . Split ( new [ ] { separator } , option ) ;
2428+ public static string [ ] Split ( this string @this , string separator , StringSplitOptions option = StringSplitOptions . None ) => Guard . NotNull ( @this , nameof ( @this ) ) . Split ( new [ ] { separator } , option ) ;
24312429
24322430 /// <summary>
24332431 /// A string extension method that converts the @this to a byte array.
24342432 /// </summary>
24352433 /// <param name="this">The @this to act on.</param>
24362434 /// <returns>@this as a byte[].</returns>
2437- public static byte [ ] ToByteArray ( this string @this ) => Encoding . UTF8 . GetBytes ( @this ) ;
2435+ public static byte [ ] ToByteArray ( this string @this ) => Encoding . UTF8 . GetBytes ( Guard . NotNull ( @this , nameof ( @this ) ) ) ;
24382436
24392437 /// <summary>
24402438 /// A string extension method that converts the @this to a byte array.
24412439 /// </summary>
24422440 /// <param name="this">The @this to act on.</param>
24432441 /// <param name="encoding">encoding</param>
24442442 /// <returns>@this as a byte[].</returns>
2445- public static byte [ ] ToByteArray ( this string @this , Encoding encoding ) => encoding . GetBytes ( @this ) ;
2443+ public static byte [ ] ToByteArray ( this string @this , Encoding encoding ) => encoding . GetBytes ( Guard . NotNull ( @this , nameof ( @this ) ) ) ;
24462444
2447- public static byte [ ] GetBytes ( this string str ) => str . GetBytes ( Encoding . UTF8 ) ;
2445+ public static byte [ ] GetBytes ( this string str ) => Guard . NotNull ( str , nameof ( str ) ) . GetBytes ( Encoding . UTF8 ) ;
24482446
2449- public static byte [ ] GetBytes ( this string str , Encoding encoding ) => encoding . GetBytes ( str ) ;
2447+ public static byte [ ] GetBytes ( this string str , Encoding encoding ) => encoding . GetBytes ( Guard . NotNull ( str , nameof ( str ) ) ) ;
24502448
24512449 /// <summary>
24522450 /// A string extension method that converts the @this to an enum.
24532451 /// </summary>
24542452 /// <typeparam name="T">Generic type parameter.</typeparam>
24552453 /// <param name="this">The @this to act on.</param>
24562454 /// <returns>@this as a T.</returns>
2457- public static T ToEnum < T > ( this string @this ) => ( T ) Enum . Parse ( typeof ( T ) , @this ) ;
2455+ public static T ToEnum < T > ( this string @this ) => ( T ) Enum . Parse ( typeof ( T ) , Guard . NotNull ( @this , nameof ( @this ) ) ) ;
24582456
24592457 /// <summary>
24602458 /// A string extension method that converts the @this to a title case.
24612459 /// </summary>
24622460 /// <param name="this">The @this to act on.</param>
24632461 /// <returns>@this as a string.</returns>
2464- public static string ToTitleCase ( this string @this ) => new CultureInfo ( "en-US" ) . TextInfo . ToTitleCase ( @this ) ;
2462+ public static string ToTitleCase ( this string @this ) => new CultureInfo ( "en-US" ) . TextInfo . ToTitleCase ( Guard . NotNull ( @this , nameof ( @this ) ) ) ;
24652463
24662464 /// <summary>
24672465 /// A string extension method that converts the @this to a title case.
24682466 /// </summary>
24692467 /// <param name="this">The @this to act on.</param>
24702468 /// <param name="cultureInfo">Information describing the culture.</param>
24712469 /// <returns>@this as a string.</returns>
2472- public static string ToTitleCase ( this string @this , CultureInfo cultureInfo ) => cultureInfo . TextInfo . ToTitleCase ( @this ) ;
2470+ public static string ToTitleCase ( this string @this , CultureInfo cultureInfo ) => cultureInfo . TextInfo . ToTitleCase ( Guard . NotNull ( @this , nameof ( @this ) ) ) ;
24732471
24742472 /// <summary>
24752473 /// A string extension method that truncates.
24762474 /// </summary>
24772475 /// <param name="this">The @this to act on.</param>
24782476 /// <param name="maxLength">The maximum length.</param>
24792477 /// <returns>A string.</returns>
2480- public static string Truncate ( this string @this , int maxLength ) => @this . Truncate ( maxLength , "..." ) ;
2478+ public static string Truncate ( this string @this , int maxLength ) => Guard . NotNull ( @this , nameof ( @this ) ) . Truncate ( maxLength , "..." ) ;
24812479
24822480 /// <summary>
24832481 /// A string extension method that truncates.
@@ -2646,39 +2644,45 @@ public static StringBuilder AppendLineIf(this StringBuilder builder, string text
26462644 /// <param name="type">type</param>
26472645 /// <returns></returns>
26482646 public static bool HasEmptyConstructor ( this Type type )
2649- => type . GetConstructors ( BindingFlags . Instance ) . Any ( c => c . GetParameters ( ) . Length == 0 ) ;
2647+ => Guard . NotNull ( type , nameof ( type ) ) . GetConstructors ( BindingFlags . Instance ) . Any ( c => c . GetParameters ( ) . Length == 0 ) ;
26502648
26512649 public static bool IsNullableType ( this Type type )
26522650 {
2651+ Guard . NotNull ( type , nameof ( type ) ) ;
26532652 return type . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) ;
26542653 }
26552654
26562655 private static readonly ConcurrentDictionary < Type , object > _defaultValues =
26572656 new ( ) ;
26582657
26592658 /// <summary>
2660- /// 根据 Type 获取默认值,实现类似 default(T) 的功能
2659+ /// get default value by type, default(T)
26612660 /// </summary>
26622661 /// <param name="type">type</param>
2663- /// <returns></returns>
2664- public static object ? GetDefaultValue ( this Type type ) =>
2665- type . IsValueType && type != typeof ( void ) ? _defaultValues . GetOrAdd ( type , Activator . CreateInstance ) : null ;
2662+ /// <returns>default value</returns>
2663+ public static object ? GetDefaultValue ( this Type type )
2664+ {
2665+ Guard . NotNull ( type , nameof ( type ) ) ;
2666+ return type . IsValueType && type != typeof ( void )
2667+ ? _defaultValues . GetOrAdd ( type , Activator . CreateInstance )
2668+ : null ;
2669+ }
26662670
26672671 /// <summary>
26682672 /// GetUnderlyingType if nullable else return self
26692673 /// </summary>
26702674 /// <param name="type">type</param>
26712675 /// <returns></returns>
26722676 public static Type Unwrap ( this Type type )
2673- => Nullable . GetUnderlyingType ( type ) ?? type ;
2677+ => Nullable . GetUnderlyingType ( Guard . NotNull ( type , nameof ( type ) ) ) ?? type ;
26742678
26752679 /// <summary>
26762680 /// GetUnderlyingType
26772681 /// </summary>
26782682 /// <param name="type">type</param>
26792683 /// <returns></returns>
26802684 public static Type ? GetUnderlyingType ( this Type type )
2681- => Nullable . GetUnderlyingType ( type ) ;
2685+ => Nullable . GetUnderlyingType ( Guard . NotNull ( type , nameof ( type ) ) ) ;
26822686
26832687 #endregion Type
26842688 }
0 commit comments