FAQ sync
[p5sagit/p5-mst-13.2.git] / ext / re / re.xs
CommitLineData
41b16711 1#if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
2# define DEBUGGING
3#endif
4
c5be433b 5#define PERL_NO_GET_CONTEXT
56953603 6#include "EXTERN.h"
7#include "perl.h"
8#include "XSUB.h"
256ddcd0 9#include "re_comp.h"
56953603 10
f9f4320a 11
97f88e98 12START_EXTERN_C
13
973f7e2e 14extern REGEXP* my_re_compile (pTHX_ const SV * const pattern, const U32 pm_flags);
49d7dfbc 15extern I32 my_regexec (pTHX_ REGEXP * const prog, char* stringarg, char* strend,
20ce7b12 16 char* strbeg, I32 minend, SV* screamer,
17 void* data, U32 flags);
f8149455 18
49d7dfbc 19extern char* my_re_intuit_start (pTHX_ REGEXP * const prog, SV *sv, char *strpos,
973f7e2e 20 char *strend, const U32 flags,
f722798b 21 struct re_scream_pos_data_s *data);
49d7dfbc 22extern SV* my_re_intuit_string (pTHX_ REGEXP * const prog);
23
24extern void my_regfree (pTHX_ REGEXP * const r);
2fdbfb4d 25
d932daed 26extern void my_reg_numbered_buff_fetch(pTHX_ REGEXP * const rx, const I32 paren,
27 SV * const usesv);
28extern void my_reg_numbered_buff_store(pTHX_ REGEXP * const rx, const I32 paren,
29 SV const * const value);
30extern I32 my_reg_numbered_buff_length(pTHX_ REGEXP * const rx,
31 const SV * const sv, const I32 paren);
32
192b9cd1 33extern SV* my_reg_named_buff(pTHX_ REGEXP * const, SV * const, SV * const,
34 const U32);
35extern SV* my_reg_named_buff_iter(pTHX_ REGEXP * const rx,
36 const SV * const lastkey, const U32 flags);
2fdbfb4d 37
49d7dfbc 38extern SV* my_reg_qr_package(pTHX_ REGEXP * const rx);
a3c0e9ca 39#if defined(USE_ITHREADS)
49d7dfbc 40extern void* my_regdupe (pTHX_ REGEXP * const r, CLONE_PARAMS *param);
a3c0e9ca 41#endif
0a28d81c 42
70685ca0 43EXTERN_C const struct regexp_engine my_reg_engine;
44
45END_EXTERN_C
46
47const struct regexp_engine my_reg_engine = {
6d5c990f 48 my_re_compile,
f9f4320a 49 my_regexec,
50 my_re_intuit_start,
51 my_re_intuit_string,
52 my_regfree,
2fdbfb4d 53 my_reg_numbered_buff_fetch,
54 my_reg_numbered_buff_store,
55 my_reg_numbered_buff_length,
192b9cd1 56 my_reg_named_buff,
57 my_reg_named_buff_iter,
49d7dfbc 58 my_reg_qr_package,
a3c0e9ca 59#if defined(USE_ITHREADS)
f9f4320a 60 my_regdupe
a3c0e9ca 61#endif
0a28d81c 62};
63
56953603 64MODULE = re PACKAGE = re
65
66void
f9f4320a 67install()
68 PPCODE:
69 PL_colorset = 0; /* Allow reinspection of ENV. */
70 /* PL_debug |= DEBUG_r_FLAG; */
71 XPUSHs(sv_2mortal(newSViv(PTR2IV(&my_reg_engine))));
72
de8c5301 73
74void
de8c5301 75regexp_pattern(sv)
76 SV * sv
77PROTOTYPE: $
78PREINIT:
49d7dfbc 79 REGEXP *re;
de8c5301 80PPCODE:
81{
82 /*
83 Checks if a reference is a regex or not. If the parameter is
84 not a ref, or is not the result of a qr// then returns false
85 in scalar context and an empty list in list context.
86 Otherwise in list context it returns the pattern and the
87 modifiers, in scalar context it returns the pattern just as it
88 would if the qr// was stringified normally, regardless as
89 to the class of the variable and any strigification overloads
90 on the object.
91 */
92
f7e71195 93 if ((re = SvRX(sv))) /* assign deliberate */
de8c5301 94 {
de8c5301 95 /* Housten, we have a regex! */
96 SV *pattern;
de8c5301 97 STRLEN patlen = 0;
98 STRLEN left = 0;
99 char reflags[6];
100
101 if ( GIMME_V == G_ARRAY ) {
102 /*
103 we are in list context so stringify
104 the modifiers that apply. We ignore "negative
105 modifiers" in this scenario.
106 */
107
bcdf7404 108 char *fptr = INT_PAT_MODS;
de8c5301 109 char ch;
bbe252da 110 U16 match_flags = (U16)((re->extflags & PMf_COMPILETIME) >> 12);
de8c5301 111
112 while((ch = *fptr++)) {
bbe252da 113 if(match_flags & 1) {
de8c5301 114 reflags[left++] = ch;
115 }
bbe252da 116 match_flags >>= 1;
de8c5301 117 }
118
119 pattern = sv_2mortal(newSVpvn(re->precomp,re->prelen));
bbe252da 120 if (re->extflags & RXf_UTF8) SvUTF8_on(pattern);
de8c5301 121
122 /* return the pattern and the modifiers */
123 XPUSHs(pattern);
124 XPUSHs(sv_2mortal(newSVpvn(reflags,left)));
125 XSRETURN(2);
126 } else {
127 /* Scalar, so use the string that Perl would return */
de8c5301 128 /* return the pattern in (?msix:..) format */
bcdf7404 129 pattern = sv_2mortal(newSVpvn(re->wrapped,re->wraplen));
bbe252da 130 if (re->extflags & RXf_UTF8)
de8c5301 131 SvUTF8_on(pattern);
132 XPUSHs(pattern);
133 XSRETURN(1);
134 }
135 } else {
136 /* It ain't a regexp folks */
137 if ( GIMME_V == G_ARRAY ) {
138 /* return the empty list */
139 XSRETURN_UNDEF;
140 } else {
141 /* Because of the (?:..) wrapping involved in a
142 stringified pattern it is impossible to get a
143 result for a real regexp that would evaluate to
144 false. Therefore we can return PL_sv_no to signify
145 that the object is not a regex, this means that one
146 can say
147
148 if (regex($might_be_a_regex) eq '(?:foo)') { }
149
150 and not worry about undefined values.
151 */
152 XSRETURN_NO;
153 }
154 }
155 /* NOT-REACHED */
256ddcd0 156}
157
158
159void
160regmust(sv)
161 SV * sv
162PROTOTYPE: $
163PREINIT:
49d7dfbc 164 REGEXP *re;
256ddcd0 165PPCODE:
166{
f7e71195 167 if ((re = SvRX(sv))) /* assign deliberate */
256ddcd0 168 {
169 SV *an = &PL_sv_no;
170 SV *fl = &PL_sv_no;
256ddcd0 171 if (re->anchored_substr) {
172 an = newSVsv(re->anchored_substr);
173 } else if (re->anchored_utf8) {
174 an = newSVsv(re->anchored_utf8);
175 }
176 if (re->float_substr) {
177 fl = newSVsv(re->float_substr);
178 } else if (re->float_utf8) {
179 fl = newSVsv(re->float_utf8);
180 }
181 XPUSHs(an);
182 XPUSHs(fl);
183 XSRETURN(2);
184 }
185 XSRETURN_UNDEF;
186}
44a2ac75 187