[pypy-commit] pypy default: Replace (pos-if-found, pos-if-not-found) tuple with (position, found)

rlamy pypy.commits at gmail.com
Fri Nov 24 15:20:42 EST 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: 
Changeset: r93169:9c9233da7cc4
Date: 2017-11-24 20:18 +0000
http://bitbucket.org/pypy/pypy/changeset/9c9233da7cc4/

Log:	Replace (pos-if-found, pos-if-not-found) tuple with (position,
	found)

diff --git a/pypy/module/_io/interp_stringio.py b/pypy/module/_io/interp_stringio.py
--- a/pypy/module/_io/interp_stringio.py
+++ b/pypy/module/_io/interp_stringio.py
@@ -176,13 +176,13 @@
             limit = len(self.buf) - self.pos
         assert limit >= 0
 
-        endpos, consumed = self._find_line_ending(
+        endpos, found = self._find_line_ending(
             # XXX: super inefficient, makes a copy of the entire contents.
             u"".join(self.buf),
             start,
             limit
         )
-        if endpos < 0:
+        if not found:
             endpos = start + limit
         assert endpos >= 0
         self.pos = endpos
diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -224,15 +224,15 @@
             ch = line[i]
             i += 1
             if ch == '\n':
-                return i, 0
+                return i, True
             if ch == '\r':
                 if i >= end:
                     break
                 if line[i] == '\n':
-                    return i + 1, 0
+                    return i + 1, True
                 else:
-                    return i, 0
-        return -1, end
+                    return i, True
+        return end, False
 
     def _find_marker(self, marker, line, start, limit):
         limit = min(limit, len(line) - start)
@@ -244,8 +244,8 @@
                     if line[i + j] != marker[j]:
                         break  # from inner loop
                 else:
-                    return i + len(marker), 0
-        return -1, end - len(marker) + 1
+                    return i + len(marker), True
+        return end - len(marker) + 1, False
 
     def _find_line_ending(self, line, start, limit):
         if self.readuniversal:
@@ -667,7 +667,7 @@
             has_data = self._ensure_data(space)
             if not has_data:
                 # end of file
-                start = endpos = 0
+                start = end_scan = 0
                 break
 
             if remnant:
@@ -677,7 +677,7 @@
                     builder.append(u'\r\n')
                     self.decoded_chars_used = 1
                     line = remnant = None
-                    start = endpos = 0
+                    start = end_scan = 0
                     break
                 else:
                     builder.append(remnant)
@@ -686,23 +686,18 @@
 
             line = self.decoded_chars
             start = self.decoded_chars_used
-
-            line_len = len(line)
             if limit > 0:
                 remaining = limit - builder.getlength()
                 assert remaining >= 0
             else:
                 remaining = sys.maxint
-            endpos, end_scan = self._find_line_ending(line, start, remaining)
-
-            if endpos >= 0:
+            end_scan, found = self._find_line_ending(line, start, remaining)
+            assert end_scan >= 0
+            if found:
                 break
 
-            assert end_scan >= 0
-            # We can put aside up to `end_scan`
             if limit >= 0 and end_scan - start >= remaining:
                 # Didn't find line ending, but reached length limit
-                endpos = end_scan
                 break
 
             # No line ending seen yet - put aside current data
@@ -720,9 +715,9 @@
 
         if line:
             # Our line ends in the current buffer
-            self.decoded_chars_used = endpos
-            if start > 0 or endpos < len(line):
-                line = line[start:endpos]
+            self.decoded_chars_used = end_scan
+            if start > 0 or end_scan < len(line):
+                line = line[start:end_scan]
             builder.append(line)
         elif remnant:
             builder.append(remnant)


More information about the pypy-commit mailing list