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