-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCheck.ContainsOnlyDigits.cs
More file actions
69 lines (62 loc) · 2.96 KB
/
Copy pathCheck.ContainsOnlyDigits.cs
File metadata and controls
69 lines (62 loc) · 2.96 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
using System;
using System.Runtime.CompilerServices;
namespace Light.GuardClauses;
public static partial class Check
{
/// <summary>
/// Checks if the specified string is non-null and every character is a Unicode decimal digit. Empty strings are valid.
/// </summary>
/// <param name="parameter">The string to be checked.</param>
/// <returns>
/// True if <paramref name="parameter" /> is non-null and every character is a Unicode decimal digit, otherwise false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsOnlyDigits(this string? parameter) =>
parameter is not null && parameter.AsSpan().ContainsOnlyDigits();
/// <summary>
/// Checks if every character in the specified span is a Unicode decimal digit. Empty spans are valid.
/// </summary>
/// <param name="parameter">The character span to be checked.</param>
/// <returns>
/// True if every character in <paramref name="parameter" /> is a Unicode decimal digit, otherwise false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsOnlyDigits(this Span<char> parameter) =>
((ReadOnlySpan<char>) parameter).ContainsOnlyDigits();
/// <summary>
/// Checks if every character in the specified read-only span is a Unicode decimal digit. Empty spans are valid.
/// </summary>
/// <param name="parameter">The read-only character span to be checked.</param>
/// <returns>
/// True if every character in <paramref name="parameter" /> is a Unicode decimal digit, otherwise false.
/// </returns>
public static bool ContainsOnlyDigits(this ReadOnlySpan<char> parameter)
{
foreach (var character in parameter)
{
if (!char.IsDigit(character))
{
return false;
}
}
return true;
}
/// <summary>
/// Checks if every character in the specified memory is a Unicode decimal digit. Empty memory is valid.
/// </summary>
/// <param name="parameter">The character memory to be checked.</param>
/// <returns>
/// True if every character in <paramref name="parameter" /> is a Unicode decimal digit, otherwise false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsOnlyDigits(this Memory<char> parameter) => parameter.Span.ContainsOnlyDigits();
/// <summary>
/// Checks if every character in the specified read-only memory is a Unicode decimal digit. Empty memory is valid.
/// </summary>
/// <param name="parameter">The read-only character memory to be checked.</param>
/// <returns>
/// True if every character in <paramref name="parameter" /> is a Unicode decimal digit, otherwise false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsOnlyDigits(this ReadOnlyMemory<char> parameter) => parameter.Span.ContainsOnlyDigits();
}