-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCheck.MustBeSubstringOf.cs
More file actions
128 lines (118 loc) · 6.81 KB
/
Copy pathCheck.MustBeSubstringOf.cs
File metadata and controls
128 lines (118 loc) · 6.81 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
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 a substring of the specified other string, or otherwise throws a <see cref="SubstringException" />.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <param name="value">The other string that must contain <paramref name="parameter" />.</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="SubstringException">Thrown when <paramref name="value" /> does not contain <paramref name="parameter" />.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="parameter" /> or <paramref name="value" /> is null.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; value:null => halt")]
public static string MustBeSubstringOf(
[NotNull] [ValidatedNotNull] this string? parameter,
string value,
[CallerArgumentExpression("parameter")] string? parameterName = null,
string? message = null
)
{
if (!value.MustNotBeNull(nameof(value), message).Contains(parameter.MustNotBeNull(parameterName, message)))
{
Throw.NotSubstring(parameter, value, parameterName, message);
}
return parameter;
}
/// <summary>
/// Ensures that the string is a substring of the specified other string, or otherwise throws your custom exception.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <param name="value">The other string that must contain <paramref name="parameter" />.</param>
/// <param name="exceptionFactory">The delegate that creates your custom exception. <paramref name="parameter" /> and <paramref name="value" /> are passed to this delegate.</param>
/// <exception cref="Exception">
/// Your custom exception thrown when <paramref name="value" /> does not contain <paramref name="parameter" />,
/// or when <paramref name="parameter" /> is null,
/// or when <paramref name="value" /> is null.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; value:null => halt")]
public static string MustBeSubstringOf(
[NotNull] [ValidatedNotNull] this string? parameter,
string value,
Func<string?, string, Exception> exceptionFactory
)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
if (parameter is null || value is null || !value.Contains(parameter))
{
Throw.CustomException(exceptionFactory, parameter, value!);
}
return parameter;
}
/// <summary>
/// Ensures that the string is a substring of the specified other string, or otherwise throws a <see cref="SubstringException" />.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <param name="value">The other string that must contain <paramref name="parameter" />.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</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="SubstringException">Thrown when <paramref name="value" /> does not contain <paramref name="parameter" />.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="parameter" /> or <paramref name="value" /> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="comparisonType" /> is not a valid <see cref="StringComparison" /> value.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; value:null => halt")]
public static string MustBeSubstringOf(
[NotNull] [ValidatedNotNull] this string? parameter,
string value,
StringComparison comparisonType,
[CallerArgumentExpression("parameter")] string? parameterName = null,
string? message = null
)
{
value.MustNotBeNull(nameof(value), message);
parameter.MustNotBeNull(parameterName, message);
if (value.IndexOf(parameter, comparisonType) == -1)
{
Throw.NotSubstring(parameter, value, comparisonType, parameterName, message);
}
return parameter;
}
/// <summary>
/// Ensures that the string is a substring of the specified other string, or otherwise throws your custom exception.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <param name="value">The other string that must contain <paramref name="parameter" />.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param>
/// <param name="exceptionFactory">The delegate that creates your custom exception. <paramref name="parameter" />, <paramref name="value" />, and <paramref name="comparisonType" /> are passed to this delegate.</param>
/// <exception cref="Exception">
/// Your custom exception thrown when <paramref name="value" /> does not contain <paramref name="parameter" />,
/// or when <paramref name="parameter" /> is null,
/// or when <paramref name="value" /> is null.
/// </exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="comparisonType" /> is not a valid <see cref="StringComparison" /> value.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; value:null => halt")]
public static string MustBeSubstringOf(
[NotNull] [ValidatedNotNull] this string? parameter,
string value,
StringComparison comparisonType,
Func<string?, string, StringComparison, Exception> exceptionFactory
)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - caller might have NRTs turned off
if (parameter is null || value is null || value.IndexOf(parameter, comparisonType) == -1)
{
Throw.CustomException(exceptionFactory, parameter, value!, comparisonType);
}
return parameter;
}
}