1 /* This file is part of the re::engine::Hooks Perl module.
2 * See http://search.cpan.org/dist/re-engine-Hooks/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "re::engine::Hooks"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
14 #define REH_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
17 # define SvPV_const(S, L) SvPV(S, L)
20 /* --- Lexical hints ------------------------------------------------------- */
22 STATIC U32 reh_hash = 0;
24 STATIC SV *reh_hint(pTHX) {
25 #define reh_hint() reh_hint(aTHX)
28 #ifdef cop_hints_fetch_pvn
29 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
31 #elif REH_HAS_PERL(5, 9, 5)
32 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
34 __PACKAGE__, __PACKAGE_LEN__,
38 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
47 /* --- Public API ---------------------------------------------------------- */
49 #include "re_engine_hooks.h"
51 typedef struct reh_action {
52 struct reh_action *next;
59 STATIC reh_action *reh_action_list = 0;
62 void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec) {
68 for (i = 0; i < len; ++i)
69 if (!isALNUM(key[i]) && key[i] != ':')
71 key_dup = PerlMemShared_malloc(len + 1);
72 memcpy(key_dup, key, len);
75 a = PerlMemShared_malloc(sizeof *a);
76 a->next = reh_action_list;
87 /* --- Private API --------------------------------------------------------- */
89 void reh_call_comp_hook(pTHX_ regexp *rx, regnode *node) {
90 SV *hint = reh_hint();
92 if (hint && SvPOK(hint)) {
94 const char *keys = SvPV_const(hint, len);
97 for (a = reh_action_list; a; a = a->next) {
99 char *p = strstr(keys, a->key);
101 if (p && (p + a->klen <= keys + len) && p[a->klen] == ' ')
102 a->comp(aTHX_ rx, node);
108 void reh_call_exec_hook(pTHX_ regexp *rx, regnode *node, regmatch_info *reginfo, regmatch_state *st) {
109 SV *hint = reh_hint();
111 if (hint && SvPOK(hint)) {
113 const char *keys = SvPV_const(hint, len);
116 for (a = reh_action_list; a; a = a->next) {
118 char *p = strstr(keys, a->key);
120 if (p && (p + a->klen <= keys + len) && p[a->klen] == ' ')
121 a->exec(aTHX_ rx, node, reginfo, st);
127 /* --- Custom regexp engine ------------------------------------------------ */
129 #if PERL_VERSION <= 10
130 # define rxREGEXP(RX) (RX)
132 # define rxREGEXP(RX) (SvANY(RX))
135 #if PERL_VERSION <= 10
136 EXTERN_C REGEXP *reh_regcomp(pTHX_ const SV * const, const U32);
138 EXTERN_C REGEXP *reh_regcomp(pTHX_ SV * const, U32);
140 EXTERN_C I32 reh_regexec(pTHX_ REGEXP * const, char *, char *,
141 char *, I32, SV *, void *, U32);
142 EXTERN_C char * reh_re_intuit_start(pTHX_ REGEXP * const, SV *, char *,
143 char *, U32, re_scream_pos_data *);
144 EXTERN_C SV * reh_re_intuit_string(pTHX_ REGEXP * const);
145 EXTERN_C void reh_regfree(pTHX_ REGEXP * const);
146 EXTERN_C void reh_reg_numbered_buff_fetch(pTHX_ REGEXP * const,
147 const I32, SV * const);
148 EXTERN_C void reh_reg_numbered_buff_store(pTHX_ REGEXP * const,
149 const I32, SV const * const);
150 EXTERN_C I32 reh_reg_numbered_buff_length(pTHX_ REGEXP * const,
151 const SV * const, const I32);
152 EXTERN_C SV * reh_reg_named_buff(pTHX_ REGEXP * const, SV * const,
153 SV * const, const U32);
154 EXTERN_C SV * reh_reg_named_buff_iter(pTHX_ REGEXP * const,
155 const SV * const, const U32);
156 EXTERN_C SV * reh_reg_qr_package(pTHX_ REGEXP * const);
158 EXTERN_C void * reh_regdupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
161 EXTERN_C const struct regexp_engine reh_regexp_engine;
164 #if PERL_VERSION <= 10
165 reh_re_compile(pTHX_ const SV * const pattern, const U32 flags)
167 reh_re_compile(pTHX_ SV * const pattern, U32 flags)
173 RX = reh_regcomp(aTHX_ pattern, flags);
176 rx->engine = &reh_regexp_engine;
181 const struct regexp_engine reh_regexp_engine = {
185 reh_re_intuit_string,
187 reh_reg_numbered_buff_fetch,
188 reh_reg_numbered_buff_store,
189 reh_reg_numbered_buff_length,
191 reh_reg_named_buff_iter,
193 #if defined(USE_ITHREADS)
198 /* --- XS ------------------------------------------------------------------ */
200 MODULE = re::engine::Hooks PACKAGE = re::engine::Hooks
208 XPUSHs(sv_2mortal(newSViv(PTR2IV(&reh_regexp_engine))));
215 reh_action *a = reh_action_list;
219 s = SvPV_const(key, len);
221 if (a->klen == len && memcmp(a->key, s, len) == 0)