Optmize InetPathFormat.validatePath.

Speeds up the check by roughly 4x.
This commit is contained in:
Sönke Ludwig 2021-01-18 14:42:50 +01:00
parent 397527a0a1
commit 4bb5fc9ce8

View file

@ -1736,13 +1736,13 @@ struct InetPathFormat {
static string validatePath(string path) static string validatePath(string path)
@nogc { @nogc {
for (size_t i = 0; i < path.length; i++) { for (size_t i = 0; i < path.length; i++) {
if (isAsciiAlphaNum(path[i]))
continue;
switch (path[i]) { switch (path[i]) {
default: default:
return "Invalid character in internet path."; return "Invalid character in internet path.";
// unreserved // unreserved
case 'A': .. case 'Z':
case 'a': .. case 'z':
case '0': .. case '9':
case '-', '.', '_', '~': case '-', '.', '_', '~':
// subdelims // subdelims
case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=': case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=':
@ -2029,6 +2029,25 @@ unittest { // test range based path
assert(stripExtension(InetPath("foo.bar.txt").head2.name).equal("foo.bar")); assert(stripExtension(InetPath("foo.bar.txt").head2.name).equal("foo.bar"));
} }
private static bool isAsciiAlphaNum(char ch)
@safe nothrow pure @nogc {
return (uint(ch) & 0xDF) - 0x41 < 26 || uint(ch) - '0' <= 9;
}
unittest {
assert(!isAsciiAlphaNum('@'));
assert(isAsciiAlphaNum('A'));
assert(isAsciiAlphaNum('Z'));
assert(!isAsciiAlphaNum('['));
assert(!isAsciiAlphaNum('`'));
assert(isAsciiAlphaNum('a'));
assert(isAsciiAlphaNum('z'));
assert(!isAsciiAlphaNum('{'));
assert(!isAsciiAlphaNum('/'));
assert(isAsciiAlphaNum('0'));
assert(isAsciiAlphaNum('9'));
assert(!isAsciiAlphaNum(':'));
}
unittest { // regression tests unittest { // regression tests
assert(NativePath("").bySegment.empty); assert(NativePath("").bySegment.empty);