@@ -22,7 +22,7 @@ pub fn get_map<S: StreamChunks>(
2222 stream : & S ,
2323 options : & MapOptions ,
2424) -> Option < SourceMap > {
25- let mut mappings = Vec :: with_capacity ( stream . mappings_size_hint ( ) ) ;
25+ let mut mappings = Vec :: new ( ) ;
2626 let mut sources: Vec < Cow < ' static , str > > = Vec :: new ( ) ;
2727 let mut sources_content: Vec < Cow < ' static , str > > = Vec :: new ( ) ;
2828 let mut names: Vec < Cow < ' static , str > > = Vec :: new ( ) ;
@@ -38,25 +38,22 @@ pub fn get_map<S: StreamChunks>(
3838 // on_source
3939 & mut |source_index, source : & str , source_content : Option < & str > | {
4040 let source_index = source_index as usize ;
41- sources. reserve ( source_index - sources. len ( ) + 1 ) ;
42- while sources. len ( ) <= source_index {
43- sources. push ( "" . into ( ) ) ;
41+ if sources. len ( ) <= source_index {
42+ sources. resize ( source_index + 1 , Cow :: Borrowed ( "" ) ) ;
4443 }
4544 sources[ source_index] = source. to_string ( ) . into ( ) ;
4645 if let Some ( source_content) = source_content {
47- sources. reserve ( source_index - sources_content. len ( ) + 1 ) ;
48- while sources_content. len ( ) <= source_index {
49- sources_content. push ( "" . into ( ) ) ;
46+ if sources_content. len ( ) <= source_index {
47+ sources_content. resize ( source_index + 1 , Cow :: Borrowed ( "" ) ) ;
5048 }
5149 sources_content[ source_index] = source_content. to_string ( ) . into ( ) ;
5250 }
5351 } ,
5452 // on_name
5553 & mut |name_index, name : & str | {
5654 let name_index = name_index as usize ;
57- names. reserve ( name_index - names. len ( ) + 1 ) ;
58- while names. len ( ) <= name_index {
59- names. push ( "" . into ( ) ) ;
55+ if names. len ( ) <= name_index {
56+ names. resize ( name_index + 1 , Cow :: Borrowed ( "" ) ) ;
6057 }
6158 names[ name_index] = name. to_string ( ) . into ( ) ;
6259 } ,
@@ -68,11 +65,6 @@ pub fn get_map<S: StreamChunks>(
6865
6966/// [StreamChunks] abstraction, see [webpack-sources source.streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
7067pub trait StreamChunks {
71- /// Estimate the number of mappings in the chunk
72- fn mappings_size_hint ( & self ) -> usize {
73- 0
74- }
75-
7668 /// [StreamChunks] abstraction
7769 fn stream_chunks (
7870 & self ,
@@ -132,34 +124,38 @@ pub struct GeneratedInfo {
132124pub fn decode_mappings < ' b , ' a : ' b > (
133125 source_map : & ' a SourceMap ,
134126) -> impl Iterator < Item = Mapping > + ' b {
135- SegmentIter {
136- line : "" ,
137- mapping_str : source_map. mappings ( ) ,
138- source_index : 0 ,
139- original_line : 1 ,
140- original_column : 0 ,
141- name_index : 0 ,
142- generated_line : 0 ,
143- segment_cursor : 0 ,
144- generated_column : 0 ,
145- nums : Vec :: with_capacity ( 6 ) ,
146- }
127+ SegmentIter :: new ( source_map. mappings ( ) )
147128}
148129
149130pub struct SegmentIter < ' a > {
150- pub mapping_str : & ' a str ,
151- pub generated_line : usize ,
152- pub generated_column : u32 ,
153- pub source_index : u32 ,
154- pub original_line : u32 ,
155- pub original_column : u32 ,
156- pub name_index : u32 ,
157- pub line : & ' a str ,
158- pub nums : Vec < i64 > ,
159- pub segment_cursor : usize ,
131+ mapping_str : & ' a str ,
132+ generated_line : usize ,
133+ generated_column : u32 ,
134+ source_index : u32 ,
135+ original_line : u32 ,
136+ original_column : u32 ,
137+ name_index : u32 ,
138+ line : & ' a str ,
139+ nums : Vec < i64 > ,
140+ segment_cursor : usize ,
160141}
161142
162143impl < ' a > SegmentIter < ' a > {
144+ pub fn new ( mapping_str : & ' a str ) -> Self {
145+ SegmentIter {
146+ line : "" ,
147+ mapping_str,
148+ source_index : 0 ,
149+ original_line : 1 ,
150+ original_column : 0 ,
151+ name_index : 0 ,
152+ generated_line : 0 ,
153+ segment_cursor : 0 ,
154+ generated_column : 0 ,
155+ nums : Vec :: with_capacity ( 5 ) ,
156+ }
157+ }
158+
163159 fn next_segment ( & mut self ) -> Option < & ' a str > {
164160 if self . line . is_empty ( ) {
165161 loop {
@@ -279,10 +275,9 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
279275 let mut active_name = false ;
280276 let mut initial = true ;
281277
282- let mut out = String :: new ( ) ;
283278 mappings. iter ( ) . fold (
284- String :: with_capacity ( mappings. len ( ) * 4 ) ,
285- |acc, mapping| {
279+ String :: with_capacity ( mappings. len ( ) * 6 ) ,
280+ |mut acc, mapping| {
286281 if active_mapping && current_line == mapping. generated_line {
287282 // A mapping is still active
288283 if mapping. original . is_some_and ( |original| {
@@ -303,38 +298,37 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
303298 }
304299 }
305300
306- out. clear ( ) ;
307301 if current_line < mapping. generated_line {
308- ( 0 ..mapping. generated_line - current_line) . for_each ( |_| out . push ( ';' ) ) ;
302+ ( 0 ..mapping. generated_line - current_line) . for_each ( |_| acc . push ( ';' ) ) ;
309303 current_line = mapping. generated_line ;
310304 current_column = 0 ;
311305 initial = false ;
312306 } else if initial {
313307 initial = false ;
314308 } else {
315- out . push ( ',' ) ;
309+ acc . push ( ',' ) ;
316310 }
317311
318- encode ( & mut out , mapping. generated_column , current_column) ;
312+ encode ( & mut acc , mapping. generated_column , current_column) ;
319313 current_column = mapping. generated_column ;
320314 if let Some ( original) = & mapping. original {
321315 active_mapping = true ;
322316 if original. source_index == current_source_index {
323- out . push ( 'A' ) ;
317+ acc . push ( 'A' ) ;
324318 } else {
325- encode ( & mut out , original. source_index , current_source_index) ;
319+ encode ( & mut acc , original. source_index , current_source_index) ;
326320 current_source_index = original. source_index ;
327321 }
328- encode ( & mut out , original. original_line , current_original_line) ;
322+ encode ( & mut acc , original. original_line , current_original_line) ;
329323 current_original_line = original. original_line ;
330324 if original. original_column == current_original_column {
331- out . push ( 'A' ) ;
325+ acc . push ( 'A' ) ;
332326 } else {
333- encode ( & mut out , original. original_column , current_original_column) ;
327+ encode ( & mut acc , original. original_column , current_original_column) ;
334328 current_original_column = original. original_column ;
335329 }
336330 if let Some ( name_index) = original. name_index {
337- encode ( & mut out , name_index, current_name_index) ;
331+ encode ( & mut acc , name_index, current_name_index) ;
338332 current_name_index = name_index;
339333 active_name = true ;
340334 } else {
@@ -343,7 +337,7 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
343337 } else {
344338 active_mapping = false ;
345339 }
346- acc + & out
340+ acc
347341 } ,
348342 )
349343}
0 commit comments