[pypy-svn] r12047 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 7 01:34:17 CEST 2005


Author: arigo
Date: Sat May  7 01:34:17 2005
New Revision: 12047

Modified:
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/test/test_stringobject.py
Log:
str.splitlines() fix.


Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Sat May  7 01:34:17 2005
@@ -679,25 +679,29 @@
  
  
 def str_splitlines__String_ANY(space, w_self, w_keepends):
-    u_self = w_self._value
+    data = w_self._value
     u_keepends  = space.int_w(w_keepends)  # truth value, but type checked
-    selflen = len(u_self)
+    selflen = len(data)
     
     L = []
-    pos = 0
-    while 1:
-        oldpos = pos
-        pos = _find(u_self, '\n', pos, selflen, 1) + 1
-        if pos  > oldpos:
-            w_item = space.wrap(u_self[oldpos:pos])
-            if not u_keepends:
-                w_item = _strip(space, w_item, W_StringObject(space,'\n'), left=0, right=1)
-            L.append(w_item)
-        else:
-            if oldpos < selflen:
-                w_item = space.wrap(u_self[oldpos:])
-                L.append(w_item)
-            break
+    i = j = 0
+    while i < selflen:
+        # Find a line and append it
+        while i < selflen and data[i] != '\n' and data[i] != '\r':
+            i += 1
+        # Skip the line break reading CRLF as one line break
+        eol = i
+        i += 1
+        if i < selflen and data[i-1] == '\r' and data[i] == '\n':
+            i += 1
+        if u_keepends:
+            eol = i
+        L.append(W_StringObject(space, data[j:eol]))
+        j = i
+
+    if j < selflen:
+        L.append(W_StringObject(space, data[j:]))
+
     return W_ListObject(space, L)
 
 def str_zfill__String_ANY(space, w_self, w_width):

Modified: pypy/dist/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_stringobject.py	Sat May  7 01:34:17 2005
@@ -351,6 +351,9 @@
         s="\none\n\two\nthree\n\n"
         assert s.splitlines() ==['', 'one', '\two', 'three', '']
         assert s.splitlines(1) ==['\n', 'one\n', '\two\n', 'three\n', '\n']
+        # Split on \r and \r\n too
+        assert '12\r34\r\n56'.splitlines() == ['12', '34', '56']
+        assert '12\r34\r\n56'.splitlines(1) == ['12\r', '34\r\n', '56']
     
     def test_find(self):
         assert 'abcdefghiabc'.find('abc') == 0



More information about the Pypy-commit mailing list