Created
December 18, 2011 21:59
-
-
Save mrjoes/1494609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First version: | |
def make_line_iter(iter): | |
buf = '' | |
last_pos = 0 | |
for p in iter: | |
buf += p | |
lb = buf.find('\n', last_pos) | |
while lb != -1: | |
yield buf[:lb + 1] | |
buf = buf[lb + 1:] | |
lb = buf.find('\n') | |
last_pos = len(buf) | |
if buf: | |
yield buf | |
if __name__ == '__main__': | |
print list(make_line_iter(['abc', 'def', 'g\nhi'])) | |
print list(make_line_iter(['a\r\n', 'b\r\n', 'test\n'])) | |
Slightly optimized version, if one buffer contains more than one line break, will work faster: | |
def make_line_iter(iter): | |
buf = '' | |
last_pos = 0 | |
for p in iter: | |
buf += p | |
n = 0 | |
lb = buf.find('\n', last_pos) | |
while lb != -1: | |
yield buf[n:lb + 1] | |
n = lb + 1 | |
lb = buf.find('\n', n) | |
if n > 0: | |
buf = buf[n:] | |
last_pos = len(buf) | |
if buf: | |
yield buf | |
if __name__ == '__main__': | |
print list(make_line_iter(['abc', 'def', 'g\nhi'])) | |
print list(make_line_iter(['a\r\n', 'b\r\n', 'test\n'])) | |
print list(make_line_iter(['a\r\nb\n', 'b\r\nc\nd\n', 'test\n'])) | |
Outputs: | |
['abcdefg\n', 'hi'] | |
['a\r\n', 'b\r\n', 'test\n'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment