Ada 2.9.2
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_search_params-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_SEARCH_PARAMS_INL_H
6#define ADA_URL_SEARCH_PARAMS_INL_H
7
8#include "ada.h"
10#include "ada/unicode.h"
12
13#include <algorithm>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace ada {
20
21// A default, empty url_search_params for use with empty iterators.
22template <typename T, ada::url_search_params_iter_type Type>
23url_search_params url_search_params_iter<T, Type>::EMPTY;
24
25inline void url_search_params::reset(std::string_view input) {
26 params.clear();
27 initialize(input);
28}
29
30inline void url_search_params::initialize(std::string_view input) {
31 if (!input.empty() && input.front() == '?') {
32 input.remove_prefix(1);
33 }
34
35 auto process_key_value = [&](const std::string_view current) {
36 auto equal = current.find('=');
37
38 if (equal == std::string_view::npos) {
39 std::string name(current);
40 std::replace(name.begin(), name.end(), '+', ' ');
41 params.emplace_back(unicode::percent_decode(name, name.find('%')), "");
42 } else {
43 std::string name(current.substr(0, equal));
44 std::string value(current.substr(equal + 1));
45
46 std::replace(name.begin(), name.end(), '+', ' ');
47 std::replace(value.begin(), value.end(), '+', ' ');
48
49 params.emplace_back(unicode::percent_decode(name, name.find('%')),
50 unicode::percent_decode(value, value.find('%')));
51 }
52 };
53
54 while (!input.empty()) {
55 auto ampersand_index = input.find('&');
56
57 if (ampersand_index == std::string_view::npos) {
58 if (!input.empty()) {
59 process_key_value(input);
60 }
61 break;
62 } else if (ampersand_index != 0) {
63 process_key_value(input.substr(0, ampersand_index));
64 }
65
66 input.remove_prefix(ampersand_index + 1);
67 }
68}
69
70inline void url_search_params::append(const std::string_view key,
71 const std::string_view value) {
72 params.emplace_back(key, value);
73}
74
75inline size_t url_search_params::size() const noexcept { return params.size(); }
76
77inline std::optional<std::string_view> url_search_params::get(
78 const std::string_view key) {
79 auto entry = std::find_if(params.begin(), params.end(),
80 [&key](auto &param) { return param.first == key; });
81
82 if (entry == params.end()) {
83 return std::nullopt;
84 }
85
86 return entry->second;
87}
88
89inline std::vector<std::string> url_search_params::get_all(
90 const std::string_view key) {
91 std::vector<std::string> out{};
92
93 for (auto &param : params) {
94 if (param.first == key) {
95 out.emplace_back(param.second);
96 }
97 }
98
99 return out;
100}
101
102inline bool url_search_params::has(const std::string_view key) noexcept {
103 auto entry = std::find_if(params.begin(), params.end(),
104 [&key](auto &param) { return param.first == key; });
105 return entry != params.end();
106}
107
108inline bool url_search_params::has(std::string_view key,
109 std::string_view value) noexcept {
110 auto entry =
111 std::find_if(params.begin(), params.end(), [&key, &value](auto &param) {
112 return param.first == key && param.second == value;
113 });
114 return entry != params.end();
115}
116
117inline std::string url_search_params::to_string() const {
119 std::string out{};
120 for (size_t i = 0; i < params.size(); i++) {
121 auto key = ada::unicode::percent_encode(params[i].first, character_set);
122 auto value = ada::unicode::percent_encode(params[i].second, character_set);
123
124 // Performance optimization: Move this inside percent_encode.
125 std::replace(key.begin(), key.end(), ' ', '+');
126 std::replace(value.begin(), value.end(), ' ', '+');
127
128 if (i != 0) {
129 out += "&";
130 }
131 out.append(key);
132 out += "=";
133 out.append(value);
134 }
135 return out;
136}
137
138inline void url_search_params::set(const std::string_view key,
139 const std::string_view value) {
140 const auto find = [&key](auto &param) { return param.first == key; };
141
142 auto it = std::find_if(params.begin(), params.end(), find);
143
144 if (it == params.end()) {
145 params.emplace_back(key, value);
146 } else {
147 it->second = value;
148 params.erase(std::remove_if(std::next(it), params.end(), find),
149 params.end());
150 }
151}
152
153inline void url_search_params::remove(const std::string_view key) {
154 params.erase(
155 std::remove_if(params.begin(), params.end(),
156 [&key](auto &param) { return param.first == key; }),
157 params.end());
158}
159
160inline void url_search_params::remove(const std::string_view key,
161 const std::string_view value) {
162 params.erase(std::remove_if(params.begin(), params.end(),
163 [&key, &value](auto &param) {
164 return param.first == key &&
165 param.second == value;
166 }),
167 params.end());
168}
169
171 std::stable_sort(params.begin(), params.end(),
172 [](const key_value_pair &lhs, const key_value_pair &rhs) {
173 return lhs.first < rhs.first;
174 });
175}
176
180
187
194
195template <typename T, url_search_params_iter_type Type>
197 return pos < params.params.size();
198}
199
200template <>
201inline std::optional<std::string_view> url_search_params_keys_iter::next() {
202 if (!has_next()) {
203 return std::nullopt;
204 }
205 return params.params[pos++].first;
206}
207
208template <>
209inline std::optional<std::string_view> url_search_params_values_iter::next() {
210 if (!has_next()) {
211 return std::nullopt;
212 }
213 return params.params[pos++].second;
214}
215
216template <>
217inline std::optional<key_value_view_pair>
219 if (!has_next()) {
220 return std::nullopt;
221 }
222 return params.params[pos++];
223}
224
225} // namespace ada
226
227#endif // ADA_URL_SEARCH_PARAMS_INL_H
Includes all definitions for Ada.
Definitions of the character sets used by unicode functions.
constexpr uint8_t WWW_FORM_URLENCODED_PERCENT_ENCODE[32]
Definition ada_idna.h:13
url_search_params_iter< std::string_view, url_search_params_iter_type::VALUES > url_search_params_values_iter
url_search_params_iter< key_value_view_pair, url_search_params_iter_type::ENTRIES > url_search_params_entries_iter
url_search_params_iter< std::string_view, url_search_params_iter_type::KEYS > url_search_params_keys_iter
void set(std::string_view key, std::string_view value)
std::vector< std::string > get_all(std::string_view key)
void remove(std::string_view key)
url_search_params_entries_iter get_entries()
std::string to_string() const
url_search_params_keys_iter get_keys()
size_t size() const noexcept
void append(std::string_view key, std::string_view value)
url_search_params_values_iter get_values()
std::optional< std::string_view > get(std::string_view key)
bool has(std::string_view key) noexcept
Definitions for all unicode specific functions.
Declaration for the URL Search Params.