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

Optimization: Check for implicit anchor #165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions include/ctre/evaluation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "atoms.hpp"
#include "atoms_unicode.hpp"
#include "starts_with_anchor.hpp"
#include "has_implicit_anchor.hpp"
#include "utility.hpp"
#include "return_type.hpp"
#include "find_captures.hpp"
Expand Down
68 changes: 68 additions & 0 deletions include/ctre/has_implicit_anchor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef CTRE__HAS_IMPLICIT_ANCHOR__HPP
#define CTRE__HAS_IMPLICIT_ANCHOR__HPP

#include "atoms.hpp"
#include "atoms_unicode.hpp"

namespace ctre {

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<repeat<0, 0, any>, Content...>) noexcept {
return true;
}
template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<possessive_repeat<0, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<possessive_repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<lazy_repeat<0, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<lazy_repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<sequence<Content...>, Tail...>) noexcept {
return has_implicit_anchor(ctll::list<Content..., Tail...>{});
}

//TODO: we may check captures as implicit anchors*, but they must not be backreferenced because for example "(.+)X\1" will not work properly with "1234X2345"
/*
template<size_t Id, typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<capture<Id, Content...>, Tail...>) noexcept {
//Id must not be backreferenced
return !id_backreference(Id) && has_implicit_anchor(ctll::list<Content..., Tail...>{});
}

template<size_t Id, typename Name, typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<capture_with_name<Id, Name, Content...>, Tail...>) noexcept {
//Id must not be backreferenced
return !id_backreference(Id) && has_implicit_anchor(ctll::list<Content..., Tail...>{});
}
*/

template<typename... Opts, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<select<Opts...>, Tail...>) noexcept {
return (has_implicit_anchor(ctll::list<Opts, Tail...>{}) && ...);
}

constexpr bool has_implicit_anchor(...) noexcept {
return false;
}
}

#endif
2 changes: 1 addition & 1 deletion include/ctre/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct search_method {
template <typename Modifier = singleline, typename ResultIterator = void, typename RE, typename IteratorBegin, typename IteratorEnd> constexpr CTRE_FORCE_INLINE static auto exec(IteratorBegin orig_begin, IteratorBegin begin, IteratorEnd end, RE) noexcept {
using result_iterator = std::conditional_t<std::is_same_v<ResultIterator, void>, IteratorBegin, ResultIterator>;

constexpr bool fixed = starts_with_anchor(Modifier{}, ctll::list<RE>{});
constexpr bool fixed = starts_with_anchor(Modifier{}, ctll::list<RE>{}) || has_implicit_anchor(ctll::list<RE>{});

auto it = begin;

Expand Down
68 changes: 67 additions & 1 deletion single-header/ctre-unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2720,6 +2720,72 @@ constexpr bool starts_with_anchor(const flags & f, ctll::list<capture_with_name<

#endif

#ifndef CTRE__HAS_IMPLICIT_ANCHOR__HPP
#define CTRE__HAS_IMPLICIT_ANCHOR__HPP

namespace ctre {

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<repeat<0, 0, any>, Content...>) noexcept {
return true;
}
template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<possessive_repeat<0, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<possessive_repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<lazy_repeat<0, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<lazy_repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<sequence<Content...>, Tail...>) noexcept {
return has_implicit_anchor(ctll::list<Content..., Tail...>{});
}

//TODO: we may check captures as implicit anchors*, but they must not be backreferenced because for example "(.+)X\1" will not work properly with "1234X2345"
/*
template<size_t Id, typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<capture<Id, Content...>, Tail...>) noexcept {
//Id must not be backreferenced
return !id_backreference(Id) && has_implicit_anchor(ctll::list<Content..., Tail...>{});
}

template<size_t Id, typename Name, typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<capture_with_name<Id, Name, Content...>, Tail...>) noexcept {
//Id must not be backreferenced
return !id_backreference(Id) && has_implicit_anchor(ctll::list<Content..., Tail...>{});
}
*/

template<typename... Opts, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<select<Opts...>, Tail...>) noexcept {
return (has_implicit_anchor(ctll::list<Opts, Tail...>{}) && ...);
}

constexpr bool has_implicit_anchor(...) noexcept {
return false;
}
}

#endif

#ifndef CTRE__RETURN_TYPE__HPP
#define CTRE__RETURN_TYPE__HPP

Expand Down Expand Up @@ -4878,7 +4944,7 @@ struct search_method {
template <typename Modifier = singleline, typename ResultIterator = void, typename RE, typename IteratorBegin, typename IteratorEnd> constexpr CTRE_FORCE_INLINE static auto exec(IteratorBegin orig_begin, IteratorBegin begin, IteratorEnd end, RE) noexcept {
using result_iterator = std::conditional_t<std::is_same_v<ResultIterator, void>, IteratorBegin, ResultIterator>;

constexpr bool fixed = starts_with_anchor(Modifier{}, ctll::list<RE>{});
constexpr bool fixed = starts_with_anchor(Modifier{}, ctll::list<RE>{}) || has_implicit_anchor(ctll::list<RE>{});

auto it = begin;

Expand Down
68 changes: 67 additions & 1 deletion single-header/ctre.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,72 @@ constexpr bool starts_with_anchor(const flags & f, ctll::list<capture_with_name<

#endif

#ifndef CTRE__HAS_IMPLICIT_ANCHOR__HPP
#define CTRE__HAS_IMPLICIT_ANCHOR__HPP

namespace ctre {

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<repeat<0, 0, any>, Content...>) noexcept {
return true;
}
template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<possessive_repeat<0, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<possessive_repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<lazy_repeat<0, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content>
constexpr bool has_implicit_anchor(ctll::list<lazy_repeat<1, 0, any>, Content...>) noexcept {
return true;
}

template<typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<sequence<Content...>, Tail...>) noexcept {
return has_implicit_anchor(ctll::list<Content..., Tail...>{});
}

//TODO: we may check captures as implicit anchors*, but they must not be backreferenced because for example "(.+)X\1" will not work properly with "1234X2345"
/*
template<size_t Id, typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<capture<Id, Content...>, Tail...>) noexcept {
//Id must not be backreferenced
return !id_backreference(Id) && has_implicit_anchor(ctll::list<Content..., Tail...>{});
}

template<size_t Id, typename Name, typename... Content, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<capture_with_name<Id, Name, Content...>, Tail...>) noexcept {
//Id must not be backreferenced
return !id_backreference(Id) && has_implicit_anchor(ctll::list<Content..., Tail...>{});
}
*/

template<typename... Opts, typename... Tail>
constexpr bool has_implicit_anchor(ctll::list<select<Opts...>, Tail...>) noexcept {
return (has_implicit_anchor(ctll::list<Opts, Tail...>{}) && ...);
}

constexpr bool has_implicit_anchor(...) noexcept {
return false;
}
}

#endif

#ifndef CTRE__RETURN_TYPE__HPP
#define CTRE__RETURN_TYPE__HPP

Expand Down Expand Up @@ -4875,7 +4941,7 @@ struct search_method {
template <typename Modifier = singleline, typename ResultIterator = void, typename RE, typename IteratorBegin, typename IteratorEnd> constexpr CTRE_FORCE_INLINE static auto exec(IteratorBegin orig_begin, IteratorBegin begin, IteratorEnd end, RE) noexcept {
using result_iterator = std::conditional_t<std::is_same_v<ResultIterator, void>, IteratorBegin, ResultIterator>;

constexpr bool fixed = starts_with_anchor(Modifier{}, ctll::list<RE>{});
constexpr bool fixed = starts_with_anchor(Modifier{}, ctll::list<RE>{}) || has_implicit_anchor(ctll::list<RE>{});

auto it = begin;

Expand Down