-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: improve ends_in_a_number by 25% #36
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,34 +7,55 @@ namespace ada::checkers { | |
|
||
// TODO: Refactor this to not use `std::vector` but use pointer arithmetic for performance. | ||
bool ends_in_a_number(const std::string_view input) noexcept { | ||
// Let parts be the result of strictly splitting input on U+002E (.). | ||
std::vector<std::string> parts = ada::helpers::split_string_view(input, '.', false); | ||
|
||
if (parts.empty()) { | ||
if (input.empty()) { | ||
return false; | ||
} | ||
|
||
size_t parts_count = std::count(input.begin(), input.end(), '.'); | ||
|
||
if (parts_count > 0) { parts_count++; } | ||
|
||
static const std::string delimiter = "."; | ||
std::string_view::iterator pointer_start = input.begin(); | ||
std::string_view::iterator pointer_end = input.end(); | ||
|
||
// If the last item in parts is the empty string, then: | ||
if (parts.back().empty()) { | ||
if (input.back() == '.') { | ||
// If parts’s size is 1, then return false. | ||
if (parts.size() == 1) { | ||
if (parts_count == 1) { | ||
return false; | ||
} | ||
|
||
// Remove the last item from parts. | ||
parts.pop_back(); | ||
pointer_end--; | ||
parts_count--; | ||
} | ||
|
||
if (std::distance(pointer_start, pointer_end) == 0) { | ||
return false; | ||
} | ||
|
||
// Let last be the last item in parts. | ||
std::string_view last = parts.back(); | ||
if (parts_count > 1) { | ||
pointer_start = std::find_end(pointer_start, pointer_end, delimiter.begin(), delimiter.end()); | ||
|
||
if (pointer_start == pointer_end) { | ||
return false; | ||
} | ||
|
||
pointer_start++; | ||
} | ||
|
||
if (std::distance(pointer_start, pointer_end) == 0) { | ||
return false; | ||
} | ||
|
||
// If last is non-empty and contains only ASCII digits, then return true. | ||
if (!last.empty() && std::all_of(last.begin(), last.end(), ::isdigit)) { | ||
if (std::all_of(pointer_start, pointer_end, ::isdigit)) { | ||
return true; | ||
} | ||
|
||
// If parsing last as an IPv4 number does not return failure, then return true. | ||
return ada::parser::parse_ipv4_number(last).has_value(); | ||
return is_ipv4_number_valid(std::string(pointer_start, pointer_end)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next step will be to remove this. I'm working on it. |
||
} | ||
|
||
// A Windows drive letter is two code points, of which the first is an ASCII alpha | ||
|
@@ -48,5 +69,30 @@ namespace ada::checkers { | |
return is_windows_drive_letter(input) && input[1] == ':'; | ||
} | ||
|
||
// This function assumes the input is not empty. | ||
ada_really_inline constexpr bool is_ipv4_number_valid(const std::string_view input) noexcept { | ||
// The first two code points are either "0X" or "0x", then: | ||
if (input.length() >= 2 && input[0] == '0' && (input[1] == 'X' || input[1] == 'x')) { | ||
if (input.length() == 2) { | ||
return true; | ||
} | ||
|
||
// Remove the first two code points from input. | ||
// If input contains a code point that is not a radix-R digit, then return failure. | ||
return input.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string_view::npos; | ||
} | ||
// Otherwise, if the first code point is U+0030 (0), then: | ||
else if (input[0] == '0') { | ||
if (input.length() == 1) { | ||
return true; | ||
} | ||
|
||
// Remove the first code point from input. | ||
// If input contains a code point that is not a radix-R digit, then return failure. | ||
return input.find_first_not_of("01234567", 1) == std::string_view::npos; | ||
} | ||
|
||
return std::all_of(input.begin(), input.end(), ::isdigit); | ||
} | ||
|
||
} // namespace ada::checkers |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting rid of std::vector in this code is assuredly a good step forward. Please merge.
std::vector will tend to allocate on the heap. You want to avoid that as much as possible. It is expensive in our context.