-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathGuard.String.cs
More file actions
335 lines (296 loc) · 15.8 KB
/
Guard.String.cs
File metadata and controls
335 lines (296 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
#pragma warning disable CS8777
namespace CommunityToolkit.Diagnostics;
/// <inheritdoc/>
partial class Guard
{
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrEmpty(string? text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (string.IsNullOrEmpty(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNullOrEmpty(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrEmpty([NotNull] string? text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (!string.IsNullOrEmpty(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrEmpty(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/>, empty or consists only of white-space characters.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/>, nor empty, nor whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrWhiteSpace(string? text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhiteSpace(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/>, empty or consists only of white-space characters.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty or whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrWhiteSpace([NotNull] string? text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (!string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhiteSpace(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsEmpty(string text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length == 0)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsEmpty(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotEmpty(string text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length != 0)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must consists only of white-space characters.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> has some not white-space characters.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (!string.IsNullOrEmpty(text) && string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsWhiteSpace(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not consists only of white-space characters.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> has only white-space characters.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (string.IsNullOrEmpty(text) || !string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotWhiteSpace(text, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is != <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo(string text, int size, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length == size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, size, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size not equal to a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is == <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeNotEqualTo(string text, int size, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length != size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(text, size, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size over a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is <= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeGreaterThan(string text, int size, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length > size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(text, size, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of at least specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is < <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeGreaterThanOrEqualTo(string text, int size, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length >= size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(text, size, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of less than a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is >= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThan(string text, int size, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length < size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(text, size, name);
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of less than or equal to a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is > <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo(string text, int size, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length <= size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, size, name);
}
/// <summary>
/// Asserts that the source <see cref="string"/> instance must have the same size of a destination <see cref="string"/> instance.
/// </summary>
/// <param name="text">The source <see cref="string"/> instance to check the size for.</param>
/// <param name="destination">The destination <see cref="string"/> instance to check the size for.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is != the one of <paramref name="destination"/>.</exception>
/// <remarks>The <see cref="string"/> type is immutable, but the name of this API is kept for consistency with the other overloads.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo(string text, string destination, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length == destination.Length)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, destination, name);
}
/// <summary>
/// Asserts that the source <see cref="string"/> instance must have a size of less than or equal to that of a destination <see cref="string"/> instance.
/// </summary>
/// <param name="text">The source <see cref="string"/> instance to check the size for.</param>
/// <param name="destination">The destination <see cref="string"/> instance to check the size for.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is > the one of <paramref name="destination"/>.</exception>
/// <remarks>The <see cref="string"/> type is immutable, but the name of this API is kept for consistency with the other overloads.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo(string text, string destination, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (text.Length <= destination.Length)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, destination, name);
}
/// <summary>
/// Asserts that the input index is valid for a given <see cref="string"/> instance.
/// </summary>
/// <param name="index">The input index to be used to access <paramref name="text"/>.</param>
/// <param name="text">The input <see cref="string"/> instance to use to validate <paramref name="index"/>.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not valid to access <paramref name="text"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsInRangeFor(int index, string text, [CallerArgumentExpression(nameof(index))] string name = "")
{
if ((uint)index < (uint)text.Length)
{
return;
}
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, text, name);
}
/// <summary>
/// Asserts that the input index is not valid for a given <see cref="string"/> instance.
/// </summary>
/// <param name="index">The input index to be used to access <paramref name="text"/>.</param>
/// <param name="text">The input <see cref="string"/> instance to use to validate <paramref name="index"/>.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is valid to access <paramref name="text"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotInRangeFor(int index, string text, [CallerArgumentExpression(nameof(index))] string name = "")
{
if ((uint)index >= (uint)text.Length)
{
return;
}
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, text, name);
}
}