Skip to content
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

Build the string directly instead of copying and then pruning tabs or newlines #536

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/ada/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ ada_really_inline void parse_prepared_path(std::string_view input,
*/
ada_really_inline void remove_ascii_tab_or_newline(std::string& input) noexcept;

/**
* @private
* Create a new string that all ASCII tab or newline characters are removed.
*/
[[nodiscard]] ada_really_inline std::string get_ascii_tab_or_newline_removed(
std::string_view input);

/**
* @private
* Return the substring from input going from index pos to the end.
Expand Down
16 changes: 13 additions & 3 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <charconv>
#include <cstring>
#include <iterator>
#include <sstream>

namespace ada::helpers {
Expand Down Expand Up @@ -155,12 +156,21 @@ ada_really_inline void remove_ascii_tab_or_newline(
// if this ever becomes a performance issue, we could use an approach similar
// to has_tabs_or_newline
input.erase(std::remove_if(input.begin(), input.end(),
[](char c) {
return ada::unicode::is_ascii_tab_or_newline(c);
}),
ada::unicode::is_ascii_tab_or_newline),
input.end());
}

ada_really_inline std::string get_ascii_tab_or_newline_removed(
std::string_view input) {
std::string res;
res.reserve(input.size());

std::copy_if(input.begin(), input.end(), std::back_insert_iterator{res},
std::not_fn(ada::unicode::is_ascii_tab_or_newline));

return res;
}

ada_really_inline std::string_view substring(std::string_view input,
size_t pos) noexcept {
ADA_ASSERT_TRUE(pos <= input.size());
Expand Down
3 changes: 1 addition & 2 deletions src/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ std::string href_from_file(std::string_view input) {
std::string tmp_buffer;
std::string_view internal_input;
if (unicode::has_tabs_or_newline(input)) {
tmp_buffer = input;
helpers::remove_ascii_tab_or_newline(tmp_buffer);
tmp_buffer = helpers::get_ascii_tab_or_newline_removed(input);
internal_input = tmp_buffer;
} else {
internal_input = input;
Expand Down
6 changes: 2 additions & 4 deletions src/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ada.h"
#include "ada/common_defs.h"
#include "ada/character_sets-inl.h"
#include "ada/helpers.h"
#include "ada/unicode.h"
#include "ada/url-inl.h"
#include "ada/log.h"
Expand Down Expand Up @@ -72,10 +73,7 @@ result_type parse_url(std::string_view user_input,
std::string tmp_buffer;
std::string_view internal_input;
if (unicode::has_tabs_or_newline(user_input)) {
tmp_buffer = user_input;
// Optimization opportunity: Instead of copying and then pruning, we could
// just directly build the string from user_input.
helpers::remove_ascii_tab_or_newline(tmp_buffer);
tmp_buffer = helpers::get_ascii_tab_or_newline_removed(user_input);
internal_input = tmp_buffer;
} else {
internal_input = user_input;
Expand Down
5 changes: 1 addition & 4 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,7 @@ ada_really_inline void url::parse_path(std::string_view input) {
std::string tmp_buffer;
std::string_view internal_input;
if (unicode::has_tabs_or_newline(input)) {
tmp_buffer = input;
// Optimization opportunity: Instead of copying and then pruning, we could
// just directly build the string from user_input.
helpers::remove_ascii_tab_or_newline(tmp_buffer);
tmp_buffer = helpers::get_ascii_tab_or_newline_removed(input);
internal_input = tmp_buffer;
} else {
internal_input = input;
Expand Down
5 changes: 1 addition & 4 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ ada_really_inline void url_aggregator::parse_path(std::string_view input) {
std::string tmp_buffer;
std::string_view internal_input;
if (unicode::has_tabs_or_newline(input)) {
tmp_buffer = input;
// Optimization opportunity: Instead of copying and then pruning, we could
// just directly build the string from user_input.
helpers::remove_ascii_tab_or_newline(tmp_buffer);
tmp_buffer = helpers::get_ascii_tab_or_newline_removed(input);
internal_input = tmp_buffer;
} else {
internal_input = input;
Expand Down
Loading