-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(normalize): move normalization to parser, such that validator is…
… always validating strict. Add Strict-property (default true) to IbanAttribute and FluentValidator extension. When Strict=true, the input must strictly match the IBAN format rules. When Strict=false, whitespace is ignored and strict character casing enforcement is disabled (meaning, the user can input in lower and uppercase). This mode is a bit more forgiving when dealing with user-input. However it does require after successful validation, that you parse the user input with IIbanParser to normalize/sanitize the input and to be able to format the IBAN in correct electronic format. See #93
- Loading branch information
Showing
22 changed files
with
335 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using IbanNet.Extensions; | ||
|
||
namespace IbanNet.Internal; | ||
|
||
internal static class InputNormalization | ||
{ | ||
/// <summary> | ||
/// Normalizes an IBAN by removing whitespace, removing non-alphanumerics and upper casing each character. | ||
/// </summary> | ||
/// <param name="value">The input value to normalize.</param> | ||
/// <returns>The normalized IBAN.</returns> | ||
internal static string? NormalizeOrNull([NotNullIfNotNull("value")] string? value) | ||
{ | ||
if (value is null) | ||
{ | ||
return null; | ||
} | ||
|
||
int length = value.Length; | ||
#if USE_SPANS | ||
// Use stack but clamp to avoid excessive stackalloc buffer. | ||
const int stackallocMaxSize = Iban.MaxLength + 6; | ||
Span<char> buffer = length <= stackallocMaxSize | ||
? stackalloc char[length] | ||
: new char[length]; | ||
#else | ||
char[] buffer = new char[length]; | ||
#endif | ||
int pos = 0; | ||
// ReSharper disable once ForCanBeConvertedToForeach - justification : performance | ||
for (int i = 0; i < length; i++) | ||
{ | ||
char ch = value[i]; | ||
if (ch.IsWhitespace()) | ||
{ | ||
continue; | ||
} | ||
|
||
if (ch.IsAsciiLetter()) | ||
{ | ||
// Inline upper case. | ||
buffer[pos++] = (char)(ch & ~' '); | ||
} | ||
else | ||
{ | ||
buffer[pos++] = ch; | ||
} | ||
} | ||
|
||
#if USE_SPANS | ||
return new string(buffer[..pos]); | ||
#else | ||
return new string(buffer, 0, pos); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.