Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/ada/common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,11 @@ namespace ada {
#define ada_lifetime_bound
#endif

#ifdef __has_include
#if __has_include(<format>)
#include <format>
#define ADA_HAS_FORMAT 1
#endif
#endif

#endif // ADA_COMMON_DEFS_H
24 changes: 24 additions & 0 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ inline bool url_pattern_component::has_regexp_groups() const noexcept
return has_regexp_groups_;
}

inline std::string url_pattern_component::to_string() const {
#ifdef ADA_HAS_FORMAT
return std::format(R"({{"pattern": "{}", "has_regexp_groups": {}}})", pattern,
has_regexp_groups_ ? "true" : "false" //,
);
#else
return "";
#endif
}

inline std::string_view url_pattern_component::get_pattern() const noexcept
ada_lifetime_bound {
return pattern;
Expand Down Expand Up @@ -63,6 +73,20 @@ url_pattern_component::create_component_match_result(
return result;
}

inline std::string url_pattern::to_string() const {
#ifdef ADA_HAS_FORMAT
return std::format(
R"({{"protocol_component": "{}", "username_component": {}, "password_component": {}, "hostname_component": {}, "port_component": {}, "pathname_component": {}, "search_component": {}, "hash_component": {}, "ignore_case": {}}})",
protocol_component.to_string(), username_component.to_string(),
password_component.to_string(), hostname_component.to_string(),
port_component.to_string(), pathname_component.to_string(),
search_component.to_string(), hash_component.to_string(),
ignore_case_ ? "true" : "false");
#else
return "";
#endif
}

inline std::string_view url_pattern::get_protocol() const ada_lifetime_bound {
// Return this's associated URL pattern's protocol component's pattern string.
return protocol_component.get_pattern();
Expand Down
4 changes: 4 additions & 0 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ class url_pattern_component {
ada_lifetime_bound ada_warn_unused;
bool has_regexp_groups() const noexcept ada_lifetime_bound ada_warn_unused;

std::string to_string() const;

private:
std::string pattern{};
std::regex_constants::syntax_option_type flags{};
Expand Down Expand Up @@ -295,6 +297,8 @@ class url_pattern {
// @see https://urlpattern.spec.whatwg.org/#url-pattern-has-regexp-groups
bool has_regexp_groups() const ada_lifetime_bound;

std::string to_string() const;

private:
url_pattern_component protocol_component{};
url_pattern_component username_component{};
Expand Down
1 change: 1 addition & 0 deletions src/url_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ url_pattern_component::compile(std::string_view input, F encoding_callback,
try {
regular_expression = std::regex(regular_expression_string, flags);
} catch (std::regex_error& error) {
(void)error;
ada_log("std::regex_error: ", error.what());
return tl::unexpected(url_pattern_errors::type_error);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/wpt_urlpattern_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <filesystem>
#include <iostream>

#include "ada/log.h"
#include "gtest/gtest.h"
#include "simdjson.h"

Expand Down Expand Up @@ -251,7 +252,9 @@ TEST(wpt_urlpattern_tests, urlpattern_test_data) {
FAIL() << "Test should have succeeded but failed" << std::endl
<< main_object.raw_json().value() << std::endl;
}

#ifdef ADA_HAS_FORMAT
ada_log("parse_result: ", parse_result->to_string());
#endif
ondemand::array exactly_empty_components;
if (!main_object["exactly_empty_components"].get_array().get(
exactly_empty_components)) {
Expand Down
Loading