[pypy-svn] pypy bytearray: (lac, mfoord) bytearray.strip / lstrip / rstrip work with memoryviews
mfoord
commits-noreply at bitbucket.org
Thu Jan 20 12:45:18 CET 2011
Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r40984:5dd9ea4ad8f1
Date: 2011-01-20 12:44 +0100
http://bitbucket.org/pypy/pypy/changeset/5dd9ea4ad8f1/
Log: (lac, mfoord) bytearray.strip / lstrip / rstrip work with
memoryviews
diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -3,6 +3,7 @@
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.inttype import wrapint
from pypy.objspace.std.multimethod import FailedToImplement
+from pypy.objspace.std.noneobject import W_NoneObject
from pypy.rlib.rarithmetic import intmask
from pypy.rlib.rstring import StringBuilder
from pypy.rlib.debug import check_annotation
@@ -18,7 +19,10 @@
from pypy.objspace.std import slicetype
from pypy.interpreter import gateway
from pypy.interpreter.buffer import RWBuffer
-from pypy.objspace.std.bytearraytype import makebytearraydata_w, getbytevalue
+from pypy.objspace.std.bytearraytype import (
+ makebytearraydata_w, getbytevalue,
+ new_bytearray
+)
from pypy.tool.sourcetools import func_with_new_name
@@ -297,6 +301,26 @@
w_bytearray.data.reverse()
return space.w_None
+_space_chars = ''.join([chr(c) for c in [9, 10, 11, 12, 13, 32]])
+
+def bytearray_strip__Bytearray_None(space, w_bytearray, w_chars):
+ return _strip(space, w_bytearray, _space_chars, 1, 1)
+
+def bytearray_strip__Bytearray_ANY(space, w_bytearray, w_chars):
+ return _strip(space, w_bytearray, space.bufferstr_w(w_chars), 1, 1)
+
+def bytearray_lstrip__Bytearray_None(space, w_bytearray, w_chars):
+ return _strip(space, w_bytearray, _space_chars, 1, 0)
+
+def bytearray_lstrip__Bytearray_ANY(space, w_bytearray, w_chars):
+ return _strip(space, w_bytearray, space.bufferstr_w(w_chars), 1, 0)
+
+def bytearray_rstrip__Bytearray_None(space, w_bytearray, w_chars):
+ return _strip(space, w_bytearray, _space_chars, 0, 1)
+
+def bytearray_rstrip__Bytearray_ANY(space, w_bytearray, w_chars):
+ return _strip(space, w_bytearray, space.bufferstr_w(w_chars), 0, 1)
+
# These methods could just delegate to the string implementation,
# but they have to return a bytearray.
def str_replace__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_str1, w_str2, w_max):
@@ -329,21 +353,6 @@
w_res = space.call_method(w_str, "capitalize")
return String2Bytearray(space, w_res)
-def str_lstrip__Bytearray_ANY(space, w_bytearray, w_chars):
- w_str = delegate_Bytearray2String(space, w_bytearray)
- w_res = space.call_method(w_str, "lstrip", w_chars)
- return String2Bytearray(space, w_res)
-
-def str_rstrip__Bytearray_ANY(space, w_bytearray, w_chars):
- w_str = delegate_Bytearray2String(space, w_bytearray)
- w_res = space.call_method(w_str, "rstrip", w_chars)
- return String2Bytearray(space, w_res)
-
-def str_strip__Bytearray_ANY(space, w_bytearray, w_chars):
- w_str = delegate_Bytearray2String(space, w_bytearray)
- w_res = space.call_method(w_str, "strip", w_chars)
- return String2Bytearray(space, w_res)
-
def str_ljust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
w_str = delegate_Bytearray2String(space, w_bytearray)
w_res = space.call_method(w_str, "ljust", w_width, w_fillchar)
@@ -462,6 +471,22 @@
setitem_slice_helper = func_with_new_name(_setitem_slice_helper,
'setitem_slice_helper')
+def _strip(space, w_bytearray, u_chars, left, right):
+ u_self = w_bytearray.data
+
+ lpos = 0
+ rpos = len(u_self)
+
+ if left:
+ while lpos < rpos and u_self[lpos] in u_chars:
+ lpos += 1
+
+ if right:
+ while rpos > lpos and u_self[rpos - 1] in u_chars:
+ rpos -= 1
+
+ return new_bytearray(space, space.w_bytearray, u_self[lpos:rpos])
+
# __________________________________________________________
# Buffer interface
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -541,11 +541,11 @@
if left:
#print "while %d < %d and -%s- in -%s-:"%(lpos, rpos, u_self[lpos],w_chars)
while lpos < rpos and u_self[lpos] in u_chars:
- lpos += 1
+ lpos += 1
if right:
while rpos > lpos and u_self[rpos - 1] in u_chars:
- rpos -= 1
+ rpos -= 1
assert rpos >= lpos # annotator hint, don't remove
return sliced(space, u_self, lpos, rpos, w_self)
diff --git a/pypy/objspace/std/test/test_bytes.py b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -78,6 +78,22 @@
assert c == bytearray('hee')
assert isinstance(c, bytearray)
+ def test_strip(self):
+ b = bytearray('mississippi ')
+
+ assert b.strip() == 'mississippi'
+ assert b.strip(None) == 'mississippi'
+
+ b = bytearray('mississippi')
+
+ for strip_type in str, memoryview:
+ assert b.strip(strip_type('i')) == 'mississipp'
+ assert b.strip(strip_type('m')) == 'ississippi'
+ assert b.strip(strip_type('pi')) == 'mississ'
+ assert b.strip(strip_type('im')) == 'ssissipp'
+ assert b.strip(strip_type('pim')) == 'ssiss'
+ assert b.strip(strip_type(b)) == ''
+
def test_iter(self):
assert list(bytearray('hello')) == [104, 101, 108, 108, 111]
@@ -148,8 +164,10 @@
check(bytearray('1\t2').expandtabs(5), '1 2')
check(bytearray(',').join(['a', bytearray('b')]), 'a,b')
- check(bytearray('abc').lstrip('a'), 'bc')
- check(bytearray('abc').rstrip('c'), 'ab')
+ check(bytearray('abca').lstrip('a'), 'bca')
+ check(bytearray('cabc').rstrip('c'), 'cab')
+ check(bytearray('abc').lstrip(memoryview('a')), 'bc')
+ check(bytearray('abc').rstrip(memoryview('c')), 'ab')
check(bytearray('aba').strip('a'), 'b')
def test_split(self):
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -11,8 +11,7 @@
str_startswith, str_endswith, str_islower, str_isupper, str_isalpha,
str_isalnum, str_isdigit, str_isspace, str_istitle,
str_upper, str_lower, str_title, str_swapcase, str_capitalize,
- str_expandtabs, str_lstrip, str_rstrip, str_strip,
- str_ljust, str_rjust, str_center, str_zfill,
+ str_expandtabs, str_ljust, str_rjust, str_center, str_zfill,
str_join, str_split, str_rsplit, str_partition, str_rpartition,
str_splitlines, str_translate)
from pypy.objspace.std.listtype import (
@@ -37,6 +36,21 @@
doc="B.reverse() -> None\n\n"
"Reverse the order of the values in B in place.")
+bytearray_strip = SMM('strip', 2, defaults=(None,),
+ doc="B.strip([bytes]) -> bytearray\n\nStrip leading "
+ "and trailing bytes contained in the argument.\nIf "
+ "the argument is omitted, strip ASCII whitespace.")
+
+bytearray_lstrip = SMM('lstrip', 2, defaults=(None,),
+ doc="B.lstrip([bytes]) -> bytearray\n\nStrip leading "
+ "bytes contained in the argument.\nIf the argument is "
+ "omitted, strip leading ASCII whitespace.")
+
+bytearray_rstrip = SMM('rstrip', 2, defaults=(None,),
+ doc="'B.rstrip([bytes]) -> bytearray\n\nStrip trailing "
+ "bytes contained in the argument.\nIf the argument is "
+ "omitted, strip trailing ASCII whitespace.")
+
def getbytevalue(space, w_value):
if space.isinstance_w(w_value, space.w_str):
string = space.str_w(w_value)
More information about the Pypy-commit
mailing list