31 lines
597 B
Python
31 lines
597 B
Python
import io
|
|
|
|
CHUNK_SIZE = 4096
|
|
CHUNK_STRIP = "\x01\x03\x0a\x20"
|
|
|
|
def each_chunk(fh: io.TextIOBase, sep: str, strip=None):
|
|
buf = ''
|
|
|
|
while True:
|
|
chunk = fh.read(CHUNK_SIZE)
|
|
|
|
if chunk == '':
|
|
ret = buf.strip(strip)
|
|
|
|
if ret != '':
|
|
yield ret
|
|
|
|
break
|
|
|
|
buf += chunk
|
|
|
|
while True:
|
|
try:
|
|
part, buf = buf.split(sep, 1)
|
|
except ValueError:
|
|
break
|
|
else:
|
|
ret = part.strip(strip)
|
|
|
|
if ret != '':
|
|
yield ret
|