Skip to content

Commit

Permalink
optimizing is_ipv4 (#561)
Browse files Browse the repository at this point in the history
* changing Visual Studio test matrix in CI

* fix

* minor redesign of is_ipv4

* some optimization

* optimization.
  • Loading branch information
lemire authored Nov 13, 2023
1 parent e91e2b1 commit 309c285
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/visual_studio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ jobs:
fail-fast: false
matrix:
include:
- {gen: Visual Studio 17 2022, arch: x64, devchecks: ON, shared: OFF}
- {gen: Visual Studio 17 2022, arch: x64, devchecks: ON, shared: ON}
- {gen: Visual Studio 17 2022, arch: Win32, devchecks: ON, shared: OFF}
- {gen: Visual Studio 17 2022, arch: Win32, devchecks: ON, shared: ON}
- {gen: Visual Studio 17 2022, arch: x64, devchecks: OFF, shared: OFF, config: Release}
- {gen: Visual Studio 17 2022, arch: x64, devchecks: ON, shared: OFF, config: Debug}
- {gen: Visual Studio 17 2022, arch: x64, devchecks: ON, shared: ON, config: Debug}
- {gen: Visual Studio 17 2022, arch: Win32, devchecks: ON, shared: OFF, config: Debug}
- {gen: Visual Studio 17 2022, arch: Win32, devchecks: ON, shared: ON, config: Debug}
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v3.6.0
- name: Configure
run: |
cmake -DADA_DEVELOPMENT_CHECKS="${{matrix.devchecks}}" -G "${{matrix.gen}}" -A ${{matrix.arch}} -DBUILD_SHARED_LIBS=${{matrix.shared}} -B build
- name: Build Debug
run: cmake --build build --config Debug --verbose
- name: Run Debug tests
- name: Build
run: cmake --build build --config "${{matrix.config}}" --verbose
- name: Run tests
working-directory: build
run: ctest -C Debug --output-on-failure
- name: Build Release
run: cmake --build build --config Release --verbose
- name: Run Release tests
working-directory: build
run: ctest -C Release --output-on-failure
run: ctest -C "${{matrix.config}}" --output-on-failure
4 changes: 3 additions & 1 deletion include/ada/checkers.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ ada_really_inline bool begins_with(std::string_view view,
std::string_view prefix);

/**
* Returns true if an input is an ipv4 address.
* Returns true if an input is an ipv4 address. It is assumed that the string
* does not contain uppercase ASCII characters (the input should have been
* lowered cased before calling this function) and is not empty.
*/
ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept;

Expand Down
50 changes: 40 additions & 10 deletions src/checkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,55 @@
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) {
// The string is not empty and does not contain upper case ASCII characters.
//
// Optimization. To be considered as a possible ipv4, the string must end
// with 'x' or a lowercase hex character.
// Most of the time, this will be false so this simple check will save a lot
// of effort.
char last_char = view.back();
// If the address ends with a dot, we need to prune it (special case).
if (last_char == '.') {
view.remove_suffix(1);
last_dot = view.rfind('.');
if (view.empty()) {
return false;
}
last_char = view.back();
}
std::string_view number =
(last_dot == std::string_view::npos) ? view : view.substr(last_dot + 1);
if (number.empty()) {
bool possible_ipv4 = (last_char >= '0' && last_char <= '9') ||
(last_char >= 'a' && last_char <= 'f') ||
last_char == 'x';
if (!possible_ipv4) {
return false;
}
// From the last character, find the last dot.
size_t last_dot = view.rfind('.');
if (last_dot != std::string_view::npos) {
// We have at least one dot.
view = view.substr(last_dot + 1);
}
/** 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;
}
// It could be hex (0x), but not if there is a single character.
if (view.size() == 1) {
return false;
}
// It must start with 0x.
if (!std::equal(view.begin(), view.begin() + 2, "0x")) {
return false;
}
// We must allow "0x".
if (view.size() == 2) {
return true;
}
return (checkers::has_hex_prefix(number) &&
std::all_of(number.begin() + 2, number.end(),
ada::unicode::is_lowercase_hex));
// We have 0x followed by some characters, we need to check that they are
// hexadecimals.
return 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 309c285

Please sign in to comment.