18#include <system_error>
20#if ADA_REGULAR_VISUAL_STUDIO
26 return !username.empty() || !password.empty();
29 return port.has_value();
31[[nodiscard]]
inline bool url::cannot_have_credentials_or_port()
const {
35 if (!host.has_value()) {
41 return host.has_value();
66 if (host.has_value()) {
75 if (!password.empty()) {
76 out.
host_start += uint32_t(password.size() + 1);
107 if (port.has_value()) {
109 running_index += helpers::fast_digit_count(*port) + 1;
114 running_index += path.size();
116 if (query.has_value()) {
124 if (hash.has_value()) {
131inline void url::update_base_hostname(std::string_view input) { host = input; }
133inline void url::update_unencoded_base_hash(std::string_view input) {
135 hash = unicode::percent_encode(input,
139inline void url::update_base_search(std::string_view input,
140 const uint8_t query_percent_encode_set[]) {
141 query = ada::unicode::percent_encode(input, query_percent_encode_set);
144inline void url::update_base_search(std::optional<std::string>&& input) {
145 query = std::move(input);
148inline void url::update_base_pathname(
const std::string_view input) {
152inline void url::update_base_username(
const std::string_view input) {
156inline void url::update_base_password(
const std::string_view input) {
160inline void url::update_base_port(std::optional<uint16_t> input) {
164constexpr void url::clear_pathname() { path.clear(); }
166constexpr void url::clear_search() { query = std::nullopt; }
169 return hash.has_value();
173 return query.has_value();
178inline void url::set_scheme(std::string&& new_scheme)
noexcept {
182 non_special_scheme = std::move(new_scheme);
186constexpr void url::copy_scheme(ada::url&& u) {
187 non_special_scheme = u.non_special_scheme;
191constexpr void url::copy_scheme(
const ada::url& u) {
192 non_special_scheme = u.non_special_scheme;
197 if (
is_special() && host.has_value() && username.empty() &&
198 password.empty() && !port.has_value()) [[likely]] {
200 const size_t host_size = host->size();
201 const size_t path_size = path.size();
202 const size_t query_size = query.has_value() ? query->size() : 0;
203 const size_t hash_size = hash.has_value() ? hash->size() : 0;
204 const size_t total =
scheme.size() + 3 + host_size + path_size +
205 (query.has_value() ? query_size + 1 : 0) +
206 (hash.has_value() ? hash_size + 1 : 0);
207 std::string output(total,
'\0');
208 char* p = output.data();
216 std::memcpy(p, host->data(), host_size);
218 std::memcpy(p, path.data(), path_size);
220 if (query.has_value()) {
223 std::memcpy(p, query->data(), query_size);
226 if (hash.has_value()) {
229 std::memcpy(p, hash->data(), hash_size);
241 output.append(non_special_scheme);
245 if (host.has_value()) {
249 output.append(username);
250 if (!password.empty()) {
252 output.append(password);
256 output.append(*host);
257 if (port.has_value()) {
260 auto [ptr, ec] = std::to_chars(port_buf, port_buf + 5, *port);
262 output.append(port_buf,
static_cast<size_t>(ptr - port_buf));
272 if (query.has_value()) {
274 output.append(*query);
276 if (hash.has_value()) {
278 output.append(*hash);
288 size += non_special_scheme.size() + 1;
290 if (host.has_value()) {
291 size += host->size();
294 size += username.size();
295 if (!password.empty()) {
296 size += 1 + password.size();
300 if (port.has_value()) {
303 size += (p >= 10000) ? 5
313 if (query.has_value()) {
314 size += 1 + query->size();
316 if (hash.has_value()) {
317 size += 1 + hash->size();
323 bool check_trailing_content)
noexcept {
324 ada_log(
"parse_port('", view,
"') ", view.size());
325 if (!view.empty() && view[0] ==
'-') {
326 ada_log(
"parse_port: view[0] == '0' && view.size() > 1");
330 uint16_t parsed_port{};
331 auto r = std::from_chars(view.data(), view.data() + view.size(), parsed_port);
332 if (
r.ec == std::errc::result_out_of_range) {
333 ada_log(
"parse_port: r.ec == std::errc::result_out_of_range");
337 ada_log(
"parse_port: ", parsed_port);
338 const auto consumed = size_t(
r.ptr - view.data());
339 ada_log(
"parse_port: consumed ", consumed);
340 if (check_trailing_content) {
342 (consumed == view.size() || view[consumed] ==
'/' ||
343 view[consumed] ==
'?' || (is_special() && view[consumed] ==
'\\'));
345 ada_log(
"parse_port: is_valid = ", is_valid);
348 auto default_port = scheme_default_port();
349 bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
350 (default_port != parsed_port);
351 port = (
r.ec == std::errc() && is_port_valid) ? std::optional(parsed_port)
#define ada_really_inline
constexpr uint8_t FRAGMENT_PERCENT_ENCODE[32]
constexpr std::string_view is_special_list[]
URL scheme utilities and constants.
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept
std::ostream & operator<<(std::ostream &out, const ada::url &u)
ada_really_inline constexpr bool is_special() const noexcept
Stores byte offsets for URL components within a buffer.
Represents a parsed URL with individual string components.
bool has_empty_hostname() const noexcept
bool has_port() const noexcept
ada_really_inline bool has_credentials() const noexcept
ada_really_inline size_t get_pathname_length() const noexcept
std::string get_search() const
std::string get_protocol() const
bool has_hostname() const noexcept
constexpr std::string_view get_pathname() const noexcept
size_t get_href_size() const noexcept
ada_really_inline std::string get_href() const
ada_really_inline ada::url_components get_components() const
constexpr bool has_search() const noexcept override
std::string to_string() const override
constexpr bool has_hash() const noexcept override
Declaration for the ada::url class.
Declaration for the URL Components.