root/todo/tags/0.1.20/util/Regex.h

Revision 443, 1.9 kB (checked in by athomas, 1 year ago)

Dev Todo:

  • A few bugfixes.
  • Added XML declaration parsing/saving. This is a stop-gap at best, but will
    hopefully be useful to some.
  • Removed reliance on builtin regex library. Hopefully the completely broken
    version of glibc that instigated the inclusion of it is now out of
    circulation.
  • Fixed a whole bunch of compiler warnings on more recent GCC versions.
Line 
1 #ifndef CRASH_REGEX
2 #define CRASH_REGEX
3
4 #include <cassert>
5 #include <cstring>
6 #include <string>
7 #include <map>
8 #include <utility>
9 #include <stdexcept>
10 #include <cassert>
11 #include <sys/types.h>
12 #include <regex.h>
13
14 #ifndef CRASH_REGEX_CACHE_THRESHOLD
15 #define CRASH_REGEX_CACHE_THRESHOLD 128
16 #endif
17
18 using namespace std;
19
20
21 /**
22         Regex is a C++ wrapper around the POSIX Regex library.
23
24         13/00/01        Added cache. This speeds up general rx construction considerably.
25         14/08/00        Created.
26 */
27
28 class Regex {
29         public :
30                 struct exception : public runtime_error { exception(string const &what) : runtime_error(what) {} };
31                 struct out_of_range : public exception { out_of_range(string const &what) : exception(what) {} };
32                 struct no_match : public exception { no_match(string const &what) : exception(what) {} };
33
34                 Regex();
35                 Regex(char const *regex);
36                 Regex(Regex const &copy);
37                 ~Regex();
38
39                 Regex &operator = (Regex const &copy);
40                 Regex &operator = (char const *regex);
41
42                 string const &source() const { return inrx; }
43
44                 /*
45                         Regex regex("'([^']*)'");
46                         string out;
47
48                                 out = regex.transform("'alec thomas'", "(\\1)");
49                                 // outputs: (alec thomas)
50                 */
51                 string transform(string const &in, string const &mask);
52
53                 int match(char const *str);
54                 int operator == (char const *str) { return match(str); }
55                 int matchStart(char const *str);
56                 int operator <= (char const *str) { return matchStart(str); }
57
58                 int substrings() {
59                         for (int i = 0; i < 50; i++)
60                                 if (matches[i].rm_so == -1) return i;
61                         return 50;
62                 }
63
64                 int subStart(unsigned index) {
65                         assert(index < 50 && matches[index].rm_so != -1);
66                         return matches[index].rm_so;
67                 }
68                 int subEnd(unsigned index) {
69                         assert(index < 50 && matches[index].rm_so != -1);
70                         return matches[index].rm_so;
71                 }
72         private :
73                 string inrx;
74                 regex_t regex;
75                 regmatch_t matches[50];
76
77                 struct Cache {
78                         Cache() : hits(0), instances(0) {}
79
80                         regex_t rx;
81                         int hits, instances;
82                 };
83                 friend struct Cache;
84
85 //              static map<string, Cache> cache;
86 };
87 #endif
Note: See TracBrowser for help on using the browser.