2
2
// Licensed under the MIT license. See LICENSE file in the project root for details.
3
3
4
4
using System ;
5
+ using System . Linq ;
5
6
using System . Runtime . CompilerServices ;
6
7
using System . Text ;
7
8
#if NETSTANDARD2_0
12
13
namespace PG . Commons . Utilities ;
13
14
14
15
/// <summary>
15
- /// Provides primitive helper methods for strings.
16
+ /// Provides primitive helper methods for strings.
16
17
/// </summary>
17
18
public static class StringUtilities
18
19
{
19
20
/// <summary>
20
- /// Checks whether a given string, when converted to bytes, is not longer than the max value of an <see cref="ushort"/>.
21
- /// Throws an <see cref="ArgumentException"/> if the string is longer.
21
+ /// Checks whether a given string, when converted to bytes, is not longer than the max value of an
22
+ /// <see cref="ushort" />.
23
+ /// Throws an <see cref="ArgumentException" /> if the string is longer.
22
24
/// </summary>
23
25
/// <param name="value">The string to validate.</param>
24
26
/// <param name="encoding">The encoding that shall be used to get the string length.</param>
@@ -32,17 +34,18 @@ public static ushort ValidateStringByteSizeUInt16(ReadOnlySpan<char> value, Enco
32
34
if ( encoding == null )
33
35
throw new ArgumentNullException ( nameof ( encoding ) ) ;
34
36
35
- var size = encoding . GetByteCount ( value ) ;
37
+ var size = encoding . GetByteCount ( value ) ;
36
38
37
39
if ( size is < 0 or > ushort . MaxValue )
38
- throw new ArgumentException ( $ "The value is longer than the expected { ushort . MaxValue } characters.", nameof ( value ) ) ;
40
+ throw new ArgumentException ( $ "The value is longer than the expected { ushort . MaxValue } characters.",
41
+ nameof ( value ) ) ;
39
42
40
43
return ( ushort ) size ;
41
44
}
42
45
43
46
/// <summary>
44
- /// Checks whether a given character sequence has no more characters than the max value of an <see cref="ushort"/>.
45
- /// Throws an <see cref="ArgumentException"/> if the string is longer.
47
+ /// Checks whether a given character sequence has no more characters than the max value of an <see cref="ushort" />.
48
+ /// Throws an <see cref="ArgumentException" /> if the string is longer.
46
49
/// </summary>
47
50
/// <param name="value">The string to validate.</param>
48
51
/// <returns>The actual length of the value in characters.</returns>
@@ -55,13 +58,14 @@ public static ushort ValidateStringCharLengthUInt16(ReadOnlySpan<char> value)
55
58
56
59
var length = value . Length ;
57
60
if ( length is < 0 or > ushort . MaxValue )
58
- throw new ArgumentException ( $ "The value is longer that the expected { ushort . MaxValue } characters.", nameof ( value ) ) ;
61
+ throw new ArgumentException ( $ "The value is longer that the expected { ushort . MaxValue } characters.",
62
+ nameof ( value ) ) ;
59
63
60
64
return ( ushort ) length ;
61
65
}
62
66
63
67
/// <summary>
64
- /// Throws an <see cref="ArgumentException"/> if the given character sequence contains non-ASCII characters.
68
+ /// Throws an <see cref="ArgumentException" /> if the given character sequence contains non-ASCII characters.
65
69
/// </summary>
66
70
/// <param name="value">The character sequence to validate.</param>
67
71
/// <exception cref="ArgumentException">The character sequence contains non-ASCII characters.</exception>
@@ -74,7 +78,7 @@ public static void ValidateIsAsciiOnly(ReadOnlySpan<char> value)
74
78
}
75
79
76
80
/// <summary>
77
- /// Throws an <see cref="ArgumentException"/> if the given character sequence contains non-ASCII characters.
81
+ /// Throws an <see cref="ArgumentException" /> if the given character sequence contains non-ASCII characters.
78
82
/// </summary>
79
83
/// <param name="value">The character sequence to validate.</param>
80
84
/// <exception cref="ArgumentException">The character sequence contains non-ASCII characters.</exception>
@@ -85,10 +89,27 @@ public static bool IsAsciiOnly(ReadOnlySpan<char> value)
85
89
throw new ArgumentNullException ( nameof ( value ) ) ;
86
90
87
91
foreach ( var ch in value )
88
- {
89
92
if ( ( uint ) ch > '\x007f ' )
90
93
return false ;
91
- }
92
94
return true ;
93
95
}
94
- }
96
+
97
+ /// <summary>
98
+ /// Basic validation algorithm.
99
+ /// </summary>
100
+ /// <param name="s"></param>
101
+ /// <returns></returns>
102
+ public static string Validate ( string ? s )
103
+ {
104
+ var stringBuilder = new StringBuilder ( ) ;
105
+
106
+ if ( string . IsNullOrEmpty ( s ) ) return string . Empty ;
107
+
108
+ foreach ( var t in s . Where ( t => t == 0x9 || t == 0xA || t == 0xD ||
109
+ ( t >= 0x20 && t <= 0xD7FF ) ||
110
+ ( t >= 0xE000 && t <= 0xFFFD ) ) )
111
+ stringBuilder . Append ( t ) ;
112
+
113
+ return stringBuilder . ToString ( ) ;
114
+ }
115
+ }
0 commit comments