PCRE2 C++ Wrapper 1.3.0
pcre2cpp
Loading...
Searching...
No Matches
exceptions.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_EXCEPTIONS_HPP_
17 #define _PCRE2CPP_EXCEPTIONS_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/types.hpp>
26 #include <pcre2cpp/utils/pcre2_data.hpp>
28 #include <pcre2cpp/utils/assert.hpp>
29 #endif
30
31namespace pcre2cpp {
32 /**
33 * @brief Function which generate error message based on pcre2 error code
34 * @ingroup pcre2cpp
35 * @tparam utf UTF type
36 * @param error_code pcre2 error code
37 * @return string value of type compatible with UTF type
38 */
39 template<utf_type utf>
41 const int error_code
42 ) noexcept {
44 using _string_type = typename _pcre2_data_t::string_type;
45 using _uchar_type = typename _pcre2_data_t::uchar_type;
47
50 return _string_type(reinterpret_cast<_char_type*>(error_message), 120);
51 }
52 return _string_type();
53 }
54
55 /**
56 * @brief Function which generates error message based on pcre2 error code and with additional information about offset
57 * @ingroup pcre2cpp
58 * @tparam utf UTF type
59 * @param error_code pcre2 error code
60 * @param error_offset offset for example in regex pattern
61 * @return string value of type compatible with UTF type
62 */
63 template<utf_type utf>
64 static _PCRE2CPP_CONSTEXPR17 typename utils::pcre2_data<utf>::string_type generate_error_message(const int error_code,
65 const size_t error_offset) noexcept {
66 using _string_type = typename utils::pcre2_data<utf>::string_type;
67
71 }
72 else
73 #endif
76 return fmt::format(u"error at {} {}", error_offset, pcre2cpp::generate_error_message<utf>(error_code));
77 }
78 else
79 #endif
82 return fmt::format(U"error at {} {}", error_offset, pcre2cpp::generate_error_message<utf>(error_code));
83 }
84 else
85 #endif
86 {
87 return _string_type();
88 }
89 }
90
91 /**
92 * @brief converts any message from any utf to utf-8
93 * @ingroup pcre2cpp
94 * @tparam utf UTF type
95 * @param message message to convert
96 * @return std::string value of message
97 */
98 template<utf_type utf>
99 static _PCRE2CPP_CONSTEXPR20 std::string convert_any_utf_to_utf8(const typename utils::pcre2_data<utf>::string_view_type message) noexcept {
102 else
103 #endif
106 std::string msg;
107 for (const auto& c : message) { msg += static_cast<std::string::value_type>(c); }
108 return msg;
109 }
110 else
111 #endif
114 std::string msg;
115 for (const auto& c : message) { msg += static_cast<std::string::value_type>(c); }
116 return msg;
117 }
118 else
119 #endif
120 {
121 return std::string();
122 }
123 }
124
126 #pragma region PCRE2CPP_EXCEPTION
127
128 /**
129 * @brief base pcre2cpp exception class
130 * @ingroup pcre2cpp
131 * @tparam utf UTF type
132 */
133 template<utf_type utf>
135 private:
141
142 /// @brief error message
144
145 public:
146 /// @brief constructor with message
149
150 /// @brief constructor with error code
154
155 /// @brief constructor with error code and error offset
159
160 /// @brief returns error message
161 _PCRE2CPP_CONSTEXPR17 const _string_type& get_error() const noexcept { return _message; }
162 };
163
164 #if _PCRE2CPP_HAS_UTF8
166 #endif
167 #if _PCRE2CPP_HAS_UTF16
169 #endif
170 #if _PCRE2CPP_HAS_UTF32
172 #endif
173
175
176 #pragma endregion PCRE2CPP_EXCEPTION
177
178 #pragma region REGEX_EXCEPTION
179
180 /**
181 * @brief regex exception class
182 * @ingroup pcre2cpp
183 * @tparam utf UTF type
184 */
185 template<utf_type utf>
187 private:
189
190 public:
191 /// @brief constructor with message
193
194 /// @brief constructor with error code and error offset
197
199 };
200
201 #if _PCRE2CPP_HAS_UTF8
203 #endif
204 #if _PCRE2CPP_HAS_UTF16
206 #endif
207 #if _PCRE2CPP_HAS_UTF32
209 #endif
210
212
213 #pragma endregion REGEX_EXCEPTION
214
215 #pragma region MATCH_RESULT_EXCEPTION
216
217 /**
218 * @brief match result exception class
219 * @ingroup pcre2cpp
220 * @tparam utf UTF type
221 */
222 template<utf_type utf>
224 private:
226
227 public:
228 /// @brief constructor with message
231
232 /// @brief constructor with error code
234
236 };
237
238 #if _PCRE2CPP_HAS_UTF8
240 #endif
241 #if _PCRE2CPP_HAS_UTF16
243 #endif
244 #if _PCRE2CPP_HAS_UTF32
246 #endif
247
249
250 #pragma endregion MATCH_RESULT_EXCEPTION
251 #endif
252} // namespace pcre2cpp
253 #endif
254#endif
base pcre2cpp exception class
Definition exceptions.hpp:134
utf_type
Enum with supported utf types.
Definition pcre2_data.hpp:31
static _PCRE2CPP_CONSTEXPR17 utils::pcre2_data< utf >::string_type generate_error_message(const int error_code) noexcept
Function which generate error message based on pcre2 error code.
Definition exceptions.hpp:40
static _PCRE2CPP_CONSTEXPR17 utils::pcre2_data< utf >::string_type generate_error_message(const int error_code, const size_t error_offset) noexcept
Function which generates error message based on pcre2 error code and with additional information abou...
Definition exceptions.hpp:64
static _PCRE2CPP_CONSTEXPR20 std::string convert_any_utf_to_utf8(const typename utils::pcre2_data< utf >::string_view_type message) noexcept
converts any message from any utf to utf-8
Definition exceptions.hpp:99
#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_ASSERTS
check if asserts are enabled
Definition config.hpp:172
#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
Translation container from pcre2 library to pcre2cpp.
Definition pcre2_data.hpp:68