2424#include " libts.h"
2525#include " Regex.h"
2626
27+ #ifdef PCRE_CONFIG_JIT
28+ struct RegexThreadKey
29+ {
30+ RegexThreadKey () {
31+ pthread_key_create (&this ->key , (void (*)(void *)) &pcre_jit_stack_free);
32+ }
33+
34+ pthread_key_t key;
35+ };
36+
37+ static RegexThreadKey k;
38+
39+ static pcre_jit_stack *
40+ get_jit_stack (void *data ATS_UNUSED )
41+ {
42+ pcre_jit_stack *jit_stack;
43+
44+ if ((jit_stack = (pcre_jit_stack *) pthread_getspecific (k.key )) == NULL ) {
45+ jit_stack = pcre_jit_stack_alloc (ats_pagesize (), 1024 * 1024 ); // 1 page min and 1MB max
46+ pthread_setspecific (k.key , (void *)jit_stack);
47+ }
48+
49+ return jit_stack;
50+ }
51+ #endif
52+
53+ bool
54+ Regex::compile (const char *pattern, unsigned flags)
55+ {
56+ const char *error;
57+ int erroffset;
58+ int options = 0 ;
59+ int study_opts = 0 ;
60+
61+ if (regex)
62+ return false ;
63+
64+ if (flags & RE_CASE_INSENSITIVE ) {
65+ options |= PCRE_CASELESS ;
66+ }
67+
68+ if (flags & RE_ANCHORED ) {
69+ options |= PCRE_ANCHORED ;
70+ }
71+
72+ regex = pcre_compile (pattern, options, &error, &erroffset, NULL );
73+ if (error) {
74+ regex = NULL ;
75+ return false ;
76+ }
77+
78+ #ifdef PCRE_CONFIG_JIT
79+ study_opts |= PCRE_STUDY_JIT_COMPILE ;
80+ #endif
81+
82+ regex_extra = pcre_study (regex, study_opts, &error);
83+
84+ #ifdef PCRE_CONFIG_JIT
85+ if (regex_extra)
86+ pcre_assign_jit_stack (regex_extra, &get_jit_stack, NULL );
87+ #endif
88+
89+ return true ;
90+ }
91+
92+ bool
93+ Regex::exec (const char *str)
94+ {
95+ return exec (str, strlen (str));
96+ }
97+
98+ bool
99+ Regex::exec (const char *str, int length)
100+ {
101+ int ovector[30 ], rv;
102+
103+ rv = pcre_exec (regex, regex_extra, str, length , 0 , 0 , ovector, countof (ovector));
104+ return rv > 0 ? true : false ;
105+ }
106+
107+ Regex::~Regex ()
108+ {
109+ if (regex_extra)
110+ #ifdef PCRE_CONFIG_JIT
111+ pcre_free_study (regex_extra);
112+ #else
113+ pcre_free (regex_extra);
114+ #endif
115+ if (regex)
116+ pcre_free (regex);
117+ }
118+
27119DFA ::~DFA ()
28120{
29121 dfa_pattern * p = _my_patterns;
30122 dfa_pattern * t;
31123
32124 while (p) {
33- if (p->_pe )
34- pcre_free (p->_pe );
35125 if (p->_re )
36- pcre_free ( p->_re ) ;
126+ delete p->_re ;
37127 if (p->_p )
38128 ats_free (p->_p );
39129 t = p->_next ;
@@ -45,31 +135,20 @@ DFA::~DFA()
45135dfa_pattern *
46136DFA ::build(const char *pattern, unsigned flags)
47137{
48- const char *error;
49- int erroffset;
50138 dfa_pattern* ret;
51- int options = PCRE_ANCHORED ;
52-
53- ret = (dfa_pattern*)ats_malloc (sizeof (dfa_pattern));
54- ret->_p = NULL ;
55-
56- if (flags & RE_CASE_INSENSITIVE ) {
57- options |= PCRE_CASELESS ;
58- }
59-
60- if (flags & RE_UNANCHORED ) {
61- options &= ~PCRE_ANCHORED ;
62- }
139+ int rv;
63140
64- ret->_re = pcre_compile (pattern, options, &error, &erroffset, NULL );
65- if (error) {
66- ats_free (ret);
67- return NULL ;
141+ if (!(flags & RE_UNANCHORED )) {
142+ flags |= RE_ANCHORED ;
68143 }
69144
70- ret->_pe = pcre_study (ret->_re , 0 , &error);
145+ ret = (dfa_pattern*)ats_malloc (sizeof (dfa_pattern));
146+ ret->_p = NULL ;
71147
72- if (error) {
148+ ret->_re = new Regex ();
149+ rv = ret->_re ->compile (pattern, flags);
150+ if (rv == -1 ) {
151+ delete ret->_re ;
73152 ats_free (ret);
74153 return NULL ;
75154 }
@@ -96,11 +175,9 @@ DFA::compile(const char **patterns, int npatterns, unsigned flags)
96175 dfa_pattern *ret = NULL ;
97176 dfa_pattern *end = NULL ;
98177 int i;
99- // char buf[128];
100178
101179 for (i = 0 ; i < npatterns; i++) {
102180 pattern = patterns[i];
103- // snprintf(buf,128,"%s",pattern);
104181 ret = build (pattern,flags);
105182 if (!ret) {
106183 continue ;
@@ -135,12 +212,10 @@ int
135212DFA ::match(const char *str, int length) const
136213{
137214 int rc;
138- int ovector[30 ];
139- // int wspace[20];
140215 dfa_pattern * p = _my_patterns;
141216
142217 while (p) {
143- rc = pcre_exec ( p->_re , p-> _pe , str, length , 0 , 0 , ovector, 30 /* ,wspace,20 */ );
218+ rc = p->_re -> exec ( str, length);
144219 if (rc > 0 ) {
145220 return p->_idx ;
146221 }
0 commit comments