diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml
index 50ff8f00..49c3cbb3 100644
--- a/.github/workflows/build-and-test.yml
+++ b/.github/workflows/build-and-test.yml
@@ -2,7 +2,7 @@ name: Build and Test
on:
push:
- branches: [main, dev]
+ branches: [main]
paths:
- 'src/**'
- 'tests/**'
@@ -13,7 +13,7 @@ on:
- 'global.json'
- '.github/workflows/build-and-test.yml'
pull_request:
- branches: [main, dev]
+ branches: [main]
paths:
- 'src/**'
- 'tests/**'
@@ -24,11 +24,18 @@ on:
- 'global.json'
- '.github/workflows/build-and-test.yml'
+permissions:
+ contents: read
+
jobs:
build-and-test:
+ name: Build and test
runs-on: ubuntu-latest
+ permissions:
+ contents: read
+
steps:
- uses: actions/checkout@v4
- name: Setup .NET
@@ -39,5 +46,71 @@ jobs:
run: dotnet restore ./Light.GuardClauses.slnx
- name: Build
run: dotnet build ./Light.GuardClauses.slnx -c Release --no-restore
+ - name: Install ReportGenerator
+ run: dotnet tool install --global dotnet-reportgenerator-globaltool --version 5.5.10
- name: Test
- run: dotnet test ./Light.GuardClauses.slnx -c Release --no-build --verbosity normal
+ run: >
+ dotnet test
+ --solution ./Light.GuardClauses.slnx
+ -c Release
+ --no-build
+ --no-restore
+ --results-directory artifacts/test-results
+ --coverage
+ --coverage-output-format cobertura
+ - name: Merge coverage
+ if: always()
+ run: >
+ reportgenerator
+ "-reports:artifacts/test-results/**/*.cobertura.xml"
+ "-targetdir:artifacts/coverage"
+ "-reporttypes:Cobertura;MarkdownSummaryGithub"
+ - name: Create coverage summary
+ uses: irongut/CodeCoverageSummary@v1.3.0
+ with:
+ filename: artifacts/coverage/Cobertura.xml
+ format: markdown
+ output: both
+ badge: true
+ thresholds: '60 85'
+ - name: Store coverage summary
+ run: mv code-coverage-results.md artifacts/coverage/code-coverage-results.md
+ - name: Upload test results
+ uses: actions/upload-artifact@v4
+ if: always()
+ with:
+ name: test-results
+ path: artifacts/test-results
+ if-no-files-found: warn
+ - name: Upload coverage report
+ uses: actions/upload-artifact@v4
+ if: always()
+ with:
+ name: coverage-report
+ path: artifacts/coverage
+ if-no-files-found: warn
+
+ coverage-comment:
+ name: Coverage comment
+ runs-on: ubuntu-latest
+ needs: build-and-test
+ if: >
+ github.event_name == 'pull_request' &&
+ github.event.pull_request.head.repo.full_name == github.repository
+
+ permissions:
+ contents: read
+ issues: write
+ pull-requests: write
+
+ steps:
+ - name: Download coverage report
+ uses: actions/download-artifact@v4
+ with:
+ name: coverage-report
+ path: artifacts/coverage
+ - name: Update pull request comment
+ uses: marocchino/sticky-pull-request-comment@v3.0.4
+ with:
+ header: code-coverage
+ path: artifacts/coverage/code-coverage-results.md
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 314026b2..ced114bd 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -9,18 +9,19 @@
Keep Microsoft.CodeAnalysis.* aligned so analyzers, code fixes, and source-export tooling use one Roslyn line.
-->
-
-
+
+
-
+
+
+
-
-
+
diff --git a/Light.GuardClauses.SingleFile.cs b/Light.GuardClauses.SingleFile.cs
index b4d45577..ec16035d 100644
--- a/Light.GuardClauses.SingleFile.cs
+++ b/Light.GuardClauses.SingleFile.cs
@@ -54,6 +54,118 @@ namespace Light.GuardClauses
// ReSharper disable once RedundantTypeDeclarationBody -- required for Source Code Transformation
internal static class Check
{
+ ///
+ /// Checks if the specified string is non-null and every character is a Unicode decimal digit. Empty strings are valid.
+ ///
+ /// The string to be checked.
+ ///
+ /// True if is non-null and every character is a Unicode decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyDigits(this string? parameter) => parameter is not null && parameter.AsSpan().ContainsOnlyDigits();
+ ///
+ /// Checks if every character in the specified span is a Unicode decimal digit. Empty spans are valid.
+ ///
+ /// The character span to be checked.
+ ///
+ /// True if every character in is a Unicode decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyDigits(this Span parameter) => ((ReadOnlySpan)parameter).ContainsOnlyDigits();
+ ///
+ /// Checks if every character in the specified read-only span is a Unicode decimal digit. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// True if every character in is a Unicode decimal digit, otherwise false.
+ ///
+ public static bool ContainsOnlyDigits(this ReadOnlySpan parameter)
+ {
+ foreach (var character in parameter)
+ {
+ if (!char.IsDigit(character))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ ///
+ /// Checks if every character in the specified memory is a Unicode decimal digit. Empty memory is valid.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// True if every character in is a Unicode decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyDigits(this Memory parameter) => parameter.Span.ContainsOnlyDigits();
+ ///
+ /// Checks if every character in the specified read-only memory is a Unicode decimal digit. Empty memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// True if every character in is a Unicode decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyDigits(this ReadOnlyMemory parameter) => parameter.Span.ContainsOnlyDigits();
+ ///
+ /// Checks if the specified string is non-null and every character is a Unicode letter or decimal digit. Empty strings are valid.
+ ///
+ /// The string to be checked.
+ ///
+ /// True if is non-null and every character is a Unicode letter or decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyLettersOrDigits(this string? parameter) => parameter is not null && parameter.AsSpan().ContainsOnlyLettersOrDigits();
+ ///
+ /// Checks if every character in the specified span is a Unicode letter or decimal digit. Empty spans are valid.
+ ///
+ /// The character span to be checked.
+ ///
+ /// True if every character in is a Unicode letter or decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyLettersOrDigits(this Span parameter) => ((ReadOnlySpan)parameter).ContainsOnlyLettersOrDigits();
+ ///
+ /// Checks if every character in the specified read-only span is a Unicode letter or decimal digit. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// True if every character in is a Unicode letter or decimal digit, otherwise false.
+ ///
+ public static bool ContainsOnlyLettersOrDigits(this ReadOnlySpan parameter)
+ {
+ foreach (var character in parameter)
+ {
+ if (!char.IsLetterOrDigit(character))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ ///
+ /// Checks if every character in the specified memory is a Unicode letter or decimal digit. Empty memory is valid.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// True if every character in is a Unicode letter or decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyLettersOrDigits(this Memory parameter) => parameter.Span.ContainsOnlyLettersOrDigits();
+ ///
+ /// Checks if every character in the specified read-only memory is a Unicode letter or decimal digit. Empty memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// True if every character in is a Unicode letter or decimal digit, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool ContainsOnlyLettersOrDigits(this ReadOnlyMemory parameter) => parameter.Span.ContainsOnlyLettersOrDigits();
///
/// Checks if the specified type derives from the other type. Internally, this method uses
/// by default so that constructed generic types and their corresponding generic type definitions are regarded as equal.
@@ -387,6 +499,129 @@ public static bool IsAscii(this ReadOnlySpan parameter)
/// Checks if the read-only byte memory contains only ASCII values.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAscii(this ReadOnlyMemory parameter) => parameter.Span.IsAscii();
+ ///
+ /// Checks if the string is non-null and valid standard Base64. Space, tab, carriage return, and line feed are ignored.
+ /// Empty and whitespace-only strings are valid.
+ ///
+ /// The string to be checked.
+ /// True if is non-null and contains valid standard Base64, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsBase64(this string? parameter) => parameter is not null && parameter.AsSpan().IsBase64();
+ ///
+ /// Checks if the span is valid standard Base64. Space, tab, carriage return, and line feed are ignored.
+ /// Empty and whitespace-only spans are valid.
+ ///
+ /// The character span to be checked.
+ /// True if contains valid standard Base64, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsBase64(this Span parameter) => ((ReadOnlySpan)parameter).IsBase64();
+ ///
+ /// Checks if the read-only span is valid standard Base64. Space, tab, carriage return, and line feed are ignored.
+ /// Empty and whitespace-only spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// True if contains valid standard Base64, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsBase64(this ReadOnlySpan parameter)
+ {
+ return IsBase64Portable(parameter);
+ }
+
+ ///
+ /// Checks if the memory is valid standard Base64. Space, tab, carriage return, and line feed are ignored.
+ /// Empty and whitespace-only memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// True if contains valid standard Base64, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsBase64(this Memory parameter) => parameter.Span.IsBase64();
+ ///
+ /// Checks if the read-only memory is valid standard Base64. Space, tab, carriage return, and line feed are ignored.
+ /// Empty and whitespace-only memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// True if contains valid standard Base64, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsBase64(this ReadOnlyMemory parameter) => parameter.Span.IsBase64();
+ private static bool IsBase64Portable(ReadOnlySpan parameter)
+ {
+ var nonWhiteSpaceCount = 0;
+ var paddingCount = 0;
+ var lastDataValue = 0;
+ foreach (var character in parameter)
+ {
+ if (character is ' ' or '\t' or '\r' or '\n')
+ {
+ continue;
+ }
+
+ ++nonWhiteSpaceCount;
+ if (character == '=')
+ {
+ if (++paddingCount > 2)
+ {
+ return false;
+ }
+
+ continue;
+ }
+
+ if (paddingCount != 0 || !TryGetBase64Value(character, out lastDataValue))
+ {
+ return false;
+ }
+ }
+
+ if ((nonWhiteSpaceCount & 3) != 0)
+ {
+ return false;
+ }
+
+ return paddingCount switch
+ {
+ 0 => true,
+ 1 => nonWhiteSpaceCount >= 4 && (lastDataValue & 0b11) == 0,
+ 2 => nonWhiteSpaceCount >= 4 && (lastDataValue & 0b1111) == 0,
+ _ => false,
+ };
+ }
+
+ private static bool TryGetBase64Value(char character, out int value)
+ {
+ if (character is >= 'A' and <= 'Z')
+ {
+ value = character - 'A';
+ return true;
+ }
+
+ if (character is >= 'a' and <= 'z')
+ {
+ value = character - 'a' + 26;
+ return true;
+ }
+
+ if (character is >= '0' and <= '9')
+ {
+ value = character - '0' + 52;
+ return true;
+ }
+
+ if (character == '+')
+ {
+ value = 62;
+ return true;
+ }
+
+ if (character == '/')
+ {
+ value = 63;
+ return true;
+ }
+
+ value = 0;
+ return false;
+ }
+
///
/// Checks if the specified character is a digit.
///
@@ -609,6 +844,64 @@ public static bool IsFileExtension(this ReadOnlySpan value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGreaterThanOrApproximately(this float value, float other) => value > other || value.IsApproximately(other);
///
+ /// Checks if the specified string is non-null and contains only ASCII hexadecimal characters. Empty strings are valid.
+ ///
+ /// The string to be checked.
+ ///
+ /// True if is non-null and contains only ASCII hexadecimal characters, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsHexadecimal(this string? parameter) => parameter is not null && parameter.AsSpan().IsHexadecimal();
+ ///
+ /// Checks if the specified span contains only ASCII hexadecimal characters. Empty spans are valid.
+ ///
+ /// The character span to be checked.
+ ///
+ /// True if contains only ASCII hexadecimal characters, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsHexadecimal(this Span parameter) => ((ReadOnlySpan)parameter).IsHexadecimal();
+ ///
+ /// Checks if the specified read-only span contains only ASCII hexadecimal characters. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// True if contains only ASCII hexadecimal characters, otherwise false.
+ ///
+ public static bool IsHexadecimal(this ReadOnlySpan parameter)
+ {
+ foreach (var character in parameter)
+ {
+ if (!IsAsciiHexDigit(character))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static bool IsAsciiHexDigit(char character) => character is >= '0' and <= '9' or >= 'A' and <= 'F' or >= 'a' and <= 'f';
+ ///
+ /// Checks if the specified memory contains only ASCII hexadecimal characters. Empty memory is valid.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// True if contains only ASCII hexadecimal characters, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsHexadecimal(this Memory parameter) => parameter.Span.IsHexadecimal();
+ ///
+ /// Checks if the specified read-only memory contains only ASCII hexadecimal characters. Empty memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// True if contains only ASCII hexadecimal characters, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsHexadecimal(this ReadOnlyMemory parameter) => parameter.Span.IsHexadecimal();
+ ///
/// Checks if the value is within the specified range.
///
/// The comparable to be checked.
@@ -675,6 +968,54 @@ public static bool IsIn([NotNull][ValidatedNotNull] this T parameter, Range char.IsLetterOrDigit(character);
///
+ /// Checks if the specified string is non-null and contains no Unicode uppercase character. Empty strings are valid.
+ ///
+ /// The string to be checked.
+ ///
+ /// True if is non-null and contains no Unicode uppercase character, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLowerCase(this string? parameter) => parameter is not null && parameter.AsSpan().IsLowerCase();
+ ///
+ /// Checks if the specified span contains no Unicode uppercase character. Empty spans are valid.
+ ///
+ /// The character span to be checked.
+ /// True if contains no Unicode uppercase character, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLowerCase(this Span parameter) => ((ReadOnlySpan)parameter).IsLowerCase();
+ ///
+ /// Checks if the specified read-only span contains no Unicode uppercase character. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// True if contains no Unicode uppercase character, otherwise false.
+ public static bool IsLowerCase(this ReadOnlySpan parameter)
+ {
+ foreach (var character in parameter)
+ {
+ if (char.IsUpper(character))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ ///
+ /// Checks if the specified memory contains no Unicode uppercase character. Empty memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// True if contains no Unicode uppercase character, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLowerCase(this Memory parameter) => parameter.Span.IsLowerCase();
+ ///
+ /// Checks if the specified read-only memory contains no Unicode uppercase character. Empty memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// True if contains no Unicode uppercase character, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsLowerCase(this ReadOnlyMemory parameter) => parameter.Span.IsLowerCase();
+ ///
/// Checks if the string is either "\n" or "\r\n". This is done independently of the current value of .
///
/// The string to be checked.
@@ -925,6 +1266,54 @@ public static bool IsSameAs([NoEnumeration] this T? parameter, [NoEnumeration
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsTrimmedAtStart(this ReadOnlySpan parameter) => parameter.Length == 0 || !parameter[0].IsWhiteSpace();
///
+ /// Checks if the specified string is non-null and contains no Unicode lowercase character. Empty strings are valid.
+ ///
+ /// The string to be checked.
+ ///
+ /// True if is non-null and contains no Unicode lowercase character, otherwise false.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsUpperCase(this string? parameter) => parameter is not null && parameter.AsSpan().IsUpperCase();
+ ///
+ /// Checks if the specified span contains no Unicode lowercase character. Empty spans are valid.
+ ///
+ /// The character span to be checked.
+ /// True if contains no Unicode lowercase character, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsUpperCase(this Span parameter) => ((ReadOnlySpan)parameter).IsUpperCase();
+ ///
+ /// Checks if the specified read-only span contains no Unicode lowercase character. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// True if contains no Unicode lowercase character, otherwise false.
+ public static bool IsUpperCase(this ReadOnlySpan parameter)
+ {
+ foreach (var character in parameter)
+ {
+ if (char.IsLower(character))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ ///
+ /// Checks if the specified memory contains no Unicode lowercase character. Empty memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// True if contains no Unicode lowercase character, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsUpperCase(this Memory parameter) => parameter.Span.IsUpperCase();
+ ///
+ /// Checks if the specified read-only memory contains no Unicode lowercase character. Empty memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// True if contains no Unicode lowercase character, otherwise false.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static bool IsUpperCase(this ReadOnlyMemory parameter) => parameter.Span.IsUpperCase();
+ ///
/// Checks if the specified GUID structurally identifies an RFC/IETF UUID version 7.
///
/// The GUID to be checked.
@@ -1329,7 +1718,13 @@ public static float MustBeApproximately(this float parameter, float other, float
return parameter;
}
- /// Ensures that the character is ASCII, or otherwise throws an .
+ ///
+ /// Ensures that the character is ASCII, or otherwise throws an .
+ ///
+ /// The character to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char MustBeAscii(this char parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1341,7 +1736,14 @@ public static char MustBeAscii(this char parameter, [CallerArgumentExpression("p
return parameter;
}
- /// Ensures that the character is ASCII, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the character is ASCII, or otherwise throws your custom exception.
+ ///
+ /// The character to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("exceptionFactory:null => halt")]
public static char MustBeAscii(this char parameter, Func exceptionFactory)
@@ -1354,7 +1756,13 @@ public static char MustBeAscii(this char parameter, Func except
return parameter;
}
- /// Ensures that the byte is ASCII, or otherwise throws an .
+ ///
+ /// Ensures that the byte is ASCII, or otherwise throws an .
+ ///
+ /// The byte to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated byte.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte MustBeAscii(this byte parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1366,7 +1774,14 @@ public static byte MustBeAscii(this byte parameter, [CallerArgumentExpression("p
return parameter;
}
- /// Ensures that the byte is ASCII, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the byte is ASCII, or otherwise throws your custom exception.
+ ///
+ /// The byte to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated byte.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("exceptionFactory:null => halt")]
public static byte MustBeAscii(this byte parameter, Func exceptionFactory)
@@ -1379,7 +1794,13 @@ public static byte MustBeAscii(this byte parameter, Func except
return parameter;
}
- /// Ensures that the string is non-null and contains only ASCII characters.
+ ///
+ /// Ensures that the string is non-null and contains only ASCII characters, or otherwise throws a .
+ ///
+ /// The string to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated string.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
public static string MustBeAscii([NotNull][ValidatedNotNull] this string? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
@@ -1387,13 +1808,20 @@ public static string MustBeAscii([NotNull][ValidatedNotNull] this string? parame
parameter.MustNotBeNull(parameterName, message);
if (!parameter.IsAscii())
{
- Throw.Argument(parameterName, message ?? $"{parameterName ?? "The string"} must contain only ASCII characters.");
+ Throw.InvalidStringContent(parameterName, message, "must contain only ASCII characters");
}
return parameter;
}
- /// Ensures that the string is non-null and contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the string is non-null and contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// The string to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated string.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
public static string MustBeAscii([NotNull][ValidatedNotNull] this string? parameter, Func exceptionFactory)
@@ -1406,7 +1834,13 @@ public static string MustBeAscii([NotNull][ValidatedNotNull] this string? parame
return parameter;
}
- /// Ensures that the character span contains only ASCII characters.
+ ///
+ /// Ensures that the character span contains only ASCII characters, or otherwise throws a .
+ ///
+ /// The character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span MustBeAscii(this Span parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1414,7 +1848,14 @@ public static Span MustBeAscii(this Span parameter, [CallerArgumentE
return parameter;
}
- /// Ensures that the character span contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the character span contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// The character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span MustBeAscii(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1422,19 +1863,32 @@ public static Span MustBeAscii(this Span parameter, ReadOnlySpanExce
return parameter;
}
- /// Ensures that the read-only character span contains only ASCII characters.
+ ///
+ /// Ensures that the read-only character span contains only ASCII characters, or otherwise throws a .
+ ///
+ /// The read-only character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
if (!parameter.IsAscii())
{
- Throw.Argument(parameterName, message ?? $"{parameterName ?? "The character span"} must contain only ASCII characters.");
+ Throw.InvalidStringContent(parameterName, message, "must contain only ASCII characters");
}
return parameter;
}
- /// Ensures that the read-only character span contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the read-only character span contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1446,7 +1900,13 @@ public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter,
return parameter;
}
- /// Ensures that the character memory contains only ASCII characters.
+ ///
+ /// Ensures that the character memory contains only ASCII characters, or otherwise throws a .
+ ///
+ /// The character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Memory MustBeAscii(this Memory parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1454,7 +1914,14 @@ public static Memory MustBeAscii(this Memory parameter, [CallerArgum
return parameter;
}
- /// Ensures that the character memory contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the character memory contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Memory MustBeAscii(this Memory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1462,7 +1929,13 @@ public static Memory MustBeAscii(this Memory parameter, ReadOnlySpan
return parameter;
}
- /// Ensures that the read-only character memory contains only ASCII characters.
+ ///
+ /// Ensures that the read-only character memory contains only ASCII characters, or otherwise throws a .
+ ///
+ /// The read-only character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1470,7 +1943,14 @@ public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory paramet
return parameter;
}
- /// Ensures that the read-only character memory contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the read-only character memory contains only ASCII characters, or otherwise throws your custom exception.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1478,7 +1958,13 @@ public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory paramet
return parameter;
}
- /// Ensures that the byte span contains only ASCII values.
+ ///
+ /// Ensures that the byte span contains only ASCII values.
+ ///
+ /// The byte span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated byte span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span MustBeAscii(this Span parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1486,7 +1972,14 @@ public static Span MustBeAscii(this Span parameter, [CallerArgumentE
return parameter;
}
- /// Ensures that the byte span contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the byte span contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// The byte span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated byte span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span MustBeAscii(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1494,7 +1987,13 @@ public static Span MustBeAscii(this Span parameter, ReadOnlySpanExce
return parameter;
}
- /// Ensures that the read-only byte span contains only ASCII values.
+ ///
+ /// Ensures that the read-only byte span contains only ASCII values.
+ ///
+ /// The read-only byte span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only byte span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1506,7 +2005,14 @@ public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter,
return parameter;
}
- /// Ensures that the read-only byte span contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the read-only byte span contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// The read-only byte span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only byte span.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1518,7 +2024,13 @@ public static ReadOnlySpan MustBeAscii(this ReadOnlySpan parameter,
return parameter;
}
- /// Ensures that the byte memory contains only ASCII values.
+ ///
+ /// Ensures that the byte memory contains only ASCII values.
+ ///
+ /// The byte memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated byte memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Memory MustBeAscii(this Memory parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1526,7 +2038,14 @@ public static Memory MustBeAscii(this Memory parameter, [CallerArgum
return parameter;
}
- /// Ensures that the byte memory contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the byte memory contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// The byte memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated byte memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Memory MustBeAscii(this Memory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1534,7 +2053,13 @@ public static Memory MustBeAscii(this Memory parameter, ReadOnlySpan
return parameter;
}
- /// Ensures that the read-only byte memory contains only ASCII values.
+ ///
+ /// Ensures that the read-only byte memory contains only ASCII values.
+ ///
+ /// The read-only byte memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only byte memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -1542,7 +2067,14 @@ public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory paramet
return parameter;
}
- /// Ensures that the read-only byte memory contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// Ensures that the read-only byte memory contains only ASCII values, or otherwise throws your custom exception.
+ ///
+ /// The read-only byte memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only byte memory.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
{
@@ -1550,6 +2082,171 @@ public static ReadOnlyMemory MustBeAscii(this ReadOnlyMemory paramet
return parameter;
}
+ ///
+ /// Ensures that the string is standard Base64 with valid padding. Space, tab, carriage return, and line feed are ignored.
+ /// Empty and whitespace-only strings are valid.
+ ///
+ /// The string to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated string.
+ /// Thrown when is null.
+ /// Thrown when the string is not valid Base64.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static string MustBeBase64([NotNull][ValidatedNotNull] this string? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.MustNotBeNull(parameterName, message);
+ if (!parameter.IsBase64())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must be valid standard Base64");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the string is valid standard Base64, or otherwise throws your custom exception.
+ ///
+ /// The string to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated string.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
+ public static string MustBeBase64([NotNull][ValidatedNotNull] this string? parameter, Func exceptionFactory)
+ {
+ if (parameter is null || !parameter.IsBase64())
+ {
+ Throw.CustomException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span is valid standard Base64. Empty and supported-whitespace-only spans are valid.
+ ///
+ /// The character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeBase64(this Span parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter).MustBeBase64(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span is valid standard Base64, or otherwise throws your custom exception.
+ ///
+ /// The character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeBase64(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter).MustBeBase64(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span is valid standard Base64. Empty and supported-whitespace-only spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeBase64(this ReadOnlySpan parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ if (!parameter.IsBase64())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must be valid standard Base64");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span is valid standard Base64, or otherwise throws your custom exception.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeBase64(this ReadOnlySpan parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ if (!parameter.IsBase64())
+ {
+ Throw.CustomSpanException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory is valid standard Base64. Empty and supported-whitespace-only memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeBase64(this Memory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeBase64(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory is valid standard Base64, or otherwise throws your custom exception.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeBase64(this Memory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeBase64(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory is valid standard Base64. Empty and supported-whitespace-only memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeBase64(this ReadOnlyMemory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.Span.MustBeBase64(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory is valid standard Base64, or otherwise throws your custom exception.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeBase64(this ReadOnlyMemory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ parameter.Span.MustBeBase64(exceptionFactory);
+ return parameter;
+ }
+
///
/// Ensures that the string is a valid email address using the default email regular expression
/// defined in , or otherwise throws an .
@@ -2110,37 +2807,40 @@ public static T MustBeGreaterThanOrEqualTo([NotNull][ValidatedNotNull] this T
}
///
- /// Ensures that the specified URI has the "http" or "https" scheme, or otherwise throws an .
+ /// Ensures that the string contains only ASCII hexadecimal characters. Empty strings are valid.
///
- /// The URI to be checked.
+ /// The string to be checked.
/// The name of the parameter (optional).
/// The message that will be passed to the resulting exception (optional).
- /// Thrown when uses a different scheme than "http" or "https".
- /// Thrown when is relative and thus has no scheme.
+ /// The validated string.
/// Thrown when is null.
+ /// Thrown when the string contains a non-hexadecimal character.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static Uri MustBeHttpOrHttpsUrl([NotNull][ValidatedNotNull] this Uri? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
+ public static string MustBeHexadecimal([NotNull][ValidatedNotNull] this string? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
{
- if (parameter.MustBeAbsoluteUri(parameterName, message).Scheme.Equals("https") == false && parameter.Scheme.Equals("http") == false)
+ parameter.MustNotBeNull(parameterName, message);
+ if (!parameter.IsHexadecimal())
{
- Throw.UriMustHaveOneSchemeOf(parameter, ["https", "http"], parameterName, message);
+ Throw.InvalidStringContent(parameterName, message, "must contain only ASCII hexadecimal characters");
}
return parameter;
}
///
- /// Ensures that the specified URI has the "http" or "https" scheme, or otherwise throws your custom exception.
+ /// Ensures that the string contains only ASCII hexadecimal characters, or otherwise throws your custom exception.
///
- /// The URI to be checked.
- /// The delegate that creates the exception to be thrown. is passed to this delegate.
- /// Your custom exception thrown when uses a different scheme than "http" or "https", or when is a relative URI, or when is null.
+ /// The string to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated string.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
- public static Uri MustBeHttpOrHttpsUrl([NotNull][ValidatedNotNull] this Uri? parameter, Func exceptionFactory)
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
+ public static string MustBeHexadecimal([NotNull][ValidatedNotNull] this string? parameter, Func exceptionFactory)
{
- if (parameter.MustBeAbsoluteUri(exceptionFactory).Scheme.Equals("https") == false && parameter.Scheme.Equals("http") == false)
+ if (parameter is null || !parameter.IsHexadecimal())
{
Throw.CustomException(exceptionFactory, parameter);
}
@@ -2149,13 +2849,174 @@ public static Uri MustBeHttpOrHttpsUrl([NotNull][ValidatedNotNull] this Uri? par
}
///
- /// Ensures that the specified URI has the "http" scheme, or otherwise throws an .
+ /// Ensures that the span contains only ASCII hexadecimal characters. Empty spans are valid.
///
- /// The URI to be checked.
+ /// The character span to be checked.
/// The name of the parameter (optional).
/// The message that will be passed to the resulting exception (optional).
- /// Thrown when uses a different scheme than "http".
- /// Thrown when is relative and thus has no scheme.
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeHexadecimal(this Span parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter).MustBeHexadecimal(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains only ASCII hexadecimal characters, or otherwise throws your custom exception.
+ ///
+ /// The character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeHexadecimal(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter).MustBeHexadecimal(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains only ASCII hexadecimal characters. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeHexadecimal(this ReadOnlySpan parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ if (!parameter.IsHexadecimal())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must contain only ASCII hexadecimal characters");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains only ASCII hexadecimal characters, or otherwise throws your custom exception.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeHexadecimal(this ReadOnlySpan parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ if (!parameter.IsHexadecimal())
+ {
+ Throw.CustomSpanException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory contains only ASCII hexadecimal characters. Empty memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeHexadecimal(this Memory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeHexadecimal(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory contains only ASCII hexadecimal characters, or otherwise throws your custom exception.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeHexadecimal(this Memory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeHexadecimal(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory contains only ASCII hexadecimal characters. Empty memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeHexadecimal(this ReadOnlyMemory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.Span.MustBeHexadecimal(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory contains only ASCII hexadecimal characters, or otherwise throws your custom exception.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeHexadecimal(this ReadOnlyMemory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ parameter.Span.MustBeHexadecimal(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the specified URI has the "http" or "https" scheme, or otherwise throws an .
+ ///
+ /// The URI to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// Thrown when uses a different scheme than "http" or "https".
+ /// Thrown when is relative and thus has no scheme.
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static Uri MustBeHttpOrHttpsUrl([NotNull][ValidatedNotNull] this Uri? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
+ {
+ if (parameter.MustBeAbsoluteUri(parameterName, message).Scheme.Equals("https") == false && parameter.Scheme.Equals("http") == false)
+ {
+ Throw.UriMustHaveOneSchemeOf(parameter, ["https", "http"], parameterName, message);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the specified URI has the "http" or "https" scheme, or otherwise throws your custom exception.
+ ///
+ /// The URI to be checked.
+ /// The delegate that creates the exception to be thrown. is passed to this delegate.
+ /// Your custom exception thrown when uses a different scheme than "http" or "https", or when is a relative URI, or when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static Uri MustBeHttpOrHttpsUrl([NotNull][ValidatedNotNull] this Uri? parameter, Func exceptionFactory)
+ {
+ if (parameter.MustBeAbsoluteUri(exceptionFactory).Scheme.Equals("https") == false && parameter.Scheme.Equals("http") == false)
+ {
+ Throw.CustomException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the specified URI has the "http" scheme, or otherwise throws an .
+ ///
+ /// The URI to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// Thrown when uses a different scheme than "http".
+ /// Thrown when is relative and thus has no scheme.
/// Thrown when is null.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
@@ -2750,6 +3611,170 @@ public static ReadOnlySpan MustBeLongerThanOrEqualTo(this ReadOnlySpan
return parameter;
}
+ ///
+ /// Ensures that the string contains no Unicode uppercase character. Empty and uncased strings are valid.
+ ///
+ /// The string to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated string.
+ /// Thrown when is null.
+ /// Thrown when the string contains an uppercase character.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static string MustBeLowerCase([NotNull][ValidatedNotNull] this string? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.MustNotBeNull(parameterName, message);
+ if (!parameter.IsLowerCase())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must contain no Unicode uppercase characters");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the string contains no Unicode uppercase character, or otherwise throws your custom exception.
+ ///
+ /// The string to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated string.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
+ public static string MustBeLowerCase([NotNull][ValidatedNotNull] this string? parameter, Func exceptionFactory)
+ {
+ if (parameter is null || !parameter.IsLowerCase())
+ {
+ Throw.CustomException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains no Unicode uppercase character. Empty and uncased spans are valid.
+ ///
+ /// The character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeLowerCase(this Span parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter).MustBeLowerCase(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains no Unicode uppercase character, or otherwise throws your custom exception.
+ ///
+ /// The character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeLowerCase(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter).MustBeLowerCase(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains no Unicode uppercase character. Empty and uncased spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeLowerCase(this ReadOnlySpan parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ if (!parameter.IsLowerCase())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must contain no Unicode uppercase characters");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains no Unicode uppercase character, or otherwise throws your custom exception.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeLowerCase(this ReadOnlySpan parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ if (!parameter.IsLowerCase())
+ {
+ Throw.CustomSpanException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory contains no Unicode uppercase character. Empty and uncased memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeLowerCase(this Memory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeLowerCase(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory contains no Unicode uppercase character, or otherwise throws your custom exception.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeLowerCase(this Memory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeLowerCase(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory contains no Unicode uppercase character. Empty and uncased memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeLowerCase(this ReadOnlyMemory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.Span.MustBeLowerCase(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory contains no Unicode uppercase character, or otherwise throws your custom exception.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeLowerCase(this ReadOnlyMemory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ parameter.Span.MustBeLowerCase(exceptionFactory);
+ return parameter;
+ }
+
///
/// Ensures that the specified is negative (less than zero), or otherwise
/// throws an .
@@ -3915,12 +4940,176 @@ public static DateTime MustBeUnspecified(this DateTime parameter, Func
- /// Ensures that the specified uses , or otherwise throws an .
+ /// Ensures that the string contains no Unicode lowercase character. Empty and uncased strings are valid.
///
- /// The date time to be checked.
+ /// The string to be checked.
/// The name of the parameter (optional).
/// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not use .
+ /// The validated string.
+ /// Thrown when is null.
+ /// Thrown when the string contains a lowercase character.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static string MustBeUpperCase([NotNull][ValidatedNotNull] this string? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.MustNotBeNull(parameterName, message);
+ if (!parameter.IsUpperCase())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must contain no Unicode lowercase characters");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the string contains no Unicode lowercase character, or otherwise throws your custom exception.
+ ///
+ /// The string to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated string.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
+ public static string MustBeUpperCase([NotNull][ValidatedNotNull] this string? parameter, Func exceptionFactory)
+ {
+ if (parameter is null || !parameter.IsUpperCase())
+ {
+ Throw.CustomException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains no Unicode lowercase character. Empty and uncased spans are valid.
+ ///
+ /// The character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeUpperCase(this Span parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter).MustBeUpperCase(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains no Unicode lowercase character, or otherwise throws your custom exception.
+ ///
+ /// The character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustBeUpperCase(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter).MustBeUpperCase(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains no Unicode lowercase character. Empty and uncased spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeUpperCase(this ReadOnlySpan parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ if (!parameter.IsUpperCase())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must contain no Unicode lowercase characters");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains no Unicode lowercase character, or otherwise throws your custom exception.
+ ///
+ /// The read-only character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustBeUpperCase(this ReadOnlySpan parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ if (!parameter.IsUpperCase())
+ {
+ Throw.CustomSpanException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory contains no Unicode lowercase character. Empty and uncased memory is valid.
+ ///
+ /// The character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeUpperCase(this Memory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeUpperCase(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the memory contains no Unicode lowercase character, or otherwise throws your custom exception.
+ ///
+ /// The character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory MustBeUpperCase(this Memory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter.Span).MustBeUpperCase(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory contains no Unicode lowercase character. Empty and uncased memory is valid.
+ ///
+ /// The read-only character memory to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeUpperCase(this ReadOnlyMemory parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.Span.MustBeUpperCase(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only memory contains no Unicode lowercase character, or otherwise throws your custom exception.
+ ///
+ /// The read-only character memory to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated read-only character memory.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ReadOnlyMemory MustBeUpperCase(this ReadOnlyMemory parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ parameter.Span.MustBeUpperCase(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the specified uses , or otherwise throws an .
+ ///
+ /// The date time to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// Thrown when does not use .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DateTime MustBeUtc(this DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
{
@@ -4233,123 +5422,451 @@ public static string MustContain([NotNull][ValidatedNotNull] this string? parame
/// The item that must be part of the immutable array.
/// The name of the parameter (optional).
/// The message that will be passed to the resulting exception (optional).
- /// Thrown when does not contain .
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ImmutableArray MustContain(this ImmutableArray parameter, T item, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
+ /// Thrown when does not contain .
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ImmutableArray MustContain(this ImmutableArray parameter, T item, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
+ {
+ if (!parameter.Contains(item))
+ {
+ Throw.MissingItem(parameter, item, parameterName, message);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the immutable array contains the specified item, or otherwise throws your custom exception.
+ ///
+ /// The immutable array to be checked.
+ /// The item that must be part of the immutable array.
+ /// The delegate that creates your custom exception. and are passed to this delegate.
+ /// Your custom exception thrown when does not contain .
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ImmutableArray MustContain(this ImmutableArray parameter, T item, Func, T, Exception> exceptionFactory)
+ {
+ if (!parameter.Contains(item))
+ {
+ Throw.CustomException(exceptionFactory, parameter, item);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the dictionary contains the specified key, or otherwise throws a .
+ /// The check is performed via , so the dictionary is
+ /// never enumerated and its key comparer is respected.
+ ///
+ /// The dictionary to be checked.
+ /// The key that must be present in the dictionary.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// Thrown when does not contain .
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static IReadOnlyDictionary MustContainKey([NotNull][ValidatedNotNull] this IReadOnlyDictionary? parameter, TKey key, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
+ {
+ if (!parameter.MustNotBeNull(parameterName, message).ContainsKey(key))
+ {
+ Throw.MissingKey(parameter, key, parameterName, message);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the dictionary contains the specified key, or otherwise throws your custom exception.
+ /// The check is performed via , so the dictionary is
+ /// never enumerated and its key comparer is respected.
+ ///
+ /// The dictionary to be checked.
+ /// The key that must be present in the dictionary.
+ /// The delegate that creates your custom exception. and are passed to this delegate.
+ /// Your custom exception thrown when does not contain , or when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static IReadOnlyDictionary MustContainKey([NotNull][ValidatedNotNull] this IReadOnlyDictionary? parameter, TKey key, Func?, TKey, Exception> exceptionFactory)
+ {
+ if (parameter is null || !parameter.ContainsKey(key))
+ {
+ Throw.CustomException(exceptionFactory, parameter, key);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the dictionary contains the specified key, or otherwise throws a .
+ /// The check is performed via , so the dictionary is
+ /// never enumerated and its key comparer is respected.
+ ///
+ /// The dictionary to be checked.
+ /// The key that must be present in the dictionary.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// Thrown when does not contain .
+ /// Thrown when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static Dictionary MustContainKey([NotNull][ValidatedNotNull] this Dictionary? parameter, TKey key, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null)
+ where TKey : notnull
+ {
+ if (!parameter.MustNotBeNull(parameterName, message).ContainsKey(key))
+ {
+ Throw.MissingKey(parameter, key, parameterName, message);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the dictionary contains the specified key, or otherwise throws your custom exception.
+ /// The check is performed via , so the dictionary is
+ /// never enumerated and its key comparer is respected.
+ ///
+ /// The dictionary to be checked.
+ /// The key that must be present in the dictionary.
+ /// The delegate that creates your custom exception. and are passed to this delegate.
+ /// Your custom exception thrown when does not contain , or when is null.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static Dictionary MustContainKey([NotNull][ValidatedNotNull] this Dictionary? parameter, TKey key, Func?, TKey, Exception> exceptionFactory)
+ where TKey : notnull
+ {
+ if (parameter is null || !parameter.ContainsKey(key))
+ {
+ Throw.CustomException(exceptionFactory, parameter, key);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the string contains only Unicode decimal digits. Empty strings are valid.
+ ///
+ /// The string to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated string.
+ /// Thrown when is null.
+ /// Thrown when the string contains a non-digit character.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull")]
+ public static string MustContainOnlyDigits([NotNull][ValidatedNotNull] this string? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ parameter.MustNotBeNull(parameterName, message);
+ if (!parameter.ContainsOnlyDigits())
+ {
+ Throw.InvalidStringContent(parameterName, message, "must contain only Unicode decimal digits");
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the string contains only Unicode decimal digits, or otherwise throws your custom exception.
+ ///
+ /// The string to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated string.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [ContractAnnotation("parameter:null => halt; parameter:notnull => notnull; exceptionFactory:null => halt")]
+ public static string MustContainOnlyDigits([NotNull][ValidatedNotNull] this string? parameter, Func exceptionFactory)
+ {
+ if (parameter is null || !parameter.ContainsOnlyDigits())
+ {
+ Throw.CustomException(exceptionFactory, parameter);
+ }
+
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains only Unicode decimal digits. Empty spans are valid.
+ ///
+ /// The character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustContainOnlyDigits(this Span parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null)
+ {
+ ((ReadOnlySpan)parameter).MustContainOnlyDigits(parameterName, message);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the span contains only Unicode decimal digits, or otherwise throws your custom exception.
+ ///
+ /// The character span to be checked.
+ ///
+ /// The delegate that creates your custom exception. is passed to this delegate.
+ ///
+ /// The validated character span.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Span MustContainOnlyDigits(this Span parameter, ReadOnlySpanExceptionFactory exceptionFactory)
+ {
+ ((ReadOnlySpan)parameter).MustContainOnlyDigits(exceptionFactory);
+ return parameter;
+ }
+
+ ///
+ /// Ensures that the read-only span contains only Unicode decimal digits. Empty spans are valid.
+ ///
+ /// The read-only character span to be checked.
+ /// The name of the parameter (optional).
+ /// The message that will be passed to the resulting exception (optional).
+ /// The validated read-only character span.
+ public static ReadOnlySpan MustContainOnlyDigits(this ReadOnlySpan