Doxygen
Loading...
Searching...
No Matches
reg::Match Class Reference

Object representing the matching results. More...

#include <src/regex.h>

Public Member Functions

 Match ()
 Creates an empty match object.
 
size_t position () const
 Returns the position of the match or std::string::npos if no position is set.
 
size_t length () const
 Returns the position of the match or std::string::npos if no length is set.
 
std::string str () const
 Return a string representing the matching part.
 
SubMatch prefix () const
 Return the part of the string before the match.
 
SubMatch suffix () const
 Return the part of the string after the match.
 
size_t size () const
 Returns the number of sub matches available in this match.
 
const SubMatchoperator[] (size_t index) const
 Returns the n-th SubMatch object.
 

Private Member Functions

void init (std::string_view str)
 
void startCapture (size_t index)
 
void endCapture (size_t index)
 
void setMatch (size_t pos, size_t len)
 

Private Attributes

std::vector< SubMatchm_subMatches
 
size_t m_captureIndex =0
 
std::string_view m_str
 
bool m_insideCapture =false
 

Friends

class Ex
 

Detailed Description

Object representing the matching results.

It consists of an array of SubMatch objects. The first entry of the array represents the whole match, any next elements represent each of the capture ranges.

For example string @42 and expression @(\\d+) will have two Submatches, match[0] will point to the input string as a whole, and match[1] will point to the number 42 only.

Definition at line 152 of file regex.h.

Constructor & Destructor Documentation

◆ Match()

reg::Match::Match ( )
inline

Creates an empty match object.

Definition at line 156 of file regex.h.

156{}

Member Function Documentation

◆ endCapture()

void reg::Match::endCapture ( size_t index)
inlineprivate

Definition at line 209 of file regex.h.

210 {
211 if (index>m_subMatches.back().position())
212 {
214 m_subMatches.back().setEnd(index);
215 m_insideCapture = false;
216 }
217 }
bool m_insideCapture
Definition regex.h:226
std::vector< SubMatch > m_subMatches
Definition regex.h:223
size_t m_captureIndex
Definition regex.h:224

References m_captureIndex, m_insideCapture, and m_subMatches.

◆ init()

void reg::Match::init ( std::string_view str)
inlineprivate

Definition at line 192 of file regex.h.

193 {
194 m_subMatches.clear();
195 m_subMatches.emplace_back(str);
196 m_str = str;
197 }
std::string_view m_str
Definition regex.h:225
std::string str() const
Return a string representing the matching part.
Definition regex.h:165

References m_str, m_subMatches, and str().

Referenced by reg::Ex::Private::matchAt().

◆ length()

size_t reg::Match::length ( ) const
inline

Returns the position of the match or std::string::npos if no length is set.

Definition at line 162 of file regex.h.

162{ return m_subMatches[0].length(); }

References m_subMatches.

Referenced by reg::Ex::Private::matchAt(), and suffix().

◆ operator[]()

const SubMatch & reg::Match::operator[] ( size_t index) const
inline

Returns the n-th SubMatch object.

Note that there is always 1 SubMatch object representing the whole match.

Definition at line 188 of file regex.h.

188{ return m_subMatches[index]; }

References m_subMatches.

◆ position()

size_t reg::Match::position ( ) const
inline

Returns the position of the match or std::string::npos if no position is set.

Definition at line 159 of file regex.h.

159{ return m_subMatches[0].position(); }

References m_subMatches.

Referenced by prefix(), and suffix().

◆ prefix()

SubMatch reg::Match::prefix ( ) const
inline

Return the part of the string before the match.

Definition at line 168 of file regex.h.

168{ SubMatch m(m_str); m.setMatch(0,position()); return m; }
size_t position() const
Returns the position of the match or std::string::npos if no position is set.
Definition regex.h:159

References m_str, position(), and reg::SubMatch::setMatch().

◆ setMatch()

void reg::Match::setMatch ( size_t pos,
size_t len )
inlineprivate

Definition at line 218 of file regex.h.

219 {
220 m_subMatches[m_captureIndex].setMatch(pos,len);
221 }

References m_captureIndex, and m_subMatches.

◆ size()

size_t reg::Match::size ( ) const
inline

Returns the number of sub matches available in this match.

Definition at line 183 of file regex.h.

183{ return m_subMatches.size(); }

References m_subMatches.

◆ startCapture()

void reg::Match::startCapture ( size_t index)
inlineprivate

Definition at line 198 of file regex.h.

199 {
200 if (!m_insideCapture) // when backtracking we can re-entry the capture multiple times
201 // only update the index, example `\s*(x)`
202 {
204 m_subMatches.emplace_back(m_str);
205 m_insideCapture = true;
206 }
207 m_subMatches.back().setStart(index);
208 }

References m_captureIndex, m_insideCapture, m_str, and m_subMatches.

◆ str()

std::string reg::Match::str ( ) const
inline

Return a string representing the matching part.

Definition at line 165 of file regex.h.

165{ return std::string{m_subMatches[0].str()}; }

References m_subMatches.

Referenced by containsWord(), init(), and initPredefined().

◆ suffix()

SubMatch reg::Match::suffix ( ) const
inline

Return the part of the string after the match.

Definition at line 171 of file regex.h.

172 {
173 SubMatch m(m_str);
174 if (!m_str.empty())
175 {
176 size_t e = position()+length();
177 m.setMatch(e,m_str.length()-e);
178 }
179 return m;
180 }
size_t length() const
Returns the position of the match or std::string::npos if no length is set.
Definition regex.h:162

References length(), m_str, position(), and reg::SubMatch::setMatch().

Friends And Related Symbol Documentation

◆ Ex

friend class Ex
friend

Definition at line 191 of file regex.h.

References Ex.

Referenced by Ex.

Member Data Documentation

◆ m_captureIndex

size_t reg::Match::m_captureIndex =0
private

Definition at line 224 of file regex.h.

Referenced by endCapture(), setMatch(), and startCapture().

◆ m_insideCapture

bool reg::Match::m_insideCapture =false
private

Definition at line 226 of file regex.h.

Referenced by endCapture(), and startCapture().

◆ m_str

std::string_view reg::Match::m_str
private

Definition at line 225 of file regex.h.

Referenced by init(), prefix(), startCapture(), and suffix().

◆ m_subMatches

std::vector<SubMatch> reg::Match::m_subMatches
private

Definition at line 223 of file regex.h.

Referenced by endCapture(), init(), length(), operator[](), position(), setMatch(), size(), startCapture(), and str().


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