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 <stdlib.h> // malloc, free
4#include <dcl/_regex.h>
5
23
24#if 1
25 #include <regex>
26 using namespace std;
27#else
28 #include <boost/regex.hpp>
29 using namespace boost;
30#endif
31
32#include <dcl/Object.h>
33#include <dcl/Regex.h>
34#include <dcl/String.h>
35#include <dcl/Array.h>
36
37#if __DCL_HAVE_THIS_FILE__
38#undef __THIS_FILE__
39static const char_t __THIS_FILE__[] = __T("dcl/_regex.cpp");
40#endif
41
42__DCL_BEGIN_NAMESPACE
43
45{
46 //wregex* p = (wregex*)malloc(sizeof(wregex));
47 //return new(p) wregex;
49 regex_handle r = (regex_handle)new wregex();
51 return r;
52}
53
55{
56 //wregex* p = (wregex*)_handle;
57 //p->~wregex();
58 //free(p);
59#ifdef __DCL_DEBUG_DELETE
60// MINGW64 g++ 은 overrided delete를 호출하지 않는다!
61#define delete(_p) __DCL_DEBUG_DELETE(_p)
62#endif
63 delete ((wregex*)_handle);
64#ifdef __DCL_DEBUG_DELETE
65#undef delete
66#endif
67}
68
70 regex_handle _handle,
71 const wchar_t* _pattern, size_t _n,
72 unsigned int _flags
73) // throws(boost::bad_expression&)
74{
75 wregex::flag_type flags = wregex::extended;// wregex::normal;
76 if (_flags & regex_icase)
77 flags |= wregex::icase;
78 if (_flags & regex_nosubs)
79 flags |= wregex::nosubs;
80#if 0
81 if (_flags & regex_newline)
82 flags |= wregex::newline_alt;
83#endif
85 ((wregex*)_handle)->assign(_pattern, _pattern + _n, flags);
87}
88
90{
91 free(_results);
92// delete[] _results;
93}
94
96 regex_handle _handle,
97 const wchar_t* _begin, const wchar_t* _end,
98 match_result** _results, unsigned int _flags
99) // throws(std::runtime_error&)
100{
101 regex_constants::match_flag_type flags = regex_constants::match_default;
102 if (_flags & regex_not_bol)
103 flags |= regex_constants::match_not_bol;
104 if (_flags & regex_not_eol)
105 flags |= regex_constants::match_not_eol;
106
107 wcmatch m;
109 bool b = regex_match(_begin, _end, m, *((const wregex*)_handle), flags);
111 if (b) {
112 match_result* match = (match_result*)malloc(sizeof(match_result) * m.size());
113 for(size_t i = 0; i < m.size(); i++) {
114 match[i].first = m[i].first;
115 match[i].second = m[i].second;
116 match[i].matched = m[i].matched;
117 }
118 *_results = match;
119 return m.size();
120 }
121 *_results = NULL;
122 return 0;
123}
124
126 regex_handle _handle,
127 const wchar_t* _begin, const wchar_t* _end,
128 unsigned int _flags
129) // throws(std::runtime_error&)
130{
131 regex_constants::match_flag_type flags = regex_constants::match_default;
132 if (_flags & regex_not_bol)
133 flags |= regex_constants::match_not_bol;
134 if (_flags & regex_not_eol)
135 flags |= regex_constants::match_not_eol;
136
138 bool r = regex_match(_begin, _end, *(const wregex*)_handle, flags);
140 return r;
141}
142
144 regex_handle _handle,
145 const wchar_t* _begin, const wchar_t* _end,
146 match_result** _results, unsigned int _flags
147) // throws(std::runtime_error&)
148{
149 regex_constants::match_flag_type flags = regex_constants::match_default;
150 if (_flags & regex_not_bol)
151 flags |= regex_constants::match_not_bol;
152 if (_flags & regex_not_eol)
153 flags |= regex_constants::match_not_eol;
154
155 wcmatch m;
157 bool r = regex_search(_begin, _end, m, *((const wregex*)_handle), flags);
159 if (r) {
160 match_result* match = (match_result*)malloc(sizeof(match_result) * m.size());
161 for(size_t i = 0; i < m.size(); i++) {
162 match[i].first = m[i].first;
163 match[i].second = m[i].second;
164 match[i].matched = m[i].matched;
165 }
166 *_results = match;
167 return m.size();
168 }
169 *_results = NULL;
170 return r;
171}
172
174 regex_handle _handle,
175 const wchar_t* _begin, const wchar_t* _end,
176 unsigned int _flags
177) // throws(std::runtime_error&)
178{
179 regex_constants::match_flag_type flags = regex_constants::match_default;
180 if (_flags & regex_not_bol)
181 flags |= regex_constants::match_not_bol;
182 if (_flags & regex_not_eol)
183 flags |= regex_constants::match_not_eol;
184
186 bool r = regex_search(_begin, _end, *(const wregex*)_handle, flags);
188 return r;
189}
190
192 const wchar_t* _regex, const wchar_t* _regexend,
193 const wchar_t* _begin, const wchar_t* _end,
194 bool _icase
196{
197 regex::flag_type flags = regex::ECMAScript;
198 if (_icase) {
199 flags |= regex::icase;
200 }
201
202 try {
204 wregex re(_regex, _regexend, flags);
206 bool r = regex_match(_begin, _end, re);
208 return r;
209 }
210 // catch (std::runtime_error& e)
211 catch (std::exception& e) {
212 throw(new RegexException(typeid(e).name(), e.what()));
213 }
214}
215
217 const char* _regex, const char* _regexend,
218 const char* _begin, const char* _end,
219 bool _icase
221{
222 regex::flag_type flags = regex::ECMAScript;
223 if (_icase) {
224 flags |= regex::icase;
225 }
226
227 try {
229 regex re(_regex, _regexend, flags);
231 bool r = regex_match(_begin, _end, re);
233 return r;
234 }
235 // catch (std::runtime_error& e)
236 catch (std::exception& e) {
237 throw(new RegexException(typeid(e).name(), e.what()));
238 }
239}
240
242 const wchar_t* _regex, const wchar_t* _regexend,
243 const wchar_t* _begin, const wchar_t* _end,
244 bool _icase
246{
247 // GNUC BUG!!
248 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
249 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
250 if (_icase) {
251 flags |= regex::icase;
252 }
253
254 size_t r = (size_t)-1;
255 try {
257 wregex re(_regex, _regexend, flags);
259 wcmatch m;
261 if (regex_search(_begin, _end, m, re)) {
262 r = m[0].first - _begin;
263 }
265 }
266 // catch (std::runtime_error& e)
267 catch (std::exception& e) {
268 throw(new RegexException(typeid(e).name(), e.what()));
269 }
270 return r;
271}
272
274 const char* _regex, const char* _regexend,
275 const char* _begin, const char* _end,
276 bool _icase
278{
279 // GNUC BUG!!
280 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
281 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
282 if (_icase) {
283 flags |= regex::icase;
284 }
285
286 size_t r = (size_t)-1;
287 try {
289 regex re(_regex, _regexend, flags);
291 cmatch m;
293 if (regex_search(_begin, _end, m, re)) {
294 r = m[0].first - _begin;
295 }
297 }
298 // catch (std::runtime_error& e)
299 catch (std::exception& e) {
300 throw(new RegexException(typeid(e).name(), e.what()));
301 }
302 return r;
303}
304
306 const wchar_t* _regex, const wchar_t* _regexend,
307 const wchar_t* _begin, const wchar_t* _end,
308 bool _icase
310{
311 // GNUC BUG!!
312 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
313 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
314 if (_icase) {
315 flags |= regex::icase;
316 }
317
318 String r;
319 try {
321 wregex re(_regex, _regexend, flags);
323 wcmatch m;
325 if (regex_search(_begin, _end, m, re)) {
326 r.assign(m[0].first, m[0].second);
327 }
329 }
330 // catch (std::runtime_error& e)
331 catch (std::exception& e) {
332 throw(new RegexException(typeid(e).name(), e.what()));
333 }
334 return r;
335}
336
338 const char* _regex, const char* _regexend,
339 const char* _begin, const char* _end,
340 bool _icase
342{
343 // GNUC BUG!!
344 // regex re(_pattern, regex::extended | (_icase ? regex::icase : 0));
345 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
346 if (_icase) {
347 flags |= regex::icase;
348 }
349
350 ByteString r;
351 try {
353 regex re(_regex, _regexend, flags);
355 cmatch m;
358 if (regex_search(_begin, _end, m, re)) {
359 r.assign(m[0].first, m[0].second);
360 }
362 }
363 // catch (std::runtime_error& e)
364 catch (std::exception& e) {
365 throw(new RegexException(typeid(e).name(), e.what()));
366 }
367 return r;
368}
369
371 const wchar_t* _regex, const wchar_t* _regexend,
372 const wchar_t* _begin, const wchar_t* _end,
373 const wchar_t* _replacement, const wchar_t* _replacementend,
374 bool _icase,
375 size_t _limit // = (size_t)-1
377{
378 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
379 if (_icase) {
380 flags |= regex::icase;
381 }
382
383 try {
384 StringBuilder r;
386 wregex re(_regex, _regexend, flags);
388 wcmatch m;
390 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
391 __DCL_ASSERT(m.size() > 0);
392 r.append(_begin, m[0].first)
393 .append(_replacement, _replacementend);
394
395 _limit--;
396 _begin = m[0].second;
397 }
399 if (_begin < _end) {
400 r.append(_begin, _end);
401 }
402
403 return r;
404 }
405 // catch (std::runtime_error& e)
406 catch (std::exception& e) {
407 throw(new RegexException(typeid(e).name(), e.what()));
408 }
409}
410
412 const char* _regex, const char* _regexend,
413 const char* _begin, const char* _end,
414 const char* _replacement, const char* _replacementend,
415 bool _icase,
416 size_t _limit // = (size_t)-1
418{
419 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
420 if (_icase) {
421 flags |= regex::icase;
422 }
423
424 try {
425 ByteStringBuilder r;
427 regex re(_regex, _regexend, flags);
429 cmatch m;
431 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
432 __DCL_ASSERT(m.size() > 0);
433 r.append(_begin, m[0].first)
434 .append(_replacement, _replacementend);
435
436 _limit--;
437 _begin = m[0].second;
438 }
440 if (_begin < _end) {
441 r.append(_begin, _end);
442 }
443
444 return r;
445 }
446 // catch (std::runtime_error& e)
447 catch (std::exception& e) {
448 throw(new RegexException(typeid(e).name(), e.what()));
449 }
450}
451
452StringArray& __regex_split(
453 const wchar_t* _regex, const wchar_t* _regexend,
454 const wchar_t* _begin, const wchar_t* _end,
455 bool _icase,
456 StringArray& _results,
457 size_t _limit // = (size_t)-1
459{
460 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
461 if (_icase) {
462 flags |= regex::icase;
463 }
464
465 try {
467 wregex re(_regex, _regexend, flags);
469 wcmatch m;
471 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
472 __DCL_ASSERT(m.size() > 0);
473 _results.add(String(_begin, m[0].first));
474
475 _limit--;
476 _begin = m[0].second;
477 }
479 if (_begin < _end) {
480 _results.add(String(_begin, _end));
481 }
482
483 return _results;
484 }
485 // catch (std::runtime_error& e)
486 catch (std::exception& e) {
487 throw(new RegexException(typeid(e).name(), e.what()));
488 }
489}
490
491ByteStringArray& __regex_split(
492 const char* _regex, const char* _regexend,
493 const char* _begin, const char* _end,
494 bool _icase,
495 ByteStringArray& _results,
496 size_t _limit // = (size_t)-1
498{
499 regex::flag_type flags = regex::ECMAScript | regex::nosubs;
500 if (_icase) {
501 flags |= regex::icase;
502 }
503
504 try {
506 regex re(_regex, _regexend, flags);
508 cmatch m;
510 while (_begin < _end && _limit && regex_search(_begin, _end, m, re)) {
511 __DCL_ASSERT(m.size() > 0);
512 _results.add(ByteString(_begin, m[0].first));
513
514 _limit--;
515 _begin = m[0].second;
516 }
518 if (_begin < _end) {
519 _results.add(ByteString(_begin, _end));
520 }
521
522 return _results;
523 }
524 // catch (std::runtime_error& e)
525 catch (std::exception& e) {
526 throw(new RegexException(typeid(e).name(), e.what()));
527 }
528}
529
530__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
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:305
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:452
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
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:370
__DCL_BEGIN_NAMESPACE regex_handle __regex_create()
Definition _regex.cpp:44
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:191
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
@ 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: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_DEBUG_ALLOC_LEAVE
Definition Object.h:708
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define __DCL_DEBUG_ALLOC_ENTER
Definition Object.h:707
#define __T(str)
Definition Object.h:60
const wchar_t * first
Definition _regex.h:33
const wchar_t * second
Definition _regex.h:34
bool matched
Definition _regex.h:35