-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCheck.MustBeTrimmedAtEnd.cs
More file actions
61 lines (56 loc) · 2.71 KB
/
Copy pathCheck.MustBeTrimmedAtEnd.cs
File metadata and controls
61 lines (56 loc) · 2.71 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
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Light.GuardClauses.ExceptionFactory;
using Light.GuardClauses.Exceptions;
using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
namespace Light.GuardClauses;
public static partial class Check
{
/// <summary>
/// Ensures that the string is not null and trimmed at the end, or otherwise throws a <see cref="StringException" />.
/// Empty strings are regarded as trimmed.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <param name="parameterName">The name of the parameter (optional).</param>
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
/// <exception cref="StringException">
/// Thrown when <paramref name="parameter" /> is not trimmed at the end, i.e. they end with white space characters.
/// Empty strings are regarded as trimmed.
/// </exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="parameter" /> is null.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
public static string MustBeTrimmedAtEnd(
[NotNull] [ValidatedNotNull] this string? parameter,
[CallerArgumentExpression("parameter")] string? parameterName = null,
string? message = null
)
{
if (!parameter.MustNotBeNull(parameterName, message).IsTrimmedAtEnd())
{
Throw.NotTrimmedAtEnd(parameter, parameterName, message);
}
return parameter;
}
/// <summary>
/// Ensures that the string is not null and trimmed at the end, or otherwise throws your custom exception.
/// Empty strings are regarded as trimmed.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <param name="exceptionFactory">The delegate that creates your custom exception. <paramref name="parameter" /> is passed to this delegate.</param>
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is null or not trimmed at the end. Empty strings are regarded as trimmed.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
public static string MustBeTrimmedAtEnd(
[NotNull] [ValidatedNotNull] this string? parameter,
Func<string?, Exception> exceptionFactory
)
{
if (parameter is null || !parameter.AsSpan().IsTrimmedAtEnd())
{
Throw.CustomException(exceptionFactory, parameter);
}
return parameter;
}
}