@@ -27,10 +27,7 @@ namespace RestSharp.Extensions
27
27
{
28
28
public static class StringExtensions
29
29
{
30
- public static string UrlDecode ( this string input )
31
- {
32
- return HttpUtility . UrlDecode ( input ) ;
33
- }
30
+ public static string UrlDecode ( this string input ) => HttpUtility . UrlDecode ( input ) ;
34
31
35
32
/// <summary>
36
33
/// Uses Uri.EscapeDataString() based on recommendations on MSDN
@@ -61,46 +58,28 @@ public static string UrlEncode(this string input)
61
58
return sb . ToString ( ) ;
62
59
}
63
60
64
- public static string HtmlDecode ( this string input )
65
- {
66
- return HttpUtility . HtmlDecode ( input ) ;
67
- }
61
+ public static string HtmlDecode ( this string input ) => HttpUtility . HtmlDecode ( input ) ;
68
62
69
- public static string HtmlEncode ( this string input )
70
- {
71
- return HttpUtility . HtmlEncode ( input ) ;
72
- }
63
+ public static string HtmlEncode ( this string input ) => HttpUtility . HtmlEncode ( input ) ;
73
64
74
- public static string UrlEncode ( this string input , Encoding encoding )
75
- {
76
- return HttpUtility . UrlEncode ( input , encoding ) ;
77
- }
65
+ public static string UrlEncode ( this string input , Encoding encoding ) => HttpUtility . UrlEncode ( input , encoding ) ;
78
66
79
- public static string HtmlAttributeEncode ( this string input )
80
- {
81
- return HttpUtility . HtmlAttributeEncode ( input ) ;
82
- }
67
+ public static string HtmlAttributeEncode ( this string input ) => HttpUtility . HtmlAttributeEncode ( input ) ;
83
68
84
69
/// <summary>
85
70
/// Check that a string is not null or empty
86
71
/// </summary>
87
72
/// <param name="input">String to check</param>
88
73
/// <returns>bool</returns>
89
- public static bool HasValue ( this string input )
90
- {
91
- return ! string . IsNullOrEmpty ( input ) ;
92
- }
74
+ public static bool HasValue ( this string input ) => ! string . IsNullOrEmpty ( input ) ;
93
75
94
76
/// <summary>
95
77
/// Remove underscores from a string
96
78
/// </summary>
97
79
/// <param name="input">String to process</param>
98
80
/// <returns>string</returns>
99
- public static string RemoveUnderscoresAndDashes ( this string input )
100
- {
101
- return input . Replace ( "_" , "" )
102
- . Replace ( "-" , "" ) ; // avoiding regex
103
- }
81
+ public static string RemoveUnderscoresAndDashes ( this string input ) =>
82
+ input . Replace ( "_" , "" ) . Replace ( "-" , "" ) ;
104
83
105
84
/// <summary>
106
85
/// Parses most common JSON date formats
@@ -116,15 +95,11 @@ public static DateTime ParseJsonDate(this string input, CultureInfo culture)
116
95
input = input . Replace ( "\r " , "" ) ;
117
96
input = input . RemoveSurroundingQuotes ( ) ;
118
97
119
- long unix ;
120
-
121
- if ( long . TryParse ( input , out unix ) )
98
+ if ( long . TryParse ( input , out var unix ) )
122
99
{
123
100
var epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
124
101
125
- if ( unix > maxAllowedTimestamp )
126
- return epoch . AddMilliseconds ( unix ) ;
127
- return epoch . AddSeconds ( unix ) ;
102
+ return unix > maxAllowedTimestamp ? epoch . AddMilliseconds ( unix ) : epoch . AddSeconds ( unix ) ;
128
103
}
129
104
130
105
if ( input . Contains ( "/Date(" ) )
@@ -180,24 +155,23 @@ private static DateTime ExtractDate(string input, string pattern, CultureInfo cu
180
155
var dt = DateTime . MinValue ;
181
156
var regex = new Regex ( pattern ) ;
182
157
183
- if ( regex . IsMatch ( input ) )
184
- {
185
- var matches = regex . Matches ( input ) ;
186
- var match = matches [ 0 ] ;
187
- var ms = Convert . ToInt64 ( match . Groups [ 1 ] . Value ) ;
188
- var epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
158
+ if ( ! regex . IsMatch ( input ) ) return dt ;
189
159
190
- dt = epoch . AddMilliseconds ( ms ) ;
160
+ var matches = regex . Matches ( input ) ;
161
+ var match = matches [ 0 ] ;
162
+ var ms = Convert . ToInt64 ( match . Groups [ 1 ] . Value ) ;
163
+ var epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
191
164
192
- // adjust if time zone modifier present
193
- if ( match . Groups . Count <= 2 || string . IsNullOrEmpty ( match . Groups [ 3 ] . Value ) ) return dt ;
165
+ dt = epoch . AddMilliseconds ( ms ) ;
194
166
195
- var mod = DateTime . ParseExact ( match . Groups [ 3 ] . Value , "HHmm" , culture ) ;
167
+ // adjust if time zone modifier present
168
+ if ( match . Groups . Count <= 2 || string . IsNullOrEmpty ( match . Groups [ 3 ] . Value ) ) return dt ;
196
169
197
- dt = match . Groups [ 2 ] . Value == "+"
198
- ? dt . Add ( mod . TimeOfDay )
199
- : dt . Subtract ( mod . TimeOfDay ) ;
200
- }
170
+ var mod = DateTime . ParseExact ( match . Groups [ 3 ] . Value , "HHmm" , culture ) ;
171
+
172
+ dt = match . Groups [ 2 ] . Value == "+"
173
+ ? dt . Add ( mod . TimeOfDay )
174
+ : dt . Subtract ( mod . TimeOfDay ) ;
201
175
202
176
return dt ;
203
177
}
@@ -208,21 +182,16 @@ private static DateTime ExtractDate(string input, string pattern, CultureInfo cu
208
182
/// <param name="input">String to check</param>
209
183
/// <param name="pattern">Pattern to match</param>
210
184
/// <returns>bool</returns>
211
- public static bool Matches ( this string input , string pattern )
212
- {
213
- return Regex . IsMatch ( input , pattern ) ;
214
- }
185
+ public static bool Matches ( this string input , string pattern ) => Regex . IsMatch ( input , pattern ) ;
215
186
216
187
/// <summary>
217
188
/// Converts a string to pascal case
218
189
/// </summary>
219
190
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
220
191
/// <param name="culture"></param>
221
192
/// <returns>string</returns>
222
- public static string ToPascalCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture )
223
- {
224
- return ToPascalCase ( lowercaseAndUnderscoredWord , true , culture ) ;
225
- }
193
+ public static string ToPascalCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture ) =>
194
+ ToPascalCase ( lowercaseAndUnderscoredWord , true , culture ) ;
226
195
227
196
/// <summary>
228
197
/// Converts a string to pascal case with the option to remove underscores
@@ -270,88 +239,72 @@ public static string ToPascalCase(this string text, bool removeUnderscores, Cult
270
239
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
271
240
/// <param name="culture"></param>
272
241
/// <returns>String</returns>
273
- public static string ToCamelCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture )
274
- {
275
- return MakeInitialLowerCase ( ToPascalCase ( lowercaseAndUnderscoredWord , culture ) ) ;
276
- }
242
+ public static string ToCamelCase ( this string lowercaseAndUnderscoredWord , CultureInfo culture ) =>
243
+ MakeInitialLowerCase ( ToPascalCase ( lowercaseAndUnderscoredWord , culture ) ) ;
277
244
278
245
/// <summary>
279
246
/// Convert the first letter of a string to lower case
280
247
/// </summary>
281
248
/// <param name="word">String to convert</param>
282
249
/// <returns>string</returns>
283
- public static string MakeInitialLowerCase ( this string word )
284
- {
285
- return string . Concat ( word . Substring ( 0 , 1 ) . ToLower ( ) , word . Substring ( 1 ) ) ;
286
- }
250
+ public static string MakeInitialLowerCase ( this string word ) =>
251
+ string . Concat ( word . Substring ( 0 , 1 ) . ToLower ( ) , word . Substring ( 1 ) ) ;
287
252
288
253
/// <summary>
289
254
/// Checks to see if a string is all uppper case
290
255
/// </summary>
291
256
/// <param name="inputString">String to check</param>
292
257
/// <returns>bool</returns>
293
- public static bool IsUpperCase ( this string inputString )
294
- {
295
- return Regex . IsMatch ( inputString , @"^[A-Z]+$" ) ;
296
- }
258
+ public static bool IsUpperCase ( this string inputString ) => Regex . IsMatch ( inputString , @"^[A-Z]+$" ) ;
297
259
298
260
/// <summary>
299
261
/// Add underscores to a pascal-cased string
300
262
/// </summary>
301
263
/// <param name="pascalCasedWord">String to convert</param>
302
264
/// <returns>string</returns>
303
- public static string AddUnderscores ( this string pascalCasedWord )
304
- {
305
- return Regex . Replace (
265
+ public static string AddUnderscores ( this string pascalCasedWord ) =>
266
+ Regex . Replace (
306
267
Regex . Replace (
307
268
Regex . Replace ( pascalCasedWord , @"([A-Z]+)([A-Z][a-z])" , "$1_$2" ) ,
308
269
@"([a-z\d])([A-Z])" ,
309
270
"$1_$2" ) ,
310
271
@"[-\s]" ,
311
272
"_" ) ;
312
- }
313
273
314
274
/// <summary>
315
275
/// Add dashes to a pascal-cased string
316
276
/// </summary>
317
277
/// <param name="pascalCasedWord">String to convert</param>
318
278
/// <returns>string</returns>
319
- public static string AddDashes ( this string pascalCasedWord )
320
- {
321
- return Regex . Replace (
279
+ public static string AddDashes ( this string pascalCasedWord ) =>
280
+ Regex . Replace (
322
281
Regex . Replace (
323
282
Regex . Replace ( pascalCasedWord , @"([A-Z]+)([A-Z][a-z])" , "$1-$2" ) ,
324
283
@"([a-z\d])([A-Z])" ,
325
284
"$1-$2" ) ,
326
285
@"[\s]" ,
327
286
"-" ) ;
328
- }
329
287
330
288
/// <summary>
331
289
/// Add an undescore prefix to a pascasl-cased string
332
290
/// </summary>
333
291
/// <param name="pascalCasedWord"></param>
334
292
/// <returns></returns>
335
- public static string AddUnderscorePrefix ( this string pascalCasedWord )
336
- {
337
- return string . Format ( "_{0}" , pascalCasedWord ) ;
338
- }
293
+ public static string AddUnderscorePrefix ( this string pascalCasedWord ) => string . Format ( "_{0}" , pascalCasedWord ) ;
339
294
340
295
/// <summary>
341
296
/// Add spaces to a pascal-cased string
342
297
/// </summary>
343
298
/// <param name="pascalCasedWord">String to convert</param>
344
299
/// <returns>string</returns>
345
- public static string AddSpaces ( this string pascalCasedWord )
346
- {
347
- return Regex . Replace (
300
+ public static string AddSpaces ( this string pascalCasedWord ) =>
301
+ Regex . Replace (
348
302
Regex . Replace (
349
303
Regex . Replace ( pascalCasedWord , @"([A-Z]+)([A-Z][a-z])" , "$1 $2" ) ,
350
304
@"([a-z\d])([A-Z])" ,
351
305
"$1 $2" ) ,
352
306
@"[-\s]" ,
353
307
" " ) ;
354
- }
355
308
356
309
/// <summary>
357
310
/// Return possible variants of a name for name matching.
0 commit comments