Skip to content

Commit f411b9c

Browse files
committed
fix str.splitlines
1 parent e480c07 commit f411b9c

File tree

1 file changed

+30
-7
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str

1 file changed

+30
-7
lines changed

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.oracle.graal.python.nodes.builtins.JoinInternalNode;
8282
import com.oracle.graal.python.nodes.call.CallDispatchNode;
8383
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
84+
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
8485
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8586
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8687
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -752,8 +753,6 @@ public String translate(String self, PDict table,
752753
}
753754

754755
protected abstract static class SpliceNode extends PNodeWithContext {
755-
private static final String MAX_CODE_POINT_HEX_RANGE = Integer.toHexString(Character.MAX_CODE_POINT + 1);
756-
757756
public static SpliceNode create() {
758757
return SpliceNodeGen.create();
759758
}
@@ -785,7 +784,7 @@ char[] doLong(char[] translatedChars, int i, long translated,
785784
}
786785

787786
private PException raiseError() {
788-
return raise(ValueError, "character mapping must be in range(0x%s)", MAX_CODE_POINT_HEX_RANGE);
787+
return raise(ValueError, "character mapping must be in range(0x%s)", Integer.toHexString(Character.MAX_CODE_POINT + 1));
789788
}
790789

791790
@Specialization
@@ -1153,12 +1152,36 @@ private PList rsplitfields(String s, int maxsplit) {
11531152
@GenerateNodeFactory
11541153
@TypeSystemReference(PythonArithmeticTypes.class)
11551154
public abstract static class SplitLinesNode extends PythonBuiltinNode {
1155+
@Child private ListAppendNode appendNode = ListAppendNode.create();
1156+
@Child private CastToBooleanNode keepEndsNode = CastToBooleanNode.createIfTrueNode();
11561157

11571158
@Specialization
1158-
@TruffleBoundary
1159-
public PList split(String str, @SuppressWarnings("unused") PNone keepends) {
1160-
String[] split = str.split("\n", -1); // -1 is needed to include trailing empty strings
1161-
return factory().createList(Arrays.copyOf(split, split.length, Object[].class));
1159+
public PList split(String self, @SuppressWarnings("unused") PNone keepends) {
1160+
return split(self, false);
1161+
}
1162+
1163+
@Specialization
1164+
public PList split(String self, boolean keepends) {
1165+
PList list = factory().createList();
1166+
int end = self.length();
1167+
String remainder = self;
1168+
while (true) {
1169+
int idx = remainder.lastIndexOf("\n");
1170+
if (idx < 0) {
1171+
break;
1172+
}
1173+
if (keepends) {
1174+
appendNode.execute(list, self.substring(idx, end));
1175+
} else {
1176+
appendNode.execute(list, self.substring(idx + 1, end));
1177+
}
1178+
end = idx;
1179+
remainder = remainder.substring(0, end);
1180+
}
1181+
if (!remainder.isEmpty()) {
1182+
appendNode.execute(list, remainder);
1183+
}
1184+
return list;
11621185
}
11631186
}
11641187

0 commit comments

Comments
 (0)