|
| 1 | +#ifndef SOURCEMETA_CORE_URLPATTERN_H_ |
| 2 | +#define SOURCEMETA_CORE_URLPATTERN_H_ |
| 3 | + |
| 4 | +#ifndef SOURCEMETA_CORE_URLPATTERN_EXPORT |
| 5 | +#include <sourcemeta/core/urlpattern_export.h> |
| 6 | +#endif |
| 7 | + |
| 8 | +// NOLINTBEGIN(misc-include-cleaner) |
| 9 | +#include <sourcemeta/core/urlpattern_error.h> |
| 10 | +#include <sourcemeta/core/urlpattern_part.h> |
| 11 | +// NOLINTEND(misc-include-cleaner) |
| 12 | + |
| 13 | +#include <compare> // std::strong_ordering |
| 14 | +#include <optional> // std::optional |
| 15 | +#include <string> // std::string |
| 16 | +#include <string_view> // std::string_view |
| 17 | +#include <unordered_map> // std::unordered_map |
| 18 | +#include <utility> // std::move |
| 19 | +#include <vector> // std::vector |
| 20 | + |
| 21 | +/// @defgroup urlpattern URL Pattern |
| 22 | +/// @brief A WHATWG URL Pattern implementation. |
| 23 | +/// |
| 24 | +/// This functionality is included as follows: |
| 25 | +/// |
| 26 | +/// ```cpp |
| 27 | +/// #include <sourcemeta/core/urlpattern.h> |
| 28 | +/// ``` |
| 29 | + |
| 30 | +namespace sourcemeta::core { |
| 31 | + |
| 32 | +/// @ingroup urlpattern |
| 33 | +struct URLPatternComponentResult { |
| 34 | + [[nodiscard]] auto size() const noexcept -> std::size_t; |
| 35 | + [[nodiscard]] auto at(const std::size_t index) const noexcept |
| 36 | + -> std::string_view; |
| 37 | + [[nodiscard]] auto at(const std::string_view name) const noexcept |
| 38 | + -> std::optional<std::string_view>; |
| 39 | + |
| 40 | + auto insert(const std::string_view value) -> void; |
| 41 | + auto insert(const std::string_view name, const std::string_view value) |
| 42 | + -> void; |
| 43 | + auto insert(const std::string_view name, const std::size_t index) -> void; |
| 44 | + |
| 45 | +private: |
| 46 | + std::vector<std::string_view> positions; |
| 47 | + std::unordered_map<std::string_view, std::size_t> names; |
| 48 | +}; |
| 49 | + |
| 50 | +/// @ingroup urlpattern |
| 51 | +struct URLPatternProtocol { |
| 52 | + URLPatternProtocol() : value{URLPatternPartAsterisk{}} {} |
| 53 | + URLPatternProtocol(const char *input); |
| 54 | + URLPatternProtocol(const URLPatternProtocol &) = default; |
| 55 | + URLPatternProtocol(URLPatternProtocol &&) noexcept = default; |
| 56 | + auto operator=(const URLPatternProtocol &) -> URLPatternProtocol & = default; |
| 57 | + auto operator=(URLPatternProtocol &&) noexcept |
| 58 | + -> URLPatternProtocol & = default; |
| 59 | + |
| 60 | + auto operator==(const URLPatternProtocol &other) const -> bool = default; |
| 61 | + auto operator<=>(const URLPatternProtocol &other) const |
| 62 | + -> std::strong_ordering; |
| 63 | + |
| 64 | + URLPatternPart value; |
| 65 | + [[nodiscard]] auto match(const std::string_view input) const |
| 66 | + -> std::optional<URLPatternComponentResult>; |
| 67 | +}; |
| 68 | + |
| 69 | +/// @ingroup urlpattern |
| 70 | +struct URLPatternUsername { |
| 71 | + URLPatternUsername() : value{URLPatternPartAsterisk{}} {} |
| 72 | + URLPatternUsername(const char *input); |
| 73 | + URLPatternUsername(const URLPatternUsername &) = default; |
| 74 | + URLPatternUsername(URLPatternUsername &&) noexcept = default; |
| 75 | + auto operator=(const URLPatternUsername &) -> URLPatternUsername & = default; |
| 76 | + auto operator=(URLPatternUsername &&) noexcept |
| 77 | + -> URLPatternUsername & = default; |
| 78 | + |
| 79 | + auto operator==(const URLPatternUsername &other) const -> bool = default; |
| 80 | + auto operator<=>(const URLPatternUsername &other) const |
| 81 | + -> std::strong_ordering; |
| 82 | + |
| 83 | + URLPatternPart value; |
| 84 | + [[nodiscard]] auto match(const std::string_view input) const |
| 85 | + -> std::optional<URLPatternComponentResult>; |
| 86 | +}; |
| 87 | + |
| 88 | +/// @ingroup urlpattern |
| 89 | +struct URLPatternPassword { |
| 90 | + URLPatternPassword() : value{URLPatternPartAsterisk{}} {} |
| 91 | + URLPatternPassword(const char *input); |
| 92 | + URLPatternPassword(const URLPatternPassword &) = default; |
| 93 | + URLPatternPassword(URLPatternPassword &&) noexcept = default; |
| 94 | + auto operator=(const URLPatternPassword &) -> URLPatternPassword & = default; |
| 95 | + auto operator=(URLPatternPassword &&) noexcept |
| 96 | + -> URLPatternPassword & = default; |
| 97 | + |
| 98 | + auto operator==(const URLPatternPassword &other) const -> bool = default; |
| 99 | + auto operator<=>(const URLPatternPassword &other) const |
| 100 | + -> std::strong_ordering; |
| 101 | + |
| 102 | + URLPatternPart value; |
| 103 | + [[nodiscard]] auto match(const std::string_view input) const |
| 104 | + -> std::optional<URLPatternComponentResult>; |
| 105 | +}; |
| 106 | + |
| 107 | +/// @ingroup urlpattern |
| 108 | +struct URLPatternHostname { |
| 109 | + URLPatternHostname() : value{{URLPatternPartAsterisk{}}} {} |
| 110 | + URLPatternHostname(const char *input); |
| 111 | + URLPatternHostname(const URLPatternHostname &) = default; |
| 112 | + URLPatternHostname(URLPatternHostname &&) noexcept = default; |
| 113 | + auto operator=(const URLPatternHostname &) -> URLPatternHostname & = default; |
| 114 | + auto operator=(URLPatternHostname &&) noexcept |
| 115 | + -> URLPatternHostname & = default; |
| 116 | + |
| 117 | + auto operator==(const URLPatternHostname &other) const -> bool = default; |
| 118 | + auto operator<=>(const URLPatternHostname &other) const |
| 119 | + -> std::strong_ordering; |
| 120 | + |
| 121 | + std::vector<URLPatternPart> value; |
| 122 | + [[nodiscard]] auto match(const std::string_view input) const |
| 123 | + -> std::optional<URLPatternComponentResult>; |
| 124 | +}; |
| 125 | + |
| 126 | +/// @ingroup urlpattern |
| 127 | +struct URLPatternPort { |
| 128 | + URLPatternPort() : value{URLPatternPartAsterisk{}} {} |
| 129 | + URLPatternPort(const char *input); |
| 130 | + URLPatternPort(const URLPatternPort &) = default; |
| 131 | + URLPatternPort(URLPatternPort &&) noexcept = default; |
| 132 | + auto operator=(const URLPatternPort &) -> URLPatternPort & = default; |
| 133 | + auto operator=(URLPatternPort &&) noexcept -> URLPatternPort & = default; |
| 134 | + |
| 135 | + auto operator==(const URLPatternPort &other) const -> bool = default; |
| 136 | + auto operator<=>(const URLPatternPort &other) const -> std::strong_ordering; |
| 137 | + |
| 138 | + URLPatternPart value; |
| 139 | + [[nodiscard]] auto match(const std::string_view input) const |
| 140 | + -> std::optional<URLPatternComponentResult>; |
| 141 | +}; |
| 142 | + |
| 143 | +/// @ingroup urlpattern |
| 144 | +struct URLPatternPathname { |
| 145 | + URLPatternPathname() : value{{URLPatternPartAsterisk{}}} {} |
| 146 | + URLPatternPathname(const char *input); |
| 147 | + URLPatternPathname(const URLPatternPathname &) = default; |
| 148 | + URLPatternPathname(URLPatternPathname &&) noexcept = default; |
| 149 | + auto operator=(const URLPatternPathname &) -> URLPatternPathname & = default; |
| 150 | + auto operator=(URLPatternPathname &&) noexcept |
| 151 | + -> URLPatternPathname & = default; |
| 152 | + |
| 153 | + auto operator==(const URLPatternPathname &other) const -> bool = default; |
| 154 | + auto operator<=>(const URLPatternPathname &other) const |
| 155 | + -> std::strong_ordering; |
| 156 | + |
| 157 | + std::vector<URLPatternPart> value; |
| 158 | + // TODO: Find a way to get rid of this |
| 159 | + bool is_bare_pattern{false}; |
| 160 | + [[nodiscard]] auto match(const std::string_view input) const |
| 161 | + -> std::optional<URLPatternComponentResult>; |
| 162 | +}; |
| 163 | + |
| 164 | +/// @ingroup urlpattern |
| 165 | +struct URLPatternSearch { |
| 166 | + URLPatternSearch() : value{URLPatternPartAsterisk{}} {} |
| 167 | + URLPatternSearch(const char *input); |
| 168 | + URLPatternSearch(const URLPatternSearch &) = default; |
| 169 | + URLPatternSearch(URLPatternSearch &&) noexcept = default; |
| 170 | + auto operator=(const URLPatternSearch &) -> URLPatternSearch & = default; |
| 171 | + auto operator=(URLPatternSearch &&) noexcept -> URLPatternSearch & = default; |
| 172 | + |
| 173 | + auto operator==(const URLPatternSearch &other) const -> bool = default; |
| 174 | + auto operator<=>(const URLPatternSearch &other) const -> std::strong_ordering; |
| 175 | + |
| 176 | + // URL Pattern treats search queries as opaque strings |
| 177 | + URLPatternPart value; |
| 178 | + [[nodiscard]] auto match(const std::string_view input) const |
| 179 | + -> std::optional<URLPatternComponentResult>; |
| 180 | +}; |
| 181 | + |
| 182 | +/// @ingroup urlpattern |
| 183 | +struct URLPatternHash { |
| 184 | + URLPatternHash() : value{URLPatternPartAsterisk{}} {} |
| 185 | + URLPatternHash(const char *input); |
| 186 | + URLPatternHash(const URLPatternHash &) = default; |
| 187 | + URLPatternHash(URLPatternHash &&) noexcept = default; |
| 188 | + auto operator=(const URLPatternHash &) -> URLPatternHash & = default; |
| 189 | + auto operator=(URLPatternHash &&) noexcept -> URLPatternHash & = default; |
| 190 | + |
| 191 | + auto operator==(const URLPatternHash &other) const -> bool = default; |
| 192 | + auto operator<=>(const URLPatternHash &other) const -> std::strong_ordering; |
| 193 | + |
| 194 | + URLPatternPart value; |
| 195 | + [[nodiscard]] auto match(const std::string_view input) const |
| 196 | + -> std::optional<URLPatternComponentResult>; |
| 197 | +}; |
| 198 | + |
| 199 | +/// @ingroup urlpattern |
| 200 | +struct URLPatternResult { |
| 201 | + std::optional<URLPatternComponentResult> protocol; |
| 202 | + std::optional<URLPatternComponentResult> username; |
| 203 | + std::optional<URLPatternComponentResult> password; |
| 204 | + std::optional<URLPatternComponentResult> hostname; |
| 205 | + std::optional<URLPatternComponentResult> port; |
| 206 | + std::optional<URLPatternComponentResult> pathname; |
| 207 | + std::optional<URLPatternComponentResult> search; |
| 208 | + std::optional<URLPatternComponentResult> hash; |
| 209 | +}; |
| 210 | + |
| 211 | +/// @ingroup urlpattern |
| 212 | +struct URLPatternInput { |
| 213 | + std::string_view protocol; |
| 214 | + std::string_view username; |
| 215 | + std::string_view password; |
| 216 | + std::string_view hostname; |
| 217 | + std::string_view port; |
| 218 | + std::string_view pathname; |
| 219 | + std::string_view search; |
| 220 | + std::string_view hash; |
| 221 | +}; |
| 222 | + |
| 223 | +/// @ingroup urlpattern |
| 224 | +/// See https://urlpattern.spec.whatwg.org/#url-pattern-struct |
| 225 | +struct URLPattern { |
| 226 | + URLPatternProtocol protocol; |
| 227 | + URLPatternUsername username; |
| 228 | + URLPatternPassword password; |
| 229 | + URLPatternHostname hostname; |
| 230 | + URLPatternPort port; |
| 231 | + URLPatternPathname pathname; |
| 232 | + URLPatternSearch search; |
| 233 | + URLPatternHash hash; |
| 234 | + |
| 235 | + [[nodiscard]] static auto parse(const std::string_view input) -> URLPattern; |
| 236 | + |
| 237 | + [[nodiscard]] auto match(const URLPatternInput &input) const |
| 238 | + -> URLPatternResult; |
| 239 | +}; |
| 240 | + |
| 241 | +} // namespace sourcemeta::core |
| 242 | + |
| 243 | +#endif |
0 commit comments