REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::compat::regex_iterator< BidirIt, CharT, Traits > Class Template Reference

Iterates the non-overlapping matches of a pattern in a sequence (std::regex_iterator). More...

#include <regex_iter.hpp>

Collaboration diagram for real::compat::regex_iterator< BidirIt, CharT, Traits >:
[legend]

Public Types

using value_type = match_results< BidirIt >
 Yielded match.
 
using difference_type = std::ptrdiff_t
 Iterator traits.
 
using pointer = const value_type *
 Arrow type.
 
using reference = const value_type &
 Dereference type.
 
using iterator_category = std::forward_iterator_tag
 std::regex_iterator parity.
 
using regex_type = basic_regex< CharT, Traits >
 The pattern type.
 

Public Member Functions

 regex_iterator ()=default
 Constructs the end sentinel.
 
 regex_iterator (BidirIt first, BidirIt last, const regex_type &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Constructs a begin iterator over [first, last) and finds the first match. A constraining match flag (see detail::real_honors) routes to the std backend, which carries the flags through the wrapped std::regex_iterator.
 
 regex_iterator (const regex_iterator &other)
 Copies the iteration position, but NOT the walker.
 
regex_iteratoroperator= (const regex_iterator &other)
 Copy-assigns the iteration position, dropping any walker this iterator owned.
 
 regex_iterator (regex_iterator &&) noexcept=default
 Moves the walker along with the position.
 
regex_iteratoroperator= (regex_iterator &&) noexcept=default
 Move-assigns, carrying the walker along with the position.
 
 ~regex_iterator ()=default
 Releases the walker, if any.
 
 regex_iterator (BidirIt first, BidirIt last, const regex_type &&re, regex_constants::match_flag_type flags=regex_constants::match_default)=delete
 Constructing from a temporary regex would dangle (std::regex_iterator parity).
 
reference operator* () const
 The current match.
 
pointer operator-> () const
 The current match.
 
regex_iteratoroperator++ ()
 Advances to the next match, becoming the end sentinel when there is none.
 
regex_iterator operator++ (int)
 Advances to the next match, returning the previous position.
 
bool operator== (const regex_iterator &other) const
 Equality. Two non-end iterators compare equal only for the same regex, sequence, flags and current match — not for a coincidental same position across different patterns.
 
bool operator!= (const regex_iterator &other) const
 Inequality, the negation of operator==.
 

Private Types

using walker_type = real::basic_match_iterator< real::detail::dynamic_storage >
 The REAL walker, when this iterator owns one. ACCELERATION ONLY: real_pos_ stays the source of truth, so a null walker costs correctness nothing and only speed.
 

Private Member Functions

void next_real ()
 Advances the real path: one step of the walker, built on first use at real_pos_.
 
void sync_std ()
 Syncs the std path from the wrapped std::regex_iterator.
 

Private Attributes

std::unique_ptr< walker_typewalker_
 Owned, never copied – see the copy constructor.
 
BidirIt begin_ {}
 Start of the sequence.
 
BidirIt end_ {}
 End of the sequence.
 
const regex_typere_ {nullptr}
 The pattern, borrowed.
 
regex_constants::match_flag_type flags_ {regex_constants::match_default}
 Match flags this iteration was built with.
 
bool real_path_ {false}
 Whether the REAL engine drives the traversal rather than the std backend.
 
std::size_t real_pos_ {}
 REAL path: byte offset the next region search starts at.
 
std::optional< std::regex_iterator< BidirIt > > std_it_
 std path: the wrapped iterator (engaged only off the REAL path).
 
value_type match_
 The current match, refilled by each increment.
 
bool at_end_ {true}
 Whether this is the end sentinel.
 

Detailed Description

template<typename BidirIt, typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
class real::compat::regex_iterator< BidirIt, CharT, Traits >

Iterates the non-overlapping matches of a pattern in a sequence (std::regex_iterator).

Same per-operation routing as regex_replace — a real-backed, non-nullable pattern drives real's linear traversal (repeated region search — a non-nullable pattern never matches empty, so the position always advances past the match and the ECMAScript and real sequences agree); the std backend and nullable patterns wrap std::regex_iterator (whose empty-match advance is ECMAScript's). The default-constructed iterator is the end sentinel.

Template Parameters
BidirItA contiguous iterator into the searched sequence.

Constructor & Destructor Documentation

◆ regex_iterator() [1/2]

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
real::compat::regex_iterator< BidirIt, CharT, Traits >::regex_iterator ( BidirIt  first,
BidirIt  last,
const regex_type re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)
inline

Constructs a begin iterator over [first, last) and finds the first match. A constraining match flag (see detail::real_honors) routes to the std backend, which carries the flags through the wrapped std::regex_iterator.

Parameters
[in]firstStart of the character sequence.
[in]lastEnd of the character sequence.
[in]reThe pattern; it must outlive this iterator.
[in]flagsMatch flags, defaulting to regex_constants::match_default.

◆ regex_iterator() [2/2]

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
real::compat::regex_iterator< BidirIt, CharT, Traits >::regex_iterator ( const regex_iterator< BidirIt, CharT, Traits > &  other)
inline

Copies the iteration position, but NOT the walker.

The walker is an accelerator over real_pos_, never the state itself, and that is what makes this cheap where the obvious designs are not. Copying it would clone the VM scratch it embeds; sharing it copy-on-write would clone on the first advance, which operator++(int) performs on every call – buying ++it at the price of it++. Leaving the copy without one costs it a single walker construction IF it ever advances, and nothing at all if it does not, which is what a post-increment's discarded result actually does.

Parameters
[in]otherThe iterator to copy.

Member Function Documentation

◆ next_real()

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
void real::compat::regex_iterator< BidirIt, CharT, Traits >::next_real ( )
inlineprivate

Advances the real path: one step of the walker, built on first use at real_pos_.

Note
ONE walker serves the whole iteration, and that is the point rather than an allocation detail. A match iterator carries the VM state where every per-haystack decision lives – the Aho-Corasick density verdict, the inner-literal density gate, the DFA warmup – so advancing by a fresh search() instead would re-derive all of them once per match, worst on exactly the families whose routing gate is sticky per haystack.
The walker is held behind a pointer and never copied. std::regex_iterator requires copies to be independent and its operator++(int) returns one, so embedding the walker would clone the VM scratch – which is what makes a match iterator an order of magnitude larger than this one – on every post-increment, buying ++it at the price of it++. A copy starts without a walker and builds one only if it advances; see the copy constructor.

◆ operator!=()

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
bool real::compat::regex_iterator< BidirIt, CharT, Traits >::operator!= ( const regex_iterator< BidirIt, CharT, Traits > &  other) const
inline

Inequality, the negation of operator==.

Parameters
[in]otherThe iterator to compare against.
Returns
Whether the two denote different iteration positions.

◆ operator*()

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
reference real::compat::regex_iterator< BidirIt, CharT, Traits >::operator* ( ) const
inline

The current match.

Returns
A reference to it, valid until the next increment.

◆ operator++() [1/2]

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
regex_iterator & real::compat::regex_iterator< BidirIt, CharT, Traits >::operator++ ( )
inline

Advances to the next match, becoming the end sentinel when there is none.

Returns
*this.

◆ operator++() [2/2]

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
regex_iterator real::compat::regex_iterator< BidirIt, CharT, Traits >::operator++ ( int  )
inline

Advances to the next match, returning the previous position.

Returns
A copy of *this as it was before the increment.

◆ operator->()

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
pointer real::compat::regex_iterator< BidirIt, CharT, Traits >::operator-> ( ) const
inline

The current match.

Returns
A pointer to it, valid until the next increment.

◆ operator=() [1/2]

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
regex_iterator & real::compat::regex_iterator< BidirIt, CharT, Traits >::operator= ( const regex_iterator< BidirIt, CharT, Traits > &  other)
inline

Copy-assigns the iteration position, dropping any walker this iterator owned.

Parameters
[in]otherThe iterator to copy.
Returns
*this.

◆ operator=() [2/2]

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
regex_iterator & real::compat::regex_iterator< BidirIt, CharT, Traits >::operator= ( regex_iterator< BidirIt, CharT, Traits > &&  )
defaultnoexcept

Move-assigns, carrying the walker along with the position.

Returns
*this.

◆ operator==()

template<typename BidirIt , typename CharT = typename std::iterator_traits<BidirIt>::value_type, typename Traits = std::regex_traits<CharT>>
bool real::compat::regex_iterator< BidirIt, CharT, Traits >::operator== ( const regex_iterator< BidirIt, CharT, Traits > &  other) const
inline

Equality. Two non-end iterators compare equal only for the same regex, sequence, flags and current match — not for a coincidental same position across different patterns.

Parameters
[in]otherThe iterator to compare against.
Returns
Whether the two denote the same iteration position.

The documentation for this class was generated from the following file: