1717using System . Text . RegularExpressions ;
1818using System . Web ;
1919
20- namespace RestSharp . Extensions ;
20+ namespace RestSharp . Extensions ;
2121
2222static class StringExtensions {
2323 static readonly Regex IsUpperCaseRegex = new ( @"^[A-Z]+$" ) ;
@@ -33,20 +33,19 @@ static class StringExtensions {
3333 static readonly Regex AddSpacesRegex1 = new ( @"[-\s]" ) ;
3434 static readonly Regex AddSpacesRegex2 = new ( @"([a-z\d])([A-Z])" ) ;
3535 static readonly Regex AddSpacesRegex3 = new ( @"([A-Z]+)([A-Z][a-z])" ) ;
36- public static string UrlDecode ( this string input ) => HttpUtility . UrlDecode ( input ) ;
36+
37+ internal static string UrlDecode ( this string input ) => HttpUtility . UrlDecode ( input ) ;
3738
3839 /// <summary>
3940 /// Uses Uri.EscapeDataString() based on recommendations on MSDN
4041 /// http://blogs.msdn.com/b/yangxind/archive/2006/11/09/don-t-use-net-system-uri-unescapedatastring-in-url-decoding.aspx
4142 /// </summary>
42- public static string UrlEncode ( this string input ) {
43+ internal static string UrlEncode ( this string input ) {
4344 const int maxLength = 32766 ;
4445
45- if ( input == null )
46- throw new ArgumentNullException ( nameof ( input ) ) ;
46+ if ( input == null ) throw new ArgumentNullException ( nameof ( input ) ) ;
4747
48- if ( input . Length <= maxLength )
49- return Uri . EscapeDataString ( input ) ;
48+ if ( input . Length <= maxLength ) return Uri . EscapeDataString ( input ) ;
5049
5150 var sb = new StringBuilder ( input . Length * 2 ) ;
5251 var index = 0 ;
@@ -67,37 +66,18 @@ public static string UrlEncode(this string input) {
6766 return sb . ToString ( ) ;
6867 }
6968
70- public static string ? UrlEncode ( this string input , Encoding encoding ) {
69+ internal static string ? UrlEncode ( this string input , Encoding encoding ) {
7170 var encoded = HttpUtility . UrlEncode ( input , encoding ) ;
7271 return encoded ? . Replace ( "+" , "%20" ) ;
7372 }
7473
75- /// <summary>
76- /// Remove underscores from a string
77- /// </summary>
78- /// <param name="input">String to process</param>
79- /// <returns>string</returns>
80- public static string RemoveUnderscoresAndDashes ( this string input ) => input . Replace ( "_" , "" ) . Replace ( "-" , "" ) ;
74+ internal static string RemoveUnderscoresAndDashes ( this string input ) => input . Replace ( "_" , "" ) . Replace ( "-" , "" ) ;
8175
82- /// <summary>
83- /// Converts a string to pascal case
84- /// </summary>
85- /// <param name="lowercaseAndUnderscoredWord">String to convert</param>
86- /// <param name="culture"></param>
87- /// <returns>string</returns>
88- public static string ToPascalCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture )
76+ internal static string ToPascalCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture )
8977 => ToPascalCase ( lowercaseAndUnderscoredWord , true , culture ) ;
9078
91- /// <summary>
92- /// Converts a string to pascal case with the option to remove underscores
93- /// </summary>
94- /// <param name="text">String to convert</param>
95- /// <param name="removeUnderscores">Option to remove underscores</param>
96- /// <param name="culture"></param>
97- /// <returns></returns>
98- public static string ToPascalCase ( this string text , bool removeUnderscores , CultureInfo culture ) {
99- if ( string . IsNullOrEmpty ( text ) )
100- return text ;
79+ internal static string ToPascalCase ( this string text , bool removeUnderscores , CultureInfo culture ) {
80+ if ( string . IsNullOrEmpty ( text ) ) return text ;
10181
10282 text = text . Replace ( '_' , ' ' ) ;
10383
@@ -113,69 +93,17 @@ string CaseWord(string word) {
11393 var restOfWord = word . Substring ( 1 ) ;
11494 var firstChar = char . ToUpper ( word [ 0 ] , culture ) ;
11595
116- if ( restOfWord . IsUpperCase ( ) )
117- restOfWord = restOfWord . ToLower ( culture ) ;
96+ if ( restOfWord . IsUpperCase ( ) ) restOfWord = restOfWord . ToLower ( culture ) ;
11897
11998 return string . Concat ( firstChar , restOfWord ) ;
12099 }
121100 }
122101
123- /// <summary>
124- /// Converts a string to camel case
125- /// </summary>
126- /// <param name="lowercaseAndUnderscoredWord">String to convert</param>
127- /// <param name="culture"></param>
128- /// <returns>String</returns>
129- public static string ToCamelCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture )
102+ internal static string ToCamelCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture )
130103 => MakeInitialLowerCase ( ToPascalCase ( lowercaseAndUnderscoredWord , culture ) , culture ) ;
131104
132- static string MakeInitialLowerCase ( this string word , CultureInfo culture )
133- => string . Concat ( word . Substring ( 0 , 1 ) . ToLower ( culture ) , word . Substring ( 1 ) ) ;
134-
135- static string AddUnderscores ( this string pascalCasedWord )
136- => AddUnderscoresRegex1 . Replace (
137- AddUnderscoresRegex2 . Replace (
138- AddUnderscoresRegex3 . Replace ( pascalCasedWord , "$1_$2" ) ,
139- "$1_$2"
140- ) ,
141- "_"
142- ) ;
143-
144- static string AddDashes ( this string pascalCasedWord )
145- => AddDashesRegex1 . Replace (
146- AddDashesRegex2 . Replace (
147- AddDashesRegex3 . Replace ( pascalCasedWord , "$1-$2" ) ,
148- "$1-$2"
149- ) ,
150- "-"
151- ) ;
152-
153- static bool IsUpperCase ( this string inputString ) => IsUpperCaseRegex . IsMatch ( inputString ) ;
154-
155- static string AddUnderscorePrefix ( this string pascalCasedWord ) => $ "_{ pascalCasedWord } ";
156-
157- static string AddSpaces ( this string pascalCasedWord )
158- => AddSpacesRegex1 . Replace (
159- AddSpacesRegex2 . Replace (
160- AddSpacesRegex3 . Replace ( pascalCasedWord , "$1 $2" ) ,
161- "$1 $2"
162- ) ,
163- " "
164- ) ;
165-
166- internal static bool IsEmpty ( this string ? value ) => string . IsNullOrWhiteSpace ( value ) ;
167-
168- internal static bool IsNotEmpty ( this string ? value ) => ! string . IsNullOrWhiteSpace ( value ) ;
169-
170- /// <summary>
171- /// Return possible variants of a name for name matching.
172- /// </summary>
173- /// <param name="name">String to convert</param>
174- /// <param name="culture">The culture to use for conversion</param>
175- /// <returns>IEnumerable<string></returns>
176- public static IEnumerable < string > GetNameVariants ( this string name , CultureInfo culture ) {
177- if ( string . IsNullOrEmpty ( name ) )
178- yield break ;
105+ internal static IEnumerable < string > GetNameVariants ( this string name , CultureInfo culture ) {
106+ if ( string . IsNullOrEmpty ( name ) ) yield break ;
179107
180108 yield return name ;
181109
@@ -216,8 +144,46 @@ public static IEnumerable<string> GetNameVariants(this string name, CultureInfo
216144 yield return name . AddSpaces ( ) . ToLower ( culture ) ;
217145 }
218146
147+ internal static bool IsEmpty ( this string ? value ) => string . IsNullOrWhiteSpace ( value ) ;
148+
149+ internal static bool IsNotEmpty ( this string ? value ) => ! string . IsNullOrWhiteSpace ( value ) ;
150+
219151 internal static string JoinToString < T > ( this IEnumerable < T > collection , string separator , Func < T , string > getString )
220152 => JoinToString ( collection . Select ( getString ) , separator ) ;
221153
222154 internal static string JoinToString ( this IEnumerable < string > strings , string separator ) => string . Join ( separator , strings ) ;
223- }
155+
156+ static string MakeInitialLowerCase ( this string word , CultureInfo culture )
157+ => string . Concat ( word . Substring ( 0 , 1 ) . ToLower ( culture ) , word . Substring ( 1 ) ) ;
158+
159+ static string AddUnderscores ( this string pascalCasedWord )
160+ => AddUnderscoresRegex1 . Replace (
161+ AddUnderscoresRegex2 . Replace (
162+ AddUnderscoresRegex3 . Replace ( pascalCasedWord , "$1_$2" ) ,
163+ "$1_$2"
164+ ) ,
165+ "_"
166+ ) ;
167+
168+ static string AddDashes ( this string pascalCasedWord )
169+ => AddDashesRegex1 . Replace (
170+ AddDashesRegex2 . Replace (
171+ AddDashesRegex3 . Replace ( pascalCasedWord , "$1-$2" ) ,
172+ "$1-$2"
173+ ) ,
174+ "-"
175+ ) ;
176+
177+ static bool IsUpperCase ( this string inputString ) => IsUpperCaseRegex . IsMatch ( inputString ) ;
178+
179+ static string AddUnderscorePrefix ( this string pascalCasedWord ) => $ "_{ pascalCasedWord } ";
180+
181+ static string AddSpaces ( this string pascalCasedWord )
182+ => AddSpacesRegex1 . Replace (
183+ AddSpacesRegex2 . Replace (
184+ AddSpacesRegex3 . Replace ( pascalCasedWord , "$1 $2" ) ,
185+ "$1 $2"
186+ ) ,
187+ " "
188+ ) ;
189+ }
0 commit comments