Use isAscii...()

This commit is contained in:
2023-04-27 16:28:37 +02:00
parent 26ec59507d
commit 7b252a5bb5
3 changed files with 17 additions and 17 deletions

View File

@ -131,7 +131,7 @@ namespace Elwig.Helpers {
} }
public static int Modulo(string a, int b) { public static int Modulo(string a, int b) {
if (a.Length == 0 || !a.All(char.IsDigit)) { if (a.Length == 0 || !a.All(char.IsAsciiDigit)) {
throw new ArgumentException("First argument has to be a decimal string"); throw new ArgumentException("First argument has to be a decimal string");
} else if (b < 2) { } else if (b < 2) {
throw new ArgumentException("Second argument has to be greater than 1"); throw new ArgumentException("Second argument has to be greater than 1");

View File

@ -34,12 +34,12 @@ namespace Elwig.Helpers {
return CheckInteger(input, required, -1); return CheckInteger(input, required, -1);
} }
public static ValidationResult CheckInteger(TextBox input, bool required, int maxLen) { private static ValidationResult CheckInteger(TextBox input, bool required, int maxLen) {
string text = ""; string text = "";
int pos = input.CaretIndex; int pos = input.CaretIndex;
for (int i = 0; i < input.Text.Length; i++) { for (int i = 0; i < input.Text.Length; i++) {
char ch = input.Text[i]; char ch = input.Text[i];
if (char.IsDigit(ch)) if (char.IsAsciiDigit(ch))
text += ch; text += ch;
if (i == input.CaretIndex - 1) if (i == input.CaretIndex - 1)
pos = text.Length; pos = text.Length;
@ -89,7 +89,7 @@ namespace Elwig.Helpers {
} else if (v == 0 && ch == '+') { } else if (v == 0 && ch == '+') {
v++; v++;
text += ch; text += ch;
} else if (v > 0 && char.IsDigit(ch)) { } else if (v > 0 && char.IsAsciiDigit(ch)) {
if (PHONE_NRS.Any(kv => text == "+" + kv.Key)) if (PHONE_NRS.Any(kv => text == "+" + kv.Key))
text += ' '; text += ' ';
if (text.StartsWith("+43 ")) { if (text.StartsWith("+43 ")) {
@ -127,7 +127,7 @@ namespace Elwig.Helpers {
for (int i = 0; i < input.Text.Length && text.Length < 256; i++) { for (int i = 0; i < input.Text.Length && text.Length < 256; i++) {
char ch = input.Text[i]; char ch = input.Text[i];
if (domain) { if (domain) {
if ((char.IsAscii(ch) && char.IsLetterOrDigit(ch)) || ".-_öäüßÖÄÜẞ".Any(c => c == ch)) { if ((char.IsAsciiLetterOrDigit(ch)) || ".-_öäüßÖÄÜẞ".Any(c => c == ch)) {
if (!(text.Last() == '.' && ch == '.')) if (!(text.Last() == '.' && ch == '.'))
text += char.ToLower(ch); text += char.ToLower(ch);
} }
@ -151,7 +151,7 @@ namespace Elwig.Helpers {
} }
var last = text.Split(".").Last(); var last = text.Split(".").Last();
if (last.Length < 2 || !last.All(ch => char.IsAscii(ch) && char.IsLower(ch))) if (last.Length < 2 || !last.All(ch => char.IsAsciiLetterLower(ch)))
return new(false, "E-Mail-Adresse ungültig"); return new(false, "E-Mail-Adresse ungültig");
return new(true, null); return new(true, null);
@ -163,9 +163,9 @@ namespace Elwig.Helpers {
int v = 0; int v = 0;
for (int i = 0; i < input.Text.Length && v < 34; i++) { for (int i = 0; i < input.Text.Length && v < 34; i++) {
char ch = input.Text[i]; char ch = input.Text[i];
if (char.IsLetterOrDigit(ch) && char.IsAscii(ch)) { if (char.IsAsciiLetterOrDigit(ch)) {
if (((v < 2 && char.IsLetter(ch)) || (v >= 2 && v < 4 && char.IsDigit(ch)) || v >= 4) && if (((v < 2 && char.IsAsciiLetter(ch)) || (v >= 2 && v < 4 && char.IsAsciiDigit(ch)) || v >= 4) &&
((!text.StartsWith("AT") && !text.StartsWith("DE")) || char.IsDigit(ch))) ((!text.StartsWith("AT") && !text.StartsWith("DE")) || char.IsAsciiDigit(ch)))
{ {
if (v != 0 && v % 4 == 0) if (v != 0 && v % 4 == 0)
text += ' '; text += ' ';
@ -190,7 +190,7 @@ namespace Elwig.Helpers {
} }
var validation = (text[4..] + text[..4]).Replace(" ", "") var validation = (text[4..] + text[..4]).Replace(" ", "")
.Select(ch => char.IsDigit(ch) ? ch.ToString() : (ch - 'A' + 10).ToString()) .Select(ch => char.IsAsciiDigit(ch) ? ch.ToString() : (ch - 'A' + 10).ToString())
.Aggregate((a, b) => a + b); .Aggregate((a, b) => a + b);
if (Utils.Modulo(validation, 97) != 1) if (Utils.Modulo(validation, 97) != 1)
return new(false, "Prüfsumme der IBAN ist falsch"); return new(false, "Prüfsumme der IBAN ist falsch");
@ -203,10 +203,10 @@ namespace Elwig.Helpers {
int pos = input.CaretIndex; int pos = input.CaretIndex;
for (int i = 0, v = 0; i < input.Text.Length; i++) { for (int i = 0, v = 0; i < input.Text.Length; i++) {
char ch = input.Text[i]; char ch = input.Text[i];
if ((v < 4 || v >= 6) && char.IsAscii(ch) && char.IsLetterOrDigit(ch)) { if ((v < 4 || v >= 6) && char.IsAsciiLetterOrDigit(ch)) {
v++; v++;
text += char.ToUpper(ch); text += char.ToUpper(ch);
} else if (v >= 4 && v < 6 && char.IsAscii(ch) && char.IsLetter(ch)) { } else if (v >= 4 && v < 6 && char.IsAsciiLetter(ch)) {
v++; v++;
text += char.ToUpper(ch); text += char.ToUpper(ch);
} }
@ -247,7 +247,7 @@ namespace Elwig.Helpers {
} }
// https://statistik.at/fileadmin/shared/QM/Standarddokumentationen/RW/std_r_land-forstw_register.pdf#page=41 // https://statistik.at/fileadmin/shared/QM/Standarddokumentationen/RW/std_r_land-forstw_register.pdf#page=41
int s = 0, v = 0; int s = 0, v;
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
s += (input.Text[i] - '0') * (7 - i); s += (input.Text[i] - '0') * (7 - i);
v = (11 - (s % 11)) % 10; v = (11 - (s % 11)) % 10;
@ -263,15 +263,15 @@ namespace Elwig.Helpers {
int pos = input.CaretIndex; int pos = input.CaretIndex;
for (int i = 0, v = 0; i < input.Text.Length; i++) { for (int i = 0, v = 0; i < input.Text.Length; i++) {
char ch = input.Text[i]; char ch = input.Text[i];
if (v < 2 && char.IsAscii(ch) && char.IsLetter(ch)) { if (v < 2 && char.IsAsciiLetter(ch)) {
v++; v++;
text += char.ToUpper(ch); text += char.ToUpper(ch);
} else if (v >= 2 && char.IsAscii(ch) && char.IsLetterOrDigit(ch)) { } else if (v >= 2 && char.IsAsciiLetterOrDigit(ch)) {
if (text.StartsWith("AT")) { if (text.StartsWith("AT")) {
if (v == 2 && (ch == 'u' || ch == 'U')) { if (v == 2 && (ch == 'u' || ch == 'U')) {
v++; v++;
text += 'U'; text += 'U';
} else if (v > 2 && char.IsDigit(ch)) { } else if (v > 2 && char.IsAsciiDigit(ch)) {
v++; v++;
text += ch; text += ch;
} }

View File

@ -154,7 +154,7 @@ namespace Elwig.Models {
public virtual BillingAddr BillingAddress { get; private set; } public virtual BillingAddr BillingAddress { get; private set; }
public int SearchScore(IEnumerable<string> keywords) { public int SearchScore(IEnumerable<string> keywords) {
keywords = keywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsDigit(c)))); keywords = keywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsAsciiDigit(c))));
if (!keywords.Any()) if (!keywords.Any())
return 0; return 0;