def findall(pat, iterable, nmax=20): pat = Star(Any(), greedy=False) + Group(pat, None) code = pat.compile() prog = ffi.new('Prog*', {'len': len(pat), 'start': code._inst}) seq = Seq(iterable) m = [Match(pat._state_i) for _ in range(nmax)] ms = ffi.new('SubMatch*[]', [x._m for x in m]) c = lib.findall(prog, seq._seq, ms, nmax) return m[:c]
def _match(pat, iterable): code = pat.compile() prog = ffi.new('Prog*', {'len': len(pat), 'start': code._inst}) seq = Seq(iterable) match = Match(pat._state_i) matched = lib.search(prog, seq._seq, match._m) if matched: return match return None
def __init__(self, iterable): self._p = [ffi.new_handle(item) for item in iterable] self._seq = ffi.new('Seq*', {'len': len(iterable), 'start': self._p})
def __init__(self, state): self.state = state self.len = len(self.state) self._pos = ffi.new('Pos[]', self.len) self._m = ffi.new('SubMatch*', {'len': self.len, 'pos': self._pos})
def __init__(self, opcode): self._inst = ffi.new('Inst*') self._inst.opcode = opcode # new_handle对象必须保存在python变量空间,直接复制给<cdata void*>不能keep alive self._o = ffi.new_handle(self) self._inst.o = self._o