REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::compat::re2::Arg Class Reference

A type-erased destination for one captured submatch, built implicitly from T*. More...

#include <arg.hpp>

Public Types

using Parser = bool(*)(const char *text, std::size_t length, void *dest)
 Writes the [text, text + length) submatch into dest, or fails.
 

Public Member Functions

constexpr Arg () noexcept
 Default-constructs a no-op Arg (same as Arg(nullptr)).
 
constexpr Arg (std::nullptr_t) noexcept
 From nullptr — skips this submatch (always "succeeds", writes nothing).
 
template<typename T >
 Arg (T *dest) noexcept
 From a destination pointer — the common case (&i, &s, &d, …).
 
constexpr Arg (void *dest, Parser parser) noexcept
 From an explicit destination and parser — the escape hatch for a custom radix or a caller-supplied type not covered by the T* constructor.
 
bool parse (const char *text, std::size_t length) const
 Parses [text, text + length) into the destination.
 

Static Private Member Functions

static bool parse_nothing (const char *, std::size_t, void *)
 The nullptr-destination parser: always succeeds, writes nothing.
 
static bool parse_bool (const char *text, std::size_t length, bool &out)
 Parses into a bool: "1"/"true" -> true, "0"/"false" -> false, else fails.
 
template<typename T >
static bool parse_integral (const char *text, std::size_t length, T &out)
 Parses into an integral type via std::from_chars (base 10, locale-free).
 
template<typename T >
static bool parse_floating (const char *text, std::size_t length, T &out)
 Parses into a floating-point type via the strto* matching T, on a NUL-terminated buffer.
 
template<typename T >
static bool parse_into (const char *text, std::size_t length, void *dest)
 Dispatches to the parser for T — the function stored as this Arg's Parser.
 

Private Attributes

void * dest_
 The type-erased destination (nullptr = skip this submatch).
 
Parser parser_
 The parser to call on parse (never nullptr).
 

Detailed Description

A type-erased destination for one captured submatch, built implicitly from T*.

Constructed implicitly from a pointer to any supported destination type (std::string, std::string_view, bool, or an integral/floating-point type), or from nullptr to skip a group without extracting it. FullMatch/PartialMatch/Consume/FindAndConsume take these by value in a variadic pack; each successful parse writes into the pointee, a failed one (a submatch that does not convert, e.g. text into an int) fails the whole match call.

Constructor & Destructor Documentation

◆ Arg() [1/2]

template<typename T >
real::compat::re2::Arg::Arg ( T *  dest)
inlinenoexcept

From a destination pointer — the common case (&i, &s, &d, …).

Template Parameters
TThe pointee type: std::string, std::string_view, bool, or an integral/floating-point type.
Parameters
[in]destThe destination; must outlive the call this Arg is passed to.

◆ Arg() [2/2]

constexpr real::compat::re2::Arg::Arg ( void *  dest,
Parser  parser 
)
inlineconstexprnoexcept

From an explicit destination and parser — the escape hatch for a custom radix or a caller-supplied type not covered by the T* constructor.

Parameters
[in]destThe destination (opaque to Arg; interpreted only by parser).
[in]parserThe parser called on parse.

Member Function Documentation

◆ parse()

bool real::compat::re2::Arg::parse ( const char *  text,
std::size_t  length 
) const
inline

Parses [text, text + length) into the destination.

Parameters
[in]textThe submatch's first byte (not necessarily NUL-terminated).
[in]lengthThe submatch's length in bytes.
Returns
true on success (or a skipped/nullptr destination); false if the submatch does not convert to the destination type.

◆ parse_bool()

static bool real::compat::re2::Arg::parse_bool ( const char *  text,
std::size_t  length,
bool &  out 
)
inlinestaticprivate

Parses into a bool: "1"/"true" -> true, "0"/"false" -> false, else fails.

Parameters
[in]textThe submatch's first byte.
[in]lengthThe submatch's length in bytes.
[out]outSet on success only.
Returns
true on success.

◆ parse_floating()

template<typename T >
static bool real::compat::re2::Arg::parse_floating ( const char *  text,
std::size_t  length,
T &  out 
)
inlinestaticprivate

Parses into a floating-point type via the strto* matching T, on a NUL-terminated buffer.

Not std::from_chars: some standard libraries explicitly delete its floating-point overload, so the strto* family is the portable floor rather than a preference. They are locale-sensitive in general, but this call site only ever sees digits, ., e, + and - from a regex submatch, and on those every locale agrees with "C".

The conversion is picked to match T rather than always going through double: parsing a float with strtod would accept a value the destination cannot hold and silently narrow it to infinity, where strtof reports the overflow through ERANGE. RE2 dispatches the same way.

Template Parameters
TA floating-point destination type.
Parameters
[in]textThe submatch's first byte.
[in]lengthThe submatch's length in bytes.
[out]outSet on success only.
Returns
true when the whole submatch is consumed and the value is within T's range.

◆ parse_integral()

template<typename T >
static bool real::compat::re2::Arg::parse_integral ( const char *  text,
std::size_t  length,
T &  out 
)
inlinestaticprivate

Parses into an integral type via std::from_chars (base 10, locale-free).

Template Parameters
TAn integral destination type.
Parameters
[in]textThe submatch's first byte.
[in]lengthThe submatch's length in bytes.
[out]outSet on success only.
Returns
true on success (the whole submatch consumed, no overflow).

◆ parse_into()

template<typename T >
static bool real::compat::re2::Arg::parse_into ( const char *  text,
std::size_t  length,
void *  dest 
)
inlinestaticprivate

Dispatches to the parser for T — the function stored as this Arg's Parser.

Template Parameters
TThe destination type.
Parameters
[in]textThe submatch's first byte.
[in]lengthThe submatch's length in bytes.
[out]destThe destination, reinterpreted as T*.
Returns
true on success.

◆ parse_nothing()

static bool real::compat::re2::Arg::parse_nothing ( const char *  ,
std::size_t  ,
void *   
)
inlinestaticprivate

The nullptr-destination parser: always succeeds, writes nothing.

Returns
true, unconditionally.

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