Skip to content

Commit 1972bbb

Browse files
committed
fix str.splitlines
1 parent f570505 commit 1972bbb

File tree

1 file changed

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

1 file changed

+7
-8
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,21 +1163,20 @@ public PList split(String self, @SuppressWarnings("unused") PNone keepends) {
11631163
@Specialization
11641164
public PList split(String self, boolean keepends) {
11651165
PList list = factory().createList();
1166-
int end = self.length();
1167-
String remainder = self;
1166+
int lastEnd = 0;
11681167
while (true) {
1169-
int idx = remainder.lastIndexOf("\n");
1170-
if (idx < 0) {
1168+
int nextIndex = self.indexOf("\n", lastEnd);
1169+
if (nextIndex == -1) {
11711170
break;
11721171
}
11731172
if (keepends) {
1174-
appendNode.execute(list, self.substring(idx, end));
1173+
appendNode.execute(list, self.substring(lastEnd, nextIndex + 1));
11751174
} else {
1176-
appendNode.execute(list, self.substring(idx + 1, end));
1175+
appendNode.execute(list, self.substring(lastEnd, nextIndex));
11771176
}
1178-
end = idx;
1179-
remainder = remainder.substring(0, end);
1177+
lastEnd = nextIndex + 1;
11801178
}
1179+
String remainder = self.substring(lastEnd, self.length());
11811180
if (!remainder.isEmpty()) {
11821181
appendNode.execute(list, remainder);
11831182
}

0 commit comments

Comments
 (0)