From d3916d9280b17f88ade851e2eef7f7ed8cdc75b2 Mon Sep 17 00:00:00 2001 From: Alex Anderson Date: Mon, 7 Dec 2020 19:29:02 -0800 Subject: [PATCH] optimization: transform select into set as much as possible --- include/ctre/actions/options.inc.hpp | 26 ++++++++++++++-- include/ctre/actions/set.inc.hpp | 46 ++++++++++++++++++++++++++++ tests/generating.cpp | 6 ++-- 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/include/ctre/actions/options.inc.hpp b/include/ctre/actions/options.inc.hpp index 93ef48ac..5684d464 100644 --- a/include/ctre/actions/options.inc.hpp +++ b/include/ctre/actions/options.inc.hpp @@ -13,13 +13,33 @@ template static constexpr auto apply(pcre::push_empty, ctl // make_alternate (A|B) template static constexpr auto apply(pcre::make_alternate, ctll::term, pcre_context, Parameters> subject) { - return pcre_context{ctll::push_front(select(), ctll::list()), subject.parameters}; + if constexpr (MatchesCharacter::template value && MatchesCharacter::template value) { + auto new_set = push_front_into_set(A{}, B{}); + return pcre_context{ ctll::push_front(new_set, ctll::list()), subject.parameters }; + } else { + return pcre_context{ ctll::push_front(select(), ctll::list()), subject.parameters }; + } } + // make_alternate (As..)|B => (As..|B) -template static constexpr auto apply(pcre::make_alternate, ctll::term, pcre_context, A, Ts...>, Parameters> subject) { - return pcre_context{ctll::push_front(select(), ctll::list()), subject.parameters}; +template static constexpr auto apply(pcre::make_alternate, ctll::term, pcre_context, A, Ts...>, Parameters> subject) { + if constexpr (MatchesCharacter::template value && MatchesCharacter::template value) { + auto new_set = push_front_into_set(A{}, B{}); + return pcre_context{ ctll::push_front(select(), ctll::list()), subject.parameters }; + } else { + return pcre_context{ ctll::push_front(select(), ctll::list()), subject.parameters }; + } } +// make_alternate [As..]|B => ([As..]|B) +template static constexpr auto apply(pcre::make_alternate, ctll::term, pcre_context, A, Ts...>, Parameters> subject) { + if constexpr (MatchesCharacter::template value) { + auto new_set = push_front_into_set(A{}, ctre::set{}); + return pcre_context{ ctll::push_front(new_set, ctll::list()), subject.parameters }; + } else { + return pcre_context{ ctll::push_front(select>(), ctll::list()), subject.parameters }; + } +} // make_optional template static constexpr auto apply(pcre::make_optional, ctll::term, pcre_context, Parameters> subject) { diff --git a/include/ctre/actions/set.inc.hpp b/include/ctre/actions/set.inc.hpp index a5a08d14..05d5280a 100644 --- a/include/ctre/actions/set.inc.hpp +++ b/include/ctre/actions/set.inc.hpp @@ -4,7 +4,53 @@ // UTILITY // add into set if not exists template