From 6052c18f735288cbdfff58e04c4df81c4305808b Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Wed, 6 Aug 2025 16:24:52 -0600 Subject: [PATCH] header_rewrit: Move templates into a header file --- plugins/header_rewrite/matcher.cc | 80 ------------------------------- plugins/header_rewrite/matcher.h | 80 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/plugins/header_rewrite/matcher.cc b/plugins/header_rewrite/matcher.cc index 75faa6a2a12..3f4c12a0ec6 100644 --- a/plugins/header_rewrite/matcher.cc +++ b/plugins/header_rewrite/matcher.cc @@ -26,86 +26,6 @@ #include "matcher.h" -static bool -match_with_modifiers(std::string_view rhs, std::string_view lhs, CondModifiers mods) -{ - // Case-aware equality - static auto equals = [](std::string_view a, std::string_view b, CondModifiers mods) -> bool { - if (has_modifier(mods, CondModifiers::MOD_NOCASE)) { - return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), [](char c1, char c2) { - return std::tolower(static_cast(c1)) == std::tolower(static_cast(c2)); - }); - } - return a == b; - }; - - // Case-aware substring search - static auto contains = [](std::string_view haystack, std::string_view needle, CondModifiers mods) -> bool { - if (!has_modifier(mods, CondModifiers::MOD_NOCASE)) { - return haystack.find(needle) != std::string_view::npos; - } - auto it = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), [](char c1, char c2) { - return std::tolower(static_cast(c1)) == std::tolower(static_cast(c2)); - }); - return it != haystack.end(); - }; - - if (has_modifier(mods, CondModifiers::MOD_EXT)) { - auto dot = rhs.rfind('.'); - return dot != std::string_view::npos && dot + 1 < rhs.size() && equals(rhs.substr(dot + 1), lhs, mods); - } - - if (has_modifier(mods, CondModifiers::MOD_SUF)) { - return rhs.size() >= lhs.size() && equals(rhs.substr(rhs.size() - lhs.size()), lhs, mods); - } - - if (has_modifier(mods, CondModifiers::MOD_PRE)) { - return rhs.size() >= lhs.size() && equals(rhs.substr(0, lhs.size()), lhs, mods); - } - - if (has_modifier(mods, CondModifiers::MOD_MID)) { - return contains(rhs, lhs, mods); - } - - return equals(rhs, lhs, mods); -} - -// Special case for strings, to allow for insensitive case comparisons for std::string matchers. -template <> -bool -Matchers::test_eq(const std::string &t) const -{ - std::string_view lhs = std::get(_data); - std::string_view rhs = t; - bool result = match_with_modifiers(rhs, lhs, _mods); - - if (pi_dbg_ctl.on()) { - debug_helper(t, " == ", result); - } - - return result; -} - -template <> -bool -Matchers::test_set(const std::string &t) const -{ - TSAssert(std::holds_alternative>(_data)); - std::string_view rhs = t; - - for (const auto &entry : std::get>(_data)) { - if (match_with_modifiers(rhs, entry, _mods)) { - if (pi_dbg_ctl.on()) { - debug_helper(t, " ∈ ", true); - } - return true; - } - } - - debug_helper(t, " ∈ ", false); - return false; -} - template <> bool Matchers::test(const sockaddr *const &addr, const Resources & /* Not used */) const diff --git a/plugins/header_rewrite/matcher.h b/plugins/header_rewrite/matcher.h index 3099b300f87..f8d2872c76e 100644 --- a/plugins/header_rewrite/matcher.h +++ b/plugins/header_rewrite/matcher.h @@ -380,3 +380,83 @@ template class Matchers : public Matcher std::variant, swoc::IPRangeSet, regexHelper> _data; CondModifiers _mods = CondModifiers::NONE; }; + +static bool +match_with_modifiers(std::string_view rhs, std::string_view lhs, CondModifiers mods) +{ + // Case-aware equality + static auto equals = [](std::string_view a, std::string_view b, CondModifiers mods) -> bool { + if (has_modifier(mods, CondModifiers::MOD_NOCASE)) { + return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), [](char c1, char c2) { + return std::tolower(static_cast(c1)) == std::tolower(static_cast(c2)); + }); + } + return a == b; + }; + + // Case-aware substring search + static auto contains = [](std::string_view haystack, std::string_view needle, CondModifiers mods) -> bool { + if (!has_modifier(mods, CondModifiers::MOD_NOCASE)) { + return haystack.find(needle) != std::string_view::npos; + } + auto it = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), [](char c1, char c2) { + return std::tolower(static_cast(c1)) == std::tolower(static_cast(c2)); + }); + return it != haystack.end(); + }; + + if (has_modifier(mods, CondModifiers::MOD_EXT)) { + auto dot = rhs.rfind('.'); + return dot != std::string_view::npos && dot + 1 < rhs.size() && equals(rhs.substr(dot + 1), lhs, mods); + } + + if (has_modifier(mods, CondModifiers::MOD_SUF)) { + return rhs.size() >= lhs.size() && equals(rhs.substr(rhs.size() - lhs.size()), lhs, mods); + } + + if (has_modifier(mods, CondModifiers::MOD_PRE)) { + return rhs.size() >= lhs.size() && equals(rhs.substr(0, lhs.size()), lhs, mods); + } + + if (has_modifier(mods, CondModifiers::MOD_MID)) { + return contains(rhs, lhs, mods); + } + + return equals(rhs, lhs, mods); +} + +// Special case for strings, to allow for insensitive case comparisons for std::string matchers. +template <> +inline bool +Matchers::test_eq(const std::string &t) const +{ + std::string_view lhs = std::get(_data); + std::string_view rhs = t; + bool result = match_with_modifiers(rhs, lhs, _mods); + + if (pi_dbg_ctl.on()) { + debug_helper(t, " == ", result); + } + + return result; +} + +template <> +inline bool +Matchers::test_set(const std::string &t) const +{ + TSAssert(std::holds_alternative>(_data)); + std::string_view rhs = t; + + for (const auto &entry : std::get>(_data)) { + if (match_with_modifiers(rhs, entry, _mods)) { + if (pi_dbg_ctl.on()) { + debug_helper(t, " ∈ ", true); + } + return true; + } + } + + debug_helper(t, " ∈ ", false); + return false; +}