|
81 | 81 | import com.oracle.graal.python.nodes.builtins.JoinInternalNode;
|
82 | 82 | import com.oracle.graal.python.nodes.call.CallDispatchNode;
|
83 | 83 | import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
|
| 84 | +import com.oracle.graal.python.nodes.expression.CastToBooleanNode; |
84 | 85 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
85 | 86 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
86 | 87 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
|
@@ -752,8 +753,6 @@ public String translate(String self, PDict table,
|
752 | 753 | }
|
753 | 754 |
|
754 | 755 | 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 |
| - |
757 | 756 | public static SpliceNode create() {
|
758 | 757 | return SpliceNodeGen.create();
|
759 | 758 | }
|
@@ -785,7 +784,7 @@ char[] doLong(char[] translatedChars, int i, long translated,
|
785 | 784 | }
|
786 | 785 |
|
787 | 786 | 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)); |
789 | 788 | }
|
790 | 789 |
|
791 | 790 | @Specialization
|
@@ -1153,12 +1152,36 @@ private PList rsplitfields(String s, int maxsplit) {
|
1153 | 1152 | @GenerateNodeFactory
|
1154 | 1153 | @TypeSystemReference(PythonArithmeticTypes.class)
|
1155 | 1154 | public abstract static class SplitLinesNode extends PythonBuiltinNode {
|
| 1155 | + @Child private ListAppendNode appendNode = ListAppendNode.create(); |
| 1156 | + @Child private CastToBooleanNode keepEndsNode = CastToBooleanNode.createIfTrueNode(); |
1156 | 1157 |
|
1157 | 1158 | @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; |
1162 | 1185 | }
|
1163 | 1186 | }
|
1164 | 1187 |
|
|
0 commit comments