2014-04-08 16:30:54 +00:00
|
|
|
import ffi
|
|
|
|
import array
|
|
|
|
|
|
|
|
|
|
|
|
pcre = ffi.open("libpcre.so.3")
|
|
|
|
|
|
|
|
# pcre *pcre_compile(const char *pattern, int options,
|
|
|
|
# const char **errptr, int *erroffset,
|
|
|
|
# const unsigned char *tableptr);
|
|
|
|
pcre_compile = pcre.func("p", "pcre_compile", "sipps")
|
|
|
|
|
|
|
|
# int pcre_exec(const pcre *code, const pcre_extra *extra,
|
|
|
|
# const char *subject, int length, int startoffset,
|
|
|
|
# int options, int *ovector, int ovecsize);
|
|
|
|
pcre_exec = pcre.func("i", "pcre_exec", "ppsiiipi")
|
|
|
|
|
2014-04-11 01:48:13 +00:00
|
|
|
IGNORECASE = I = 1
|
|
|
|
MULTILINE = M = 2
|
|
|
|
DOTALL = S = 4
|
|
|
|
VERBOSE = X = 8
|
|
|
|
PCRE_ANCHORED = 0x10
|
|
|
|
|
2014-04-08 16:30:54 +00:00
|
|
|
|
|
|
|
class PCREMatch:
|
|
|
|
|
|
|
|
def __init__(self, s, num_matches, offsets):
|
|
|
|
self.s = s
|
|
|
|
self.num = num_matches
|
|
|
|
self.offsets = offsets
|
|
|
|
|
|
|
|
def group(self, n):
|
|
|
|
return self.s[self.offsets[n*2]:self.offsets[n*2+1]]
|
|
|
|
|
|
|
|
|
|
|
|
class PCREPattern:
|
|
|
|
|
|
|
|
def __init__(self, compiled_ptn):
|
|
|
|
self.obj = compiled_ptn
|
|
|
|
|
2014-04-11 01:48:13 +00:00
|
|
|
def search(self, s, _flags=0):
|
2014-04-08 16:30:54 +00:00
|
|
|
ov = array.array('i', [0, 0, 0] * 2)
|
2014-04-11 01:48:13 +00:00
|
|
|
num = pcre_exec(self.obj, None, s, len(s), 0, _flags, ov, len(ov))
|
2014-04-08 16:30:54 +00:00
|
|
|
if num == -1:
|
|
|
|
# No match
|
|
|
|
return None
|
|
|
|
return PCREMatch(s, num, ov)
|
|
|
|
|
2014-04-11 01:48:13 +00:00
|
|
|
def match(self, s):
|
|
|
|
return self.search(s, PCRE_ANCHORED)
|
|
|
|
|
2014-04-08 16:30:54 +00:00
|
|
|
|
|
|
|
def compile(pattern, flags=0):
|
|
|
|
errptr = bytes(4)
|
|
|
|
erroffset = bytes(4)
|
|
|
|
regex = pcre_compile(pattern, flags, errptr, erroffset, None)
|
|
|
|
assert regex
|
|
|
|
return PCREPattern(regex)
|
|
|
|
|
|
|
|
|
|
|
|
def search(pattern, string, flags=0):
|
|
|
|
r = compile(pattern, flags)
|
|
|
|
return r.search(string)
|