Skip to content

Commit 4ffd6fb

Browse files
committed
add bytearray.{l,r,}strip and tests
1 parent ac589a3 commit 4ffd6fb

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,9 @@ def test_binary_op():
523523
assert b"123" <= b"1234"
524524
assert b"123" >= b"123"
525525
assert not (b"123" >= b"1234")
526+
527+
528+
def test_strip_bytearray():
529+
assert bytearray(b'abc').strip(b'ac') == b'b'
530+
assert bytearray(b'abc').lstrip(b'ac') == b'bc'
531+
assert bytearray(b'abc').rstrip(b'ac') == b'ab'

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/ByteArrayBuiltins.java

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,17 +709,68 @@ Object doGeneric(@SuppressWarnings("unused") Object self) {
709709
}
710710
}
711711

712-
@Builtin(name = "rstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
712+
@Builtin(name = "lstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
713713
@GenerateNodeFactory
714-
abstract static class RStripNode extends PythonBuiltinNode {
714+
abstract static class LStripNode extends PythonBinaryBuiltinNode {
715715
@Specialization
716+
PByteArray strip(PByteArray self, @SuppressWarnings("unused") PNone bytes,
717+
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
718+
byte[] bs = toBytesNode.execute(self);
719+
int i = 0;
720+
for (i = 0; i < bs.length; i++) {
721+
if (!isWhitespace(bs[i])) {
722+
break;
723+
}
724+
}
725+
return newBytesFrom(bs, i);
726+
}
727+
716728
@TruffleBoundary
729+
private static boolean isWhitespace(byte b) {
730+
return Character.isWhitespace(b);
731+
}
732+
733+
@Specialization
734+
PByteArray strip(PByteArray self, PBytes bytes,
735+
@Cached("create()") BytesNodes.ToBytesNode selfToBytesNode,
736+
@Cached("create()") BytesNodes.ToBytesNode otherToBytesNode) {
737+
byte[] stripBs = selfToBytesNode.execute(bytes);
738+
byte[] bs = otherToBytesNode.execute(self);
739+
int i = 0;
740+
outer: for (i = 0; i < bs.length; i++) {
741+
for (byte b : stripBs) {
742+
if (b == bs[i]) {
743+
continue outer;
744+
}
745+
}
746+
break;
747+
}
748+
return newBytesFrom(bs, i);
749+
}
750+
751+
private PByteArray newBytesFrom(byte[] bs, int i) {
752+
byte[] out;
753+
if (i != 0) {
754+
int len = bs.length - i;
755+
out = new byte[len];
756+
System.arraycopy(bs, i, out, 0, len);
757+
} else {
758+
out = bs;
759+
}
760+
return factory().createByteArray(out);
761+
}
762+
}
763+
764+
@Builtin(name = "rstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
765+
@GenerateNodeFactory
766+
abstract static class RStripNode extends PythonBinaryBuiltinNode {
767+
@Specialization
717768
PByteArray strip(PByteArray self, @SuppressWarnings("unused") PNone bytes,
718769
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
719770
byte[] bs = toBytesNode.execute(self);
720771
int len = bs.length;
721772
for (int i = bs.length - 1; i >= 0; i--) {
722-
if (Character.isWhitespace(bs[i])) {
773+
if (isWhitespace(bs[i])) {
723774
len--;
724775
} else {
725776
break;
@@ -728,8 +779,12 @@ PByteArray strip(PByteArray self, @SuppressWarnings("unused") PNone bytes,
728779
return newBytesUpTo(bs, len);
729780
}
730781

731-
@Specialization
732782
@TruffleBoundary
783+
private static boolean isWhitespace(byte b) {
784+
return Character.isWhitespace(b);
785+
}
786+
787+
@Specialization
733788
PByteArray strip(PByteArray self, PBytes bytes,
734789
@Cached("create()") BytesNodes.ToBytesNode selfToBytesNode,
735790
@Cached("create()") BytesNodes.ToBytesNode otherToBytesNode) {
@@ -740,10 +795,10 @@ PByteArray strip(PByteArray self, PBytes bytes,
740795
for (byte b : stripBs) {
741796
if (b == bs[i]) {
742797
len--;
743-
} else {
744-
break outer;
798+
continue outer;
745799
}
746800
}
801+
break;
747802
}
748803
return newBytesUpTo(bs, len);
749804
}
@@ -759,5 +814,4 @@ private PByteArray newBytesUpTo(byte[] bs, int len) {
759814
return factory().createByteArray(out);
760815
}
761816
}
762-
763817
}

graalpython/lib-graalpython/bytearray.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@
4040
# an empty file for now
4141

4242
bytearray.decode = bytes.decode
43+
44+
45+
def strip(self, what=None):
46+
return self.lstrip(what).rstrip(what)
47+
48+
49+
bytearray.strip = strip

0 commit comments

Comments
 (0)