Skip to content

Commit 2a4f716

Browse files
committed
regex_exception update
1 parent 4db6ed1 commit 2a4f716

11 files changed

+359
-145
lines changed
Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*
2-
* regex.cpp
1+
/*
2+
* match_result.hpp
33
* Copyright (c) 2025, Patryk Antosik (MAIPA01)
44
* This file is part of the pcre2cpp project, which is licensed under the BSD 3-Clause License with Attribution Requirement.
5-
*
5+
*
66
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
77
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
88
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution.
@@ -18,15 +18,15 @@
1818
* 8. Exception for large projects: If this code is used as part of a larger project (e.g., an application, framework, or other libraries), permission is not required for the sale of the unmodified version, as long as:
1919
* - The project is more than just a wrapper for the original code.
2020
* - The code is an integral part of the larger project, and the project does not merely involve selling the original code.
21-
*
21+
*
2222
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23-
*
23+
*
2424
* ---
2525
* PCRE2 Exception:
2626
* Notwithstanding the above, you are permitted to link or otherwise combine this software with the PCRE2 library (or a modified version of that library), and distribute the resulting binaries without restriction, provided that you do not modify the PCRE2 library itself.
2727
* Furthermore, the second condition in the BSD license (covering binary redistributions) does not apply to software that includes this wrapper as part of a larger binary package, unless the package itself uses PCRE2 independently.
2828
*/
29-
29+
3030
/*
3131
* This file is part of the pcre2cpp project, which includes code from the PCRE2 library.
3232
* Copyright (c) 2025, Patryk Antosik (MAIPA01)
@@ -44,24 +44,42 @@
4444
* Exemption for binary redistributions applies as per PCRE2's BSD license terms.
4545
*/
4646

47-
#include "pch.hpp"
48-
#include <regex.hpp>
49-
#pragma comment(lib, "pcre2-8-static.lib")
50-
#pragma comment(lib, "pcre2-posix-static.lib")
47+
#pragma once
48+
#include "pcre2cpp_types.hpp"
49+
#include "pcre2_data.hpp"
50+
51+
namespace pcre2cpp {
52+
template<size_t utf>
53+
class basic_match_result {
54+
private:
55+
using _string_type = _pcre2_data<utf>::string_type;
5156

52-
using namespace pcre2cpp;
53-
using namespace std;
57+
size_t _error_code = 0;
58+
size_t _offset = 0;
59+
_string_type _value = _string_type();
5460

55-
regex_exception::regex_exception(int error_code, size_t error_offset)
56-
: runtime_error(_generate_message(error_code, error_offset)) {}
61+
public:
62+
constexpr basic_match_result() = default;
63+
constexpr basic_match_result(size_t error_code)
64+
: _error_code(error_code) {
65+
}
66+
constexpr basic_match_result(size_t offset, const _string_type& value)
67+
: _offset(offset), _value(value) {
68+
}
69+
constexpr basic_match_result(const basic_match_result<utf>& mr)
70+
: _error_code(mr._error_code), _offset(mr._offset), _value(mr._value) {
71+
}
72+
~basic_match_result() = default;
5773

58-
string regex_exception::_generate_message(int error_code, size_t error_offset) {
59-
string message = "error at "s.append(to_string(error_offset)).append(" ");
74+
constexpr basic_match_result<utf>& operator=(const basic_match_result<utf>& mr) {
75+
this->_error_code = mr._error_code;
76+
this->_offset = mr._offset;
77+
this->_value = mr._value;
78+
return *this;
79+
}
6080

61-
unsigned char error_message[256];
62-
int size = pcre2_get_error_message_8(error_code, error_message, 256);
63-
if (size != PCRE2_ERROR_BADDATA) {
64-
message.append(string((const char*)error_message, 256));
65-
}
66-
return message;
81+
constexpr size_t getErrorCode() const { return _error_code; }
82+
constexpr size_t getOffset() const { return _offset; }
83+
constexpr _string_type getValue() const { return _value; }
84+
};
6785
}

include/pcre2_data.hpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* pcre2_data.hpp
3+
* Copyright (c) 2025, Patryk Antosik (MAIPA01)
4+
* This file is part of the pcre2cpp project, which is licensed under the BSD 3-Clause License with Attribution Requirement.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7+
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
8+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution.
9+
* 3. All advertising materials mentioning features or use of this software must display the following acknowledgment:
10+
* "This product includes software developed by Patryk Antosik (MAIPA01) as part of the pcre2cpp project."
11+
* 4. If you modify this software and distribute it, you must include a notice stating that the original work was created by Patryk Antosik (MAIPA01) as part of the pcre2cpp project.
12+
* 5. Neither the name of the author nor the name "pcre2cpp" may be used to endorse or promote products derived from this software without specific prior written permission.
13+
* 6. Sale of unmodified code: The unmodified version of this software may not be sold without the explicit permission of the author.
14+
* 7. Sale of modified code: Modified versions of the code may be sold, provided that:
15+
* - Proper attribution to Patryk Antosik (MAIPA01) as the original author is maintained.
16+
* - The modified code is clearly marked as a modified version.
17+
* - The modified code must be accompanied by documentation that includes references to the original version and its author.
18+
* 8. Exception for large projects: If this code is used as part of a larger project (e.g., an application, framework, or other libraries), permission is not required for the sale of the unmodified version, as long as:
19+
* - The project is more than just a wrapper for the original code.
20+
* - The code is an integral part of the larger project, and the project does not merely involve selling the original code.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
*
24+
* ---
25+
* PCRE2 Exception:
26+
* Notwithstanding the above, you are permitted to link or otherwise combine this software with the PCRE2 library (or a modified version of that library), and distribute the resulting binaries without restriction, provided that you do not modify the PCRE2 library itself.
27+
* Furthermore, the second condition in the BSD license (covering binary redistributions) does not apply to software that includes this wrapper as part of a larger binary package, unless the package itself uses PCRE2 independently.
28+
*/
29+
30+
/*
31+
* This file is part of the pcre2cpp project, which includes code from the PCRE2 library.
32+
* Copyright (c) 2025, Patryk Antosik (MAIPA01)
33+
* This project is licensed under the BSD 3-Clause License with Attribution Requirement.
34+
*
35+
* PCRE2 library included in this project:
36+
* Copyright (c) 2016-2024, University of Cambridge.
37+
* All rights reserved.
38+
*
39+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
40+
* 1. Redistributions of source code must retain the above copyright notices, this list of conditions, and the following disclaimer.
41+
* 2. Redistributions in binary form must reproduce the above copyright notices, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution.
42+
* 3. Neither the name of the University of Cambridge nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
43+
*
44+
* Exemption for binary redistributions applies as per PCRE2's BSD license terms.
45+
*/
46+
47+
#pragma once
48+
#include "pcre2cpp_types.hpp"
49+
50+
namespace pcre2cpp {
51+
template<size_t utf>
52+
struct _pcre2_data {};
53+
54+
template<>
55+
struct _pcre2_data<8> {
56+
using code_type = pcre2_code_8;
57+
using string_ptr_type = PCRE2_SPTR8;
58+
using compile_ctx_type = pcre2_compile_context_8;
59+
using general_ctx_type = pcre2_general_context_8;
60+
using match_ctx_type = pcre2_match_context_8;
61+
using match_data_type = pcre2_match_data_8;
62+
using string_type = std::string;
63+
using string_char_type = char;
64+
using uchar_type = PCRE2_UCHAR8;
65+
66+
static constexpr size_t utf = 8;
67+
static constexpr code_type* (*compile)(string_ptr_type, size_t,
68+
uint32_t, int*, size_t*, compile_ctx_type*) = pcre2_compile_8;
69+
static constexpr match_data_type* (*match_data_from_pattern)
70+
(const code_type*, general_ctx_type*) = pcre2_match_data_create_from_pattern_8;
71+
static constexpr int (*match)(const code_type*, string_ptr_type,
72+
size_t, size_t, uint32_t, match_data_type*, match_ctx_type*) = pcre2_match_8;
73+
static constexpr size_t* (*get_ovector_ptr)(match_data_type*) = pcre2_get_ovector_pointer_8;
74+
static constexpr void(*match_data_free)(match_data_type*) = pcre2_match_data_free_8;
75+
static constexpr void(*code_free)(code_type*) = pcre2_code_free_8;
76+
static constexpr int(*get_error_message)(int, uchar_type*, size_t) = pcre2_get_error_message_8;
77+
};
78+
79+
template<>
80+
struct _pcre2_data<16> {
81+
using code_type = pcre2_code_16;
82+
using string_ptr_type = PCRE2_SPTR16;
83+
using compile_ctx_type = pcre2_compile_context_16;
84+
using general_ctx_type = pcre2_general_context_16;
85+
using match_ctx_type = pcre2_match_context_16;
86+
using match_data_type = pcre2_match_data_16;
87+
using string_type = std::wstring;
88+
using string_char_type = wchar_t;
89+
using uchar_type = PCRE2_UCHAR16;
90+
91+
static constexpr size_t utf = 16;
92+
static constexpr code_type* (*compile)(string_ptr_type, size_t,
93+
uint32_t, int*, size_t*, compile_ctx_type*) = pcre2_compile_16;
94+
static constexpr match_data_type* (*match_data_from_pattern)
95+
(const code_type*, general_ctx_type*) = pcre2_match_data_create_from_pattern_16;
96+
static constexpr int (*match)(const code_type*, string_ptr_type,
97+
size_t, size_t, uint32_t, match_data_type*, match_ctx_type*) = pcre2_match_16;
98+
static constexpr size_t* (*get_ovector_ptr)(match_data_type*) = pcre2_get_ovector_pointer_16;
99+
static constexpr void(*match_data_free)(match_data_type*) = pcre2_match_data_free_16;
100+
static constexpr void(*code_free)(code_type*) = pcre2_code_free_16;
101+
static constexpr int(*get_error_message)(int, uchar_type*, size_t) = pcre2_get_error_message_16;
102+
};
103+
104+
template<>
105+
struct _pcre2_data<32> {
106+
using code_type = pcre2_code_32;
107+
using string_ptr_type = PCRE2_SPTR32;
108+
using compile_ctx_type = pcre2_compile_context_32;
109+
using general_ctx_type = pcre2_general_context_32;
110+
using match_ctx_type = pcre2_match_context_32;
111+
using match_data_type = pcre2_match_data_32;
112+
using string_type = std::u32string;
113+
using string_char_type = char32_t;
114+
using uchar_type = PCRE2_UCHAR32;
115+
116+
static constexpr size_t utf = 32;
117+
static constexpr code_type* (*compile)(string_ptr_type, size_t,
118+
uint32_t, int*, size_t*, compile_ctx_type*) = pcre2_compile_32;
119+
static constexpr match_data_type* (*match_data_from_pattern)
120+
(const code_type*, general_ctx_type*) = pcre2_match_data_create_from_pattern_32;
121+
static constexpr int (*match)(const code_type*, string_ptr_type,
122+
size_t, size_t, uint32_t, match_data_type*, match_ctx_type*) = pcre2_match_32;
123+
static constexpr size_t* (*get_ovector_ptr)(match_data_type*) = pcre2_get_ovector_pointer_32;
124+
static constexpr void(*match_data_free)(match_data_type*) = pcre2_match_data_free_32;
125+
static constexpr void(*code_free)(code_type*) = pcre2_code_free_32;
126+
static constexpr int(*get_error_message)(int, uchar_type*, size_t) = pcre2_get_error_message_32;
127+
};
128+
}

include/pcre2cpp.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@
4646

4747
#pragma once
4848
#include "pcre2cpp_types.hpp"
49+
#include "regex_exception.hpp"
50+
#include "match_result.hpp"
4951
#include "regex.hpp"

include/pcre2cpp_config.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#pragma region VERSION
4+
#define PCRE2CPP_VERSION_MAJOR 1
5+
#define PCRE2CPP_VERSION_MINOR 0
6+
#define PCRE2CPP_VERSION_PATCH 0
7+
8+
#define PCRE2CPP_VERSION_TO_STRING(major, minor, patch) #major #minor #patch
9+
#define PCRE2CPP_VERSION_TO_INT(major, minor, patch) (major * 100 + minor * 10 + patch)
10+
11+
#define PCRE2CPP_VERSION_STRING PCRE2CPP_VERSION_TO_STRING(PCRE2CPP_VERSION_MAJOR, \
12+
PCRE2CPP_VERSION_MINOR, \
13+
PCRE2CPP_VERSION_PATCH)
14+
#define PCRE2CPP_VERSION_INT PCRE2CPP_VERSION_TO_INT(PCRE2CPP_VERSION_MAJOR, \
15+
PCRE2CPP_VERSION_MINOR, \
16+
PCRE2CPP_VERSION_PATCH)
17+
#define PCRE2CPP_VERSION PCRE2CPP_VERSION_STRING
18+
#pragma endregion
19+
20+
#pragma region LAST_UPDATE
21+
#define PCRE2CPP_LAST_UPDATE_DAY 12
22+
#define PCRE2CPP_LAST_UPDATE_MONTH 3
23+
#define PCRE2CPP_LAST_UPDATE_YEAR 2025
24+
25+
#define PCRE2CPP_LAST_UPDATE_DATE "12.03.2025"
26+
#pragma endregion

include/pcre2cpp_libs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
#pragma once
4848
#define PCRE2_STATIC
4949
#define PCRE2_CODE_UNIT_WIDTH 0
50-
#include <pcre2/pcre2.h>
50+
#include "pcre2/pcre2.h"
5151
#include <string>
5252
#include <stdexcept>

include/pcre2cpp_types.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@
4545
*/
4646

4747
#pragma once
48+
#include "pcre2cpp_config.hpp"
4849
#include "pcre2cpp_libs.hpp"
4950

5051
namespace pcre2cpp {
51-
class regex_exception;
52+
template<size_t utf> class basic_regex_exception;
5253
template<size_t utf> class basic_match_result;
5354
template<size_t utf> class basic_regex;
5455

56+
using regex_exception = basic_regex_exception<8>;
57+
using wregex_exception = basic_regex_exception<16>;
58+
using regex_exception_32 = basic_regex_exception<32>;
59+
5560
using match_result = basic_match_result<8>;
5661
using wmatch_result = basic_match_result<16>;
5762
using match_result_32 = basic_match_result<32>;

0 commit comments

Comments
 (0)