Fig bug in split_buffer where input was being discarded

Also, write some tests for it.
This commit is contained in:
Aanand Prasad 2014-01-22 17:44:04 +00:00
commit e8472be6d5
3 changed files with 64 additions and 19 deletions

View file

@ -83,3 +83,28 @@ def mkdir(path, permissions=0o700):
def docker_url():
return os.environ.get('DOCKER_HOST')
def split_buffer(reader, separator):
"""
Given a generator which yields strings and a separator string,
joins all input, splits on the separator and yields each chunk.
Unlike string.split(), each chunk includes the trailing
separator, except for the last one if none was found on the end
of the input.
"""
buffered = str('')
separator = str(separator)
for data in reader:
buffered += data
while True:
index = buffered.find(separator)
if index == -1:
break
yield buffered[:index+1]
buffered = buffered[index+1:]
if len(buffered) > 0:
yield buffered