@@ -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