Loading...
Searching...
No Matches
url_search_params.h
Go to the documentation of this file.
#ifndef ADA_URL_SEARCH_PARAMS_H
#define ADA_URL_SEARCH_PARAMS_H
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace ada {
enum class url_search_params_iter_type {
KEYS,
VALUES,
ENTRIES,
};
template <typename T, url_search_params_iter_type Type>
struct url_search_params_iter;
typedef std::pair<std::string_view, std::string_view> key_value_view_pair;
using url_search_params_keys_iter =
url_search_params_iter<std::string_view, url_search_params_iter_type::KEYS>;
using url_search_params_values_iter =
url_search_params_iter<std::string_view,
url_search_params_iter_type::VALUES>;
using url_search_params_entries_iter =
url_search_params_iter<key_value_view_pair,
url_search_params_iter_type::ENTRIES>;
struct url_search_params {
url_search_params() = default;
explicit url_search_params(const std::string_view input) {
initialize(input);
}
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 size() const noexcept;
inline void append(std::string_view key, std::string_view value);
inline void remove(std::string_view key);
inline void remove(std::string_view key, std::string_view value);
inline std::optional<std::string_view> get(std::string_view key);
inline std::vector<std::string> get_all(std::string_view key);
inline bool has(std::string_view key) noexcept;
inline bool has(std::string_view key, std::string_view value) noexcept;
inline void set(std::string_view key, std::string_view value);
inline void sort();
inline std::string to_string() const;
inline url_search_params_keys_iter get_keys();
inline url_search_params_values_iter get_values();
inline url_search_params_entries_iter get_entries();
inline auto begin() const { return params.begin(); }
inline auto end() const { return params.end(); }
inline auto front() const { return params.front(); }
inline auto back() const { return params.back(); }
inline auto operator[](size_t index) const { return params[index]; }
void reset(std::string_view input);
private:
typedef std::pair<std::string, std::string> key_value_pair;
std::vector<key_value_pair> params{};
void initialize(std::string_view init);
template <typename T, url_search_params_iter_type Type>
friend struct url_search_params_iter;
}; // url_search_params
template <typename T, url_search_params_iter_type Type>
struct url_search_params_iter {
inline url_search_params_iter() : params(EMPTY) {}
url_search_params_iter(const url_search_params_iter &u) = default;
url_search_params_iter(url_search_params_iter &&u) noexcept = default;
url_search_params_iter &operator=(url_search_params_iter &&u) noexcept =
default;
url_search_params_iter &operator=(const url_search_params_iter &u) = default;
~url_search_params_iter() = default;
inline std::optional<T> next();
inline bool has_next() const;
private:
static url_search_params EMPTY;
inline url_search_params_iter(url_search_params &params_) : params(params_) {}
url_search_params &params;
size_t pos = 0;
friend struct url_search_params;
};
} // namespace ada
#endif
ada
Definition ada_idna.h:13
ada::url_search_params_values_iter
url_search_params_iter< std::string_view, url_search_params_iter_type::VALUES > url_search_params_values_iter
Definition url_search_params.h:40
ada::url_search_params_entries_iter
url_search_params_iter< key_value_view_pair, url_search_params_iter_type::ENTRIES > url_search_params_entries_iter
Definition url_search_params.h:44
ada::url_search_params_keys_iter
url_search_params_iter< std::string_view, url_search_params_iter_type::KEYS > url_search_params_keys_iter
Definition url_search_params.h:37
ada::url_search_params_iter_type
url_search_params_iter_type
Iterator types for url_search_params iteration.
Definition url_search_params.h:24
ada::url_search_params_iter_type::ENTRIES
@ ENTRIES
Definition url_search_params.h:27
ada::url_search_params_iter_type::KEYS
@ KEYS
Definition url_search_params.h:25
ada::url_search_params_iter_type::VALUES
@ VALUES
Definition url_search_params.h:26
ada::key_value_view_pair
std::pair< std::string_view, std::string_view > key_value_view_pair
Definition url_search_params.h:34
ada::url_search_params_iter
JavaScript-style iterator for url_search_params.
Definition url_search_params.h:224
ada::url_search_params_iter< std::string_view, url_search_params_iter_type::KEYS >::url_search_params
friend struct url_search_params
Definition url_search_params.h:252
ada::url_search_params_iter::url_search_params_iter
url_search_params_iter(const url_search_params_iter &u)=default
ada::url_search_params_iter::url_search_params_iter
url_search_params_iter()
Definition url_search_params.h:225
ada::url_search_params_iter::operator=
url_search_params_iter & operator=(url_search_params_iter &&u) noexcept=default
ada::url_search_params_iter::next
std::optional< T > next()
ada::url_search_params_iter::url_search_params_iter
url_search_params_iter(url_search_params_iter &&u) noexcept=default
ada::url_search_params_iter::has_next
bool has_next() const
Definition url_search_params-inl.h:263
ada::url_search_params_iter::~url_search_params_iter
~url_search_params_iter()=default
ada::url_search_params_iter::operator=
url_search_params_iter & operator=(const url_search_params_iter &u)=default
ada::url_search_params
Class for parsing and manipulating URL query strings.
Definition url_search_params.h:60
ada::url_search_params::set
void set(std::string_view key, std::string_view value)
Definition url_search_params-inl.h:137
ada::url_search_params::begin
auto begin() const
Definition url_search_params.h:184
ada::url_search_params::end
auto end() const
Definition url_search_params.h:185
ada::url_search_params::get_all
std::vector< std::string > get_all(std::string_view key)
Definition url_search_params-inl.h:89
ada::url_search_params::front
auto front() const
Definition url_search_params.h:186
ada::url_search_params::back
auto back() const
Definition url_search_params.h:187
ada::url_search_params::remove
void remove(std::string_view key)
Definition url_search_params-inl.h:152
ada::url_search_params::url_search_params
url_search_params(const url_search_params &u)=default
ada::url_search_params::url_search_params
url_search_params(url_search_params &&u) noexcept=default
ada::url_search_params::url_search_params_iter
friend struct url_search_params_iter
Definition url_search_params.h:209
ada::url_search_params::operator[]
auto operator[](size_t index) const
Definition url_search_params.h:188
ada::url_search_params::get_entries
url_search_params_entries_iter get_entries()
Definition url_search_params-inl.h:258
ada::url_search_params::url_search_params
url_search_params()=default
ada::url_search_params::to_string
std::string to_string() const
Definition url_search_params-inl.h:116
ada::url_search_params::operator=
url_search_params & operator=(url_search_params &&u) noexcept=default
ada::url_search_params::get_keys
url_search_params_keys_iter get_keys()
Definition url_search_params-inl.h:244
ada::url_search_params::size
size_t size() const noexcept
Definition url_search_params-inl.h:75
ada::url_search_params::sort
void sort()
Definition url_search_params-inl.h:164
ada::url_search_params::operator=
url_search_params & operator=(const url_search_params &u)=default
ada::url_search_params::append
void append(std::string_view key, std::string_view value)
Definition url_search_params-inl.h:70
ada::url_search_params::url_search_params
url_search_params(const std::string_view input)
Definition url_search_params.h:67
ada::url_search_params::get_values
url_search_params_values_iter get_values()
Definition url_search_params-inl.h:251
ada::url_search_params::~url_search_params
~url_search_params()=default
ada::url_search_params::get
std::optional< std::string_view > get(std::string_view key)
Definition url_search_params-inl.h:77
ada::url_search_params::has
bool has(std::string_view key) noexcept
Definition url_search_params-inl.h:102