Skip to content

Commit

Permalink
minor redesign of is_ipv4
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Nov 13, 2023
1 parent c273d16 commit e0ee054
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/checkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ namespace ada::checkers {

ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
size_t last_dot = view.rfind('.');
if (last_dot == view.size() - 1) {
view.remove_suffix(1);
last_dot = view.rfind('.');
if (last_dot != std::string_view::npos) {
// We have at least one dot.
//
// If the address ends with a dot, we need to prune it (special case).
if (last_dot == view.size() - 1) {
view.remove_suffix(1);
last_dot = view.rfind('.');
if (last_dot != std::string_view::npos) {
view = view.substr(last_dot + 1);
}
} else {
// We have at least one dot and the address does not end with a dot.
view = view.substr(last_dot + 1);
}
}
std::string_view number =
(last_dot == std::string_view::npos) ? view : view.substr(last_dot + 1);
if (number.empty()) {
if (view.empty()) {
return false;
}
/** Optimization opportunity: we have basically identified the last number of
the ipv4 if we return true here. We might as well parse it and have at
least one number parsed when we get to parse_ipv4. */
if (std::all_of(number.begin(), number.end(), ada::checkers::is_digit)) {
if (std::all_of(view.begin(), view.end(), ada::checkers::is_digit)) {
return true;
}
return (checkers::has_hex_prefix(number) &&
std::all_of(number.begin() + 2, number.end(),
ada::unicode::is_lowercase_hex));
return (checkers::has_hex_prefix(view) &&
(view.size() == 2 || std::all_of(view.begin() + 2, view.end(),
ada::unicode::is_lowercase_hex)));
}

// for use with path_signature, we include all characters that need percent
Expand Down

0 comments on commit e0ee054

Please sign in to comment.