Skip to content

Commit 28b2327

Browse files
committed
implement URLPattern skeleton
1 parent a383f41 commit 28b2327

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

Diff for: include/ada.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "ada/url_aggregator-inl.h"
2727
#include "ada/url_search_params.h"
2828
#include "ada/url_search_params-inl.h"
29+
#include "ada/url_pattern.h"
2930

3031
// Public API
3132
#include "ada/ada_version.h"

Diff for: include/ada/url_pattern.h

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @file url_pattern.h
3+
* @brief Declaration for the URLPattern implementation.
4+
*/
5+
#ifndef ADA_URL_PATTERN_H
6+
#define ADA_URL_PATTERN_H
7+
8+
#include <string>
9+
#include <unordered_map>
10+
11+
namespace ada {
12+
13+
// URLPattern is a Web Platform standard API for matching URLs against a
14+
// pattern syntax (think of it as a regular expression for URLs). It is
15+
// defined in https://wicg.github.io/urlpattern.
16+
// More information about the URL Pattern syntax can be found at
17+
// https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
18+
class URLPattern {
19+
public:
20+
class Component {
21+
public:
22+
explicit Component(std::string_view pattern, std::string_view regex,
23+
const std::vector<std::string>& names);
24+
25+
// TODO(anonrig): Move these implementations to `url_pattern-inl.h`
26+
std::string_view get_pattern() const noexcept ada_lifetime_bound {
27+
return pattern;
28+
}
29+
std::string_view get_regex() const noexcept ada_lifetime_bound {
30+
return regex;
31+
}
32+
const std::vector<std::string>& get_names() const noexcept
33+
ada_lifetime_bound {
34+
return names;
35+
}
36+
37+
private:
38+
// Disallow copy.
39+
Component(const Component&);
40+
41+
// The normalized pattern for this component.
42+
std::string pattern = "";
43+
// The generated JavaScript regular expression for this component.
44+
std::string regex = "";
45+
// The list of sub-component names extracted for this component.
46+
std::vector<std::string> names;
47+
};
48+
49+
// A structure providing matching patterns for individual components
50+
// of a URL. When a URLPattern is created, or when a URLPattern is
51+
// used to match or test against a URL, the input can be given as
52+
// either a string or a URLPatternInit struct. If a string is given,
53+
// it will be parsed to create a URLPatternInit. The URLPatternInit
54+
// API is defined as part of the URLPattern specification.
55+
struct Init {
56+
std::optional<std::string> protocol;
57+
std::optional<std::string> username;
58+
std::optional<std::string> password;
59+
std::optional<std::string> hostname;
60+
std::optional<std::string> port;
61+
std::optional<std::string> pathname;
62+
std::optional<std::string> search;
63+
std::optional<std::string> hash;
64+
65+
std::optional<std::string> base_url;
66+
};
67+
68+
using Input = std::variant<std::string, Init>;
69+
70+
// A struct providing the URLPattern matching results for a single
71+
// URL component. The URLPatternComponentResult is only ever used
72+
// as a member attribute of a URLPatternResult struct. The
73+
// URLPatternComponentResult API is defined as part of the URLPattern
74+
// specification.
75+
struct ComponentResult {
76+
std::string input;
77+
std::unordered_map<std::string, std::string> groups;
78+
};
79+
80+
// A struct providing the URLPattern matching results for all
81+
// components of a URL. The URLPatternResult API is defined as
82+
// part of the URLPattern specification.
83+
struct Result {
84+
std::vector<Input> inputs;
85+
ComponentResult protocol;
86+
ComponentResult username;
87+
ComponentResult password;
88+
ComponentResult hostname;
89+
ComponentResult port;
90+
ComponentResult pathname;
91+
ComponentResult search;
92+
ComponentResult hash;
93+
};
94+
95+
struct Options {
96+
bool ignore_case = false;
97+
};
98+
99+
explicit URLPattern(std::optional<Input> input,
100+
std::optional<std::string_view> base_url,
101+
std::optional<Options> options);
102+
103+
std::optional<Result> exec(std::optional<Input> input,
104+
std::optional<std::string> base_url);
105+
bool test(std::optional<Input> input,
106+
std::optional<std::string_view> base_url);
107+
108+
// TODO(anonrig): Move these to `url_pattern-inl.h`.
109+
const Component& get_protocol() const ada_lifetime_bound { return protocol; }
110+
const Component& get_username() const ada_lifetime_bound { return username; }
111+
const Component& get_password() const ada_lifetime_bound { return password; }
112+
const Component& get_port() const ada_lifetime_bound { return port; }
113+
const Component& get_pathname() const ada_lifetime_bound { return pathname; }
114+
const Component& get_search() const ada_lifetime_bound { return search; }
115+
const Component& get_hash() const ada_lifetime_bound { return hash; }
116+
117+
// If ignoreCase is true, the JavaScript regular expression created for each
118+
// pattern must use the `vi` flag. Otherwise, they must use the `v` flag.
119+
// TODO(anonrig): Move these to `url_pattern-inl.h`.
120+
bool case_ignored() const ada_lifetime_bound { return ignore_case; }
121+
122+
private:
123+
Component protocol;
124+
Component username;
125+
Component password;
126+
Component port;
127+
Component pathname;
128+
Component search;
129+
Component hash;
130+
bool ignore_case = false;
131+
};
132+
133+
} // namespace ada
134+
135+
#endif

Diff for: src/ada.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
#include "parser.cpp"
99
#include "url_components.cpp"
1010
#include "url_aggregator.cpp"
11+
#include "url_pattern.cpp"
1112
#include "ada_c.cpp"

Diff for: src/url_pattern.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "ada.h"
2+
3+
#include <optional>
4+
#include <string>
5+
6+
namespace ada {
7+
8+
URLPattern::Component::Component(std::string_view pattern,
9+
std::string_view regex,
10+
const std::vector<std::string>& names) {
11+
// TODO: Implement this
12+
return {.pattern = pattern, .regex = regex, .names = std::move(names)};
13+
}
14+
15+
std::optional<URLPattern::Result> URLPattern::exec(
16+
std::optional<Input> input, std::optional<std::string> base_url) {
17+
// TODO: Implement this
18+
return std::nullopt;
19+
}
20+
21+
bool URLPattern::test(std::optional<Input> input,
22+
std::optional<std::string_view> base_url) {
23+
// TODO: Implement this
24+
return false;
25+
}
26+
27+
} // namespace ada

0 commit comments

Comments
 (0)