17
17
using System . Text . RegularExpressions ;
18
18
using System . Web ;
19
19
20
- namespace RestSharp . Extensions ;
20
+ namespace RestSharp . Extensions ;
21
21
22
22
static class StringExtensions {
23
23
static readonly Regex IsUpperCaseRegex = new ( @"^[A-Z]+$" ) ;
@@ -33,20 +33,19 @@ static class StringExtensions {
33
33
static readonly Regex AddSpacesRegex1 = new ( @"[-\s]" ) ;
34
34
static readonly Regex AddSpacesRegex2 = new ( @"([a-z\d])([A-Z])" ) ;
35
35
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 ) ;
37
38
38
39
/// <summary>
39
40
/// Uses Uri.EscapeDataString() based on recommendations on MSDN
40
41
/// http://blogs.msdn.com/b/yangxind/archive/2006/11/09/don-t-use-net-system-uri-unescapedatastring-in-url-decoding.aspx
41
42
/// </summary>
42
- public static string UrlEncode ( this string input ) {
43
+ internal static string UrlEncode ( this string input ) {
43
44
const int maxLength = 32766 ;
44
45
45
- if ( input == null )
46
- throw new ArgumentNullException ( nameof ( input ) ) ;
46
+ if ( input == null ) throw new ArgumentNullException ( nameof ( input ) ) ;
47
47
48
- if ( input . Length <= maxLength )
49
- return Uri . EscapeDataString ( input ) ;
48
+ if ( input . Length <= maxLength ) return Uri . EscapeDataString ( input ) ;
50
49
51
50
var sb = new StringBuilder ( input . Length * 2 ) ;
52
51
var index = 0 ;
@@ -67,37 +66,18 @@ public static string UrlEncode(this string input) {
67
66
return sb . ToString ( ) ;
68
67
}
69
68
70
- public static string ? UrlEncode ( this string input , Encoding encoding ) {
69
+ internal static string ? UrlEncode ( this string input , Encoding encoding ) {
71
70
var encoded = HttpUtility . UrlEncode ( input , encoding ) ;
72
71
return encoded ? . Replace ( "+" , "%20" ) ;
73
72
}
74
73
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 ( "-" , "" ) ;
81
75
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 )
89
77
=> ToPascalCase ( lowercaseAndUnderscoredWord , true , culture ) ;
90
78
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 ;
101
81
102
82
text = text . Replace ( '_' , ' ' ) ;
103
83
@@ -113,69 +93,17 @@ string CaseWord(string word) {
113
93
var restOfWord = word . Substring ( 1 ) ;
114
94
var firstChar = char . ToUpper ( word [ 0 ] , culture ) ;
115
95
116
- if ( restOfWord . IsUpperCase ( ) )
117
- restOfWord = restOfWord . ToLower ( culture ) ;
96
+ if ( restOfWord . IsUpperCase ( ) ) restOfWord = restOfWord . ToLower ( culture ) ;
118
97
119
98
return string . Concat ( firstChar , restOfWord ) ;
120
99
}
121
100
}
122
101
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 )
130
103
=> MakeInitialLowerCase ( ToPascalCase ( lowercaseAndUnderscoredWord , culture ) , culture ) ;
131
104
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 ;
179
107
180
108
yield return name ;
181
109
@@ -216,8 +144,46 @@ public static IEnumerable<string> GetNameVariants(this string name, CultureInfo
216
144
yield return name . AddSpaces ( ) . ToLower ( culture ) ;
217
145
}
218
146
147
+ internal static bool IsEmpty ( this string ? value ) => string . IsNullOrWhiteSpace ( value ) ;
148
+
149
+ internal static bool IsNotEmpty ( this string ? value ) => ! string . IsNullOrWhiteSpace ( value ) ;
150
+
219
151
internal static string JoinToString < T > ( this IEnumerable < T > collection , string separator , Func < T , string > getString )
220
152
=> JoinToString ( collection . Select ( getString ) , separator ) ;
221
153
222
154
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