Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some optional result logging #812

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/url_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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