Skip to content

Commit 56bda71

Browse files
Daniel Lemireanonrig
authored andcommitted
Move url_search_params to all-inline
1 parent 4a7eced commit 56bda71

File tree

4 files changed

+62
-74
lines changed

4 files changed

+62
-74
lines changed

include/ada/url_search_params-inl.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,60 @@ inline bool url_search_params::has(const std::string_view key) noexcept {
5454
return entry != params.end();
5555
}
5656

57+
inline std::string url_search_params::to_string() {
58+
std::string out{};
59+
for (size_t i = 0; i < params.size(); i++) {
60+
auto [key, value] = params[i];
61+
62+
if (i != 0) {
63+
out += "&";
64+
}
65+
out.append(key);
66+
out += "=";
67+
out.append(value);
68+
}
69+
return out;
70+
}
71+
72+
inline void url_search_params::set(const std::string_view key,
73+
const std::string_view value) {
74+
const auto find = [&key](auto &param) { return param.first == key; };
75+
76+
auto it = std::find_if(params.begin(), params.end(), find);
77+
78+
if (it == params.end()) {
79+
params.emplace_back(key, value);
80+
} else {
81+
it->second = value;
82+
params.erase(std::remove_if(std::next(it), params.end(), find),
83+
params.end());
84+
}
85+
}
86+
87+
inline void url_search_params::remove(const std::string_view key) {
88+
params.erase(
89+
std::remove_if(params.begin(), params.end(),
90+
[&key](auto &param) { return param.first == key; }),
91+
params.end());
92+
}
93+
94+
inline void url_search_params::remove(const std::string_view key,
95+
const std::string_view value) {
96+
params.erase(std::remove_if(params.begin(), params.end(),
97+
[&key, &value](auto &param) {
98+
return param.first == key &&
99+
param.second == value;
100+
}),
101+
params.end());
102+
}
103+
104+
inline void url_search_params::sort() {
105+
std::stable_sort(params.begin(), params.end(),
106+
[](const key_value_pair &lhs, const key_value_pair &rhs) {
107+
return lhs.first < rhs.first;
108+
});
109+
}
110+
57111
} // namespace ada
58112

59113
#endif // ADA_URL_SEARCH_PARAMS_INL_H

include/ada/url_search_params.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
#ifndef ADA_URL_SEARCH_PARAMS_H
66
#define ADA_URL_SEARCH_PARAMS_H
77

8-
namespace ada {
9-
108
#include <optional>
119
#include <string>
1210
#include <string_view>
1311
#include <vector>
1412

13+
namespace ada {
14+
1515
/**
1616
* @see https://url.spec.whatwg.org/#interface-urlsearchparams
1717
*/
@@ -34,8 +34,8 @@ struct url_search_params {
3434
/**
3535
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-delete
3636
*/
37-
void remove(std::string_view key);
38-
void remove(std::string_view key, std::string_view value);
37+
inline void remove(std::string_view key);
38+
inline void remove(std::string_view key, std::string_view value);
3939

4040
/**
4141
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-get
@@ -55,17 +55,17 @@ struct url_search_params {
5555
/**
5656
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-set
5757
*/
58-
void set(std::string_view key, std::string_view value);
58+
inline void set(std::string_view key, std::string_view value);
5959

6060
/**
6161
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-sort
6262
*/
63-
void sort();
63+
inline void sort();
6464

6565
/**
6666
* @see https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
6767
*/
68-
std::string to_string();
68+
inline std::string to_string();
6969

7070
private:
7171
typedef std::pair<std::string, std::string> key_value_pair;

src/ada.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
#include "parser.cpp"
1111
#include "url_components.cpp"
1212
#include "url_aggregator.cpp"
13-
#include "ada_c.cpp"
14-
#include "url_search_params.cpp"
13+
#include "ada_c.cpp"

src/url_search_params.cpp

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)