Skip to content

Commit a1ac234

Browse files
authored
refactor base prefix and rename fragment to hash (#335)
* refactor: remove `_base_` from function names * refactor: rename fragment as hash * fix: apply suggestions
1 parent b1b4513 commit a1ac234

File tree

13 files changed

+81
-90
lines changed

13 files changed

+81
-90
lines changed

include/ada/helpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ void encode_json(std::string_view view, out_iter out);
3030
* This function is used to prune a fragment from a url, and returning the
3131
* removed string if input has fragment.
3232
*
33-
* @details prune_fragment seeks the first '#' and returns everything after it
33+
* @details prune_hash seeks the first '#' and returns everything after it
3434
* as a string_view, and modifies (in place) the input so that it points at
3535
* everything before the '#'. If no '#' is found, the input is left unchanged
3636
* and std::nullopt is returned.
3737
*
3838
* @attention The function is non-allocating and it does not throw.
3939
* @returns Note that the returned string_view might be empty!
4040
*/
41-
ada_really_inline std::optional<std::string_view> prune_fragment(
41+
ada_really_inline std::optional<std::string_view> prune_hash(
4242
std::string_view& input) noexcept;
4343

4444
/**

include/ada/url-inl.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ size_t url::get_pathname_length() const noexcept { return path.size(); }
105105
}
106106
}
107107

108-
if (fragment.has_value()) {
108+
if (hash.has_value()) {
109109
out.hash_start = uint32_t(running_index);
110110
}
111111

@@ -116,8 +116,8 @@ inline void url::update_base_hostname(std::string_view input) { host = input; }
116116

117117
inline void url::update_unencoded_base_hash(std::string_view input) {
118118
// We do the percent encoding
119-
fragment = unicode::percent_encode(
120-
input, ada::character_sets::FRAGMENT_PERCENT_ENCODE);
119+
hash = unicode::percent_encode(input,
120+
ada::character_sets::FRAGMENT_PERCENT_ENCODE);
121121
}
122122

123123
inline void url::update_base_search(std::string_view input,
@@ -145,15 +145,13 @@ inline void url::update_base_port(std::optional<uint16_t> input) {
145145
port = input;
146146
}
147147

148-
inline void url::clear_base_pathname() { path = ""; }
148+
inline void url::clear_pathname() { path.clear(); }
149149

150-
inline void url::clear_base_search() { query = std::nullopt; }
150+
inline void url::clear_search() { query = std::nullopt; }
151151

152-
inline bool url::base_fragment_has_value() const {
153-
return fragment.has_value();
154-
}
152+
inline bool url::has_hash() const { return hash.has_value(); }
155153

156-
inline bool url::base_search_has_value() const { return query.has_value(); }
154+
inline bool url::has_search() const { return query.has_value(); }
157155

158156
inline void url::set_protocol_as_file() { type = ada::scheme::type::FILE; }
159157

@@ -201,8 +199,8 @@ inline void url::copy_scheme(const ada::url &u) {
201199
if (query.has_value()) {
202200
output += "?" + query.value();
203201
}
204-
if (fragment.has_value()) {
205-
output += "#" + fragment.value();
202+
if (hash.has_value()) {
203+
output += "#" + hash.value();
206204
}
207205
return output;
208206
}

include/ada/url.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct url : url_base {
8989
* further processing on the resource the URL’s other components identify. It
9090
* is initially null.
9191
*/
92-
std::optional<std::string> fragment{};
92+
std::optional<std::string> hash{};
9393

9494
/** @private */
9595
inline void update_unencoded_base_hash(std::string_view input);
@@ -111,13 +111,13 @@ struct url : url_base {
111111
/** @private */
112112
inline void update_base_port(std::optional<uint16_t> input);
113113
/** @private */
114-
inline void clear_base_pathname() override;
114+
inline void clear_pathname() override;
115115
/** @private */
116-
inline void clear_base_search() override;
116+
inline void clear_search() override;
117117
/** @private */
118-
inline bool base_fragment_has_value() const override;
118+
inline bool has_hash() const override;
119119
/** @private */
120-
inline bool base_search_has_value() const override;
120+
inline bool has_search() const override;
121121
/** @private set this URL's type to file */
122122
inline void set_protocol_as_file();
123123
/** @return true if it has an host but it is the empty string */

include/ada/url_aggregator-inl.h

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ inline void url_aggregator::update_base_search(std::string_view input) {
174174
ADA_ASSERT_TRUE(validate());
175175
ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
176176
if (input.empty()) {
177-
clear_base_search();
177+
clear_search();
178178
return;
179179
}
180180

@@ -412,9 +412,8 @@ inline void url_aggregator::append_base_username(const std::string_view input) {
412412
ADA_ASSERT_TRUE(validate());
413413
}
414414

415-
inline void url_aggregator::clear_base_password() {
416-
ada_log("url_aggregator::clear_base_password ", to_string(), "\n",
417-
to_diagram());
415+
inline void url_aggregator::clear_password() {
416+
ada_log("url_aggregator::clear_password ", to_string(), "\n", to_diagram());
418417
ADA_ASSERT_TRUE(validate());
419418
if (!has_password()) {
420419
return;
@@ -442,7 +441,7 @@ inline void url_aggregator::update_base_password(const std::string_view input) {
442441

443442
// TODO: Optimization opportunity. Merge the following removal functions.
444443
if (input.empty()) {
445-
clear_base_password();
444+
clear_password();
446445

447446
// Remove username too, if it is empty.
448447
if (!has_non_empty_username()) {
@@ -543,7 +542,7 @@ inline void url_aggregator::update_base_port(uint32_t input) {
543542
ada_log("url_aggregator::update_base_port");
544543
ADA_ASSERT_TRUE(validate());
545544
if (input == url_components::omitted) {
546-
clear_base_port();
545+
clear_port();
547546
return;
548547
}
549548
// calling std::to_string(input.value()) is unfortunate given that the port
@@ -569,8 +568,8 @@ inline void url_aggregator::update_base_port(uint32_t input) {
569568
ADA_ASSERT_TRUE(validate());
570569
}
571570

572-
inline void url_aggregator::clear_base_port() {
573-
ada_log("url_aggregator::clear_base_port");
571+
inline void url_aggregator::clear_port() {
572+
ada_log("url_aggregator::clear_port");
574573
ADA_ASSERT_TRUE(validate());
575574
if (components.port == url_components::omitted) {
576575
return;
@@ -593,8 +592,8 @@ inline uint32_t url_aggregator::retrieve_base_port() const {
593592
return components.port;
594593
}
595594

596-
inline void url_aggregator::clear_base_search() {
597-
ada_log("url_aggregator::clear_base_search");
595+
inline void url_aggregator::clear_search() {
596+
ada_log("url_aggregator::clear_search");
598597
ADA_ASSERT_TRUE(validate());
599598
if (components.search_start == url_components::omitted) {
600599
return;
@@ -618,8 +617,8 @@ inline void url_aggregator::clear_base_search() {
618617
ADA_ASSERT_TRUE(validate());
619618
}
620619

621-
inline void url_aggregator::clear_base_hash() {
622-
ada_log("url_aggregator::clear_base_hash");
620+
inline void url_aggregator::clear_hash() {
621+
ada_log("url_aggregator::clear_hash");
623622
ADA_ASSERT_TRUE(validate());
624623
if (components.hash_start == url_components::omitted) {
625624
return;
@@ -635,8 +634,8 @@ inline void url_aggregator::clear_base_hash() {
635634
ADA_ASSERT_TRUE(validate());
636635
}
637636

638-
inline void url_aggregator::clear_base_pathname() {
639-
ada_log("url_aggregator::clear_base_pathname");
637+
inline void url_aggregator::clear_pathname() {
638+
ada_log("url_aggregator::clear_pathname");
640639
ADA_ASSERT_TRUE(validate());
641640
uint32_t ending_index = uint32_t(buffer.size());
642641
if (components.search_start != url_components::omitted) {
@@ -660,19 +659,18 @@ inline void url_aggregator::clear_base_pathname() {
660659
if (components.hash_start != url_components::omitted) {
661660
components.hash_start -= difference;
662661
}
663-
ada_log("url_aggregator::clear_base_pathname completed, running checks...");
662+
ada_log("url_aggregator::clear_pathname completed, running checks...");
664663
#if ADA_DEVELOPMENT_CHECKS
665664
ADA_ASSERT_EQUAL(get_pathname(), "",
666665
"pathname should have been cleared on buffer=" + buffer +
667666
" with " + components.to_string() + "\n" + to_diagram());
668667
#endif
669668
ADA_ASSERT_TRUE(validate());
670-
ada_log(
671-
"url_aggregator::clear_base_pathname completed, running checks... ok");
669+
ada_log("url_aggregator::clear_pathname completed, running checks... ok");
672670
}
673671

674-
inline void url_aggregator::clear_base_hostname() {
675-
ada_log("url_aggregator::clear_base_hostname");
672+
inline void url_aggregator::clear_hostname() {
673+
ada_log("url_aggregator::clear_hostname");
676674
ADA_ASSERT_TRUE(validate());
677675
if (!has_authority()) {
678676
return;
@@ -706,13 +704,13 @@ inline void url_aggregator::clear_base_hostname() {
706704
ADA_ASSERT_TRUE(validate());
707705
}
708706

709-
inline bool url_aggregator::base_fragment_has_value() const {
710-
ada_log("url_aggregator::base_fragment_has_value");
707+
inline bool url_aggregator::has_hash() const {
708+
ada_log("url_aggregator::has_hash");
711709
return components.hash_start != url_components::omitted;
712710
}
713711

714-
inline bool url_aggregator::base_search_has_value() const {
715-
ada_log("url_aggregator::base_search_has_value");
712+
inline bool url_aggregator::has_search() const {
713+
ada_log("url_aggregator::has_search");
716714
return components.search_start != url_components::omitted;
717715
}
718716

@@ -859,7 +857,7 @@ ada_really_inline size_t url_aggregator::parse_port(
859857
if (r.ec == std::errc() && scheme_default_port() != parsed_port) {
860858
update_base_port(parsed_port);
861859
} else {
862-
clear_base_port();
860+
clear_port();
863861
}
864862
}
865863
return consumed;

include/ada/url_aggregator.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,22 @@ struct url_aggregator : url_base {
202202
/** @private */
203203
inline uint32_t retrieve_base_port() const;
204204
/** @private */
205-
inline void clear_base_port();
205+
inline void clear_port();
206206
/** @private if there is no hostname, then this function does nothing, if
207207
* there is, we make it empty */
208-
inline void clear_base_hostname();
208+
inline void clear_hostname();
209209
/** @private */
210-
inline void clear_base_hash();
210+
inline void clear_hash();
211211
/** @private */
212-
inline void clear_base_pathname() override;
212+
inline void clear_pathname() override;
213213
/** @private */
214-
inline void clear_base_search() override;
214+
inline void clear_search() override;
215215
/** @private */
216-
inline void clear_base_password();
216+
inline void clear_password();
217217
/** @private */
218-
inline bool base_fragment_has_value() const override;
218+
inline bool has_hash() const override;
219219
/** @private */
220-
inline bool base_search_has_value() const override;
220+
inline bool has_search() const override;
221221
/** @private */
222222
inline bool has_dash_dot() const noexcept;
223223
/** @private */

include/ada/url_base.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ struct url_base {
9696
virtual std::string to_string() const = 0;
9797

9898
/** @private */
99-
virtual inline void clear_base_pathname() = 0;
99+
virtual inline void clear_pathname() = 0;
100100

101101
/** @private */
102-
virtual inline void clear_base_search() = 0;
102+
virtual inline void clear_search() = 0;
103103

104104
/** @private */
105-
virtual inline bool base_fragment_has_value() const = 0;
105+
virtual inline bool has_hash() const = 0;
106106

107107
/** @private */
108-
virtual inline bool base_search_has_value() const = 0;
108+
virtual inline bool has_search() const = 0;
109109

110110
}; // url_base
111111

src/helpers.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ ada_unused std::string get_state(ada::state s) {
8484
}
8585
}
8686

87-
ada_really_inline std::optional<std::string_view> prune_fragment(
87+
ada_really_inline std::optional<std::string_view> prune_hash(
8888
std::string_view& input) noexcept {
8989
// compiles down to 20--30 instructions including a class to memchr (C
9090
// function). this function should be quite fast.
9191
size_t location_of_first = input.find('#');
9292
if (location_of_first == std::string_view::npos) {
9393
return std::nullopt;
9494
}
95-
std::string_view fragment = input;
96-
fragment.remove_prefix(location_of_first + 1);
95+
std::string_view hash = input;
96+
hash.remove_prefix(location_of_first + 1);
9797
input.remove_suffix(input.size() - location_of_first);
98-
return fragment;
98+
return hash;
9999
}
100100

101101
ada_really_inline bool shorten_path(std::string& path,
@@ -581,8 +581,8 @@ ada_really_inline void strip_trailing_spaces_from_opaque_path(
581581
url_type& url) noexcept {
582582
ada_log("helpers::strip_trailing_spaces_from_opaque_path");
583583
if (!url.has_opaque_path) return;
584-
if (url.base_fragment_has_value()) return;
585-
if (url.base_search_has_value()) return;
584+
if (url.has_hash()) return;
585+
if (url.has_search()) return;
586586

587587
auto path = std::string(url.get_pathname());
588588
while (!path.empty() && path.back() == ' ') {

src/parser.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ result_type parse_url(std::string_view user_input,
8585
helpers::trim_c0_whitespace(url_data);
8686

8787
// Optimization opportunity. Most websites do not have fragment.
88-
std::optional<std::string_view> fragment = helpers::prune_fragment(url_data);
88+
std::optional<std::string_view> fragment = helpers::prune_hash(url_data);
8989
// We add it last so that an implementation like ada::url_aggregator
9090
// can append it last to its internal buffer, thus improving performance.
9191

@@ -442,7 +442,7 @@ result_type parse_url(std::string_view user_input,
442442
// Otherwise, if c is not the EOF code point:
443443
else if (input_position != input_size) {
444444
// Set url’s query to null.
445-
url.clear_base_search();
445+
url.clear_search();
446446
if constexpr (result_type_is_ada_url) {
447447
// Shorten url’s path.
448448
helpers::shorten_path(url.path, url.type);
@@ -857,7 +857,7 @@ result_type parse_url(std::string_view user_input,
857857
// Otherwise, if c is not the EOF code point:
858858
else if (input_position != input_size) {
859859
// Set url’s query to null.
860-
url.clear_base_search();
860+
url.clear_search();
861861

862862
// If the code point substring from pointer to the end of input does
863863
// not start with a Windows drive letter, then shorten url’s path.
@@ -874,11 +874,7 @@ result_type parse_url(std::string_view user_input,
874874
// Otherwise:
875875
else {
876876
// Set url’s path to an empty list.
877-
if constexpr (result_type_is_ada_url) {
878-
url.path.clear();
879-
} else {
880-
url.clear_base_pathname();
881-
}
877+
url.clear_pathname();
882878
url.has_opaque_path = true;
883879
}
884880

src/url-getters.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ namespace ada {
8585
[[nodiscard]] std::string url::get_hash() const noexcept {
8686
// If this’s URL’s fragment is either null or the empty string, then return
8787
// the empty string. Return U+0023 (#), followed by this’s URL’s fragment.
88-
return (!fragment.has_value() || (fragment.value().empty()))
89-
? ""
90-
: "#" + fragment.value();
88+
return (!hash.has_value() || (hash.value().empty())) ? ""
89+
: "#" + hash.value();
9190
}
9291

9392
} // namespace ada

src/url-setters.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ bool url::set_port(const std::string_view input) {
150150

151151
void url::set_hash(const std::string_view input) {
152152
if (input.empty()) {
153-
fragment = std::nullopt;
153+
hash = std::nullopt;
154154
helpers::strip_trailing_spaces_from_opaque_path(*this);
155155
return;
156156
}
157157

158158
std::string new_value;
159159
new_value = input[0] == '#' ? input.substr(1) : input;
160160
helpers::remove_ascii_tab_or_newline(new_value);
161-
fragment = unicode::percent_encode(
162-
new_value, ada::character_sets::FRAGMENT_PERCENT_ENCODE);
161+
hash = unicode::percent_encode(new_value,
162+
ada::character_sets::FRAGMENT_PERCENT_ENCODE);
163163
return;
164164
}
165165

@@ -225,7 +225,7 @@ bool url::set_href(const std::string_view input) {
225225
port = out->port;
226226
path = out->path;
227227
query = out->query;
228-
fragment = out->fragment;
228+
hash = out->hash;
229229
type = out->type;
230230
non_special_scheme = out->non_special_scheme;
231231
has_opaque_path = out->has_opaque_path;

0 commit comments

Comments
 (0)