Loading...
Searching...
No Matches
url_pattern_regex.cpp
Go to the documentation of this file.
#if ADA_INCLUDE_URL_PATTERN
#include "ada/url_pattern_regex.h"
namespace ada::url_pattern_regex {
#ifdef ADA_USE_UNSAFE_STD_REGEX_PROVIDER
std::optional<std::regex> std_regex_provider::create_instance(
std::string_view pattern, bool ignore_case) {
// Let flags be an empty string.
// If options's ignore case is true then set flags to "vi".
// Otherwise set flags to "v"
auto flags = ignore_case
? std::regex::icase | std::regex_constants::ECMAScript
: std::regex_constants::ECMAScript;
try {
return std::regex(pattern.data(), pattern.size(), flags);
} catch (const std::regex_error& e) {
(void)e;
ada_log("std_regex_provider::create_instance failed:", e.what());
return std::nullopt;
}
}
std::optional<std::vector<std::optional<std::string>>>
std_regex_provider::regex_search(std::string_view input,
const std::regex& pattern) {
// Use iterator-based regex_search to avoid string allocation
std::match_results<std::string_view::const_iterator> match_result;
try {
if (!std::regex_search(input.begin(), input.end(), match_result, pattern,
std::regex_constants::match_any)) {
return std::nullopt;
}
} catch (const std::regex_error& e) {
(void)e;
ada_log("std_regex_provider::regex_search failed:", e.what());
return std::nullopt;
}
std::vector<std::optional<std::string>> matches;
// If input is empty, let's assume the result will be empty as well.
if (input.empty() || match_result.empty()) {
return matches;
}
matches.reserve(match_result.size());
for (size_t i = 1; i < match_result.size(); ++i) {
if (auto entry = match_result[i]; entry.matched) {
matches.emplace_back(entry.str());
}
}
return matches;
}
bool std_regex_provider::regex_match(std::string_view input,
const std::regex& pattern) {
try {
return std::regex_match(input.begin(), input.end(), pattern);
} catch (const std::regex_error& e) {
(void)e;
ada_log("std_regex_provider::regex_match failed:", e.what());
return false;
}
}
#endif // ADA_USE_UNSAFE_STD_REGEX_PROVIDER
} // namespace ada::url_pattern_regex
#endif // ADA_INCLUDE_URL_PATTERN
url_pattern_regex.h