-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
url/ParseScheme: Minor performance improvements
- Loading branch information
Showing
3 changed files
with
41 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,28 +70,50 @@ func IsValidURLScheme(s string) bool { | |
// regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+\-\.]*$`).MatchString(s) | ||
// However such an implementation would be a lot slower when called repeatedly. | ||
// Instead we directly build code that implements this regex. | ||
// | ||
// For this we first check that the string is non-empty. | ||
// Then we check that the first character is not a '+', '-' or '.' | ||
// Then we check that each character is either alphanumeric or a '+', '-' or '.' | ||
|
||
if len(s) == 0 { | ||
// Iterate over the bytes in this string. | ||
// We can do this safely, because any valid scheme must be ascii. | ||
bytes := []byte(s) | ||
if len(bytes) == 0 { | ||
return false | ||
} | ||
|
||
// if the first rune is invalid, no need to check everything else | ||
if s[0] == '+' || s[0] == '-' || s[0] == '.' { | ||
return false | ||
var ( | ||
idx = 0 // current index | ||
c = bytes[0] // current character | ||
) | ||
|
||
// scheme must start with a letter | ||
// so omit the loop preamble | ||
goto start | ||
|
||
nextLetter: | ||
// go to the next letter | ||
// or be done | ||
idx++ | ||
if idx >= len(bytes) { | ||
return true | ||
} | ||
|
||
for _, r := range s { | ||
if !(('a' <= r && r <= 'z') || ('A' <= r && r <= 'Z')) && // more efficient version of [email protected](r) for ascii | ||
r != '+' && | ||
r != '-' && | ||
r != '.' { | ||
return false | ||
} | ||
// get the current letter | ||
c = bytes[idx] | ||
|
||
if '0' <= c && c <= '9' { | ||
goto nextLetter | ||
} | ||
|
||
if c == '+' || c == '-' || c == '.' { | ||
goto nextLetter | ||
} | ||
|
||
start: | ||
if 'a' <= c && c <= 'z' { | ||
goto nextLetter | ||
} | ||
|
||
if 'A' <= c && c <= 'Z' { | ||
goto nextLetter | ||
} | ||
|
||
return true | ||
return false | ||
} |
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