1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Globalization ;
4
3
5
4
using JetBrains . Annotations ;
6
5
@@ -13,24 +12,26 @@ namespace CodeJam.Strings
13
12
public partial class StringExtensions
14
13
{
15
14
/// <summary>
16
- /// Retrieves a substring from <paramref name="str"/>.
15
+ /// Retrieves a substring from <paramref name="str" />.
17
16
/// </summary>
18
- /// <param name="str"></param>
17
+ /// <param name="str">
18
+ /// String to retrieve substring from.
19
+ /// </param>
19
20
/// <param name="origin">
20
21
/// Specifies the beginning, or the end as a reference point for offset, using a value of type
21
- /// <see cref="StringOrigin"/>.
22
+ /// <see cref="StringOrigin" />.
22
23
/// </param>
23
24
/// <param name="length">The number of characters in the substring.</param>
24
25
/// <returns>
25
- /// A string that is equivalent to the substring of length <paramref name="length"/> that begins at
26
- /// <paramref name="origin"/> in <paramref name="str"/>, or Empty if length of <paramref name="str"/>
27
- /// or <paramref name="length"/> is zero.
26
+ /// A string that is equivalent to the substring of length <paramref name="length" /> that begins at
27
+ /// <paramref name="origin" /> in <paramref name="str" />, or Empty if length of <paramref name="str" />
28
+ /// or <paramref name="length" /> is zero.
28
29
/// </returns>
29
30
[ NotNull ]
30
31
[ Pure ]
31
32
public static string Substring ( [ NotNull ] this string str , StringOrigin origin , int length )
32
33
{
33
- if ( str == null ) throw new ArgumentNullException ( nameof ( str ) ) ;
34
+ Code . NotNull ( str , nameof ( str ) ) ;
34
35
35
36
// Fast path
36
37
var strLen = str . Length ;
@@ -45,40 +46,61 @@ public static string Substring([NotNull] this string str, StringOrigin origin, i
45
46
case StringOrigin . End :
46
47
return str . Substring ( strLen - length , length ) ;
47
48
default :
48
- throw new ArgumentOutOfRangeException ( nameof ( origin ) , origin , null ) ;
49
+ throw CodeExceptions . Argument ( nameof ( origin ) , $ "Invalid { nameof ( StringOrigin ) } value." ) ;
49
50
}
50
51
}
51
52
52
53
/// <summary>
53
54
/// Retrieves prefix of length <paramref name="length"/>.
54
55
/// </summary>
55
- /// <param name="str"></param>
56
+ /// <param name="str">String to retrieve prefix from. </param>
56
57
/// <param name="length">The number of characters in the substring.</param>
58
+ /// <returns>
59
+ /// Prefix of specified length, or <paramref name="str"/> itself, if total length less than required.
60
+ /// </returns>
57
61
[ NotNull ]
58
62
[ Pure ]
59
63
public static string Prefix ( [ NotNull ] this string str , int length ) => str . Substring ( StringOrigin . Begin , length ) ;
60
64
61
65
/// <summary>
62
66
/// Retrieves prefix of length <paramref name="length"/>.
63
67
/// </summary>
64
- /// <param name="str"></param>
68
+ /// <param name="str">String to retrieve suffix from. </param>
65
69
/// <param name="length">The number of characters in the substring.</param>
70
+ /// <returns>
71
+ /// Suffix of specified length, or <paramref name="str"/> itself, if total length less than required.
72
+ /// </returns>
66
73
[ NotNull ]
67
74
[ Pure ]
68
75
public static string Suffix ( [ NotNull ] this string str , int length ) => str . Substring ( StringOrigin . End , length ) ;
69
76
70
77
/// <summary>
71
78
/// Trims <paramref name="str"/> prefix if it equals to <paramref name="prefix"/>.
72
79
/// </summary>
80
+ /// <param name="str">String to trim.</param>
81
+ /// <param name="prefix">Prefix to trim.</param>
82
+ /// <returns>Trimmed <paramref name="str"/>, or original <paramref name="str"/> if prefix not exists.</returns>
83
+ [ NotNull ]
84
+ [ Pure ]
85
+ public static string TrimPrefix ( [ NotNull ] this string str , [ CanBeNull ] string prefix ) =>
86
+ TrimPrefix ( str , prefix , StringComparer . CurrentCulture ) ;
87
+
88
+ /// <summary>
89
+ /// Trims <paramref name="str"/> prefix if it equals to <paramref name="prefix"/>.
90
+ /// </summary>
91
+ /// <param name="str">String to trim.</param>
92
+ /// <param name="prefix">Prefix to trim.</param>
93
+ /// <param name="comparer">Comparer to compare value of prefix.</param>
94
+ /// <returns>Trimmed <paramref name="str"/>, or original <paramref name="str"/> if prefix not exists.</returns>
73
95
[ NotNull ]
74
96
[ Pure ]
75
97
public static string TrimPrefix (
76
98
[ NotNull ] this string str ,
77
99
[ CanBeNull ] string prefix ,
78
100
[ NotNull ] IEqualityComparer < string > comparer )
79
101
{
80
- if ( str == null ) throw new ArgumentNullException ( nameof ( str ) ) ;
81
- if ( comparer == null ) throw new ArgumentNullException ( nameof ( comparer ) ) ;
102
+ Code . NotNull ( str , nameof ( str ) ) ;
103
+ Code . NotNull ( comparer , nameof ( comparer ) ) ;
82
104
83
105
// FastPath
84
106
if ( prefix == null )
@@ -91,26 +113,22 @@ public static string TrimPrefix(
91
113
return ! comparer . Equals ( prefix , actPrefix ) ? str : str . Substring ( prefixLen ) ;
92
114
}
93
115
94
- /// <summary>
95
- /// Trims <paramref name="str"/> prefix if it equals to <paramref name="prefix"/>.
96
- /// </summary>
97
- [ NotNull ]
98
- [ Pure ]
99
- public static string TrimPrefix ( [ NotNull ] this string str , [ CanBeNull ] string prefix ) =>
100
- TrimPrefix ( str , prefix , StringComparer . CurrentCulture ) ;
101
-
102
116
/// <summary>
103
117
/// Trims <paramref name="str"/> suffix if it equals to <paramref name="suffix"/>.
104
118
/// </summary>
119
+ /// <param name="str">String to trim.</param>
120
+ /// <param name="suffix">Suffix to trim.</param>
121
+ /// <param name="comparer">Comparer to compare value of suffix.</param>
122
+ /// <returns>Trimmed <paramref name="str"/>, or original <paramref name="str"/> if suffix does not exists.</returns>
105
123
[ NotNull ]
106
124
[ Pure ]
107
125
public static string TrimSuffix (
108
126
[ NotNull ] this string str ,
109
127
[ CanBeNull ] string suffix ,
110
128
[ NotNull ] IEqualityComparer < string > comparer )
111
129
{
112
- if ( str == null ) throw new ArgumentNullException ( nameof ( str ) ) ;
113
- if ( comparer == null ) throw new ArgumentNullException ( nameof ( comparer ) ) ;
130
+ Code . NotNull ( str , nameof ( str ) ) ;
131
+ Code . NotNull ( comparer , nameof ( comparer ) ) ;
114
132
115
133
// FastPath
116
134
if ( suffix == null )
@@ -127,6 +145,9 @@ public static string TrimSuffix(
127
145
/// <summary>
128
146
/// Trims <paramref name="str"/> prefix if it equals to <paramref name="suffix"/>.
129
147
/// </summary>
148
+ /// <param name="str">String to trim.</param>
149
+ /// <param name="suffix">Suffix to trim.</param>
150
+ /// <returns>Trimmed <paramref name="str"/>, or original <paramref name="str"/> if suffix does not exists.</returns>
130
151
[ NotNull ]
131
152
[ Pure ]
132
153
public static string TrimSuffix ( [ NotNull ] this string str , [ CanBeNull ] string suffix ) =>
@@ -137,13 +158,18 @@ public static string TrimSuffix([NotNull] this string str, [CanBeNull] string su
137
158
/// <summary>
138
159
/// Returns size in bytes string representation.
139
160
/// </summary>
161
+ /// <param name="value">Value to represent.</param>
162
+ /// <returns>Value as size in bytes</returns>
140
163
[ NotNull ]
141
164
[ Pure ]
142
- public static string ToByteSizeString ( this long value ) => ToByteSizeString ( value , CultureInfo . CurrentCulture ) ;
165
+ public static string ToByteSizeString ( this long value ) => ToByteSizeString ( value , null ) ;
143
166
144
167
/// <summary>
145
168
/// Returns size in bytes string representation.
146
169
/// </summary>
170
+ /// <param name="value">Value to represent.</param>
171
+ /// <param name="provider">Format provider for number part of value</param>
172
+ /// <returns>Value as size in bytes</returns>
147
173
[ NotNull ]
148
174
[ Pure ]
149
175
public static string ToByteSizeString ( this long value , [ CanBeNull ] IFormatProvider provider )
@@ -180,10 +206,8 @@ public static IEnumerable<string> SplitWithTrim([NotNull] this string source, pa
180
206
// TODO: For performance reasons must be reimplemented using FSM parser.
181
207
var parts = source . Split ( separators ) ;
182
208
foreach ( var part in parts )
183
- {
184
209
if ( ! part . IsNullOrWhiteSpace ( ) )
185
210
yield return part . Trim ( ) ;
186
- }
187
211
}
188
212
189
213
/// <summary>
@@ -199,6 +223,7 @@ public static IEnumerable<string> SplitWithTrim([NotNull] this string source, pa
199
223
public static unsafe string ToHexString ( [ NotNull ] this byte [ ] data )
200
224
{
201
225
Code . NotNull ( data , nameof ( data ) ) ;
226
+
202
227
if ( data . Length == 0 )
203
228
return string . Empty ;
204
229
@@ -272,6 +297,10 @@ public static unsafe string ToHexString([NotNull] this byte[] data, [CanBeNull]
272
297
/// <summary>
273
298
/// Remove one set of leading and trailing double quote characters, if both are present.
274
299
/// </summary>
300
+ /// <param name="arg">String to unquote.</param>
301
+ /// <returns>
302
+ /// Unquoted <paramref name="arg"/>, if <paramref name="arg"/> is quoted, or <paramref name="arg"/> itself.
303
+ /// </returns>
275
304
public static string Unquote ( this string arg )
276
305
{
277
306
bool quoted ;
@@ -281,11 +310,22 @@ public static string Unquote(this string arg)
281
310
/// <summary>
282
311
/// Remove one set of leading and trailing double quote characters, if both are present.
283
312
/// </summary>
313
+ /// <param name="arg">String to unquote.</param>
314
+ /// <param name="quoted">Set to true, if <paramref name="arg"/> was quoted.</param>
315
+ /// <returns>
316
+ /// Unquoted <paramref name="arg"/>, if <paramref name="arg"/> is quoted, or <paramref name="arg"/> itself.
317
+ /// </returns>
284
318
public static string Unquote ( this string arg , out bool quoted ) => Unquote ( arg , '"' , out quoted ) ;
285
319
286
320
/// <summary>
287
321
/// Remove one set of leading and trailing d<paramref name="quotationChar"/>, if both are present.
288
322
/// </summary>
323
+ /// <param name="arg">String to unquote.</param>
324
+ /// <param name="quotationChar">Quotation char</param>
325
+ /// <param name="quoted">Set to true, if <paramref name="arg"/> was quoted.</param>
326
+ /// <returns>
327
+ /// Unquoted <paramref name="arg"/>, if <paramref name="arg"/> is quoted, or <paramref name="arg"/> itself.
328
+ /// </returns>
289
329
public static string Unquote ( this string arg , char quotationChar , out bool quoted )
290
330
{
291
331
if ( arg . Length > 1 && arg [ 0 ] == quotationChar && arg [ arg . Length - 1 ] == quotationChar )
0 commit comments