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

View File

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