DCL 3.7.4
Loading...
Searching...
No Matches
Regex.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#include <string.h> // for strlen
4#include <exception> // runtime_error
5
6#include <dcl/Regex.h>
7#include <dcl/Charset.h>
8#include <dcl/Array.h>
9
10#if __DCL_HAVE_ALLOC_DEBUG
11#undef __DCL_ALLOC_LEVEL
12#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
13#endif
14
15#if __DCL_HAVE_THIS_FILE__
16#undef __THIS_FILE__
17static const char_t __THIS_FILE__[] = __T("dcl/Regex.cpp");
18#endif
19
20__DCL_BEGIN_NAMESPACE
21
23
24RegexException::RegexException(const char* _name, const char* _what)
26{
27 StringBuilder sb;
28 sb.append(AsciiDecoder::decode(_name))
29 .append(__T(":"))
30 .append(AsciiDecoder::decode(_what));
31 __message = sb.toString();
32}
33
34String RegexException::toString() const
35{
36 return __message;
37}
38
40{
41 __results = NULL;
42 __size = 0;
43}
44
46{
47 if (__results) {
48 __matches_free(__results);
49// __results = NULL;
50 }
51}
52
53void Regex::MatchResults::attach(match_result* _results, size_t _size)
54{
55 __DCL_ASSERT(_results != NULL);
56
57 if (__results) {
58 __matches_free(__results);
59 }
60 __results = _results;
61 __size = _size;
62}
63
66{
67 __DCL_ASSERT(__results != NULL);
68
69 if (!(_index < __size)) {
70 throw(new InvalidIndexException(0, (ssize_t)(__size - 1), _index));
71 }
72 return __results[_index];
73}
74
76{
77 if (__handle) {
78 __regex_destroy(__handle);
79// __handle = NULL;
80 }
81}
82
84{
85 __handle = NULL;
86}
87
88Regex::Regex(const wchar_t* _pattern, size_t _n,
89 unsigned int _flags) // = 0)
91{
92 __handle = NULL;
93 compile(_pattern, _n, _flags);
94}
95
96Regex::Regex(const String& _pattern, unsigned int _flags) // = 0)
98{
99 __handle = NULL;
100 compile(_pattern.data(), _pattern.length(), _flags);
101}
102
103void Regex::compile(const wchar_t* _pattern, size_t _n,
104 unsigned int _flags) // = 0);
106{
107 if (!__handle) {
108 __handle = __regex_create();
109 }
110
111 try {
112 __regex_compile(__handle, _pattern, _n, _flags);
113 }
114// catch (boost::bad_expression& e)
115 catch (std::exception& e) {
116 throw(new RegexException(typeid(e).name(), e.what()));
117 }
118}
119
120bool Regex::search(const wchar_t* _begin, const wchar_t* _end,
121 unsigned int _flags)
123{
124 __DCL_ASSERT(__handle != NULL);
125
126 bool r = false;
127 try {
128 r = __regex_search(__handle, _begin, _end, _flags);
129 }
130// catch (std::runtime_error& e)
131 catch (std::exception& e) {
132 throw(new RegexException(typeid(e).name(), e.what()));
133 }
134
135 return r;
136}
137
138bool Regex::search(const wchar_t* _begin, const wchar_t* _end,
139 Regex::MatchResults& _results, unsigned int _flags) // = 0)
141{
142 __DCL_ASSERT(__handle != NULL);
143
144 try {
145 match_result* results = NULL;
146 size_t n = __regex_search(__handle, _begin, _end, &results, _flags);
147 if (n) {
148 _results.attach(results, n);
149 return true;
150 }
151 }
152// catch (std::runtime_error& e)
153 catch (std::exception& e) {
154 throw(new RegexException(typeid(e).name(), e.what()));
155 }
156
157 return false;
158}
159
160bool Regex::match(const wchar_t* _begin, const wchar_t* _end,
161 unsigned int _flags)
163{
164 __DCL_ASSERT(__handle != NULL);
165
166 bool r = false;
167 try {
168 r = __regex_match(__handle, _begin, _end, _flags);
169 }
170// catch (std::runtime_error& e)
171 catch (std::exception& e) {
172 throw(new RegexException(typeid(e).name(), e.what()));
173 }
174
175 return r;
176}
177
178bool Regex::match(const wchar_t* _begin, const wchar_t* _end,
179 Regex::MatchResults& _results, unsigned int _flags) // = 0)
181{
182 __DCL_ASSERT(__handle != NULL);
183
184 try {
185 match_result* results = NULL;
186 size_t n = __regex_match(__handle, _begin, _end, &results, _flags);
187 if (n) {
188 _results.attach(results, n);
189 return true;
190 }
191 }
192// catch (std::runtime_error& e)
193 catch (std::exception& e) {
194 throw(new RegexException(typeid(e).name(), e.what()));
195 }
196
197 return false;
198}
199
201 const String& _string,
202 const String& _replacement,
203 size_t _limit
205{
206 const wchar_t* _begin = _string.data();
207 const wchar_t* _end = _string.data() + _string.length();
208 StringBuilder r;
209
210 MatchResults results;
211 while (_begin < _end && 0 < _limit && search(_begin, _end, results)) {
212 r.append(_begin, results[0].first);
213 r.append(_replacement);
214
215 _limit--;
216 _begin = results[0].second;
217 }
218
219 if (_begin < _end) {
220 r.append(_begin, _end);
221 }
222
223 return r;
224}
225
227 const wchar_t* _begin, const wchar_t* _end,
228 StringArray& _results,
229 size_t _limit // = (size_t)-1 // unlimited
231{
232 __DCL_ASSERT(_begin != NULL && _end != NULL);
233 __DCL_ASSERT(_begin <= _end);
234
235 MatchResults results;
236 while(_begin < _end && _limit > 0 && search(_begin, _end, results)) {
237 _results.add(String(_begin, results[0].first - _begin));
238
239 _limit--;
240 _begin = results[0].second;
241 }
242
243 if (_begin < _end) {
244 _results.add(String(_end, _end - _begin));
245 }
246
247 return _results.size();
248}
249
251 const wchar_t* _regex,
252 const wchar_t* _string,
253 bool _icase // = false
255{
256 __DCL_ASSERT_PARAM(_regex != NULL);
257 __DCL_ASSERT_PARAM(_string != NULL);
258 return __regex_search(
259 _regex, _regex + String::length(_regex),
260 _string, _string + String::length(_string),
261 _icase
262 ) != (size_t)-1;
263}
264
266 const char* _regex,
267 const char* _string,
268 bool _icase // = false
270{
271 __DCL_ASSERT_PARAM(_regex != NULL);
272 __DCL_ASSERT_PARAM(_string != NULL);
273 return __regex_search(
274 _regex, _regex + ByteString::length(_regex),
275 _string, _string + ByteString::length(_string),
276 _icase
277 ) != (size_t)-1;;
278}
279
280__DCL_END_NAMESPACE
bool __regex_search(regex_handle _handle, const wchar_t *_begin, const wchar_t *_end, match_result **_results, unsigned int _flags)
Definition _regex.cpp:143
void __matches_free(match_result *_results)
Definition _regex.cpp:89
regex_handle __regex_create()
Definition _regex.cpp:44
size_t __regex_match(regex_handle _handle, const wchar_t *_begin, const wchar_t *_end, match_result **_results, unsigned int _flags)
Definition _regex.cpp:95
void __regex_compile(regex_handle _handle, const wchar_t *_pattern, size_t _n, unsigned int _flags)
Definition _regex.cpp:69
void __regex_destroy(regex_handle _handle)
Definition _regex.cpp:54
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
wchar_t char_t
Definition Config.h:247
#define __DCL_THROWS1(e)
Definition Config.h:152
IOException *size_t r
Definition MediaInfo.cpp:82
#define __DCL_ASSERT_PARAM(expr)
Definition Object.h:409
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:245
#define __T(str)
Definition Object.h:60
static String decode(const char *_mbs, size_t _mbslen=(size_t) -1)
virtual String toString() const
Definition Exception.cpp:40
const match_result & operator[](size_t _index) const __DCL_THROWS1(InvalidIndexException *)
Definition Regex.cpp:64
String __message
Definition Regex.h:28
size_t split(const wchar_t *_begin, const wchar_t *_end, StringArray &_results, size_t _limit=(size_t) -1) __DCL_THROWS1(RegexException *)
Definition Regex.cpp:226
~Regex()
Definition Regex.cpp:75
bool search(const wchar_t *_begin, const wchar_t *_end, unsigned int _flags=0) __DCL_THROWS1(RegexException *)
Definition Regex.cpp:120
Regex()
Definition Regex.cpp:83
bool match(const wchar_t *_begin, const wchar_t *_end, unsigned int _flags) __DCL_THROWS1(RegexException *)
Definition Regex.cpp:160
void compile(const wchar_t *_pattern, size_t _n, unsigned int _flags=0) __DCL_THROWS1(RegexException *)
Definition Regex.cpp:103
String replace(const String &_string, const String &_replacement, size_t _limit=(size_t) -1) __DCL_THROWS1(RegexException *)
Definition Regex.cpp:200
static bool test(const wchar_t *_regex, const wchar_t *_string, bool _icase=false) __DCL_THROWS1(RegexException *)
Definition Regex.cpp:250