PCRE2 C++ Wrapper 1.3.0
pcre2cpp
Loading...
Searching...
No Matches
regex.hpp
1/*
2 * pcre2cpp - PCRE2 cpp wrapper
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/pcre2cpp/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 *
9 * PCRE2 library included in this project:
10 * Copyright (c) 2016-2024, University of Cambridge.
11 *
12 * See the LICENSE_PCRE2 file for details: https://github.com/MAIPA01/pcre2cpp/blob/main/LICENSE_PCRE2
13 */
14
15#pragma once
16#ifndef _PCRE2CPP_REGEX_HPP_
17 #define _PCRE2CPP_REGEX_HPP_
18
19 #include <pcre2cpp/config.hpp>
20
22_PCRE2CPP_ERROR("This is only available for c++17 and greater!");
23 #else
24
25 #include <pcre2cpp/exceptions/exceptions.hpp>
26 #include <pcre2cpp/match/match_error_codes.hpp>
27 #include <pcre2cpp/match/match_options.hpp>
28 #include <pcre2cpp/match/match_result.hpp>
29 #include <pcre2cpp/regex/compile_options.hpp>
30 #include <pcre2cpp/types.hpp>
31 #include <pcre2cpp/utils/pcre2_data.hpp>
32
33namespace pcre2cpp {
34 /**
35 * @brief Basic PCRE2 Regex container
36 * @ingroup pcre2cpp
37 * @tparam utf UTF type
38 */
39 template<utf_type utf>
41 private:
43
45 using _code_ptr = std::shared_ptr<_code_type>;
47 using _match_data_ptr = std::shared_ptr<_match_data_type>;
59 #endif
60
61 /// @brief pointer to compiled pcre2 code
62 _code_ptr _code = nullptr;
63 /// @brief pointer to match data of pcre2 code
65 /// @brief pointer to conversion table of named groups to their index
67
68 public:
69 /// @brief basic regex container with pattern and compile options
71 const compile_options opts = compile_options_bits::None) _PCRE2CPP_NOEXCEPT {
72 // Compile Code
73 int error_code;
75
77 &error_code, &error_offset, nullptr);
78
79
81 pcre2cpp_assert(code != nullptr, "Failed to initialize code: {}",
83 #else
84 if (code == nullptr) { throw _regex_exception(error_code, error_offset); }
85 #endif
86
87
89
90 // Get Named Sub Values
92
94 _uchar_type* name_table = nullptr;
96
100
101 for (size_t i = 0; i != name_count; ++i) {
104
106 while (*entry_end != 0 && static_cast<size_t>(entry_end - entry) < name_entry_size - 3) { entry_end += 1; }
108 static_cast<size_t>(index) - 1);
109 }
110
111 // Create Match Data
114 }
115
116 /// @brief default copy constructor
117 _PCRE2CPP_CONSTEXPR17 basic_regex(const basic_regex& other) noexcept = default;
118 /// @brief default move constructor
119 _PCRE2CPP_CONSTEXPR17 basic_regex(basic_regex&& other) noexcept = default;
120
121 /// @brief default destructor
122 _PCRE2CPP_CONSTEXPR20 ~basic_regex() noexcept = default;
123
124 /// @brief default copy assign operator
125 _PCRE2CPP_CONSTEXPR17 basic_regex& operator=(const basic_regex& other) noexcept = default;
126 /// @brief default move assign operator
128
129 /// @brief returns true if match was found
130 _PCRE2CPP_CONSTEXPR17 bool match(const _string_view_type text, const size_t offset = 0,
131 const match_options opts = match_options_bits::None) const _PCRE2CPP_NOEXCEPT {
132 const int match_code = _pcre2_data_t::match(_code.get(), reinterpret_cast<_sptr_type>(text.data()), text.size(),
133 offset, opts, _match_data.get(), nullptr);
134
135 return match_code != static_cast<int>(match_error_codes::NoMatch) && match_code > 0;
136 }
137
138 /// @brief returns true if match was found and result is stored in result variable
139 _PCRE2CPP_CONSTEXPR20 bool match(const _string_view_type text, _match_result_type& result, const size_t offset = 0,
140 const match_options opts = match_options_bits::None) const noexcept {
141 const int match_code = _pcre2_data_t::match(_code.get(), reinterpret_cast<_sptr_type>(text.data()), text.size(),
142 offset, opts, _match_data.get(), nullptr);
143
144 if (match_code == static_cast<int>(match_error_codes::NoMatch) || match_code <= 0) {
146 return false;
147 }
148
150 const size_t matchStart = offsetVector[0];
151 const size_t matchEnd = offsetVector[1];
155 };
156
158
161 for (size_t i = 1; i != offsetVectorsCount; ++i) {
162 const size_t subMatchStart = offsetVector[i * 2];
163 const size_t subMatchEnd = offsetVector[i * 2 + 1];
164
166 else {
170 });
171 }
172 }
173
175 return true;
176 }
177
178 /// @brief returns true if match was found, and it has relative offset == 0
179 _PCRE2CPP_CONSTEXPR17 bool match_at(const _string_view_type text, const size_t offset = 0) const noexcept {
181 return match_at(text, result, offset);
182 }
183
184 /// @brief returns true if match was found, and it has relative offset == 0 and result is stored in result variable
186 const size_t offset = 0) const noexcept {
187 if (!match(text, result, offset)) { return false; }
188
191 return false;
192 }
193
194 return true;
195 }
196
197 /// @brief returns true if any match was found and all results store in results array
198 _PCRE2CPP_CONSTEXPR17 bool match_all(const _string_view_type text, std::vector<_match_result_type>& results,
199 size_t offset = 0) const noexcept {
202 while (match(text, result, offset)) {
207 },
209
211 }
212
213 return results.size() != 0;
214 }
215 };
216
219 #endif
222 #endif
225 #endif
226
228
229 template<utf_type utf = default_utf_type>
230 bool is_pattern_valid(const typename utils::pcre2_data<utf>::string_view_type pattern,
231 const compile_options opts = compile_options_bits::None) noexcept {
233 using code_t = typename pcre2_data_t::code_type;
235
236 // Compile Code
237 int error_code;
239
241 &error_offset, nullptr);
242
243 if (code == nullptr) { return false; }
244
246 return true;
247 }
248} // namespace pcre2cpp
249 #endif
250#endif
Basic container to result data of match function.
Definition match_result.hpp:84
base pcre2cpp exception class
Definition exceptions.hpp:134
Basic PCRE2 Regex container.
Definition regex.hpp:40
typename _pcre2_data_t::match_data_type _match_data_type
Definition regex.hpp:46
typename _pcre2_data_t::string_type _string_type
Definition regex.hpp:48
_PCRE2CPP_CONSTEXPR17 basic_regex(const basic_regex &other) noexcept=default
default copy constructor
_code_ptr _code
pointer to compiled pcre2 code
Definition regex.hpp:62
typename _pcre2_data_t::string_view_type _string_view_type
Definition regex.hpp:49
typename _pcre2_data_t::uchar_type _uchar_type
Definition regex.hpp:56
_PCRE2CPP_CONSTEXPR20 bool match(const _string_view_type text, _match_result_type &result, const size_t offset=0, const match_options opts=match_options_bits::None) const noexcept
returns true if match was found and result is stored in result variable
Definition regex.hpp:139
std::shared_ptr< _named_sub_values_table > _named_sub_values_table_ptr
Definition regex.hpp:55
_PCRE2CPP_CONSTEXPR17 bool match_all(const _string_view_type text, std::vector< _match_result_type > &results, size_t offset=0) const noexcept
returns true if any match was found and all results store in results array
Definition regex.hpp:198
_PCRE2CPP_CONSTEXPR20 basic_regex(const _string_view_type pattern, const compile_options opts=compile_options_bits::None) _PCRE2CPP_NOEXCEPT
basic regex container with pattern and compile options
Definition regex.hpp:70
_named_sub_values_table_ptr _named_sub_values
pointer to conversion table of named groups to their index
Definition regex.hpp:66
_PCRE2CPP_CONSTEXPR17 bool match(const _string_view_type text, const size_t offset=0, const match_options opts=match_options_bits::None) const _PCRE2CPP_NOEXCEPT
returns true if match was found
Definition regex.hpp:130
_match_data_ptr _match_data
pointer to match data of pcre2 code
Definition regex.hpp:64
typename _pcre2_data_t::string_char_type _string_char_type
Definition regex.hpp:50
basic_match_result< utf > _match_result_type
Definition regex.hpp:52
basic_match_value< utf > _match_value_type
Definition regex.hpp:51
_PCRE2CPP_CONSTEXPR17 bool match_at(const _string_view_type text, _match_result_type &result, const size_t offset=0) const noexcept
returns true if match was found, and it has relative offset == 0 and result is stored in result varia...
Definition regex.hpp:185
std::shared_ptr< _code_type > _code_ptr
Definition regex.hpp:45
typename _pcre2_data_t::sptr_type _sptr_type
Definition regex.hpp:53
_PCRE2CPP_CONSTEXPR17 basic_regex & operator=(basic_regex &&other) noexcept=default
default move assign operator
_PCRE2CPP_CONSTEXPR17 basic_regex & operator=(const basic_regex &other) noexcept=default
default copy assign operator
utils::pcre2_data< utf > _pcre2_data_t
Definition regex.hpp:42
std::shared_ptr< _match_data_type > _match_data_ptr
Definition regex.hpp:47
_PCRE2CPP_CONSTEXPR17 basic_regex(basic_regex &&other) noexcept=default
default move constructor
_PCRE2CPP_CONSTEXPR20 ~basic_regex() noexcept=default
default destructor
_PCRE2CPP_CONSTEXPR17 bool match_at(const _string_view_type text, const size_t offset=0) const noexcept
returns true if match was found, and it has relative offset == 0
Definition regex.hpp:179
typename _pcre2_data_t::named_sub_values_table _named_sub_values_table
Definition regex.hpp:54
typename _pcre2_data_t::code_type _code_type
Definition regex.hpp:44
#define _PCRE2CPP_NOEXCEPT
Definition config.hpp:176
static constexpr auto default_utf_type
default utf type for types like regex etc...
Definition pcre2_data.hpp:50
utf_type
Enum with supported utf types.
Definition pcre2_data.hpp:31
@ UTF_16
value for UTF-16 support
Definition pcre2_data.hpp:38
@ UTF_32
value for UTF-32 support
Definition pcre2_data.hpp:42
@ UTF_8
value for UTF-8 support
Definition pcre2_data.hpp:34
#define _PCRE2CPP_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:236
#define _PCRE2CPP_CONSTEXPR20
constexpr keyword for c++20 and higher
Definition config.hpp:253
#define _PCRE2CPP_HAS_UTF32
check if support for UTF-32 is enabled
Definition config.hpp:215
#define _PCRE2CPP_HAS_CXX17
check if compiler has c++ version greater or equal to c++17
Definition config.hpp:133
#define _PCRE2CPP_HAS_UTF8
check if support for UTF-8 is enabled
Definition config.hpp:203
#define _PCRE2CPP_HAS_UTF16
check if support for UTF-16 is enabled
Definition config.hpp:209
#define _PCRE2CPP_HAS_EXCEPTIONS
check if exceptions are enabled
Definition config.hpp:171
Definition types.hpp:30
basic_regex< utf_type::UTF_16 > u16regex
Definition regex.hpp:221
basic_regex< utf_type::UTF_32 > u32regex
Definition regex.hpp:224
basic_regex< default_utf_type > regex
Definition regex.hpp:227
bool is_pattern_valid(const typename utils::pcre2_data< utf >::string_view_type pattern, const compile_options opts=compile_options_bits::None) noexcept
Definition regex.hpp:230
basic_regex< utf_type::UTF_8 > u8regex
Definition regex.hpp:218
Match value container.
Definition match_result.hpp:39
Translation container from pcre2 library to pcre2cpp.
Definition pcre2_data.hpp:68