-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCheck.IsSubstringOf.cs
More file actions
46 lines (43 loc) · 2.34 KB
/
Copy pathCheck.IsSubstringOf.cs
File metadata and controls
46 lines (43 loc) · 2.34 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
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
namespace Light.GuardClauses;
public static partial class Check
{
/// <summary>
/// Checks if the string is a substring of the other string.
/// </summary>
/// <param name="value">The string to be checked.</param>
/// <param name="other">The other string.</param>
/// <returns>True if <paramref name="value" /> is a substring of <paramref name="other" />, else false.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="value" /> or <paramref name="other" /> is null.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("value:null => halt; other:null => halt")]
// ReSharper disable RedundantNullableFlowAttribute
public static bool IsSubstringOf(
[NotNull] [ValidatedNotNull] this string value,
[NotNull] [ValidatedNotNull] string other
) =>
other.MustNotBeNull(nameof(other)).Contains(value);
// ReSharper restore RedundantNullableFlowAttribute
/// <summary>
/// Checks if the string is a substring of the other string.
/// </summary>
/// <param name="value">The string to be checked.</param>
/// <param name="other">The other string.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param>
/// <returns>True if <paramref name="value" /> is a substring of <paramref name="other" />, else false.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="value" /> or <paramref name="other" /> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="comparisonType" /> is not a valid <see cref="StringComparison" /> value.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("value:null => halt; other:null => halt")]
// ReSharper disable RedundantNullableFlowAttribute
public static bool IsSubstringOf(
[NotNull] [ValidatedNotNull] this string value,
[NotNull] [ValidatedNotNull] string other,
StringComparison comparisonType
) =>
other.MustNotBeNull(nameof(other)).IndexOf(value, comparisonType) != -1;
// ReSharper disable RedundantNullableFlowAttribute
}