I'm working on some code that needs to serialize Perl regexes, including any regex flags. Only a subset of flags are supported, so I need to detect when unsupported flags like /u
are in the regex object.
The current version of the code does this:
static void serialize_regex_flags(buffer *buf, SV *sv) {
char flags[] = {0,0,0,0,0,0};
unsigned int i = 0, f = 0;
STRLEN string_length;
char *string = SvPV(sv, string_length);
Then manually processes string
char-by-char to find flags.
The problem here is that the stringification of regex flags changed (I think in Perl 5.14) from e.g. (?i-xsm:foo)
to (?^i:foo)
, which makes parsing a pain.
I could check the version of perl
, or just write the parser to handle both cases, but something tells me there must be a superior method of introspection available.
In Perl, you'd use
re::regexp_pattern
.As you can see from the source of
regexp_pattern
, there's no function in the API to obtain that information, so I recommend that you call that function too from XS too.perlcall covers calling Perl functions from C. I came up with the following untested code:
Usage: