20
20
using ServiceStack . Text ;
21
21
using ServiceStack . Text . Common ;
22
22
using ServiceStack . Text . Support ;
23
+ using static System . String ;
23
24
24
25
namespace ServiceStack
25
26
{
@@ -57,19 +58,19 @@ public static string BaseConvert(this string source, int from, int to)
57
58
var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
58
59
var len = source . Length ;
59
60
if ( len == 0 )
60
- throw new Exception ( string . Format ( "Parameter: '{0}' is not valid integer (in base {1})." , source , from ) ) ;
61
+ throw new Exception ( Format ( "Parameter: '{0}' is not valid integer (in base {1})." , source , from ) ) ;
61
62
var minus = source [ 0 ] == '-' ? "-" : "" ;
62
63
var src = minus == "" ? source : source . Substring ( 1 ) ;
63
64
len = src . Length ;
64
65
if ( len == 0 )
65
- throw new Exception ( string . Format ( "Parameter: '{0}' is not valid integer (in base {1})." , source , from ) ) ;
66
+ throw new Exception ( Format ( "Parameter: '{0}' is not valid integer (in base {1})." , source , from ) ) ;
66
67
67
68
var d = 0 ;
68
69
for ( int i = 0 ; i < len ; i ++ ) // Convert to decimal
69
70
{
70
71
int c = chars . IndexOf ( src [ i ] ) ;
71
72
if ( c >= from )
72
- throw new Exception ( string . Format ( "Parameter: '{0}' is not valid integer (in base {1})." , source , from ) ) ;
73
+ throw new Exception ( Format ( "Parameter: '{0}' is not valid integer (in base {1})." , source , from ) ) ;
73
74
d = d * from + c ;
74
75
}
75
76
if ( to == 10 || d == 0 )
@@ -91,7 +92,7 @@ public static string EncodeXml(this string value)
91
92
92
93
public static string EncodeJson ( this string value )
93
94
{
94
- return String . Concat
95
+ return Concat
95
96
( "\" " ,
96
97
value . Replace ( "\\ " , "\\ \\ " ) . Replace ( "\" " , "\\ \" " ) . Replace ( "\r " , "" ) . Replace ( "\n " , "\\ n" ) ,
97
98
"\" "
@@ -106,7 +107,7 @@ public static string EncodeJsv(this string value)
106
107
}
107
108
return String . IsNullOrEmpty ( value ) || ! JsWriter . HasAnyEscapeChars ( value )
108
109
? value
109
- : String . Concat
110
+ : Concat
110
111
(
111
112
JsWriter . QuoteString ,
112
113
value . Replace ( JsWriter . QuoteString , TypeSerializer . DoubleQuoteString ) ,
@@ -126,7 +127,7 @@ public static string DecodeJsv(this string value)
126
127
127
128
public static string UrlEncode ( this string text , bool upperCase = false )
128
129
{
129
- if ( string . IsNullOrEmpty ( text ) ) return text ;
130
+ if ( String . IsNullOrEmpty ( text ) ) return text ;
130
131
131
132
var sb = StringBuilderThreadStatic . Allocate ( ) ;
132
133
var fmt = upperCase ? "X2" : "x2" ;
@@ -188,7 +189,7 @@ public static string UrlDecode(this string text)
188
189
189
190
public static string HexUnescape ( this string text , params char [ ] anyCharOf )
190
191
{
191
- if ( string . IsNullOrEmpty ( text ) ) return null ;
192
+ if ( String . IsNullOrEmpty ( text ) ) return null ;
192
193
if ( anyCharOf == null || anyCharOf . Length == 0 ) return text ;
193
194
194
195
var sb = StringBuilderThreadStatic . Allocate ( ) ;
@@ -221,7 +222,7 @@ public static string UrlFormat(this string url, params string[] urlComponents)
221
222
encodedUrlComponents [ i ] = x . UrlEncode ( ) ;
222
223
}
223
224
224
- return String . Format ( url , encodedUrlComponents ) ;
225
+ return Format ( url , encodedUrlComponents ) ;
225
226
}
226
227
227
228
public static string ToRot13 ( this string value )
@@ -472,7 +473,7 @@ public static string[] SplitOnLast(this string strVal, string needle)
472
473
473
474
public static string WithoutExtension ( this string filePath )
474
475
{
475
- if ( string . IsNullOrEmpty ( filePath ) )
476
+ if ( String . IsNullOrEmpty ( filePath ) )
476
477
return null ;
477
478
478
479
var extPos = filePath . LastIndexOf ( '.' ) ;
@@ -484,11 +485,11 @@ public static string WithoutExtension(this string filePath)
484
485
485
486
public static string GetExtension ( this string filePath )
486
487
{
487
- if ( string . IsNullOrEmpty ( filePath ) )
488
+ if ( String . IsNullOrEmpty ( filePath ) )
488
489
return null ;
489
490
490
491
var extPos = filePath . LastIndexOf ( '.' ) ;
491
- return extPos == - 1 ? string . Empty : filePath . Substring ( extPos ) ;
492
+ return extPos == - 1 ? Empty : filePath . Substring ( extPos ) ;
492
493
}
493
494
494
495
static readonly char [ ] DirSeps = new [ ] { '\\ ' , '/' } ;
@@ -554,27 +555,27 @@ public static T FromCsv<T>(this string csv)
554
555
555
556
public static string FormatWith ( this string text , params object [ ] args )
556
557
{
557
- return String . Format ( text , args ) ;
558
+ return Format ( text , args ) ;
558
559
}
559
560
560
561
public static string Fmt ( this string text , params object [ ] args )
561
562
{
562
- return String . Format ( text , args ) ;
563
+ return Format ( text , args ) ;
563
564
}
564
565
565
566
public static string Fmt ( this string text , object arg1 )
566
567
{
567
- return String . Format ( text , arg1 ) ;
568
+ return Format ( text , arg1 ) ;
568
569
}
569
570
570
571
public static string Fmt ( this string text , object arg1 , object arg2 )
571
572
{
572
- return String . Format ( text , arg1 , arg2 ) ;
573
+ return Format ( text , arg1 , arg2 ) ;
573
574
}
574
575
575
576
public static string Fmt ( this string text , object arg1 , object arg2 , object arg3 )
576
577
{
577
- return String . Format ( text , arg1 , arg2 , arg3 ) ;
578
+ return Format ( text , arg1 , arg2 , arg3 ) ;
578
579
}
579
580
580
581
public static bool StartsWithIgnoreCase ( this string text , string startsWith )
@@ -663,7 +664,7 @@ public static string ExtractContents(this string fromText, string uniqueMarker,
663
664
664
665
public static string StripHtml ( this string html )
665
666
{
666
- return string . IsNullOrEmpty ( html ) ? null : StripHtmlRegEx . Replace ( html , "" ) ;
667
+ return String . IsNullOrEmpty ( html ) ? null : StripHtmlRegEx . Replace ( html , "" ) ;
667
668
}
668
669
669
670
public static string Quoted ( this string text )
@@ -675,7 +676,7 @@ public static string Quoted(this string text)
675
676
676
677
public static string StripQuotes ( this string text )
677
678
{
678
- return string . IsNullOrEmpty ( text ) || text . Length < 2
679
+ return String . IsNullOrEmpty ( text ) || text . Length < 2
679
680
? text
680
681
: text [ 0 ] == '"' && text [ text . Length - 1 ] == '"'
681
682
? text . Substring ( 1 , text . Length - 2 )
@@ -703,7 +704,7 @@ public static string StripMarkdownMarkup(this string markdown)
703
704
private const int LowerCaseOffset = 'a' - 'A' ;
704
705
public static string ToCamelCase ( this string value )
705
706
{
706
- if ( string . IsNullOrEmpty ( value ) ) return value ;
707
+ if ( String . IsNullOrEmpty ( value ) ) return value ;
707
708
708
709
var len = value . Length ;
709
710
var newValue = new char [ len ] ;
@@ -729,7 +730,7 @@ public static string ToCamelCase(this string value)
729
730
730
731
public static string ToPascalCase ( this string value )
731
732
{
732
- if ( string . IsNullOrEmpty ( value ) ) return value ;
733
+ if ( String . IsNullOrEmpty ( value ) ) return value ;
733
734
734
735
if ( value . IndexOf ( '_' ) >= 0 )
735
736
{
@@ -754,7 +755,7 @@ public static string ToTitleCase(this string value)
754
755
755
756
public static string ToLowercaseUnderscore ( this string value )
756
757
{
757
- if ( string . IsNullOrEmpty ( value ) ) return value ;
758
+ if ( String . IsNullOrEmpty ( value ) ) return value ;
758
759
value = value . ToCamelCase ( ) ;
759
760
760
761
var sb = StringBuilderThreadStatic . Allocate ( ) ;
@@ -790,11 +791,11 @@ public static string SafeSubstring(this string value, int startIndex)
790
791
791
792
public static string SafeSubstring ( this string value , int startIndex , int length )
792
793
{
793
- if ( string . IsNullOrEmpty ( value ) ) return string . Empty ;
794
+ if ( String . IsNullOrEmpty ( value ) ) return Empty ;
794
795
if ( value . Length >= ( startIndex + length ) )
795
796
return value . Substring ( startIndex , length ) ;
796
797
797
- return value . Length > startIndex ? value . Substring ( startIndex ) : String . Empty ;
798
+ return value . Length > startIndex ? value . Substring ( startIndex ) : Empty ;
798
799
}
799
800
800
801
public static bool IsAnonymousType ( this Type type )
@@ -807,7 +808,7 @@ public static bool IsAnonymousType(this Type type)
807
808
808
809
public static int CompareIgnoreCase ( this string strA , string strB )
809
810
{
810
- return string . Compare ( strA , strB , PclExport . Instance . InvariantComparisonIgnoreCase ) ;
811
+ return Compare ( strA , strB , PclExport . Instance . InvariantComparisonIgnoreCase ) ;
811
812
}
812
813
813
814
public static bool EndsWithInvariant ( this string str , string endsWith )
@@ -827,7 +828,7 @@ public static T ToEnum<T>(this string value)
827
828
828
829
public static T ToEnumOrDefault < T > ( this string value , T defaultValue )
829
830
{
830
- if ( string . IsNullOrEmpty ( value ) ) return defaultValue ;
831
+ if ( String . IsNullOrEmpty ( value ) ) return defaultValue ;
831
832
return ( T ) Enum . Parse ( typeof ( T ) , value , true ) ;
832
833
}
833
834
@@ -858,17 +859,17 @@ public static string ToHttps(this string url)
858
859
859
860
public static bool IsEmpty ( this string value )
860
861
{
861
- return string . IsNullOrEmpty ( value ) ;
862
+ return String . IsNullOrEmpty ( value ) ;
862
863
}
863
864
864
865
public static bool IsNullOrEmpty ( this string value )
865
866
{
866
- return string . IsNullOrEmpty ( value ) ;
867
+ return String . IsNullOrEmpty ( value ) ;
867
868
}
868
869
869
870
public static bool EqualsIgnoreCase ( this string value , string other )
870
871
{
871
- return string . Equals ( value , other , StringComparison . CurrentCultureIgnoreCase ) ;
872
+ return String . Equals ( value , other , StringComparison . CurrentCultureIgnoreCase ) ;
872
873
}
873
874
874
875
public static string ReplaceFirst ( this string haystack , string needle , string replacement )
@@ -904,18 +905,18 @@ public static bool ContainsAny(this string text, params string[] testMatches)
904
905
905
906
public static string SafeVarName ( this string text )
906
907
{
907
- if ( String . IsNullOrEmpty ( text ) ) return null ;
908
+ if ( string . IsNullOrEmpty ( text ) ) return null ;
908
909
return InvalidVarCharsRegex . Replace ( text , "_" ) ;
909
910
}
910
911
911
912
public static string Join ( this List < string > items )
912
913
{
913
- return String . Join ( JsWriter . ItemSeperatorString , items . ToArray ( ) ) ;
914
+ return string . Join ( JsWriter . ItemSeperatorString , items . ToArray ( ) ) ;
914
915
}
915
916
916
917
public static string Join ( this List < string > items , string delimeter )
917
918
{
918
- return String . Join ( delimeter , items . ToArray ( ) ) ;
919
+ return string . Join ( delimeter , items . ToArray ( ) ) ;
919
920
}
920
921
921
922
public static string ToParentPath ( this string path )
@@ -941,15 +942,15 @@ public static string RemoveCharFlags(this string text, bool[] charFlags)
941
942
copy [ nonWsPos ++ ] = @char ;
942
943
}
943
944
944
- return new String ( copy , 0 , nonWsPos ) ;
945
+ return new string ( copy , 0 , nonWsPos ) ;
945
946
}
946
947
947
948
public static string ToNullIfEmpty ( this string text )
948
949
{
949
- return String . IsNullOrEmpty ( text ) ? null : text ;
950
+ return string . IsNullOrEmpty ( text ) ? null : text ;
950
951
}
951
952
952
- private static char [ ] SystemTypeChars = new [ ] { '<' , '>' , '+' } ;
953
+ private static readonly char [ ] SystemTypeChars = { '<' , '>' , '+' } ;
953
954
954
955
public static bool IsUserType ( this Type type )
955
956
{
@@ -970,11 +971,13 @@ public static bool IsSystemType(this Type type)
970
971
|| type . Name . IndexOfAny ( SystemTypeChars ) >= 0 ;
971
972
}
972
973
974
+ public static bool IsTuple ( this Type type ) => type . Name . StartsWith ( "Tuple`" ) ;
975
+
973
976
public static bool IsInt ( this string text )
974
977
{
975
- if ( String . IsNullOrEmpty ( text ) ) return false ;
978
+ if ( string . IsNullOrEmpty ( text ) ) return false ;
976
979
int ret ;
977
- return Int32 . TryParse ( text , out ret ) ;
980
+ return int . TryParse ( text , out ret ) ;
978
981
}
979
982
980
983
public static int ToInt ( this string text )
@@ -985,18 +988,18 @@ public static int ToInt(this string text)
985
988
public static int ToInt ( this string text , int defaultValue )
986
989
{
987
990
int ret ;
988
- return Int32 . TryParse ( text , out ret ) ? ret : defaultValue ;
991
+ return int . TryParse ( text , out ret ) ? ret : defaultValue ;
989
992
}
990
993
991
994
public static long ToInt64 ( this string text )
992
995
{
993
- return Int64 . Parse ( text ) ;
996
+ return long . Parse ( text ) ;
994
997
}
995
998
996
999
public static long ToInt64 ( this string text , long defaultValue )
997
1000
{
998
1001
long ret ;
999
- return Int64 . TryParse ( text , out ret ) ? ret : defaultValue ;
1002
+ return long . TryParse ( text , out ret ) ? ret : defaultValue ;
1000
1003
}
1001
1004
1002
1005
public static float ToFloat ( this string text )
@@ -1056,7 +1059,7 @@ public static bool Glob(this string value, string pattern)
1056
1059
return false ;
1057
1060
1058
1061
default :
1059
- if ( value . Length == pos || Char . ToUpper ( pattern [ pos ] ) != Char . ToUpper ( value [ pos ] ) )
1062
+ if ( value . Length == pos || char . ToUpper ( pattern [ pos ] ) != char . ToUpper ( value [ pos ] ) )
1060
1063
{
1061
1064
return false ;
1062
1065
}
@@ -1132,9 +1135,7 @@ public static int CountOccurrencesOf(this string text, char needle)
1132
1135
1133
1136
public static string NormalizeNewLines ( this string text )
1134
1137
{
1135
- return text != null
1136
- ? text . Replace ( "\r \n " , "\n " )
1137
- : null ;
1138
+ return text ? . Replace ( "\r \n " , "\n " ) ;
1138
1139
}
1139
1140
1140
1141
#if ! LITE
0 commit comments