13 #include <flatmap.hpp> 15 constexpr
auto target_pattern = ctll::fixed_string{R
"(^($|/[^#?\s]+)?(.*?)?(#[A-Za-z_\-]+)?$)"}; 17 static constexpr
auto target_match(std::string_view sv) noexcept {
return ctre::match<target_pattern>(sv); }
19 constexpr
auto target_queries = ctll::fixed_string{R
"([&;]?([^=&;]+)=?([^=&;]*))"}; 22 #ifndef HE_WA_CTRE_URL_SPLIT 23 constexpr
auto url_pattern = ctll::fixed_string{R
"(^(?:(http[s]?|ftp)://)?([^/]+?)(?::(\d+))?(?:$|/)([^#?\s]+)?(.*?)?(#[A-Za-z_\-]+)?$)"}; 25 static constexpr
auto url_match(std::string_view sv) noexcept {
return ctre::match<url_pattern>(sv); }
29 [[deprecated]] constexpr
auto url_pat_split1 = ctll::fixed_string{R
"(^(?:(http[s]?|ftp)://)?([^/:]+)(.+)$)"}; 30 [[deprecated]] constexpr auto url_pat_split2 = ctll::fixed_string{R
"(^(?::(\d+))?(?:$|/)([^#?\s]+)?(.*?)?(#[A-Za-z_\-]+)?$)"}; 32 [[deprecated]] static constexpr
auto url_match_split1(std::string_view sv) noexcept {
return ctre::match<url_pat_split1>(sv); }
33 [[deprecated]]
static constexpr
auto url_match_split2(std::string_view sv) noexcept {
return ctre::match<url_pat_split2>(sv); }
38 std::string_view
url{};
42 flatmap<std::string_view, std::string_view>
queries = {};
44 constexpr std::string_view
match() noexcept {
46 auto [s_all, s_path, s_query, s_fragment] = res;
53 for (
auto [s_match, s_val] : ctre::range<target_path_parts>(
path))
54 path_parts.emplace_back(s_val);
55 if (sv_query.length() > 0)
56 sv_query.remove_prefix(1);
57 for (
auto [s_match, s_name, s_value] : ctre::range<target_queries>(sv_query))
58 queries.emplace(s_name, s_value);
78 [[nodiscard]] constexpr std::string_view
getPath() const noexcept {
return path; }
80 [[nodiscard]] constexpr std::string_view
getURL() const noexcept {
return url; }
82 [[nodiscard]] constexpr
const flatmap<std::string_view, std::string_view>&
getQueries() const noexcept {
return queries; }
85 [[nodiscard]] constexpr std::string_view
operator[](std::string_view key)
const noexcept {
86 if (
auto it = queries.find(key); it != queries.end())
91 [[nodiscard]] constexpr std::optional<bool>
getBool(std::string_view key)
const noexcept {
92 const auto val = (*this)[key];
93 if (val.data() ==
nullptr)
97 if (val ==
"1" || val ==
"yes" || val ==
"true")
99 if (val ==
"0" || val ==
"no" || val ==
"false")
107 std::string_view scheme;
108 std::string_view host;
109 unsigned short port{0};
111 void parse_all() noexcept {
113 #ifndef HE_WA_CTRE_URL_SPLIT 114 auto [s_all, s_scheme, s_host, s_port, s_path, s_query, s_fragment] =
url_match(
url);
116 auto [s_all1, s_scheme, s_host, s_next] = url_match_split1(
url);
117 auto [s_all2, s_port, s_path, s_query, s_fragment] = url_match_split2(s_next.to_view());
122 port = std::strtoul(s_port.to_view().data(),
nullptr, 10);
154 [[nodiscard]] constexpr std::string_view
getScheme() const noexcept {
return scheme; }
156 [[nodiscard]] constexpr std::string_view
getHost() const noexcept {
return host; }
161 [[nodiscard]] constexpr
unsigned short getPort() const noexcept {
return port; }
168 const auto it =
cexpr::find(sv.begin(), sv.end(),
',');
169 sv.remove_prefix(it != sv.end() ? std::distance(sv.begin(), it) + 1 : sv.size());
172 [[nodiscard]] constexpr std::string_view
operator*() const noexcept {
174 while (!csv.empty() && csv[0] ==
',')
175 csv.remove_prefix(1);
176 const auto it =
cexpr::find(csv.begin(), csv.end(),
',');
177 return sv.substr(0, std::distance(sv.begin(), it));
181 [[nodiscard]] constexpr
CSVIterator end() const noexcept {
return {sv.substr(sv.size(), 0)}; }
URLParser(std::string_view uri) noexcept
Definition: urlparser.hpp:130
std::string_view url
Definition: urlparser.hpp:38
void parse() noexcept
Definition: urlparser.hpp:61
flatmap< std::string_view, std::string_view > queries
Definition: urlparser.hpp:42
constexpr const flatmap< std::string_view, std::string_view > & getQueries() const noexcept
Definition: urlparser.hpp:82
constexpr std::string_view match() noexcept
Definition: urlparser.hpp:44
constexpr std::string_view getPath() const noexcept
Definition: urlparser.hpp:78
Definition: urlparser.hpp:164
Definition: urlparser.hpp:105
static constexpr auto url_match(std::string_view sv) noexcept
Definition: urlparser.hpp:25
TargetParser(std::string_view url)
Definition: urlparser.hpp:66
std::string_view path
Definition: urlparser.hpp:39
constexpr auto url_pattern
Definition: urlparser.hpp:23
URLParser(const char *uri) noexcept
Definition: urlparser.hpp:128
constexpr bool operator!=(const CSVIterator &oth) const noexcept
Definition: urlparser.hpp:180
constexpr std::string_view getScheme() const noexcept
Definition: urlparser.hpp:154
constexpr std::string_view operator[](std::string_view key) const noexcept
Definition: urlparser.hpp:85
void setURL(const char *v)
Definition: urlparser.hpp:68
constexpr std::string_view operator*() const noexcept
Definition: urlparser.hpp:172
constexpr CSVIterator & operator++() noexcept
Definition: urlparser.hpp:167
void parse_queries(std::string_view sv_query) noexcept
Definition: urlparser.hpp:51
constexpr auto target_path_parts
Definition: urlparser.hpp:20
constexpr auto target_queries
Definition: urlparser.hpp:19
std::vector< std::string_view > path_parts
Definition: urlparser.hpp:41
constexpr const std::vector< std::string_view > & getPathParts() const noexcept
Definition: urlparser.hpp:83
constexpr InputIt find(InputIt first, InputIt last, const T &value)
Definition: cexpr_algs.hpp:4
constexpr std::string_view getHost() const noexcept
Definition: urlparser.hpp:156
void setURL(std::string_view sv)
Definition: urlparser.hpp:149
Definition: urlparser.hpp:36
static constexpr auto target_match(std::string_view sv) noexcept
Definition: urlparser.hpp:17
constexpr CSVIterator end() const noexcept
Definition: urlparser.hpp:181
constexpr unsigned short getPort() const noexcept
Definition: urlparser.hpp:161
std::string_view sv
Definition: urlparser.hpp:165
constexpr TargetParser()=default
constexpr auto target_pattern
Definition: urlparser.hpp:15
constexpr bool operator==(const CSVIterator &oth) const noexcept
Definition: urlparser.hpp:179
constexpr std::optional< bool > getBool(std::string_view key) const noexcept
Definition: urlparser.hpp:91
void setURL(std::string_view sv)
Definition: urlparser.hpp:73
constexpr std::string_view getURL() const noexcept
Definition: urlparser.hpp:80
void setURL(const char *v)
Definition: urlparser.hpp:144