DCL 4.0
Loading...
Searching...
No Matches
_regex.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2#include <stdlib.h> // malloc, free
3#include <dcl/_regex.h>
4
22
23#if 1
24 #include <regex>
25 using namespace std;
26#else
27 #include <boost/regex.hpp>
28 using namespace boost;
29#endif
30
31#include <dcl/Object.h>
32#include <dcl/Regex.h>
33#include <dcl/String.h>
34#include <dcl/Array.h>
35
36#if __DCL_DEBUG
37#undef __THIS_FILE__
38 static const char_t __THIS_FILE__[] = __T("dcl/_regex.cpp");
39#endif
40
41__DCL_BEGIN_NAMESPACE
42
44{
45 //wregex* p = (wregex*)malloc(sizeof(wregex));
46 //return new(p) wregex;
48 regex_handle r = (regex_handle)new wregex();
50 return r;
51}
52
54{
55 //wregex* p = (wregex*)_handle;
56 //p->~wregex();
57 //free(p);
58 delete (wregex*)_handle;
59}
60
62 regex_handle _handle,
63 const wchar_t* _pattern, size_t _n,
64 unsigned int _flags
65 ) // throws(boost::bad_expression&)
66{
67 wregex::flag_type flags = wregex::extended;// wregex::normal;
68 if (_flags & regex_icase)
69 flags |= wregex::icase;
70 if (_flags & regex_nosubs)
71 flags |= wregex::nosubs;
72#if 0
73 if (_flags & regex_newline)
74 flags |= wregex::newline_alt;
75#endif
77 ((wregex*)_handle)->assign(_pattern, _pattern + _n, flags);
79}
80
82{
83 free(_results);
84// delete[] _results;
85}
86
88 regex_handle _handle,
89 const wchar_t* _begin, const wchar_t* _end,
90 match_result** _results, unsigned int _flags
91 ) // throws(std::runtime_error&)
92{
93 regex_constants::match_flag_type flags = regex_constants::match_default;
94 if (_flags & regex_not_bol)
95 flags |= regex_constants::match_not_bol;
96 if (_flags & regex_not_eol)
97 flags |= regex_constants::match_not_eol;
98
99 wcmatch m;
101 bool b = regex_match(_begin, _end, m, *((const wregex*)_handle), flags);
103 if (b)
104 {
105 match_result* match = (match_result*)malloc(sizeof(match_result) * m.size());
106 for(size_t i = 0; i < m.size(); i++)
107 {
108 match[i].first = m[i].first;
109 match[i].second = m[i].second;
110 match[i].matched = m[i].matched;
111 }
112 *_results = match;
113 return m.size();
114 }
115 *_results = NULL;
116 return 0;
117}
118
120 regex_handle _handle,
121 const wchar_t* _begin, const wchar_t* _end,
122 unsigned int _flags
123 ) // throws(std::runtime_error&)
124{
125 regex_constants::match_flag_type flags = regex_constants::match_default;
126 if (_flags & regex_not_bol)
127 flags |= regex_constants::match_not_bol;
128 if (_flags & regex_not_eol)
129 flags |= regex_constants::match_not_eol;
130
132 bool r = regex_match(_begin, _end, *(const wregex*)_handle, flags);
134 return r;
135}
136
138 regex_handle _handle,
139 const wchar_t* _begin, const wchar_t* _end,
140 match_result** _results, unsigned int _flags
141 ) // throws(std::runtime_error&)
142{
143 regex_constants::match_flag_type flags = regex_constants::match_default;
144 if (_flags & regex_not_bol)
145 flags |= regex_constants::match_not_bol;
146 if (_flags & regex_not_eol)
147 flags |= regex_constants::match_not_eol;
148
149 wcmatch m;
151 bool r = regex_search(_begin, _end, m, *((const wregex*)_handle), flags);
153 if (r)
154 {
155 match_result* match = (match_result*)malloc(sizeof(match_result) * m.size());
156 for(size_t i = 0; i < m.size(); i++)
157 {
158 match[i].first = m[i].first;
159 match[i].second = m[i].second;
160 match[i].matched = m[i].matched;
161 }
162 *_results = match;
163 return m.size();
164 }
165 *_results = NULL;
166 return r;
167}
168
170 regex_handle _handle,
171 const wchar_t* _begin, const wchar_t* _end,
172 unsigned int _flags
173 ) // throws(std::runtime_error&)
174{
175 regex_constants::match_flag_type flags = regex_constants::match_default;
176 if (_flags & regex_not_bol)
177 flags |= regex_constants::match_not_bol;
178 if (_flags & regex_not_eol)
179 flags |= regex_constants::match_not_eol;
180
182 bool r = regex_search(_begin, _end, *(const wregex*)_handle, flags);
184 return r;
185}
186
188 const wchar_t* _regex, const wchar_t* _regexend,
189 const wchar_t* _begin, const wchar_t* _end,
190 bool _icase
192{
193 regex::flag_type flags = regex::ECMAScript;
194 if (_icase) {
195 flags |= regex::icase;
196 }
197
198 try {
200 wregex re(_regex, _regexend, flags);
202 bool r = regex_match(_begin, _end, re);
204 return r;
205 }
206 // catch (std::runtime_error& e)
207 catch (std::exception& e) {
208 throw(new RegexException(typeid(e).name(), e.what()));
209 }
210}
211
213 const char* _regex, const char* _regexend,
214 const char* _begin, const char* _end,
215 bool _icase
217{
218 regex::flag_type flags = regex::ECMAScript;
219 if (_icase) {
220 flags |= regex::icase;
221 }
222
223 try {
225 regex re(_regex, _regexend, flags);
227 bool r = regex_match(_begin, _end, re);
229 return r;
230 }
231 // catch (std::runtime_error& e)
232 catch (std::exception& e) {
233 throw(new RegexException(typeid(e).name(), e.what()));
234 }
235}
236
238 const wchar_t* _regex, const wchar_t* _regexend,
239 const wchar_t* _begin, const wchar_t* _end,
240 bool _icase
242{
243 // GNUC BUG!!
244 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
245 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
246 if (_icase) {
247 flags |= regex::icase;
248 }
249
250 size_t r = (size_t)-1;
251 try {
253 wregex re(_regex, _regexend, flags);
255 wcmatch m;
257 if (regex_search(_begin, _end, m, re)) {
258 r = m[0].first - _begin;
259 }
261 }
262 // catch (std::runtime_error& e)
263 catch (std::exception& e) {
264 throw(new RegexException(typeid(e).name(), e.what()));
265 }
266 return r;
267}
268
270 const char* _regex, const char* _regexend,
271 const char* _begin, const char* _end,
272 bool _icase
274{
275 // GNUC BUG!!
276 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
277 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
278 if (_icase) {
279 flags |= regex::icase;
280 }
281
282 size_t r = (size_t)-1;
283 try {
285 regex re(_regex, _regexend, flags);
287 cmatch m;
290 if (regex_search(_begin, _end, m, re)) {
291 r = m[0].first - _begin;
292 }
294 }
295 // catch (std::runtime_error& e)
296 catch (std::exception& e) {
297 throw(new RegexException(typeid(e).name(), e.what()));
298 }
299 return r;
300}
301
303 const wchar_t* _regex, const wchar_t* _regexend,
304 const wchar_t* _begin, const wchar_t* _end,
305 bool _icase
307{
308 // GNUC BUG!!
309 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
310 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
311 if (_icase) {
312 flags |= regex::icase;
313 }
314
315 String r;
316 try {
318 wregex re(_regex, _regexend, flags);
320 wcmatch m;
322 if (regex_search(_begin, _end, m, re)) {
323 r.assign(m[0].first, m[0].second);
324 }
326 }
327 // catch (std::runtime_error& e)
328 catch (std::exception& e) {
329 throw(new RegexException(typeid(e).name(), e.what()));
330 }
331 return r;
332}
333
335 const char* _regex, const char* _regexend,
336 const char* _begin, const char* _end,
337 bool _icase
339{
340 // GNUC BUG!!
341 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
342 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
343 if (_icase) {
344 flags |= regex::icase;
345 }
346
347 ByteString r;
348 try {
350 regex re(_regex, _regexend, flags);
352 cmatch m;
355 if (regex_search(_begin, _end, m, re)) {
356 r.assign(m[0].first, m[0].second);
357 }
359 }
360 // catch (std::runtime_error& e)
361 catch (std::exception& e) {
362 throw(new RegexException(typeid(e).name(), e.what()));
363 }
364 return r;
365}
366
368 const wchar_t* _regex, const wchar_t* _regexend,
369 const wchar_t* _begin, const wchar_t* _end,
370 const wchar_t* _replacement, const wchar_t* _replacementend,
371 bool _icase,
372 size_t _limit // = (size_t)-1
374{
375 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
376 if (_icase) {
377 flags |= regex::icase;
378 }
379
380 try {
381 StringBuilder r;
383 wregex re(_regex, _regexend, flags);
385 wcmatch m;
387 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
388 __DCL_ASSERT(m.size() > 0);
389 r.append(_begin, m[0].first)
390 .append(_replacement, _replacementend);
391
392 _limit--;
393 _begin = m[0].second;
394 }
395
396 if (_begin < _end) {
397 r.append(_begin, _end);
398 }
399
400 return r;
401 }
402 // catch (std::runtime_error& e)
403 catch (std::exception& e) {
404 throw(new RegexException(typeid(e).name(), e.what()));
405 }
406}
407
409 const char* _regex, const char* _regexend,
410 const char* _begin, const char* _end,
411 const char* _replacement, const char* _replacementend,
412 bool _icase,
413 size_t _limit // = (size_t)-1
415{
416 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
417 if (_icase) {
418 flags |= regex::icase;
419 }
420
421 try {
422 ByteStringBuilder r;
424 regex re(_regex, _regexend, flags);
426 cmatch m;
428 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
429 __DCL_ASSERT(m.size() > 0);
430 r.append(_begin, m[0].first)
431 .append(_replacement, _replacementend);
432
433 _limit--;
434 _begin = m[0].second;
435 }
436
437 if (_begin < _end) {
438 r.append(_begin, _end);
439 }
440
441 return r;
442 }
443 // catch (std::runtime_error& e)
444 catch (std::exception& e) {
445 throw(new RegexException(typeid(e).name(), e.what()));
446 }
447}
448
449StringArray& __regex_split(
450 const wchar_t* _regex, const wchar_t* _regexend,
451 const wchar_t* _begin, const wchar_t* _end,
452 bool _icase,
453 StringArray& _results,
454 size_t _limit // = (size_t)-1
456{
457 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
458 if (_icase) {
459 flags |= regex::icase;
460 }
461
462 try {
464 wregex re(_regex, _regexend, flags);
466 wcmatch m;
468 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
469 __DCL_ASSERT(m.size() > 0);
470 _results.add(String(_begin, m[0].first));
471
472 _limit--;
473 _begin = m[0].second;
474 }
475
476 if (_begin < _end) {
477 _results.add(String(_begin, _end));
478 }
479
480 return _results;
481 }
482 // catch (std::runtime_error& e)
483 catch (std::exception& e) {
484 throw(new RegexException(typeid(e).name(), e.what()));
485 }
486}
487
488ByteStringArray& __regex_split(
489 const char* _regex, const char* _regexend,
490 const char* _begin, const char* _end,
491 bool _icase,
492 ByteStringArray& _results,
493 size_t _limit // = (size_t)-1
495{
496 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
497 if (_icase) {
498 flags |= regex::icase;
499 }
500
501 try {
503 regex re(_regex, _regexend, flags);
505 cmatch m;
507 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
508 __DCL_ASSERT(m.size() > 0);
509 _results.add(ByteString(_begin, m[0].first));
510
511 _limit--;
512 _begin = m[0].second;
513 }
514
515 if (_begin < _end) {
516 _results.add(ByteString(_begin, _end));
517 }
518
519 return _results;
520 }
521 // catch (std::runtime_error& e)
522 catch (std::exception& e) {
523 throw(new RegexException(typeid(e).name(), e.what()));
524 }
525}
526
527__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:137
void __matches_free(match_result *_results)
Definition _regex.cpp:81
String __regex_substring(const wchar_t *_regex, const wchar_t *_regexend, const wchar_t *_begin, const wchar_t *_end, bool _icase) __DCL_THROWS1(RegexException *)
Definition _regex.cpp:302
StringArray & __regex_split(const wchar_t *_regex, const wchar_t *_regexend, const wchar_t *_begin, const wchar_t *_end, bool _icase, StringArray &_results, size_t _limit) __DCL_THROWS1(RegexException *)
Definition _regex.cpp:449
size_t __regex_match(regex_handle _handle, const wchar_t *_begin, const wchar_t *_end, match_result **_results, unsigned int _flags)
Definition _regex.cpp:87
String __regex_replace(const wchar_t *_regex, const wchar_t *_regexend, const wchar_t *_begin, const wchar_t *_end, const wchar_t *_replacement, const wchar_t *_replacementend, bool _icase, size_t _limit) __DCL_THROWS1(RegexException *)
Definition _regex.cpp:367
__DCL_BEGIN_NAMESPACE regex_handle __regex_create()
Definition _regex.cpp:43
bool __regex_matches(const wchar_t *_regex, const wchar_t *_regexend, const wchar_t *_begin, const wchar_t *_end, bool _icase) __DCL_THROWS1(RegexException *)
Definition _regex.cpp:187
void __regex_compile(regex_handle _handle, const wchar_t *_pattern, size_t _n, unsigned int _flags)
Definition _regex.cpp:61
void __regex_destroy(regex_handle _handle)
Definition _regex.cpp:53
@ regex_not_bol
Definition _regex.h:39
@ regex_not_eol
Definition _regex.h:40
@ regex_icase
Definition _regex.h:18
@ regex_newline
Definition _regex.h:20
@ regex_nosubs
Definition _regex.h:19
void * regex_handle
Definition _regex.h:12
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#define __DCL_THROWS1(e)
Definition Config.h:167
#define __DCL_DEBUG_ALLOC_LEAVE
Definition Object.h:647
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define __DCL_DEBUG_ALLOC_ENTER
Definition Object.h:646
#define __T(str)
Definition Object.h:44
ByteString r
const wchar_t * first
Definition _regex.h:33
const wchar_t * second
Definition _regex.h:34
bool matched
Definition _regex.h:35