60
60
import java .nio .charset .UnsupportedCharsetException ;
61
61
import java .util .Arrays ;
62
62
import java .util .List ;
63
+ import java .util .regex .Matcher ;
64
+ import java .util .regex .Pattern ;
63
65
64
66
import org .graalvm .nativeimage .ImageInfo ;
65
67
@@ -1215,6 +1217,8 @@ private PList rsplitfields(VirtualFrame frame, String s, int maxsplit, AppendNod
1215
1217
public abstract static class SplitLinesNode extends PythonBinaryBuiltinNode {
1216
1218
@ Child private AppendNode appendNode = AppendNode .create ();
1217
1219
1220
+ private static final Pattern LINEBREAK_PATTERN = Pattern .compile ("\\ R" );
1221
+
1218
1222
@ Specialization
1219
1223
PList doString (String self , @ SuppressWarnings ("unused" ) PNone keepends ) {
1220
1224
return doStringKeepends (self , false );
@@ -1224,17 +1228,15 @@ PList doString(String self, @SuppressWarnings("unused") PNone keepends) {
1224
1228
PList doStringKeepends (String self , boolean keepends ) {
1225
1229
PList list = factory ().createList ();
1226
1230
int lastEnd = 0 ;
1227
- while (true ) {
1228
- int nextIndex = PString .indexOf (self , "\n " , lastEnd );
1229
- if (nextIndex == -1 ) {
1230
- break ;
1231
- }
1231
+ Matcher matcher = getMatcher (self );
1232
+ while (matcherFind (matcher )) {
1233
+ int end = matcherEnd (matcher );
1232
1234
if (keepends ) {
1233
- appendNode .execute (list , PString .substring (self , lastEnd , nextIndex + 1 ));
1235
+ appendNode .execute (list , PString .substring (self , lastEnd , end ));
1234
1236
} else {
1235
- appendNode .execute (list , PString .substring (self , lastEnd , nextIndex ));
1237
+ appendNode .execute (list , PString .substring (self , lastEnd , matcherStart ( matcher ) ));
1236
1238
}
1237
- lastEnd = nextIndex + 1 ;
1239
+ lastEnd = end ;
1238
1240
}
1239
1241
String remainder = PString .substring (self , lastEnd );
1240
1242
if (!remainder .isEmpty ()) {
@@ -1243,6 +1245,26 @@ PList doStringKeepends(String self, boolean keepends) {
1243
1245
return list ;
1244
1246
}
1245
1247
1248
+ @ TruffleBoundary
1249
+ private static int matcherStart (Matcher matcher ) {
1250
+ return matcher .start ();
1251
+ }
1252
+
1253
+ @ TruffleBoundary
1254
+ private static int matcherEnd (Matcher matcher ) {
1255
+ return matcher .end ();
1256
+ }
1257
+
1258
+ @ TruffleBoundary
1259
+ private static boolean matcherFind (Matcher matcher ) {
1260
+ return matcher .find ();
1261
+ }
1262
+
1263
+ @ TruffleBoundary
1264
+ private static Matcher getMatcher (String self ) {
1265
+ return LINEBREAK_PATTERN .matcher (self );
1266
+ }
1267
+
1246
1268
@ Specialization (replaces = {"doString" , "doStringKeepends" })
1247
1269
PList doGeneric (Object self , Object keepends ,
1248
1270
@ Cached CastToJavaStringCheckedNode castSelfNode ,
0 commit comments