Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (50 sloc) 1.43 KB
inline C <<end
#include <string.h>
#include <pcre.h>
#include "pbox.h"
#define OVECCOUNT 30
int ovector[OVECCOUNT];
datum mtab = 0;
end
def re: obj:
inline C dotall <<end
return int2datum(PCRE_DOTALL);
end
(run pat) (re.make pat (re.dotall))
(make pat opts):
def compile: obj:
inline C (run.) <<end
pcre *re;
const char *error;
int erroffset, opts;
opts = datum2int(n_opts);
re = pcre_compile((char *) n_pat, opts | PCRE_UTF8, &error, &erroffset, NULL);
if (re == NULL) return nil;
return make_pbox(re, free);
end
def compiled (compile.)
def r: obj:
(run subj) (r.match subj (r.runopts))
(find subj) (r.match subj (r.findopts))
inline C (runopts.) <<end
return int2datum(PCRE_ANCHORED);
end
inline C (findopts.) <<end
return int2datum(0);
end
inline C (match subj opts) <<end
char *res;
int rc, res_size, opts;
pcre *re = (pcre *) rcv[2];
opts = datum2int(n_opts);
rc = pcre_exec(re, NULL, (char *) n_subj, datum_size(n_subj), 0, opts, ovector,
OVECCOUNT);
if (rc < 0) return nil;
if (rc == 0) return nil;
res = ((char *) n_subj) + ovector[0];
res_size = ovector[1] - ovector[0];
/* for now, assume UTF-8 len == res_size */
return make_str_init(res_size, res);
end
. r
. re