[pypy-commit] pypy default: make our file's universal newline .readline implementation less ridiculous

cfbolz pypy.commits at gmail.com
Tue Mar 28 17:29:39 EDT 2017


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: 
Changeset: r90856:aa29d9052da9
Date: 2017-03-28 23:28 +0200
http://bitbucket.org/pypy/pypy/changeset/aa29d9052da9/

Log:	make our file's universal newline .readline implementation less
	ridiculous (still room for optimization left, but this fixes the
	worst issue)

diff --git a/rpython/rlib/streamio.py b/rpython/rlib/streamio.py
--- a/rpython/rlib/streamio.py
+++ b/rpython/rlib/streamio.py
@@ -1032,11 +1032,13 @@
             # we can safely read without reading past an end-of-line
             startindex, peeked = self.base.peek()
             assert 0 <= startindex <= len(peeked)
-            pn = peeked.find("\n", startindex)
-            pr = peeked.find("\r", startindex)
-            if pn < 0: pn = len(peeked)
-            if pr < 0: pr = len(peeked)
-            c = self.read(min(pn, pr) - startindex + 1)
+            cl_or_lf_pos = len(peeked)
+            for i in range(startindex, len(peeked)):
+                ch = peeked[i]
+                if ch == '\n' or ch == '\r':
+                    cl_or_lf_pos = i
+                    break
+            c = self.read(cl_or_lf_pos - startindex + 1)
             if not c:
                 break
             result.append(c)


More information about the pypy-commit mailing list