@@ -31,13 +31,21 @@ impl Insertion {
31
31
original_ordering : None
32
32
}
33
33
}
34
+
35
+ pub fn len ( & self ) -> usize {
36
+ match & self . text {
37
+ InsertionText :: Static ( str) => str. len ( ) ,
38
+ InsertionText :: Dynamic ( str) => str. len ( )
39
+ }
40
+ }
34
41
}
35
42
36
43
struct InsertionList {
37
44
list : VecDeque < Insertion > ,
38
45
vars : Vec < String >
39
46
}
40
47
48
+ #[ allow( dead_code) ]
41
49
impl InsertionList {
42
50
pub const fn new ( ) -> InsertionList {
43
51
InsertionList {
@@ -472,24 +480,34 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
472
480
}
473
481
insertions. list . make_contiguous ( ) . sort_by ( |a, b| a. offset . cmp ( & b. offset ) ) ;
474
482
475
- let mut result = input. to_string ( ) ;
483
+ let mut previous_offset = 0 ;
484
+ let mut result = String :: with_capacity ( input. len ( ) + insertions. list . iter ( ) . map ( |s| s. len ( ) ) . sum :: < usize > ( ) ) ;
476
485
let mut debug_tag = "" . to_string ( ) ;
477
- for insertion in insertions. list . iter ( ) . rev ( ) {
486
+ for insertion in insertions. list . iter ( ) {
487
+ if usize:: from ( insertion. offset ) != previous_offset {
488
+ assert ! ( usize :: from( insertion. offset) >= previous_offset) ;
489
+ result. push_str ( & input[ previous_offset..insertion. offset . into ( ) ] ) ;
490
+ previous_offset = insertion. offset . into ( ) ;
491
+ }
492
+
478
493
let text;
479
494
match & insertion. text {
480
495
InsertionText :: Dynamic ( str) => { text = str. as_str ( ) ; }
481
496
InsertionText :: Static ( str) => { text = str; }
482
497
}
483
- let ( before , after ) = result . split_at ( insertion . offset . into ( ) ) ;
498
+
484
499
if with_debug_tags {
485
500
debug_tag = [
486
501
"/*i" , insertion. original_ordering . unwrap ( ) . to_string ( ) . as_str ( ) , "@" ,
487
502
u32:: from ( insertion. offset ) . to_string ( ) . as_str ( ) ,
488
503
if text. contains ( "/*" ) { "" } else { "*/" }
489
504
] . concat ( ) ;
490
505
}
491
- result = [ before, debug_tag. as_str ( ) , text, debug_tag. as_str ( ) , after] . concat ( ) ;
506
+ result. push_str ( debug_tag. as_str ( ) ) ;
507
+ result. push_str ( text) ;
508
+ result. push_str ( debug_tag. as_str ( ) ) ;
492
509
}
510
+ result. push_str ( & input[ previous_offset..] ) ;
493
511
494
512
result
495
513
}
0 commit comments