Skip to content

Commit

Permalink
add pluggable regex engine support
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 10, 2025
1 parent c9e2576 commit ee9559b
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/ada.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "ada/url_pattern-inl.h"
#include "ada/url_pattern_helpers.h"
#include "ada/url_pattern_helpers-inl.h"
#include "ada/url_pattern_regex.h"

// Public API
#include "ada/ada_version.h"
Expand Down
6 changes: 5 additions & 1 deletion include/ada/implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ada/url.h"
#include "ada/state.h"
#include "ada/url_aggregator.h"
#include "ada/url_pattern_regex.h"

namespace ada {
enum class errors : uint8_t { type_error };
Expand Down Expand Up @@ -56,12 +57,15 @@ bool can_parse(std::string_view input,
* @param input valid UTF-8 string or URLPatternInit struct
* @param base_url an optional valid UTF-8 string
* @param options an optional url_pattern_options struct
* @param regex_provider an optional regex provider. if not provided, it will
* use ada::url_pattern_regex::std_regex_provider
* @return url_pattern instance
*/
ada_warn_unused tl::expected<url_pattern, errors> parse_url_pattern(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url = nullptr,
const url_pattern_options* options = nullptr);
const url_pattern_options* options = nullptr,
std::optional<url_pattern_regex::provider> regex_provider = std::nullopt);

/**
* Computes a href string from a file path. The function assumes
Expand Down
4 changes: 3 additions & 1 deletion include/ada/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <variant>

#include "ada/expected.h"
#include "ada/url_pattern_regex.h"

/**
* @private
Expand Down Expand Up @@ -53,7 +54,8 @@ extern template url parse_url_impl<url>(std::string_view user_input,

tl::expected<url_pattern, errors> parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
const std::string_view* base_url, const url_pattern_options* options,
url_pattern_regex::provider&& regex_provider);

} // namespace ada::parser

Expand Down
14 changes: 8 additions & 6 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ada/implementation.h"
#include "ada/expected.h"
#include "ada/url_pattern_regex.h"

#include <regex>
#include <string>
Expand All @@ -21,7 +22,8 @@ template <typename result_type, typename url_pattern_init,
typename url_pattern_options>
tl::expected<result_type, errors> parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
const std::string_view* base_url, const url_pattern_options* options,
url_pattern_regex::provider&& regex_provider);
}

// Important: C++20 allows us to use concept rather than `using` or `typedef
Expand Down Expand Up @@ -270,10 +272,8 @@ struct url_pattern_options {
// https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
class url_pattern {
public:
url_pattern() = default;
explicit url_pattern(std::optional<url_pattern_input>&& input,
std::optional<std::string_view>&& base_url,
std::optional<url_pattern_options>&& options);
explicit url_pattern(url_pattern_regex::provider&& regex_provider)
: regex_provider_(std::move(regex_provider)) {}

/**
* @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
Expand Down Expand Up @@ -328,12 +328,14 @@ class url_pattern {
url_pattern_component search_component{};
url_pattern_component hash_component{};
bool ignore_case_ = false;
url_pattern_regex::provider regex_provider_;

template <typename result_type, typename url_pattern_init,
typename url_pattern_options>
friend tl::expected<result_type, errors> parser::parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
const std::string_view* base_url, const url_pattern_options* options,
url_pattern_regex::provider&& regex_provider);
};

} // namespace ada
Expand Down
27 changes: 27 additions & 0 deletions include/ada/url_pattern_regex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file url_search_params.h
* @brief Declaration for the URL Search Params
*/
#ifndef ADA_URL_PATTERN_REGEX_H
#define ADA_URL_PATTERN_REGEX_H

#include <regex>

namespace ada::url_pattern_regex {

class provider {
struct type {};

std::optional<type> create_regex_instance(std::string_view pattern,
bool ignore_case);

std::optional<std::vector<std::string>> regex_search(std::string_view input, std::string_view pattern);
};

class std_regex_provider : public provider {

};

} // namespace ada::url_pattern_regex

#endif // ADA_URL_PATTERN_REGEX_H
1 change: 1 addition & 0 deletions src/ada.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
#include "url_aggregator.cpp"
#include "url_pattern.cpp"
#include "url_pattern_helpers.cpp"
#include "url_pattern_regex.cpp"
#include "ada_c.cpp"
7 changes: 5 additions & 2 deletions src/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ ada_warn_unused std::string to_string(ada::encoding_type type) {

ada_warn_unused tl::expected<url_pattern, errors> parse_url_pattern(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options) {
return parser::parse_url_pattern_impl(std::move(input), base_url, options);
const std::string_view* base_url, const url_pattern_options* options,
std::optional<url_pattern_regex::provider> regex_provider) {
return parser::parse_url_pattern_impl(
std::move(input), base_url, options,
regex_provider.value_or(url_pattern_regex::std_regex_provider()));
}

} // namespace ada
5 changes: 3 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,8 @@ result_type parse_url_impl(std::string_view user_input,

tl::expected<url_pattern, errors> parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options) {
const std::string_view* base_url, const url_pattern_options* options,
url_pattern_regex::provider&& regex_provider) {
// Let init be null.
url_pattern_init init;

Expand Down Expand Up @@ -983,7 +984,7 @@ tl::expected<url_pattern, errors> parse_url_pattern_impl(
}

// Let urlPattern be a new URL pattern.
auto url_pattern_ = url_pattern{};
auto url_pattern_ = url_pattern(std::move(regex_provider));

// Set urlPattern’s protocol component to the result of compiling a component
// given processedInit["protocol"], canonicalize a protocol, and default
Expand Down
Empty file added src/url_pattern_regex.cpp
Empty file.

0 comments on commit ee9559b

Please sign in to comment.