/
auroraos
/
mirror_ada
Обзор
Документация
Войти
/
auroraos
/
mirror_ada
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
src/url_pattern_regex.cpp
78 строк
3 KB
Abdul Rawoof Khan
report capture groups for empty input in regex_search (#1188)
19 июл 2026, 22:10
Не верифицирован
19 июл 2026, 22:10
308110b
Код
Авторство
О чём код?
#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; // An empty input can still match with capture groups (e.g. "(x*)" or an // optional ":a?" against ""), so the group values must be extracted here // just like for a non-empty input. A successful regex_search always leaves // the full match at index 0, so match_result is never empty at this point. if (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()); } else { // A capture group that did not participate in the match is undefined, // not absent. Emitting nullopt keeps the result aligned by position with // the component's group name list; dropping it shifts every later group // onto the wrong name. matches.emplace_back(std::nullopt); } } 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