-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp_interface.h
77 lines (63 loc) · 1.57 KB
/
regexp_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/** \file regexp_interface.h
* Interface to regular expression stuff, to keep it in C with only one copy.
*/
#include "mudstring.h"
#include "config.h"
#if defined REGEXP_PCRE
#include <pcre.h>
#else
// Old regexp stuff, as found in regexp.[ch]
extern char *compile (const char *, char *, char *, int);
extern int step (const char *, const char *);
#endif
class RegularExpression
{
#if defined REGEXP_PCRE
pcre* _compileresults;
#else
char _compileresults[BUFFER_LEN];
#endif
public:
RegularExpression(const String& pattern);
~RegularExpression();
bool Match(const String& target);
};
// Use PCRE (Perl Compatible Regular Expressions)
#if defined(REGEXP_PCRE)
inline RegularExpression::RegularExpression(const String& pattern)
{
const char* errptr;
int erroffset;
_compileresults = pcre_compile(pattern.c_str(), PCRE_CASELESS, &errptr, &erroffset, NULL);
}
inline RegularExpression::~RegularExpression()
{
if(_compileresults)
{
pcre_free(_compileresults);
_compileresults = NULL;
}
}
inline bool RegularExpression::Match(const String& target)
{
if(!_compileresults)
return false;
if(pcre_exec(_compileresults, NULL, target.c_str(), target.length(), 0, 0, NULL, 0) < 0)
{
return false;
}
return true;
}
#else
inline RegularExpression::RegularExpression(const String& regexp)
{
compile(regexp.c_str(), _compileresults, _compileresults+sizeof(_compileresults), '\0');
}
inline RegularExpression::~RegularExpression()
{
}
inline bool RegularExpression::Match(const String& target)
{
return step(target.c_str(), _compileresults);
}
#endif