-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,63 @@ | ||
/** | ||
* @file url_search_params-inl.h | ||
* @brief Inline declarations for the URL Search Params | ||
*/ | ||
#ifndef ADA_URL_SEARCH_PARAMS_INL_H | ||
#define ADA_URL_SEARCH_PARAMS_INL_H | ||
|
||
#include "ada.h" | ||
#include "ada/url_search_params.h" | ||
|
||
#include <algorithm> | ||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace ada { | ||
|
||
inline void url_search_params::append(const std::string_view key, | ||
const std::string_view value) { | ||
params.emplace_back(std::string(key), std::string(value)); | ||
} | ||
|
||
inline size_t url_search_params::get_size() const noexcept { | ||
return params.size(); | ||
} | ||
|
||
inline std::optional<std::string_view> url_search_params::get( | ||
const std::string_view key) { | ||
auto entry = std::find_if(params.begin(), params.end(), [&key](auto param) { | ||
return std::get<0>(param) == key; | ||
}); | ||
|
||
if (entry == params.end()) { | ||
return std::nullopt; | ||
} | ||
|
||
return std::get<1>(*entry); | ||
} | ||
|
||
inline std::vector<std::string> url_search_params::get_all( | ||
const std::string_view key) { | ||
std::vector<std::string> out{}; | ||
|
||
for (auto& param : params) { | ||
if (std::get<0>(param) == key) { | ||
out.emplace_back(std::get<1>(param)); | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
|
||
inline bool url_search_params::has(const std::string_view key) noexcept { | ||
auto entry = std::find_if(params.begin(), params.end(), [&key](auto param) { | ||
return std::get<0>(param) == key; | ||
}); | ||
return entry != params.end(); | ||
} | ||
|
||
} // namespace ada | ||
|
||
#endif // ADA_URL_SEARCH_PARAMS_INL_H |
This file contains 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,72 @@ | ||
/** | ||
* @file url_search_params.h | ||
* @brief Declaration for the URL Search Params | ||
*/ | ||
#ifndef ADA_URL_SEARCH_PARAMS_H | ||
#define ADA_URL_SEARCH_PARAMS_H | ||
|
||
namespace ada { | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#interface-urlsearchparams | ||
*/ | ||
struct url_search_params { | ||
url_search_params() = default; | ||
url_search_params(const url_search_params &u) = default; | ||
url_search_params(url_search_params &&u) noexcept = default; | ||
url_search_params &operator=(url_search_params &&u) noexcept = default; | ||
url_search_params &operator=(const url_search_params &u) = default; | ||
~url_search_params() = default; | ||
|
||
[[nodiscard]] inline size_t get_size() const noexcept; | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-append | ||
*/ | ||
inline void append(std::string_view key, std::string_view value); | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-delete | ||
*/ | ||
inline void remove(std::string_view key); | ||
inline void remove(std::string_view key, std::string_view value); | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-get | ||
*/ | ||
inline std::optional<std::string_view> get(std::string_view key); | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-getall | ||
*/ | ||
inline std::vector<std::string> get_all(std::string_view key); | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-has | ||
*/ | ||
inline bool has(std::string_view key) noexcept; | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-set | ||
*/ | ||
inline void set(std::string_view key, std::string_view value); | ||
|
||
/** | ||
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-sort | ||
*/ | ||
inline void sort() const noexcept; | ||
|
||
inline std::string to_string(); | ||
|
||
private: | ||
uint32_t size{0}; | ||
std::vector<std::tuple<std::string, std::string>> params{}; | ||
}; // url_search_params | ||
|
||
} // namespace ada | ||
#endif |
This file contains 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 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,62 @@ | ||
#include "ada.h" | ||
#include "ada/url_search_params.h" | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace ada { | ||
|
||
inline void url_search_params::remove(const std::string_view key) { | ||
params.erase( | ||
std::remove_if(params.begin(), params.end(), | ||
[&key](auto param) { return std::get<0>(param) == key; }), | ||
params.end()); | ||
} | ||
|
||
inline void url_search_params::remove(const std::string_view key, | ||
std::string_view value) { | ||
params.erase(std::remove_if(params.begin(), params.end(), | ||
[&key, &value](auto param) { | ||
return std::get<0>(param) == key && | ||
std::get<1>(param) == value; | ||
}), | ||
params.end()); | ||
} | ||
|
||
inline void url_search_params::sort() const noexcept { | ||
// TODO: Implement this | ||
} | ||
|
||
inline void url_search_params::set(const std::string_view key, | ||
const std::string_view value) { | ||
params.erase( | ||
std::remove_if(params.begin(), params.end(), | ||
[&key](auto param) { return std::get<0>(param) == key; }), | ||
params.end()); | ||
|
||
params.emplace_back(std::string(key), std::string(value)); | ||
} | ||
|
||
inline std::string url_search_params::to_string() { | ||
if (params.empty()) { | ||
return ""; | ||
} | ||
std::string out = "?"; | ||
for (size_t i = 0; i < params.size(); i++) { | ||
auto param = params[i]; | ||
auto key = std::get<0>(param); | ||
auto value = std::get<1>(param); | ||
|
||
if (i != 0) { | ||
out += "&"; | ||
} | ||
out.append(key); | ||
out += "="; | ||
out.append(value); | ||
} | ||
return out; | ||
} | ||
|
||
} // namespace ada |
This file contains 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 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 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,13 @@ | ||
#include "ada.h" | ||
#include "gtest/gtest.h" | ||
|
||
TEST(url_search_params, append) { | ||
auto search_params = ada::url_search_params(); | ||
search_params.append("key", "value"); | ||
ASSERT_EQ(search_params.get_size(), 1); | ||
ASSERT_TRUE(search_params.has("key")); | ||
search_params.append("key", "value2"); | ||
ASSERT_EQ(search_params.get_size(), 2); | ||
ASSERT_EQ(search_params.get_all("key").size(), 2); | ||
SUCCEED(); | ||
} |