PCRE2 C++ Wrapper 1.3.0
pcre2cpp
Loading...
Searching...
No Matches
match_result.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_MATCH_RESULT_HPP_
17 #define _PCRE2CPP_MATCH_RESULT_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/types.hpp>
28 #include <pcre2cpp/utils/pcre2_data.hpp>
29
30namespace pcre2cpp {
31 #pragma region MATCH_VALUE
32
33 /**
34 * @brief Match value container
35 * @ingroup pcre2cpp
36 * @tparam utf UTF type
37 */
38 template<utf_type utf>
40 private:
41 using _string_type = typename utils::pcre2_data<utf>::string_type;
42
43 public:
44 /// @brief offset relative to search offset
46 /// @brief match value
48 };
49
52 #endif
55 #endif
58 #endif
59
61 #pragma endregion
62
63 #pragma region SUB_MATCH_VALUE
64
65 /**
66 * @brief Sub match value container
67 * @ingroup pcre2cpp
68 */
70 /// @brief offset relative to search offset
72 /// @brief size of value
73 size_t size;
74 };
75
76 #pragma endregion
77
78 /**
79 * @brief Basic container to result data of match function
80 * @ingroup pcre2cpp
81 * @tparam utf UTF type
82 */
83 template<utf_type utf>
85 public:
86 /// @brief error offset (returned when value doesn't exist or when error has occurred)
87 static _PCRE2CPP_CONSTEXPR17 size_t bad_offset = std::numeric_limits<size_t>::max();
88
89 private:
92 using _code_ptr = std::shared_ptr<_code_type>;
98 #endif
101
102 /// @brief Result data container
104 /// @brief keeps search offset
105 size_t search_offset = bad_offset;
106 /// @brief keeps whole result
107 _match_value result = { bad_offset, _string_type() };
108 /// @brief keeps sub results pointers
110 /// @brief keeps named sub values mapping
112 /// @brief keeps regex code data in case regex object was destroyed
113 _code_ptr code = nullptr;
114 };
115
116 /// @brief Result data
118
119 /// @brief returns out of bounds error in correct utf format
122 if _PCRE2CPP_CONSTEXPR17 (utf == utf_type::UTF_8) { return "Subexpression index out of bounds or has no value"; }
123 else
124 #endif
127 return u"Subexpression index out of bounds or has no value";
128 }
129 else
130 #endif
133 return U"Subexpression index out of bounds or has no value";
134 }
135 else
136 #endif
137 {
138 return _string_view_type();
139 }
140 }
141
142 /// @brief returns subexpression not found error in correct utf format
146 return fmt::format("Subexpression with provided name '{}' not found", name);
147 }
148 else
149 #endif
152 return fmt::format(u"Subexpression with provided name '{}' not found", name);
153 }
154 else
155 #endif
158 return fmt::format(U"Subexpression with provided name '{}' not found", name);
159 }
160 else
161 #endif
162 {
163 return _string_type();
164 }
165 }
166
167 /// @brief returns true if sub result group with given name exists in named groups table
172 #else
174 #endif
175 }
176
177 /// @brief returns true if sub result has value and idx wasn't out of bounds
178 _PCRE2CPP_CONSTEXPR17 bool _has_sub_value(const size_t idx) const noexcept {
180 return idx < subResults.size() && subResults[idx].has_value();
181 }
182
183 /// @brief returns group index of group with given name
192
193 /// @brief returns sub value data of group with provided index
202
203 public:
204 #pragma region CONSTRUCTORS
205 /// @brief default constructor
207
208 /// @brief constructor with error code
210
211 /// @brief constructor with no value but also without error
212 _PCRE2CPP_CONSTEXPR17 basic_match_result(const size_t search_offset, const _named_sub_values_table_ptr& named_sub_values,
213 const _code_ptr& regex_compiled_code) noexcept
218 }) {}
219
220 /// @brief constructor with good result
231
232 /// @brief default copy constructor
234 /// @brief default move constructor
236 #pragma endregion
237
238 /// @brief default destructor
240
241 /// @brief default copy assign operator
243 /// @brief default move assign operator
245
246 #pragma region ERRORS
247
248 /// @brief returns true if result holds error
250
251 /// @brief return error code
253 if (!has_error()) { return match_error_codes::None; }
254 return std::get<match_error_codes>(_data);
255 }
256
257 /// @brief returns error message
259 if (!has_error()) { return _string_type(); }
260 return pcre2cpp::generate_error_message<utf>(static_cast<int>(std::get<match_error_codes>(_data)));
261 }
262
264 /// @brief throws error if result holds error with error message based on error code
266 if (!has_error()) { return; }
267 throw _match_result_exception(static_cast<int>(std::get<match_error_codes>(_data)));
268 }
269 #endif
270
271 #pragma endregion ERRORS
272
273 #pragma region RESULTS
274
275 /// @brief returns true when result holds some result not error
277
278 /// @brief returns true when result has value
279 _PCRE2CPP_CONSTEXPR17 bool has_value() const noexcept {
280 if (!has_result()) { return false; }
282 }
283
284 /// @brief returns true when result has sub value on given index
285 _PCRE2CPP_CONSTEXPR17 bool has_sub_value(const size_t idx) const noexcept {
286 if (!has_value()) { return false; }
287 return _has_sub_value(idx);
288 }
289
290 /// @brief returns true when result has sub value with given name
294
295 /// @brief returns search offset
296 _PCRE2CPP_CONSTEXPR17 size_t get_search_offset() const noexcept {
297 if (!has_result()) { return bad_offset; }
299 }
300
301 #pragma region RESULT
302
303 /// @brief returns match result container
305 if (!has_value()) { return { bad_offset, _string_type() }; }
307 }
308
309 /// @brief returns offset of value from the beginning of searched string
311 if (!has_value()) { return bad_offset; }
312 const auto& value = std::get<_value_result_data>(_data);
314 }
315
316 /// @brief return offset relative to search offset
318 if (!has_value()) { return bad_offset; }
320 }
321
322 /// @brief returns size of match value
323 _PCRE2CPP_CONSTEXPR17 size_t get_result_size() const noexcept {
324 if (!has_value()) { return 0; }
326 }
327
328 /// @brief returns match string value
330 if (!has_value()) { return _string_type(); }
332 }
333
334 #pragma endregion RESULT
335
336 #pragma region ALL_SUB_RESULTS
337
338 /// @brief returns all sub results
340 if (!has_value()) { return {}; }
342 }
343
344 /// @brief returns sub results count
346
347 /// @brief returns sub results offsets from the beginning of search string
349 if (!has_value()) { return {}; }
350
351 const auto& value = std::get<_value_result_data>(_data);
352
355 for (const auto& subResult : value.sub_results) {
356 if (subResult.has_value()) {
358 }
359 else { offsets.push_back(bad_offset); }
360 }
361 return offsets;
362 }
363
364 /// @brief returns sub results offsets relative to search offset
366 if (!has_value()) { return {}; }
367
368 const auto& value = std::get<_value_result_data>(_data);
369
372 for (const auto& subResult : value.sub_results) {
373 if (subResult.has_value()) {
375 }
376 else { offsets.push_back(bad_offset); }
377 }
378 return offsets;
379 }
380
381 /// @brief returns sub results offsets relative to result offset
383 if (!has_value()) { return {}; }
384
386
389 for (const auto& subResult : sub_results) {
391 else { offsets.push_back(bad_offset); }
392 }
393 return offsets;
394 }
395
396 /// @brief returns sub results value sizes
398 if (!has_value()) { return {}; }
399
401
404 for (const auto& subResult : sub_results) {
406 else { values.push_back(0); }
407 }
408 return values;
409 }
410
411 /// @brief returns sub results string values
413 if (!has_value()) { return {}; }
414
415 const auto& data = std::get<_value_result_data>(_data);
416 const auto& value = data.result.value;
417 const auto& sub_results = data.sub_results;
418
421 for (const auto& subResult : sub_results) {
422 if (subResult.has_value()) {
424 }
425 else { values.push_back(_string_type()); }
426 }
427 return values;
428 }
429
430 #pragma endregion ALL_SUB_RESULTS
431
432 #pragma region SUB_RESULTS_BY_IDX
433
434 /// @brief returns sub result container
438
439 /// @brief returns sub result offset from the beginning of searched string
447
448 /// @brief returns sub result offset relative to search offset
456
457 /// @brief returns sub result offset relative to result offset
462
463 /// @brief returns sub result value size
468
469 /// @brief returns sub result string value
477
478 #pragma endregion
479
480 #pragma region SUB_RESULTS_BY_NAME
481
482 /// @brief returns sub result container
486
487 /// @brief returns sub result offset from the beginning of searched string
491
492 /// @brief returns sub result offset relative to search offset
496
497 /// @brief returns sub result offset relative to result offset
501
502 /// @brief returns sub result value size
506
507 /// @brief returns sub result string value
511
512 #pragma endregion
513
514 #pragma endregion RESULTS
515 };
516
519 #endif
522 #endif
525 #endif
526
528} // namespace pcre2cpp
529 #endif
530#endif
Basic container to result data of match function.
Definition match_result.hpp:84
_PCRE2CPP_CONSTEXPR17 bool has_result() const noexcept
returns true when result holds some result not error
Definition match_result.hpp:276
_PCRE2CPP_CONSTEXPR17 match_error_codes get_error_code() const noexcept
return error code
Definition match_result.hpp:252
_PCRE2CPP_CONSTEXPR17 basic_match_result(basic_match_result &&other) noexcept=default
default move constructor
typename _pcre2_data_t::string_type _string_type
Definition match_result.hpp:93
_PCRE2CPP_CONSTEXPR17 basic_match_result & operator=(const basic_match_result &other) noexcept=default
default copy assign operator
_PCRE2CPP_CONSTEXPR17 bool has_sub_value(const _string_view_type name) const noexcept
returns true when result has sub value with given name
Definition match_result.hpp:291
_PCRE2CPP_CONSTEXPR17 basic_match_result(const match_error_codes error_code) noexcept
constructor with error code
Definition match_result.hpp:209
_PCRE2CPP_CONSTEXPR17 size_t get_search_offset() const noexcept
returns search offset
Definition match_result.hpp:296
_PCRE2CPP_CONSTEXPR17 basic_match_result() noexcept=default
default constructor
typename _pcre2_data_t::named_sub_values_table _named_sub_values_table
Definition match_result.hpp:99
_PCRE2CPP_CONSTEXPR17 basic_match_result & operator=(basic_match_result &&other) noexcept=default
default move assign operator
_PCRE2CPP_CONSTEXPR17 size_t _get_named_sub_result_idx(const _string_view_type name) const _PCRE2CPP_NOEXCEPT
returns group index of group with given name
Definition match_result.hpp:184
std::variant< match_error_codes, _value_result_data > _data
Result data.
Definition match_result.hpp:117
_PCRE2CPP_CONSTEXPR17 basic_match_result(const basic_match_result &other) noexcept=default
default copy constructor
std::shared_ptr< _named_sub_values_table > _named_sub_values_table_ptr
Definition match_result.hpp:100
std::shared_ptr< _code_type > _code_ptr
Definition match_result.hpp:92
basic_match_value< utf > _match_value
Definition match_result.hpp:95
_PCRE2CPP_CONSTEXPR17 const sub_match_value & _get_sub_value(const size_t idx) const _PCRE2CPP_NOEXCEPT
returns sub value data of group with provided index
Definition match_result.hpp:194
static _PCRE2CPP_CONSTEXPR17 size_t bad_offset
error offset (returned when value doesn't exist or when error has occurred)
Definition match_result.hpp:87
typename _pcre2_data_t::code_type _code_type
Definition match_result.hpp:91
_PCRE2CPP_CONSTEXPR17 _string_type get_error_message() const noexcept
returns error message
Definition match_result.hpp:258
_PCRE2CPP_CONSTEXPR17 _match_value get_result() const noexcept
returns match result container
Definition match_result.hpp:304
_PCRE2CPP_CONSTEXPR17 size_t get_result_global_offset() const noexcept
returns offset of value from the beginning of searched string
Definition match_result.hpp:310
_PCRE2CPP_CONSTEXPR17 bool has_error() const noexcept
returns true if result holds error
Definition match_result.hpp:249
_PCRE2CPP_CONSTEXPR17 _string_type get_result_value() const noexcept
returns match string value
Definition match_result.hpp:329
static _PCRE2CPP_CONSTEXPR17 _string_view_type _get_out_of_bounds_string() noexcept
returns out of bounds error in correct utf format
Definition match_result.hpp:120
_PCRE2CPP_CONSTEXPR17 size_t get_result_size() const noexcept
returns size of match value
Definition match_result.hpp:323
_PCRE2CPP_CONSTEXPR17 size_t get_result_relative_offset() const noexcept
return offset relative to search offset
Definition match_result.hpp:317
_PCRE2CPP_CONSTEXPR20 ~basic_match_result() noexcept=default
default destructor
_PCRE2CPP_CONSTEXPR17 bool has_value() const noexcept
returns true when result has value
Definition match_result.hpp:279
_PCRE2CPP_CONSTEXPR17 basic_match_result(const size_t search_offset, const _named_sub_values_table_ptr &named_sub_values, const _code_ptr &regex_compiled_code) noexcept
constructor with no value but also without error
Definition match_result.hpp:212
static _PCRE2CPP_CONSTEXPR17 _string_type _get_subexpression_not_found(const _string_view_type name) noexcept
returns subexpression not found error in correct utf format
Definition match_result.hpp:143
typename _pcre2_data_t::string_view_type _string_view_type
Definition match_result.hpp:94
_PCRE2CPP_CONSTEXPR17 bool has_sub_value(const size_t idx) const noexcept
returns true when result has sub value on given index
Definition match_result.hpp:285
_PCRE2CPP_CONSTEXPR17 basic_match_result(const size_t search_offset, const _match_value &result, const std::vector< std::optional< sub_match_value > > &sub_results, const _named_sub_values_table_ptr &named_sub_values, const _code_ptr &regex_compiled_code) noexcept
constructor with good result
Definition match_result.hpp:221
utils::pcre2_data< utf > _pcre2_data_t
Definition match_result.hpp:90
_PCRE2CPP_CONSTEXPR17 bool _has_sub_value(const size_t idx) const noexcept
returns true if sub result has value and idx wasn't out of bounds
Definition match_result.hpp:178
_PCRE2CPP_CONSTEXPR17 bool _has_named_sub_result(const _string_view_type name) const noexcept
returns true if sub result group with given name exists in named groups table
Definition match_result.hpp:168
base pcre2cpp exception class
Definition exceptions.hpp:134
#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
match_error_codes
Error codes which can be returned when match fail (now not used but maybe in future).
Definition match_error_codes.hpp:31
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_CXX20
check if compiler has c++ version greater or equal to c++20 and if user enabled c++20 features using ...
Definition config.hpp:145
#define _PCRE2CPP_HAS_EXCEPTIONS
check if exceptions are enabled
Definition config.hpp:171
Definition types.hpp:30
basic_match_result< utf_type::UTF_8 > u8match_result
Definition match_result.hpp:518
basic_match_result< utf_type::UTF_32 > u32match_result
Definition match_result.hpp:524
basic_match_result< utf_type::UTF_16 > u16match_result
Definition match_result.hpp:521
basic_match_value< default_utf_type > match_value
Definition match_result.hpp:60
basic_match_value< utf_type::UTF_16 > u16match_value
Definition match_result.hpp:54
basic_match_result< default_utf_type > match_result
Definition match_result.hpp:527
basic_match_value< utf_type::UTF_32 > u32match_value
Definition match_result.hpp:57
basic_match_value< utf_type::UTF_8 > u8match_value
Definition match_result.hpp:51
Result data container.
Definition match_result.hpp:103
size_t search_offset
keeps search offset
Definition match_result.hpp:105
_match_value result
keeps whole result
Definition match_result.hpp:107
_named_sub_values_table_ptr named_sub_values
keeps named sub values mapping
Definition match_result.hpp:111
_code_ptr code
keeps regex code data in case regex object was destroyed
Definition match_result.hpp:113
Match value container.
Definition match_result.hpp:39
typename utils::pcre2_data< utf >::string_type _string_type
Definition match_result.hpp:41
_string_type value
match value
Definition match_result.hpp:47
size_t relative_offset
offset relative to search offset
Definition match_result.hpp:45
Sub match value container.
Definition match_result.hpp:69
size_t size
size of value
Definition match_result.hpp:73
size_t relative_offset
offset relative to search offset
Definition match_result.hpp:71
Translation container from pcre2 library to pcre2cpp.
Definition pcre2_data.hpp:68