Skip to content

Commit fd128c1

Browse files
committed
changed to smart pointers
1 parent abcd60a commit fd128c1

5 files changed

Lines changed: 129 additions & 97 deletions

File tree

include/pcre2cpp/match_result.hpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@ namespace pcre2cpp {
2424
using _string_type = _pcre2_data<utf>::string_type;
2525
using _match_value = std::pair<size_t, _string_type>;
2626
using _match_result_exception = basic_match_result_exception<utf>;
27+
using _named_sub_values_table = std::unordered_map<_string_type, size_t>;
28+
using _named_sub_values_table_ptr = std::shared_ptr<_named_sub_values_table>;
2729

2830
struct _value_result_data {
2931
bool found = false;
3032
size_t search_offset = 0;
3133
_match_value result = { 0, _string_type() };
3234
std::vector<_match_value> sub_results = {};
33-
std::unordered_map<_string_type, size_t> named_sub_values = {};
35+
_named_sub_values_table_ptr named_sub_values = {};
3436
};
3537

3638
std::variant<int, _value_result_data> _data = _value_result_data();
3739

3840
constexpr size_t _get_named_sub_result_idx(const _string_type& name) const {
3941
auto& named_sub_values = std::get<_value_result_data>(_data).named_sub_values;
40-
auto itr = named_sub_values.find(name);
41-
if (itr == named_sub_values.end()) {
42+
auto itr = named_sub_values->find(name);
43+
if (itr == named_sub_values->end()) {
4244
_string_type message = "Subexpression with provided name '";
4345
message += name;
4446
message += "' not found";
@@ -59,21 +61,16 @@ namespace pcre2cpp {
5961
constexpr basic_match_result(int error_code) noexcept
6062
: _data(error_code) {}
6163
constexpr basic_match_result(const size_t& search_offset,
62-
const std::unordered_map<_string_type, size_t> named_sub_values) noexcept
64+
const _named_sub_values_table_ptr& named_sub_values) noexcept
6365
: _data(_value_result_data{ false, search_offset, { 0, _string_type() }, {}, named_sub_values }) {}
6466
constexpr basic_match_result(const size_t& search_offset, const _match_value& result,
6567
const std::vector<_match_value>& sub_results,
66-
const std::unordered_map<_string_type, size_t> named_sub_values) noexcept
68+
const _named_sub_values_table_ptr& named_sub_values) noexcept
6769
: _data(_value_result_data{ true, search_offset, result, sub_results, named_sub_values }) { }
68-
constexpr basic_match_result(const basic_match_result<utf>& mr) noexcept
69-
: _data(mr._data) {
70-
}
70+
constexpr basic_match_result(const basic_match_result<utf>& mr) noexcept = default;
7171
~basic_match_result() = default;
7272

73-
constexpr basic_match_result<utf>& operator=(const basic_match_result<utf>& mr) noexcept {
74-
this->_data = mr._data;
75-
return *this;
76-
}
73+
constexpr basic_match_result<utf>& operator=(const basic_match_result<utf>& mr) noexcept = default;
7774

7875
#pragma region ERRORS
7976
constexpr bool has_error() const noexcept { return std::holds_alternative<int>(_data); }
@@ -165,7 +162,7 @@ namespace pcre2cpp {
165162

166163
std::vector<size_t> offsets;
167164
offsets.reserve(value.sub_results.size());
168-
for (const auto& [offset, value] : value.sub_results) {
165+
for (const auto& [offset, subvalue] : value.sub_results) {
169166
offsets.push_back(value.search_offset + value.result.first + offset);
170167
}
171168
return offsets;
@@ -177,7 +174,7 @@ namespace pcre2cpp {
177174

178175
std::vector<size_t> offsets;
179176
offsets.reserve(value.sub_results.size());
180-
for (const auto& [offset, value] : value.sub_results) {
177+
for (const auto& [offset, subvalue] : value.sub_results) {
181178
offsets.push_back(value.result.first + offset);
182179
}
183180
return offsets;
@@ -189,7 +186,7 @@ namespace pcre2cpp {
189186

190187
std::vector<size_t> offsets;
191188
offsets.reserve(sub_results.size());
192-
for (const auto& [offset, value] : sub_results) {
189+
for (const auto& [offset, subvalue] : sub_results) {
193190
offsets.push_back(offset);
194191
}
195192
return offsets;
@@ -212,8 +209,8 @@ namespace pcre2cpp {
212209

213210
std::vector<_string_type> values;
214211
values.reserve(sub_results.size());
215-
for (const auto& [offset, value] : sub_results) {
216-
values.push_back(value);
212+
for (const auto& [offset, subvalue] : sub_results) {
213+
values.push_back(subvalue);
217214
}
218215
return values;
219216
}

include/pcre2cpp/pcre2cpp_config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#pragma region VERSION
1818
#define PCRE2CPP_VERSION_MAJOR 1
1919
#define PCRE2CPP_VERSION_MINOR 1
20-
#define PCRE2CPP_VERSION_PATCH 0
20+
#define PCRE2CPP_VERSION_PATCH 1
2121

2222
#define _PCRE2CPP_STRINGIFY_HELPER(x) #x
2323

@@ -36,7 +36,7 @@
3636
#pragma endregion VERSION
3737

3838
#pragma region LAST_UPDATE
39-
#define PCRE2CPP_LAST_UPDATE_DAY 23
39+
#define PCRE2CPP_LAST_UPDATE_DAY 24
4040
#define PCRE2CPP_LAST_UPDATE_MONTH 9
4141
#define PCRE2CPP_LAST_UPDATE_YEAR 2025
4242

include/pcre2cpp/pcre2cpp_libs.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
#include <stdexcept>
2020
#include <vector>
2121
#include <unordered_map>
22-
#include <variant>
22+
#include <variant>
23+
#include <memory>

include/pcre2cpp/regex.hpp

Lines changed: 66 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,60 @@ namespace pcre2cpp {
2121
class basic_regex {
2222
private:
2323
using _code_type = _pcre2_data<utf>::code_type;
24+
using _code_ptr = std::shared_ptr<_code_type>;
2425
using _match_data_type = _pcre2_data<utf>::match_data_type;
26+
using _match_data_ptr = std::shared_ptr<_match_data_type>;
2527
using _string_type = _pcre2_data<utf>::string_type;
2628
using _string_char_type = _pcre2_data<utf>::string_char_type;
2729
using _match_result_type = basic_match_result<utf>;
2830
using _string_ptr_type = _pcre2_data<utf>::string_ptr_type;
31+
using _named_sub_values_table = std::unordered_map<_string_type, size_t>;
32+
using _named_sub_values_table_ptr = std::shared_ptr<_named_sub_values_table>;
2933

30-
size_t* _copies_num;
31-
_code_type* _code;
32-
_match_data_type* _match_data;
33-
std::unordered_map<_string_type, size_t> _named_sub_values{};
34+
_code_ptr _code = nullptr;
35+
_match_data_ptr _match_data = nullptr;
36+
_named_sub_values_table_ptr _named_sub_values = nullptr;
3437

3538
public:
3639
constexpr basic_regex(const _string_char_type* pattern, size_t pattern_size,
3740
regex_compile_options opts = regex_compile_options::NONE) {
38-
_copies_num = new size_t(1);
3941

42+
// Compile Code
4043
int error_code = 0;
4144
size_t error_offset = 0;
42-
_code = _pcre2_data<utf>::compile((_string_ptr_type)pattern,
45+
_code_type* code = _pcre2_data<utf>::compile((_string_ptr_type)pattern,
4346
pattern_size, (uint32_t)opts, &error_code, &error_offset, nullptr);
4447

45-
if (_code == nullptr) {
48+
if (code == nullptr) {
4649
throw basic_regex_exception<utf>(error_code, error_offset);
4750
}
51+
_code = std::shared_ptr<_code_type>(code, _pcre2_data<utf>::code_free);
52+
53+
// Get Named Sub Values
54+
_named_sub_values = std::make_shared<_named_sub_values_table>();
4855

4956
size_t name_count = 0;
5057
unsigned char* name_table = nullptr;
5158
size_t name_entry_size = 0;
5259

53-
_pcre2_data<utf>::get_info(_code, PCRE2_INFO_NAMECOUNT, &name_count);
54-
_pcre2_data<utf>::get_info(_code, PCRE2_INFO_NAMETABLE, &name_table);
55-
_pcre2_data<utf>::get_info(_code, PCRE2_INFO_NAMEENTRYSIZE, &name_entry_size);
60+
_pcre2_data<utf>::get_info(_code.get(), PCRE2_INFO_NAMECOUNT, &name_count);
61+
_pcre2_data<utf>::get_info(_code.get(), PCRE2_INFO_NAMETABLE, &name_table);
62+
_pcre2_data<utf>::get_info(_code.get(), PCRE2_INFO_NAMEENTRYSIZE, &name_entry_size);
5663

5764
for (size_t i = 0; i != name_count; ++i) {
5865
unsigned char* entry = name_table + i * name_entry_size + 2;
59-
int index = _pcre2_data<utf>::substring_number_from_name(_code, entry);
66+
int index = _pcre2_data<utf>::substring_number_from_name(_code.get(), entry);
6067

6168
unsigned char* entry_end = entry + 1;
6269
while (*entry_end != '\0' && entry_end - entry < name_entry_size - 3) {
6370
entry_end += 1;
6471
}
65-
_named_sub_values[_string_type(entry, entry_end)] = ((size_t)index) - 1;
72+
(*_named_sub_values)[_string_type(entry, entry_end)] = ((size_t)index) - 1;
6673
}
6774

68-
_match_data = _pcre2_data<utf>::match_data_from_pattern(_code, nullptr);
75+
// Create Match Data
76+
_match_data_type* match_data = _pcre2_data<utf>::match_data_from_pattern(_code.get(), nullptr);
77+
_match_data = std::shared_ptr<_match_data_type>(match_data, _pcre2_data<utf>::match_data_free);
6978
}
7079
constexpr basic_regex(const _string_type& pattern,
7180
regex_compile_options opts = regex_compile_options::NONE)
@@ -74,44 +83,54 @@ namespace pcre2cpp {
7483
constexpr basic_regex(const _string_char_type (&pattern)[N],
7584
regex_compile_options opts = regex_compile_options::NONE)
7685
: basic_regex(pattern, N - 1, opts) {}
77-
constexpr basic_regex(const basic_regex<utf>& r)
78-
: _code(r._code), _match_data(r._match_data), _named_sub_values(r._named_sub_values),
79-
_copies_num(r._copies_num) {
80-
++(*_copies_num);
86+
constexpr basic_regex(const basic_regex<utf>& r) noexcept = default;
87+
virtual ~basic_regex() = default;
88+
89+
constexpr basic_regex<utf>& operator=(const basic_regex<utf>& r) noexcept = default;
90+
91+
#pragma region MATCH
92+
constexpr bool match(const _string_char_type* text, size_t text_size,
93+
regex_match_options opts, size_t offset = 0) const {
94+
int match_code =
95+
_pcre2_data<utf>::match(_code.get(), (_string_ptr_type)text,
96+
text_size, offset, (uint32_t)opts, _match_data.get(), nullptr);
97+
98+
return match_code > 0 && match_code != PCRE2_ERROR_NOMATCH;
8199
}
82-
virtual ~basic_regex() {
83-
--(*_copies_num);
84-
if (*_copies_num == 0) {
85-
_pcre2_data<utf>::match_data_free(_match_data);
86-
_match_data = nullptr;
87-
88-
_pcre2_data<utf>::code_free(_code);
89-
_code = nullptr;
90-
}
100+
constexpr bool match(const _string_type& text, regex_match_options opts, size_t offset = 0) const {
101+
return match(text.c_str(), text.length(), opts, offset);
102+
}
103+
template<size_t N>
104+
constexpr bool match(const _string_char_type(&text)[N], regex_match_options opts, size_t offset = 0) const {
105+
return match(text, N - 1, opts, offset);
91106
}
92107

93-
constexpr basic_regex<utf>& operator=(const basic_regex<utf>& r) {
94-
this->_code = r._code;
95-
this->_match_data = r._match_data;
96-
this->_named_sub_values = r._named_sub_values;
97-
this->_copies_num = r._copies_num;
98-
++(*_copies_num);
99-
return *this;
108+
constexpr bool match(const _string_char_type* text, size_t text_size, size_t offset = 0) const {
109+
return match(text, text_size, regex_match_options::NONE, offset);
100110
}
111+
constexpr bool match(const _string_type& text, size_t offset = 0) const {
112+
return match(text.c_str(), text.length(), offset);
113+
}
114+
template<size_t N>
115+
constexpr bool match(const _string_char_type(&text)[N], size_t offset = 0) const {
116+
return match(text, N - 1, offset);
117+
}
118+
119+
#pragma endregion MATCH
101120

102121
#pragma region MATCH_WITH_RESULT
103122
constexpr bool match(const _string_char_type* text, size_t text_size,
104123
_match_result_type& result, regex_match_options opts, size_t offset = 0) const {
105124
int match_code =
106-
_pcre2_data<utf>::match(_code, (_string_ptr_type)text,
107-
text_size, offset, (uint32_t)opts, _match_data, nullptr);
125+
_pcre2_data<utf>::match(_code.get(), (_string_ptr_type)text,
126+
text_size, offset, (uint32_t)opts, _match_data.get(), nullptr);
108127

109128
if (match_code > 0 && match_code != PCRE2_ERROR_NOMATCH) {
110-
size_t* ovector = _pcre2_data<utf>::get_ovector_ptr(_match_data);
129+
size_t* ovector = _pcre2_data<utf>::get_ovector_ptr(_match_data.get());
111130
std::pair<size_t, _string_type> value = { ovector[0] - offset,
112131
_string_type(text + ovector[0], ovector[1] - ovector[0]) };
113132

114-
size_t ovectors_count = _pcre2_data<utf>::get_ovector_count(_match_data);
133+
size_t ovectors_count = _pcre2_data<utf>::get_ovector_count(_match_data.get());
115134
std::vector<std::pair<size_t, _string_type>> sub_values;
116135
for (size_t i = 1; i != ovectors_count; ++i) {
117136
sub_values.push_back({
@@ -150,35 +169,19 @@ namespace pcre2cpp {
150169
}
151170
#pragma endregion MATCH_WITH_RESULT
152171

153-
#pragma region MATCH
154-
constexpr bool match(const _string_char_type* text, size_t text_size,
155-
regex_match_options opts, size_t offset = 0) const {
156-
int match_code =
157-
_pcre2_data<utf>::match(_code, (_string_ptr_type)text,
158-
text_size, offset, (uint32_t)opts, _match_data, nullptr);
159-
160-
return match_code > 0 && match_code != PCRE2_ERROR_NOMATCH;
161-
}
162-
constexpr bool match(const _string_type& text, regex_match_options opts, size_t offset = 0) const {
163-
return match(text.c_str(), text.length(), opts, offset);
164-
}
165-
template<size_t N>
166-
constexpr bool match(const _string_char_type(&text)[N], regex_match_options opts, size_t offset = 0) const {
167-
return match(text, N - 1, opts, offset);
168-
}
169-
170-
constexpr bool match(const _string_char_type* text, size_t text_size, size_t offset = 0) const {
171-
return match(text, text_size, regex_match_options::NONE, offset);
172+
#pragma region MATCH_AT
173+
constexpr bool match_at(const _string_char_type* text, size_t text_size, size_t offset = 0) const {
174+
_match_result_type result;
175+
return match_at(text, text_size, result, offset);
172176
}
173-
constexpr bool match(const _string_type& text, size_t offset = 0) const {
174-
return match(text.c_str(), text.length(), offset);
177+
constexpr bool match_at(const _string_type& text, size_t offset = 0) const {
178+
return match_at(text.c_str(), text.length(), offset);
175179
}
176180
template<size_t N>
177-
constexpr bool match(const _string_char_type(&text)[N], size_t offset = 0) const {
178-
return match(text, N - 1, offset);
181+
constexpr bool match_at(const _string_char_type(&text)[N], size_t offset = 0) const {
182+
return match_at(text, N - 1, offset);
179183
}
180-
181-
#pragma endregion MATCH
184+
#pragma endregion MATCH_AT
182185

183186
#pragma region MATCH_AT_WITH_RESULT
184187
constexpr bool match_at(const _string_char_type* text, size_t text_size,
@@ -202,28 +205,14 @@ namespace pcre2cpp {
202205
}
203206
#pragma endregion MATCH_AT_WITH_RESULT
204207

205-
#pragma region MATCH_AT
206-
constexpr bool match_at(const _string_char_type* text, size_t text_size, size_t offset = 0) const {
207-
_match_result_type result;
208-
return match_at(text, text_size, result, offset);
209-
}
210-
constexpr bool match_at(const _string_type& text, size_t offset = 0) const {
211-
return match_at(text.c_str(), text.length(), offset);
212-
}
213-
template<size_t N>
214-
constexpr bool match_at(const _string_char_type(&text)[N], size_t offset = 0) const {
215-
return match_at(text, N - 1, offset);
216-
}
217-
#pragma endregion MATCH_AT
218-
219208
#pragma region MATCH_ALL_WITH_RESULT
220209
constexpr bool match_all(const _string_char_type* text, size_t text_size, std::vector<_match_result_type>& results, size_t offset = 0) const {
221210
std::vector<_match_result_type> temp_results;
222211

223212
size_t start_offset = offset;
224213
_match_result_type result;
225214
while (match(text + offset, text_size - offset, result)) {
226-
temp_results.push_back(_match_result_type(start_offset, { offset + result.get_result_relative_offset(), result.get_result_value() },
215+
temp_results.push_back(_match_result_type(start_offset, std::make_pair(offset + result.get_result_relative_offset(), result.get_result_value()),
227216
result.get_sub_results(), _named_sub_values));
228217
offset += result.get_result_relative_offset() + result.get_result_value().length();
229218
}

0 commit comments

Comments
 (0)