mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
src: refactor EndsInANumber in node_url.cc and adds IsIPv4NumberValid
Removes the use of vector in EndsInANumber and uses IsIPv4NumberValid instead of parsing the number to check if it is valid. Fixes: https://github.com/nodejs/performance/issues/36 Refs: https://github.com/ada-url/ada/pull/36 PR-URL: https://github.com/nodejs/node/pull/46227 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
08bafc84f6
commit
b06298c98e
1 file changed
+57
-17
+57
-17
@@ -175,6 +175,8 @@ CHAR_TEST(8, IsC0ControlOrSpace, (ch >= '\0' && ch <= ' '))
|
||||
// https://infra.spec.whatwg.org/#ascii-digit
|
||||
CHAR_TEST(8, IsASCIIDigit, (ch >= '0' && ch <= '9'))
|
||||
|
||||
CHAR_TEST(8, IsASCIIOcDigit, (ch >= '0' && ch <= '7'))
|
||||
|
||||
// https://infra.spec.whatwg.org/#ascii-hex-digit
|
||||
CHAR_TEST(8, IsASCIIHexDigit, (IsASCIIDigit(ch) ||
|
||||
(ch >= 'A' && ch <= 'F') ||
|
||||
@@ -407,29 +409,67 @@ int64_t ParseIPv4Number(const char* start, const char* end) {
|
||||
return strtoll(start, nullptr, R);
|
||||
}
|
||||
|
||||
// https://url.spec.whatwg.org/#ends-in-a-number-checker
|
||||
bool EndsInANumber(const std::string& input) {
|
||||
std::vector<std::string> parts = SplitString(input, '.', false);
|
||||
|
||||
if (parts.empty()) return false;
|
||||
|
||||
if (parts.back().empty()) {
|
||||
if (parts.size() == 1) return false;
|
||||
parts.pop_back();
|
||||
// https://url.spec.whatwg.org/#ipv4-number-parser
|
||||
bool IsIPv4NumberValid(const std::string_view input) {
|
||||
if (input.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& last = parts.back();
|
||||
// If a number starts with '0' it might be a number with base 8 or base
|
||||
// 16. If not, checking if all characters are digits proves that it is a
|
||||
// base 10 number.
|
||||
if (input.size() >= 2 && input[0] == '0') {
|
||||
if (input[1] == 'X' || input[1] == 'x') {
|
||||
if (input.size() == 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If last is non-empty and contains only ASCII digits, then return true
|
||||
if (!last.empty() && std::all_of(last.begin(), last.end(), ::isdigit)) {
|
||||
return std::all_of(input.begin() + 2, input.end(), [](const char& c) {
|
||||
return IsASCIIHexDigit(c);
|
||||
});
|
||||
}
|
||||
|
||||
return std::all_of(input.begin() + 1, input.end(), [](const char& c) {
|
||||
return IsASCIIOcDigit(c);
|
||||
});
|
||||
}
|
||||
|
||||
return std::all_of(input.begin(), input.end(), [](const char& c) {
|
||||
return IsASCIIDigit(c);
|
||||
});
|
||||
}
|
||||
|
||||
// https://url.spec.whatwg.org/#ends-in-a-number-checker
|
||||
inline bool EndsInANumber(const std::string_view input) {
|
||||
if (input.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char delimiter = '.';
|
||||
auto last_index = input.size() - 1;
|
||||
if (input.back() == delimiter) {
|
||||
--last_index;
|
||||
}
|
||||
|
||||
std::string_view last{};
|
||||
auto pos = input.find_last_of(delimiter, last_index);
|
||||
if (pos == std::string_view::npos) {
|
||||
last = input.substr(0, last_index);
|
||||
} else {
|
||||
last = input.substr(pos + 1, last_index - pos);
|
||||
}
|
||||
|
||||
if (last.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (std::all_of(last.begin(), last.end(), [](const char& c) {
|
||||
return IsASCIIDigit(c);
|
||||
})) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* last_str = last.c_str();
|
||||
int64_t num = ParseIPv4Number(last_str, last_str + last.size());
|
||||
if (num >= 0) return true;
|
||||
|
||||
return false;
|
||||
return IsIPv4NumberValid(last);
|
||||
}
|
||||
|
||||
void URLHost::ParseIPv4Host(const char* input, size_t length) {
|
||||
|
||||
Reference in new issue
Block a user