RESTinio
std_regex_engine.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Regex engine for using std::regex.
7 */
8 
9 #pragma once
10 
11 #include <regex>
12 
13 namespace restinio
14 {
15 
16 namespace router
17 {
18 
19 //
20 // std_regex_engine_t
21 //
22 
23 //! Regex engine implementation for using with standard regex implementation.
25 {
29 
30  static constexpr std::size_t
32  {
33  // The size of match results for standard regexes cannot be reserved
34  // and grows as needed, so no limits beforehand.
35  return std::numeric_limits< std::size_t >::max();
36  }
37 
38  //! Create compiled regex object for a given route.
39  static auto
41  //! Regular expression (the pattern).
42  string_view_t r,
43  //! Option for case sensativity.
44  bool is_case_sensative )
45  {
46  auto regex_flags = std::regex::ECMAScript;
47 
48  if( !is_case_sensative )
49  {
50  regex_flags |= std::regex::icase;
51  }
52 
53  return compiled_regex_t{ r.data(), r.size(), regex_flags };
54  }
55 
56  //! Wrapper function for matching logic invokation.
57  static auto
59  string_view_t target_path,
60  const compiled_regex_t & r,
61  match_results_t & match_results )
62  {
63  std::cmatch matches;
64  if(
65  std::regex_search(
66  target_path.data(),
67  target_path.data() + target_path.size(),
68  matches,
69  r ) )
70  {
71  std::transform(
72  matches.begin(),
73  matches.end(),
74  std::back_inserter( match_results ),
75  [ begin = target_path.data() ]( const auto & m ){
76  return matched_item_descriptor_t{ m.first - begin, m.second - begin };
77  } );
78 
79  return true;
80  }
81  return false;
82  }
83 
84  //! Get the beginning of a submatch.
85  static auto
86  submatch_begin_pos( const matched_item_descriptor_t & m )
87  {
88  return m.first;
89  }
90 
91  //! Get the end of a submatch.
92  static auto
93  submatch_end_pos( const matched_item_descriptor_t & m )
94  {
95  return m.second;
96  }
97 };
98 
99 } /* namespace router */
100 
101 } /* namespace restinio */
static auto compile_regex(string_view_t r, bool is_case_sensative)
Create compiled regex object for a given route.
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.
Regex engine implementation for using with standard regex implementation.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition: value_or.hpp:64
static auto submatch_begin_pos(const matched_item_descriptor_t &m)
Get the beginning of a submatch.
static auto submatch_end_pos(const matched_item_descriptor_t &m)
Get the end of a submatch.
static constexpr std::size_t max_capture_groups()