Loading...
Searching...
No Matches
std_regex_engine.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <regex>
namespace restinio
{
namespace router
{
//
// std_regex_engine_t
//
struct std_regex_engine_t
{
using compiled_regex_t = std::regex;
using match_results_t = std::vector< std::pair< std::size_t, size_t > >;
using matched_item_descriptor_t = match_results_t::value_type;
static constexpr std::size_t
max_capture_groups()
{
// The size of match results for standard regexes cannot be reserved
// and grows as needed, so no limits beforehand.
return std::numeric_limits< std::size_t >::max();
}
static auto
compile_regex(
string_view_t r,
bool is_case_sensative )
{
auto regex_flags = std::regex::ECMAScript;
if( !is_case_sensative )
{
regex_flags |= std::regex::icase;
}
return compiled_regex_t{ r.data(), r.size(), regex_flags };
}
static auto
try_match(
string_view_t target_path,
const compiled_regex_t & r,
match_results_t & match_results )
{
std::cmatch matches;
if(
std::regex_search(
target_path.data(),
target_path.data() + target_path.size(),
matches,
r ) )
{
std::transform(
matches.begin(),
matches.end(),
std::back_inserter( match_results ),
[ begin = target_path.data() ]( const auto & m ){
return matched_item_descriptor_t{ m.first - begin, m.second - begin };
} );
return true;
}
return false;
}
static auto
submatch_begin_pos( const matched_item_descriptor_t & m )
{
return m.first;
}
static auto
submatch_end_pos( const matched_item_descriptor_t & m )
{
return m.second;
}
};
} /* namespace router */
} /* namespace restinio */
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::router::std_regex_engine_t
Regex engine implementation for using with standard regex implementation.
Definition: std_regex_engine.hpp:25
restinio::router::std_regex_engine_t::match_results_t
std::vector< std::pair< std::size_t, size_t > > match_results_t
Definition: std_regex_engine.hpp:27
restinio::router::std_regex_engine_t::submatch_end_pos
static auto submatch_end_pos(const matched_item_descriptor_t &m)
Get the end of a submatch.
Definition: std_regex_engine.hpp:93
restinio::router::std_regex_engine_t::matched_item_descriptor_t
match_results_t::value_type matched_item_descriptor_t
Definition: std_regex_engine.hpp:28
restinio::router::std_regex_engine_t::try_match
static auto try_match(string_view_t target_path, const compiled_regex_t &r, match_results_t &match_results)
Wrapper function for matching logic invokation.
Definition: std_regex_engine.hpp:58
restinio::router::std_regex_engine_t::compiled_regex_t
std::regex compiled_regex_t
Definition: std_regex_engine.hpp:26
restinio::router::std_regex_engine_t::submatch_begin_pos
static auto submatch_begin_pos(const matched_item_descriptor_t &m)
Get the beginning of a submatch.
Definition: std_regex_engine.hpp:86
restinio::router::std_regex_engine_t::compile_regex
static auto compile_regex(string_view_t r, bool is_case_sensative)
Create compiled regex object for a given route.
Definition: std_regex_engine.hpp:40
restinio::router::std_regex_engine_t::max_capture_groups
static constexpr std::size_t max_capture_groups()
Definition: std_regex_engine.hpp:31