Ada 2.9.2
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
implementation.cpp
Go to the documentation of this file.
1#include <string_view>
2
3#include "ada.h"
4#include "ada/common_defs.h"
5#include "ada/parser.h"
6#include "ada/url.h"
8
9namespace ada {
10
11template <class result_type>
12ada_warn_unused tl::expected<result_type, ada::errors> parse(
13 std::string_view input, const result_type* base_url) {
14 result_type u =
16 if (!u.is_valid) {
17 return tl::unexpected(errors::generic_error);
18 }
19 return u;
20}
21
22template ada::result<url> parse<url>(std::string_view input,
23 const url* base_url = nullptr);
25 std::string_view input, const url_aggregator* base_url = nullptr);
26
27std::string href_from_file(std::string_view input) {
28 // This is going to be much faster than constructing a URL.
29 std::string tmp_buffer;
30 std::string_view internal_input;
31 if (unicode::has_tabs_or_newline(input)) {
32 tmp_buffer = input;
33 helpers::remove_ascii_tab_or_newline(tmp_buffer);
34 internal_input = tmp_buffer;
35 } else {
36 internal_input = input;
37 }
38 std::string path;
39 if (internal_input.empty()) {
40 path = "/";
41 } else if ((internal_input[0] == '/') || (internal_input[0] == '\\')) {
42 helpers::parse_prepared_path(internal_input.substr(1),
44 } else {
45 helpers::parse_prepared_path(internal_input, ada::scheme::type::FILE, path);
46 }
47 return "file://" + path;
48}
49
50bool can_parse(std::string_view input, const std::string_view* base_input) {
51 ada::url_aggregator base_aggregator;
52 ada::url_aggregator* base_pointer = nullptr;
53
54 if (base_input != nullptr) {
56 *base_input, nullptr);
57 if (!base_aggregator.is_valid) {
58 return false;
59 }
60 base_pointer = &base_aggregator;
61 }
62
65 base_pointer);
66 return result.is_valid;
67}
68
70 switch (type) {
72 return "UTF-8";
74 return "UTF-16LE";
76 return "UTF-16BE";
77 default:
79 }
80}
81
82} // namespace ada
Includes all definitions for Ada.
Common definitions for cross-platform compiler support.
#define ada_warn_unused
Definition common_defs.h:81
result_type parse_url_impl(std::string_view user_input, const result_type *base_url=nullptr)
Definition parser.cpp:15
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)
ada_warn_unused std::string to_string(encoding_type type)
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
encoding_type
void unreachable()
tl::expected< result_type, ada::errors > result
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Definitions for the parser.
Lightweight URL struct.
bool is_valid
Definition url_base.h:50
Declaration for the URL.
Declaration for the basic URL definitions.