Merge pull request #1975 from nghttp2/refactor-char-comp

Refactor character comparison
This commit is contained in:
Tatsuhiro Tsujikawa 2023-10-17 20:57:52 +09:00 committed by GitHub
commit 56b762fa55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -105,17 +105,34 @@ int nghttp2_inet_pton(int af, const char *src, void *dst) {
const char UPPER_XDIGITS[] = "0123456789ABCDEF";
bool in_rfc3986_unreserved_chars(const char c) {
static constexpr char unreserved[] = {'-', '.', '_', '~'};
return is_alpha(c) || is_digit(c) ||
std::find(std::begin(unreserved), std::end(unreserved), c) !=
std::end(unreserved);
switch (c) {
case '-':
case '.':
case '_':
case '~':
return true;
}
return is_alpha(c) || is_digit(c);
}
bool in_rfc3986_sub_delims(const char c) {
static constexpr char sub_delims[] = {'!', '$', '&', '\'', '(', ')',
'*', '+', ',', ';', '='};
return std::find(std::begin(sub_delims), std::end(sub_delims), c) !=
std::end(sub_delims);
switch (c) {
case '!':
case '$':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
return true;
}
return false;
}
std::string percent_encode(const unsigned char *target, size_t len) {
@ -140,16 +157,37 @@ std::string percent_encode(const std::string &target) {
}
bool in_token(char c) {
static constexpr char extra[] = {'!', '#', '$', '%', '&', '\'', '*', '+',
'-', '.', '^', '_', '`', '|', '~'};
return is_alpha(c) || is_digit(c) ||
std::find(std::begin(extra), std::end(extra), c) != std::end(extra);
switch (c) {
case '!':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '*':
case '+':
case '-':
case '.':
case '^':
case '_':
case '`':
case '|':
case '~':
return true;
}
return is_alpha(c) || is_digit(c);
}
bool in_attr_char(char c) {
static constexpr char bad[] = {'*', '\'', '%'};
return util::in_token(c) &&
std::find(std::begin(bad), std::end(bad), c) == std::end(bad);
switch (c) {
case '*':
case '\'':
case '%':
return false;
}
return util::in_token(c);
}
StringRef percent_encode_token(BlockAllocator &balloc,