CppCMS

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

regex_match.h
1 //
2 // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_REGEX_MATCH_H
9 #define BOOSTER_REGEX_MATCH_H
10
11 #include <algorithm>
12 #include <iterator>
13 #include <string>
14 #include <vector>
15 #include <string.h>
16
17 namespace booster {
18
24 template<typename Iterator>
25 class sub_match : public std::pair<Iterator,Iterator> {
26 public:
27 typedef Iterator iterator;
28 typedef typename std::iterator_traits<Iterator>::value_type value_type;
29 typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
30
34 typedef std::basic_string<value_type> string_type;
35 typedef std::pair<Iterator,Iterator> pair_type;
36
41 bool matched;
42
46 difference_type length() const
47 {
48 if(matched)
49 return std::distance(pair_type::first,pair_type::second);
50 return 0;
51 }
52
56 operator string_type() const
57 {
58 return str();
59 }
60
64 string_type str() const
65 {
66 if(matched)
67 return string_type(pair_type::first,pair_type::second);
68 else
69 return string_type();
70
71 }
75 int compare(sub_match const &other) const
76 {
77 return str().compare(other.str());
78 }
82 int compare(string_type const &other) const
83 {
84 return str().compare(other);
85 }
89 int compare(value_type const *s) const
90 {
91 return str().compare(s);
92 }
96 sub_match() : matched(false)
97 {
98 }
99 };
100
101 typedef sub_match<char const *> csub_match;
102 typedef sub_match<std::string::const_iterator> ssub_match;
103
104
109 template<typename Iterator>
110 class match_results {
111 public:
115 match_results()
116 {
117 begin_ = Iterator();
118 end_ = Iterator();
119 }
123 typedef sub_match<Iterator> value_type;
124
128 value_type operator[](int n) const
129 {
130 value_type r;
131 if(n < 0 || n >= int(offsets_.size()))
132 return r;
133 if(offsets_[n].first == -1)
134 return r;
135 r.matched = true;
136 r.first = begin_;
137 r.second = begin_;
138 std::advance(r.first,offsets_[n].first);
139 std::advance(r.second,offsets_[n].second);
140 return r;
141 }
142
146 size_t size() const
147 {
148 return offsets_.size();
149 }
150
154 value_type suffix()
155 {
156 value_type r;
157 if(offsets_.empty())
158 return r;
159 r.first = begin_;
160 r.second = end_;
161 std::advance(r.first,offsets_.back().second);
162 r.matched = r.first != r.second;
163 return r;
164 }
165
169 value_type prefix()
170 {
171 value_type r;
172 if(offsets_.empty() || offsets_[0].first == 0)
173 return r;
174 r.matched = true;
175 r.first = begin_;
176 r.second = begin_;
177 std::advance(r.second,offsets_[0].first);
178 return r;
179 }
180
182
183 void assign(Iterator begin,Iterator end,std::vector<std::pair<int,int> > &offsets)
184 {
185 begin_ = begin;
186 end_ = end;
187 offsets_.swap(offsets);
188 }
189
191
192 private:
193 Iterator begin_,end_;
194 std::vector<std::pair<int,int> > offsets_;
195 };
196
197 typedef match_results<char const *> cmatch;
198 typedef match_results<std::string::const_iterator> smatch;
199
204 template<typename Regex>
205 bool regex_match(char const *begin,char const *end,cmatch &m, Regex const &r,int flags = 0)
206 {
207 std::vector<std::pair<int,int> > map;
208 bool res = r.match(begin,end,map,flags);
209 if(!res) return false;
210 m.assign(begin,end,map);
211 return true;
212 }
217
218 template<typename Regex>
219 bool regex_match(std::string const &s,smatch &m, Regex const &r,int flags = 0)
220 {
221 std::vector<std::pair<int,int> > map;
222 bool res = r.match(s.c_str(),s.c_str()+s.size(),map,flags);
223 if(!res) return false;
224 m.assign(s.begin(),s.end(),map);
225 return true;
226 }
231
232 template<typename Regex>
233 bool regex_match(char const *s,cmatch &m, Regex const &r,int flags = 0)
234 {
235 std::vector<std::pair<int,int> > map;
236 char const *begin=s;
237 char const *end = begin+strlen(begin);
238 bool res = r.match(begin,end,map,flags);
239 if(!res) return false;
240 m.assign(begin,end,map);
241 return true;
242 }
243
248 template<typename Regex>
249 bool regex_search(char const *begin,char const *end,cmatch &m, Regex const &r,int flags = 0)
250 {
251 std::vector<std::pair<int,int> > map;
252 bool res = r.search(begin,end,map,flags);
253 if(!res) return false;
254 m.assign(begin,end,map);
255 return true;
256 }
257
262 template<typename Regex>
263 bool regex_search(std::string const &s,smatch &m, Regex const &r,int flags = 0)
264 {
265 std::vector<std::pair<int,int> > map;
266 bool res = r.search(s.c_str(),s.c_str()+s.size(),map,flags);
267 if(!res) return false;
268 m.assign(s.begin(),s.end(),map);
269 return true;
270 }
271
276
277 template<typename Regex>
278 bool regex_search(char const *s,cmatch &m, Regex const &r,int flags = 0)
279 {
280 std::vector<std::pair<int,int> > map;
281 char const *begin=s;
282 char const *end = begin+strlen(begin);
283 bool res = r.search(begin,end,map,flags);
284 if(!res) return false;
285 m.assign(begin,end,map);
286 return true;
287 }
288
292 template<typename Regex>
293 bool regex_match(char const *begin,char const *end, Regex const &r,int flags = 0)
294 {
295 return r.match(begin,end,flags);
296 }
300
301 template<typename Regex>
302 bool regex_match(std::string const &s, Regex const &r,int flags = 0)
303 {
304 return r.match(s.c_str(),s.c_str()+s.size(),flags);
305 }
309
310 template<typename Regex>
311 bool regex_match(char const *s, Regex const &r,int flags = 0)
312 {
313 return r.match(s,s+strlen(s),flags);
314 }
315
319 template<typename Regex>
320 bool regex_search(char const *begin,char const *end, Regex const &r,int flags = 0)
321 {
322 return r.search(begin,end,flags);
323 }
324
328 template<typename Regex>
329 bool regex_search(std::string const &s, Regex const &r,int flags = 0)
330 {
331 return r.search(s.c_str(),s.c_str()+s.size(),flags);
332 }
333
337 template<typename Regex>
338 bool regex_search(char const *s, Regex const &r,int flags = 0)
339 {
340 return r.search(s,s+strlen(s),flags);
341 }
342
343
344 // sub -- sub
345 template<typename Iterator>
346 bool operator==(sub_match<Iterator> const &l,sub_match<Iterator> const &r) { return l.compare(r) == 0; }
347 template<typename Iterator>
348 bool operator!=(sub_match<Iterator> const &l,sub_match<Iterator> const &r) { return l.compare(r) != 0; }
349 template<typename Iterator>
350 bool operator< (sub_match<Iterator> const &l,sub_match<Iterator> const &r) { return l.compare(r) < 0; }
351 template<typename Iterator>
352 bool operator> (sub_match<Iterator> const &l,sub_match<Iterator> const &r) { return l.compare(r) > 0; }
353 template<typename Iterator>
354 bool operator<=(sub_match<Iterator> const &l,sub_match<Iterator> const &r) { return l.compare(r) <= 0; }
355 template<typename Iterator>
356 bool operator>=(sub_match<Iterator> const &l,sub_match<Iterator> const &r) { return l.compare(r) >= 0; }
357
358 // str -- sub
359 template<typename Iterator>
360 bool operator==(
361 typename sub_match<Iterator>::string_type const &l,
362 sub_match<Iterator> const &r)
363 { return l.compare(r) == 0; }
364 template<typename Iterator>
365 bool operator!=(
366 typename sub_match<Iterator>::string_type const &l,
367 sub_match<Iterator> const &r)
368 { return l.compare(r) != 0; }
369
370 template<typename Iterator>
371 bool operator<=(
372 typename sub_match<Iterator>::string_type const &l,
373 sub_match<Iterator> const &r)
374 { return l.compare(r) <= 0; }
375 template<typename Iterator>
376 bool operator>=(
377 typename sub_match<Iterator>::string_type const &l,
378 sub_match<Iterator> const &r)
379 { return l.compare(r) >= 0; }
380
381 template<typename Iterator>
382 bool operator<(
383 typename sub_match<Iterator>::string_type const &l,
384 sub_match<Iterator> const &r)
385 { return l.compare(r) <0; }
386 template<typename Iterator>
387 bool operator>(
388 typename sub_match<Iterator>::string_type const &l,
389 sub_match<Iterator> const &r)
390 { return l.compare(r) > 0; }
391
392 // sub -- str
393
394 template<typename Iterator>
395 bool operator==(
396 sub_match<Iterator> const &l,
397 typename sub_match<Iterator>::string_type const &r
398 )
399 { return l.compare(r) == 0; }
400 template<typename Iterator>
401 bool operator!=(
402 sub_match<Iterator> const &l,
403 typename sub_match<Iterator>::string_type const &r
404 )
405 { return l.compare(r) != 0; }
406
407 template<typename Iterator>
408 bool operator<=(
409 sub_match<Iterator> const &l,
410 typename sub_match<Iterator>::string_type const &r
411 )
412 { return l.compare(r) <= 0; }
413
414 template<typename Iterator>
415 bool operator>=(
416 sub_match<Iterator> const &l,
417 typename sub_match<Iterator>::string_type const &r
418 )
419 { return l.compare(r) >= 0; }
420
421 template<typename Iterator>
422 bool operator<(
423 sub_match<Iterator> const &l,
424 typename sub_match<Iterator>::string_type const &r
425 )
426 { return l.compare(r) <0; }
427
428 template<typename Iterator>
429 bool operator>(
430 sub_match<Iterator> const &l,
431 typename sub_match<Iterator>::string_type const &r
432 )
433 { return l.compare(r) > 0; }
434
435 // * -- sub
436 template<typename Iterator>
437 bool operator==(
438 typename std::iterator_traits<Iterator>::value_type const *l,
439 sub_match<Iterator> const &r)
440 { return r.compare(l) ==0; }
441 template<typename Iterator>
442 bool operator!=(
443 typename std::iterator_traits<Iterator>::value_type const *l,
444 sub_match<Iterator> const &r)
445 { return r.compare(l) !=0; }
446
447
448 template<typename Iterator>
449 bool operator<=(
450 typename std::iterator_traits<Iterator>::value_type const *l,
451 sub_match<Iterator> const &r)
452 { return r.compare(l) > 0; }
453 template<typename Iterator>
454 bool operator>=(
455 typename std::iterator_traits<Iterator>::value_type const *l,
456 sub_match<Iterator> const &r)
457 { return r.compare(l) < 0; }
458
459 template<typename Iterator>
460 bool operator<(
461 typename std::iterator_traits<Iterator>::value_type const *l,
462 sub_match<Iterator> const &r)
463 { return r.compare(l) >=0; }
464 template<typename Iterator>
465 bool operator>(
466 typename std::iterator_traits<Iterator>::value_type const *l,
467 sub_match<Iterator> const &r)
468 { return r.compare(l) <= 0; }
469
470
471 // sub -- *
472
473 template<typename Iterator>
474 bool operator==(
475 sub_match<Iterator> const &l,
476 typename std::iterator_traits<Iterator>::value_type const *r
477 )
478 { return l.compare(r) == 0; }
479 template<typename Iterator>
480 bool operator!=(
481 sub_match<Iterator> const &l,
482 typename std::iterator_traits<Iterator>::value_type const *r
483 )
484 { return l.compare(r) != 0; }
485
486 template<typename Iterator>
487 bool operator<=(
488 sub_match<Iterator> const &l,
489 typename std::iterator_traits<Iterator>::value_type const *r
490 )
491 { return l.compare(r) <= 0; }
492
493 template<typename Iterator>
494 bool operator>=(
495 sub_match<Iterator> const &l,
496 typename std::iterator_traits<Iterator>::value_type const *r
497 )
498 { return l.compare(r) >= 0; }
499
500 template<typename Iterator>
501 bool operator<(
502 sub_match<Iterator> const &l,
503 typename std::iterator_traits<Iterator>::value_type const *r
504 )
505 { return l.compare(r) <0; }
506
507 template<typename Iterator>
508 bool operator>(
509 sub_match<Iterator> const &l,
510 typename std::iterator_traits<Iterator>::value_type const *r
511 )
512 { return l.compare(r) > 0; }
513
514 // add +
515
516 template<typename Iterator>
517 typename sub_match<Iterator>::string_type
518 operator+(sub_match<Iterator> const &l,sub_match<Iterator> const &r)
519 { return l.str() + r.str(); }
520
521 template<typename Iterator>
522 typename sub_match<Iterator>::string_type
523 operator+(sub_match<Iterator> const &l,typename sub_match<Iterator>::string_type const &r)
524 { return l.str() + r; }
525
526 template<typename Iterator>
527 typename sub_match<Iterator>::string_type
528 operator+(typename sub_match<Iterator>::string_type const &l,sub_match<Iterator> const &r)
529 { return l + r.str(); }
530
531 template<typename Iterator>
532 typename sub_match<Iterator>::string_type
533 operator+(sub_match<Iterator> const &l,typename sub_match<Iterator>::value_type const *r)
534 { return l.str() + r; }
535
536 template<typename Iterator>
537 typename sub_match<Iterator>::string_type
538 operator+(typename sub_match<Iterator>::value_type const *l,sub_match<Iterator> const &r)
539 { return l + r.str(); }
540
541 }
542
543
544
545 #endif
booster::sub_match::sub_match
sub_match()
Definition: regex_match.h:96
booster::match_results
The object that hold the result of matching a regular expression against the text using regex_match a...
Definition: regex_match.h:110
booster::match_results::prefix
value_type prefix()
Definition: regex_match.h:169
booster::regex_search
bool regex_search(char const *begin, char const *end, cmatch &m, Regex const &r, int flags=0)
Definition: regex_match.h:249
booster::match_results::operator[]
value_type operator[](int n) const
Definition: regex_match.h:128
booster::sub_match::compare
int compare(string_type const &other) const
Definition: regex_match.h:82
booster::match_results::match_results
match_results()
Definition: regex_match.h:115
booster::sub_match::matched
bool matched
Definition: regex_match.h:41
booster::sub_match::string_type
std::basic_string< value_type > string_type
Definition: regex_match.h:34
booster::regex_match
bool regex_match(char const *begin, char const *end, cmatch &m, Regex const &r, int flags=0)
Definition: regex_match.h:205
booster::match_results::value_type
sub_match< Iterator > value_type
Definition: regex_match.h:123
booster::sub_match
This class represents a single captures subexpression.
Definition: regex_match.h:25
booster::sub_match::compare
int compare(sub_match const &other) const
Definition: regex_match.h:75
booster::sub_match::compare
int compare(value_type const *s) const
Definition: regex_match.h:89
booster::match_results::size
size_t size() const
Definition: regex_match.h:146
booster::sub_match::length
difference_type length() const
Definition: regex_match.h:46
booster
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
booster::match_results::suffix
value_type suffix()
Definition: regex_match.h:154
booster::sub_match::str
string_type str() const
Definition: regex_match.h:64