Skip to content

Commit

Permalink
Optimization: Check for implicit anchor
Browse files Browse the repository at this point in the history
Improves code generation for cases where .* can be treated as an anchor
  • Loading branch information
Andersama committed Jul 9, 2021
1 parent eeada60 commit b34a5c2
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 3 deletions.
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

0 comments on commit b34a5c2

Please sign in to comment.