Skip to content

Commit 03cd692

Browse files
committed
Fix str.partition and str.rpartition
1 parent 797f64a commit 03cd692

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_unicode.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_lower
5454
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_mul
5555
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_none_arguments
56+
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_partition
5657
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_printable_repr
5758
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_printing
5859
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_replace_id
@@ -61,6 +62,8 @@
6162
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_rfind
6263
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_rindex
6364
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_rjust
65+
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_rpartition
66+
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_rsplit
6467
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_slice
6568
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_splitlines
6669
*graalpython.lib-python.3.test.test_unicode.UnicodeTest.test_startswith

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -920,36 +920,71 @@ private static String capitalize(String self) {
920920
}
921921
}
922922

923+
// str.partition
924+
@Builtin(name = "partition", minNumOfPositionalArgs = 2)
925+
@GenerateNodeFactory
926+
public abstract static class PartitionNode extends PythonBinaryBuiltinNode {
927+
928+
@Specialization
929+
Object doString(String self, String sep) {
930+
if (sep.isEmpty()) {
931+
throw raise(ValueError, ErrorMessages.EMPTY_SEPARATOR);
932+
}
933+
int indexOf = self.indexOf(sep);
934+
String[] partitioned = new String[3];
935+
if (indexOf == -1) {
936+
partitioned[0] = self;
937+
partitioned[1] = "";
938+
partitioned[2] = "";
939+
} else {
940+
partitioned[0] = PString.substring(self, 0, indexOf);
941+
partitioned[1] = sep;
942+
partitioned[2] = PString.substring(self, indexOf + sep.length());
943+
}
944+
return factory().createTuple(partitioned);
945+
}
946+
947+
@Specialization(replaces = "doString")
948+
Object doGeneric(Object self, Object sep,
949+
@Cached CastToJavaStringCheckedNode castSelfNode,
950+
@Cached CastToJavaStringCheckedNode castSepNode) {
951+
String selfStr = castSelfNode.cast(self, ErrorMessages.REQUIRES_STR_OBJECT_BUT_RECEIVED_P, "partition", self);
952+
String sepStr = castSepNode.cast(sep, ErrorMessages.MUST_BE_STR_NOT_P, sep);
953+
return doString(selfStr, sepStr);
954+
}
955+
}
956+
923957
// str.rpartition
924958
@Builtin(name = "rpartition", minNumOfPositionalArgs = 2)
925959
@GenerateNodeFactory
926960
public abstract static class RPartitionNode extends PythonBinaryBuiltinNode {
927961

928962
@Specialization
929-
PList doString(String self, String sep,
930-
@Shared("appendNode") @Cached AppendNode appendNode) {
963+
Object doString(String self, String sep) {
964+
if (sep.isEmpty()) {
965+
throw raise(ValueError, ErrorMessages.EMPTY_SEPARATOR);
966+
}
931967
int lastIndexOf = self.lastIndexOf(sep);
932-
PList list = factory().createList();
968+
String[] partitioned = new String[3];
933969
if (lastIndexOf == -1) {
934-
appendNode.execute(list, "");
935-
appendNode.execute(list, "");
936-
appendNode.execute(list, self);
970+
partitioned[0] = "";
971+
partitioned[1] = "";
972+
partitioned[2] = self;
937973
} else {
938-
appendNode.execute(list, PString.substring(self, 0, lastIndexOf));
939-
appendNode.execute(list, sep);
940-
appendNode.execute(list, PString.substring(self, lastIndexOf + sep.length()));
974+
partitioned[0] = PString.substring(self, 0, lastIndexOf);
975+
partitioned[1] = sep;
976+
partitioned[2] = PString.substring(self, lastIndexOf + sep.length());
941977
}
942-
return list;
978+
return factory().createTuple(partitioned);
943979
}
944980

945981
@Specialization(replaces = "doString")
946-
PList doGeneric(Object self, Object sep,
982+
Object doGeneric(Object self, Object sep,
947983
@Cached CastToJavaStringCheckedNode castSelfNode,
948-
@Cached CastToJavaStringCheckedNode castSepNode,
949-
@Shared("appendNode") @Cached AppendNode appendNode) {
984+
@Cached CastToJavaStringCheckedNode castSepNode) {
950985
String selfStr = castSelfNode.cast(self, ErrorMessages.REQUIRES_STR_OBJECT_BUT_RECEIVED_P, "rpartition", self);
951986
String sepStr = castSepNode.cast(sep, ErrorMessages.MUST_BE_STR_NOT_P, sep);
952-
return doString(selfStr, sepStr, appendNode);
987+
return doString(selfStr, sepStr);
953988
}
954989
}
955990

graalpython/lib-graalpython/str.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
import _codecs
2525

2626

27-
def partition(self, sep):
28-
l = self.split(sep, 1)
29-
if len(l) == 1:
30-
return l[0], "", ""
31-
else:
32-
return l[0], sep, l[1]
33-
34-
35-
str.partition = partition
36-
37-
3827
def expandtabs(self, tabsize=8):
3928
"""
4029
S.expandtabs(tabsize=8) -> str

0 commit comments

Comments
 (0)