Beispiel #1
0
def finditer_alt(pattern, iterable):
    """
    An experimental implementation of finditer.
    The idea here is to make an implementation that is able to work with any
    iterator, not necessariry the indexable ones.
    This implies (among other things) that each element is feeded only once and
    then discarded.
    """
    assert isinstance(pattern, Pattern)
    pattern = Star(Star(Any(), greedy=False) + Group(pattern, None))
    code = pattern.compile()
    vm = VirtualMachine(code)
    m = Match()
    new = Match()
    # Start VM
    vm.do_epsilon_transitions()
    vm.cutoff()
    for x in iterable:
        if not vm.is_alive():
            break
        vm.feed(x)
        vm.do_epsilon_transitions()
        new.state = vm.accepting_state(None)
        if new.state != None:
            if m.state == None:
                m.state = new.state
            elif m.start() == new.start() and m.end() < new.end():
                m.state = new.state
            elif m.start() != new.start():
                yield m
                m = new
                new = Match()
        vm.cutoff()
    if m.state != None:
        yield m
Beispiel #2
0
def _match(pattern, iterable):
    assert isinstance(pattern, Pattern)
    code = pattern.compile()
    vm = VirtualMachine(code)
    m = Match()
    # Start VM
    vm.do_epsilon_transitions()
    m.state = vm.accepting_state(None)
    vm.cutoff()
    for x in iterable:
        if not vm.is_alive():
            break
        vm.feed(x)
        vm.do_epsilon_transitions()
        m.state = vm.accepting_state(m.state)
        vm.cutoff()
    if m.state == None:
        return None
    return m