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