-
-
Notifications
You must be signed in to change notification settings - Fork 14
Implement better URI query parsing support #2369
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
+646
−50
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #include <sourcemeta/core/uri.h> | ||
|
|
||
| #include <cstddef> // std::size_t, std::string_view::npos | ||
| #include <optional> // std::optional | ||
| #include <string_view> // std::string_view | ||
| #include <utility> // std::pair | ||
|
|
||
| namespace { | ||
|
|
||
| auto parse_pair(const std::string_view raw, const std::size_t pair_start) | ||
| -> std::pair<std::string_view, std::string_view> { | ||
| const auto separator{raw.find('&', pair_start)}; | ||
| const auto pair_end{separator == std::string_view::npos ? raw.size() | ||
| : separator}; | ||
| const auto pair_view{raw.substr(pair_start, pair_end - pair_start)}; | ||
| const auto equals_index{pair_view.find('=')}; | ||
| if (equals_index == std::string_view::npos) { | ||
| return {pair_view, std::string_view{}}; | ||
| } | ||
| return {pair_view.substr(0, equals_index), | ||
| pair_view.substr(equals_index + 1)}; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace sourcemeta::core { | ||
|
|
||
| URI::Query::Query(const std::string_view raw) : raw_{raw} {} | ||
|
|
||
| auto URI::Query::raw() const -> std::string_view { return this->raw_; } | ||
|
|
||
| auto URI::query() const -> std::optional<URI::Query> { | ||
| if (!this->query_.has_value()) { | ||
| return std::nullopt; | ||
| } | ||
| return URI::Query{this->query_.value()}; | ||
| } | ||
|
|
||
| URI::Query::const_iterator::const_iterator(const std::string_view raw, | ||
| const std::size_t pair_start) | ||
| : raw_{raw}, pair_start_{pair_start} { | ||
| if (this->pair_start_ != std::string_view::npos) { | ||
| this->current_ = parse_pair(this->raw_, this->pair_start_); | ||
| } | ||
| } | ||
|
|
||
| auto URI::Query::const_iterator::operator*() const -> reference { | ||
|
jviotti marked this conversation as resolved.
|
||
| return this->current_; | ||
| } | ||
|
|
||
| auto URI::Query::const_iterator::operator->() const -> pointer { | ||
| return &this->current_; | ||
| } | ||
|
|
||
| auto URI::Query::const_iterator::operator++() -> const_iterator & { | ||
| const auto separator{this->raw_.find('&', this->pair_start_)}; | ||
| if (separator == std::string_view::npos) { | ||
| this->pair_start_ = std::string_view::npos; | ||
| } else { | ||
| this->pair_start_ = separator + 1; | ||
| this->current_ = parse_pair(this->raw_, this->pair_start_); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| auto URI::Query::const_iterator::operator++(int) -> const_iterator { | ||
| auto previous{*this}; | ||
| ++(*this); | ||
| return previous; | ||
| } | ||
|
|
||
| auto URI::Query::const_iterator::operator==(const const_iterator &other) const | ||
| -> bool { | ||
| return this->pair_start_ == other.pair_start_; | ||
|
jviotti marked this conversation as resolved.
|
||
| } | ||
|
|
||
| auto URI::Query::const_iterator::operator!=(const const_iterator &other) const | ||
| -> bool { | ||
| return !(*this == other); | ||
| } | ||
|
|
||
| auto URI::Query::begin() const -> const_iterator { | ||
| if (this->raw_.empty()) { | ||
| return this->end(); | ||
| } | ||
| return const_iterator{this->raw_, 0}; | ||
| } | ||
|
|
||
| auto URI::Query::end() const -> const_iterator { | ||
| return const_iterator{this->raw_, std::string_view::npos}; | ||
| } | ||
|
|
||
| // First-wins on duplicates, matching WHATWG `URLSearchParams.get()` | ||
| // and most major URL libraries | ||
| auto URI::Query::at(const std::string_view name) const | ||
| -> std::optional<std::string_view> { | ||
| for (const auto &pair : *this) { | ||
| if (pair.first == name) { | ||
| return pair.second; | ||
| } | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| } // namespace sourcemeta::core | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
P2: Use
#pragma warning(push/pop)instead ofwarning(default: 4251)in this header to avoid leaking warning-state changes to includers.Prompt for AI agents
Tip: Review your code locally with the cubic CLI to iterate faster.