def _find_pattern(pattern, buf, iterator, start=0):
    """Find pattern in buf, appending new data from iterator to buf if
    necessary
    """

    while len(buf) <= start + len(pattern):
        buf.extend(next(iterator))
    while True:
        pos = buf.find(pattern, start)
        if pos >= 0:
            assert pos >= start
            return pos
        else:
            start = len(buf) - len(pattern) - 1
            buf.extend(next(iterator))