Ada 3.4.4
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
implementation.cpp
Go to the documentation of this file.
2
3#include <string_view>
4
5#include "ada/common_defs.h"
6#include "ada/parser.h"
7#include "ada/url.h"
9
10namespace ada {
11
12template <class result_type>
13ada_warn_unused tl::expected<result_type, errors> parse(
14 std::string_view input, const result_type* base_url) {
16 ada::parser::parse_url_impl<result_type, true>(input, base_url);
17 if (!u.is_valid) {
18 return tl::unexpected(errors::type_error);
19 }
20 return u;
21}
22
23template ada::result<url> parse<url>(std::string_view input,
24 const url* base_url = nullptr);
26 std::string_view input, const url_aggregator* base_url = nullptr);
27
28std::string href_from_file(std::string_view input) {
29 // This is going to be much faster than constructing a URL.
30 std::string tmp_buffer;
31 std::string_view internal_input;
32 if (unicode::has_tabs_or_newline(input)) {
34 helpers::remove_ascii_tab_or_newline(tmp_buffer);
36 } else {
38 }
39 std::string path;
40 if (internal_input.empty()) {
41 path = "/";
42 } else if ((internal_input[0] == '/') || (internal_input[0] == '\\')) {
43 helpers::parse_prepared_path(internal_input.substr(1),
45 } else {
46 helpers::parse_prepared_path(internal_input, ada::scheme::type::FILE, path);
47 }
48 return "file://" + path;
49}
50
51bool can_parse(std::string_view input, const std::string_view* base_input) {
54
55 if (base_input != nullptr) {
56 base_aggregator = ada::parser::parse_url_impl<ada::url_aggregator, false>(
57 *base_input, nullptr);
58 if (!base_aggregator.is_valid) {
59 return false;
60 }
62 }
63
65 ada::parser::parse_url_impl<ada::url_aggregator, false>(input,
67 return result.is_valid;
68}
69
71 switch (type) {
73 return "UTF-8";
75 return "UTF-16LE";
77 return "UTF-16BE";
78 default:
80 }
81}
82
83} // namespace ada
Cross-platform compiler macros and common definitions.
#define ada_warn_unused
Definition common_defs.h:89
Definition ada_idna.h:13
bool can_parse(std::string_view input, const std::string_view *base_input=nullptr)
template ada::result< url > parse< url >(std::string_view input, const url *base_url)
std::string href_from_file(std::string_view path)
tl::expected< result_type, ada::errors > result
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
encoding_type
Character encoding types for URL processing.
ada_warn_unused std::string_view to_string(encoding_type type)
void unreachable()
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Low-level URL parsing functions.
Memory-efficient URL representation using a single buffer.
Declaration for the ada::url class.
Declaration for the ada::url_aggregator class.