Skip to content

Commit 2da2430

Browse files
committed
refactor code
1 parent 4ffd6fb commit 2da2430

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

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

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

712-
@Builtin(name = "lstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
713-
@GenerateNodeFactory
714-
abstract static class LStripNode extends PythonBinaryBuiltinNode {
712+
abstract static class AStripNode extends PythonBinaryBuiltinNode {
713+
int mod() {
714+
throw new RuntimeException();
715+
}
716+
717+
int stop(@SuppressWarnings("unused") byte[] bs) {
718+
throw new RuntimeException();
719+
}
720+
721+
int start(@SuppressWarnings("unused") byte[] bs) {
722+
throw new RuntimeException();
723+
}
724+
725+
PByteArray newBytesFrom(@SuppressWarnings("unused") byte[] bs, @SuppressWarnings("unused") int i) {
726+
throw new RuntimeException();
727+
}
728+
715729
@Specialization
716730
PByteArray strip(PByteArray self, @SuppressWarnings("unused") PNone bytes,
717731
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
718732
byte[] bs = toBytesNode.execute(self);
719-
int i = 0;
720-
for (i = 0; i < bs.length; i++) {
733+
int i = start(bs);
734+
int stop = stop(bs);
735+
for (; i != stop; i += mod()) {
721736
if (!isWhitespace(bs[i])) {
722737
break;
723738
}
@@ -736,8 +751,9 @@ PByteArray strip(PByteArray self, PBytes bytes,
736751
@Cached("create()") BytesNodes.ToBytesNode otherToBytesNode) {
737752
byte[] stripBs = selfToBytesNode.execute(bytes);
738753
byte[] bs = otherToBytesNode.execute(self);
739-
int i = 0;
740-
outer: for (i = 0; i < bs.length; i++) {
754+
int i = start(bs);
755+
int stop = stop(bs);
756+
outer: for (; i != stop; i += mod()) {
741757
for (byte b : stripBs) {
742758
if (b == bs[i]) {
743759
continue outer;
@@ -748,7 +764,13 @@ PByteArray strip(PByteArray self, PBytes bytes,
748764
return newBytesFrom(bs, i);
749765
}
750766

751-
private PByteArray newBytesFrom(byte[] bs, int i) {
767+
}
768+
769+
@Builtin(name = "lstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
770+
@GenerateNodeFactory
771+
abstract static class LStripNode extends AStripNode {
772+
@Override
773+
PByteArray newBytesFrom(byte[] bs, int i) {
752774
byte[] out;
753775
if (i != 0) {
754776
int len = bs.length - i;
@@ -759,52 +781,30 @@ private PByteArray newBytesFrom(byte[] bs, int i) {
759781
}
760782
return factory().createByteArray(out);
761783
}
762-
}
763784

764-
@Builtin(name = "rstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
765-
@GenerateNodeFactory
766-
abstract static class RStripNode extends PythonBinaryBuiltinNode {
767-
@Specialization
768-
PByteArray strip(PByteArray self, @SuppressWarnings("unused") PNone bytes,
769-
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
770-
byte[] bs = toBytesNode.execute(self);
771-
int len = bs.length;
772-
for (int i = bs.length - 1; i >= 0; i--) {
773-
if (isWhitespace(bs[i])) {
774-
len--;
775-
} else {
776-
break;
777-
}
778-
}
779-
return newBytesUpTo(bs, len);
785+
@Override
786+
int mod() {
787+
return 1;
780788
}
781789

782-
@TruffleBoundary
783-
private static boolean isWhitespace(byte b) {
784-
return Character.isWhitespace(b);
790+
@Override
791+
int stop(byte[] bs) {
792+
return bs.length;
785793
}
786794

787-
@Specialization
788-
PByteArray strip(PByteArray self, PBytes bytes,
789-
@Cached("create()") BytesNodes.ToBytesNode selfToBytesNode,
790-
@Cached("create()") BytesNodes.ToBytesNode otherToBytesNode) {
791-
byte[] stripBs = selfToBytesNode.execute(bytes);
792-
byte[] bs = otherToBytesNode.execute(self);
793-
int len = bs.length;
794-
outer: for (int i = bs.length - 1; i >= 0; i--) {
795-
for (byte b : stripBs) {
796-
if (b == bs[i]) {
797-
len--;
798-
continue outer;
799-
}
800-
}
801-
break;
802-
}
803-
return newBytesUpTo(bs, len);
795+
@Override
796+
int start(byte[] bs) {
797+
return 0;
804798
}
799+
}
805800

806-
private PByteArray newBytesUpTo(byte[] bs, int len) {
801+
@Builtin(name = "rstrip", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, keywordArguments = {"bytes"})
802+
@GenerateNodeFactory
803+
abstract static class RStripNode extends AStripNode {
804+
@Override
805+
PByteArray newBytesFrom(byte[] bs, int i) {
807806
byte[] out;
807+
int len = i + 1;
808808
if (len != bs.length) {
809809
out = new byte[len];
810810
System.arraycopy(bs, 0, out, 0, len);
@@ -813,5 +813,20 @@ private PByteArray newBytesUpTo(byte[] bs, int len) {
813813
}
814814
return factory().createByteArray(out);
815815
}
816+
817+
@Override
818+
int mod() {
819+
return -1;
820+
}
821+
822+
@Override
823+
int stop(byte[] bs) {
824+
return -1;
825+
}
826+
827+
@Override
828+
int start(byte[] bs) {
829+
return bs.length - 1;
830+
}
816831
}
817832
}

0 commit comments

Comments
 (0)