diff --git a/include/ada/common_defs.h b/include/ada/common_defs.h index cbac9029a..2300c2ed5 100644 --- a/include/ada/common_defs.h +++ b/include/ada/common_defs.h @@ -250,4 +250,11 @@ namespace ada { #define ada_lifetime_bound #endif +#ifdef __has_include +#if __has_include() +#include +#define ADA_HAS_FORMAT 1 +#endif +#endif + #endif // ADA_COMMON_DEFS_H diff --git a/include/ada/url_pattern-inl.h b/include/ada/url_pattern-inl.h index 6f35108b8..de669500e 100644 --- a/include/ada/url_pattern-inl.h +++ b/include/ada/url_pattern-inl.h @@ -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; @@ -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(); diff --git a/include/ada/url_pattern.h b/include/ada/url_pattern.h index 4ea7d0f5d..e6d9ee18a 100644 --- a/include/ada/url_pattern.h +++ b/include/ada/url_pattern.h @@ -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{}; @@ -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{}; diff --git a/src/url_pattern.cpp b/src/url_pattern.cpp index 1dfda780c..7b49f87ee 100644 --- a/src/url_pattern.cpp +++ b/src/url_pattern.cpp @@ -508,8 +508,8 @@ url_pattern_component::compile(std::string_view input, F encoding_callback, std::regex_constants::syntax_option_type flags = options.ignore_case ? std::regex::icase | - std::regex_constants::syntax_option_type::ECMAScript - : std::regex_constants::syntax_option_type::ECMAScript; + std::regex_constants::ECMAScript + : std::regex_constants::ECMAScript; // Let pattern string be the result of running generate a pattern // string given part list and options. @@ -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); } diff --git a/tests/wpt_urlpattern_tests.cpp b/tests/wpt_urlpattern_tests.cpp index 137798a23..6685fe81d 100644 --- a/tests/wpt_urlpattern_tests.cpp +++ b/tests/wpt_urlpattern_tests.cpp @@ -1,6 +1,7 @@ #include #include +#include "ada/log.h" #include "gtest/gtest.h" #include "simdjson.h" @@ -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)) {