@@ -31,13 +31,21 @@ impl Insertion {
3131 original_ordering : None
3232 }
3333 }
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+ }
3441}
3542
3643struct InsertionList {
3744 list : VecDeque < Insertion > ,
3845 vars : Vec < String >
3946}
4047
48+ #[ allow( dead_code) ]
4149impl InsertionList {
4250 pub const fn new ( ) -> InsertionList {
4351 InsertionList {
@@ -472,24 +480,34 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
472480 }
473481 insertions. list . make_contiguous ( ) . sort_by ( |a, b| a. offset . cmp ( & b. offset ) ) ;
474482
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 > ( ) ) ;
476485 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+
478493 let text;
479494 match & insertion. text {
480495 InsertionText :: Dynamic ( str) => { text = str. as_str ( ) ; }
481496 InsertionText :: Static ( str) => { text = str; }
482497 }
483- let ( before , after ) = result . split_at ( insertion . offset . into ( ) ) ;
498+
484499 if with_debug_tags {
485500 debug_tag = [
486501 "/*i" , insertion. original_ordering . unwrap ( ) . to_string ( ) . as_str ( ) , "@" ,
487502 u32:: from ( insertion. offset ) . to_string ( ) . as_str ( ) ,
488503 if text. contains ( "/*" ) { "" } else { "*/" }
489504 ] . concat ( ) ;
490505 }
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 ( ) ) ;
492509 }
510+ result. push_str ( & input[ previous_offset..] ) ;
493511
494512 result
495513}
0 commit comments