@@ -22,13 +22,10 @@ const {
22
22
NumberIsNaN,
23
23
ObjectSetPrototypeOf,
24
24
RegExpPrototypeExec,
25
- RegExpPrototypeSymbolReplace,
26
- RegExpPrototypeSymbolSplit,
27
25
StringPrototypeCodePointAt,
28
26
StringPrototypeEndsWith,
29
27
StringPrototypeRepeat,
30
28
StringPrototypeSlice,
31
- StringPrototypeSplit,
32
29
StringPrototypeStartsWith,
33
30
StringPrototypeTrim,
34
31
Symbol,
@@ -75,7 +72,7 @@ const kHistorySize = 30;
75
72
const kMaxUndoRedoStackSize = 2048 ;
76
73
const kMincrlfDelay = 100 ;
77
74
// \r\n, \n, or \r followed by something other than \n
78
- const lineEnding = / \r ? \n | \r (? ! \n ) / ;
75
+ const lineEnding = / \r ? \n | \r (? ! \n ) / g ;
79
76
80
77
const kLineObjectStream = Symbol ( 'line object stream' ) ;
81
78
const kQuestionCancel = Symbol ( 'kQuestionCancel' ) ;
@@ -589,31 +586,40 @@ class Interface extends InterfaceConstructor {
589
586
this [ kSawReturnAt ] &&
590
587
DateNow ( ) - this [ kSawReturnAt ] <= this . crlfDelay
591
588
) {
592
- string = RegExpPrototypeSymbolReplace ( / ^ \n / , string , '' ) ;
589
+ if ( StringPrototypeCodePointAt ( string ) === 10 ) string = StringPrototypeSlice ( string , 1 ) ;
593
590
this [ kSawReturnAt ] = 0 ;
594
591
}
595
592
596
593
// Run test() on the new string chunk, not on the entire line buffer.
597
- const newPartContainsEnding = RegExpPrototypeExec ( lineEnding , string ) !== null ;
598
-
599
- if ( this [ kLine_buffer ] ) {
600
- string = this [ kLine_buffer ] + string ;
601
- this [ kLine_buffer ] = null ;
602
- }
603
- if ( newPartContainsEnding ) {
594
+ let newPartContainsEnding = RegExpPrototypeExec ( lineEnding , string ) ;
595
+ if ( newPartContainsEnding !== null ) {
596
+ if ( this [ kLine_buffer ] ) {
597
+ string = this [ kLine_buffer ] + string ;
598
+ this [ kLine_buffer ] = null ;
599
+ newPartContainsEnding = RegExpPrototypeExec ( lineEnding , string ) ;
600
+ }
604
601
this [ kSawReturnAt ] = StringPrototypeEndsWith ( string , '\r' ) ?
605
602
DateNow ( ) :
606
603
0 ;
607
604
608
- // Got one or more newlines; process into "line" events
609
- const lines = StringPrototypeSplit ( string , lineEnding ) ;
605
+ const indexes = [ 0 , newPartContainsEnding . index , lineEnding . lastIndex ] ;
606
+ let nextMatch ;
607
+ while ( ( nextMatch = RegExpPrototypeExec ( lineEnding , string ) ) !== null ) {
608
+ ArrayPrototypePush ( indexes , nextMatch . index , lineEnding . lastIndex ) ;
609
+ }
610
+ const lastIndex = indexes . length - 1 ;
610
611
// Either '' or (conceivably) the unfinished portion of the next line
611
- string = ArrayPrototypePop ( lines ) ;
612
- this [ kLine_buffer ] = string ;
613
- for ( let n = 0 ; n < lines . length ; n ++ ) this [ kOnLine ] ( lines [ n ] ) ;
612
+ this [ kLine_buffer ] = StringPrototypeSlice ( string , indexes [ lastIndex ] ) ;
613
+ for ( let i = 1 ; i < lastIndex ; i += 2 ) {
614
+ this [ kOnLine ] ( StringPrototypeSlice ( string , indexes [ i - 1 ] , indexes [ i ] ) ) ;
615
+ }
614
616
} else if ( string ) {
615
617
// No newlines this time, save what we have for next time
616
- this [ kLine_buffer ] = string ;
618
+ if ( this [ kLine_buffer ] ) {
619
+ this [ kLine_buffer ] += string ;
620
+ } else {
621
+ this [ kLine_buffer ] = string ;
622
+ }
617
623
}
618
624
}
619
625
@@ -1321,12 +1327,18 @@ class Interface extends InterfaceConstructor {
1321
1327
// falls through
1322
1328
default :
1323
1329
if ( typeof s === 'string' && s ) {
1324
- const lines = RegExpPrototypeSymbolSplit ( / \r \n | \n | \r / , s ) ;
1325
- for ( let i = 0 , len = lines . length ; i < len ; i ++ ) {
1326
- if ( i > 0 ) {
1330
+ let nextMatch = RegExpPrototypeExec ( lineEnding , s ) ;
1331
+ if ( nextMatch !== null ) {
1332
+ this [ kInsertString ] ( StringPrototypeSlice ( s , 0 , nextMatch . index ) ) ;
1333
+ let { lastIndex } = lineEnding ;
1334
+ while ( ( nextMatch = RegExpPrototypeExec ( lineEnding , s ) ) !== null ) {
1327
1335
this [ kLine ] ( ) ;
1336
+ this [ kInsertString ] ( StringPrototypeSlice ( s , lastIndex , nextMatch . index ) ) ;
1337
+ ( { lastIndex } = lineEnding ) ;
1328
1338
}
1329
- this [ kInsertString ] ( lines [ i ] ) ;
1339
+ if ( lastIndex === s . length ) this [ kLine ] ( ) ;
1340
+ } else {
1341
+ this [ kInsertString ] ( s ) ;
1330
1342
}
1331
1343
}
1332
1344
}
0 commit comments