@@ -22,7 +22,7 @@ pub fn get_map<S: StreamChunks>(
22
22
stream : & S ,
23
23
options : & MapOptions ,
24
24
) -> Option < SourceMap > {
25
- let mut mappings = Vec :: with_capacity ( stream . mappings_size_hint ( ) ) ;
25
+ let mut mappings = Vec :: new ( ) ;
26
26
let mut sources: Vec < Cow < ' static , str > > = Vec :: new ( ) ;
27
27
let mut sources_content: Vec < Cow < ' static , str > > = Vec :: new ( ) ;
28
28
let mut names: Vec < Cow < ' static , str > > = Vec :: new ( ) ;
@@ -38,25 +38,22 @@ pub fn get_map<S: StreamChunks>(
38
38
// on_source
39
39
& mut |source_index, source : & str , source_content : Option < & str > | {
40
40
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 ( "" ) ) ;
44
43
}
45
44
sources[ source_index] = source. to_string ( ) . into ( ) ;
46
45
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 ( "" ) ) ;
50
48
}
51
49
sources_content[ source_index] = source_content. to_string ( ) . into ( ) ;
52
50
}
53
51
} ,
54
52
// on_name
55
53
& mut |name_index, name : & str | {
56
54
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 ( "" ) ) ;
60
57
}
61
58
names[ name_index] = name. to_string ( ) . into ( ) ;
62
59
} ,
@@ -68,11 +65,6 @@ pub fn get_map<S: StreamChunks>(
68
65
69
66
/// [StreamChunks] abstraction, see [webpack-sources source.streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
70
67
pub trait StreamChunks {
71
- /// Estimate the number of mappings in the chunk
72
- fn mappings_size_hint ( & self ) -> usize {
73
- 0
74
- }
75
-
76
68
/// [StreamChunks] abstraction
77
69
fn stream_chunks (
78
70
& self ,
@@ -132,34 +124,38 @@ pub struct GeneratedInfo {
132
124
pub fn decode_mappings < ' b , ' a : ' b > (
133
125
source_map : & ' a SourceMap ,
134
126
) -> 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 ( ) )
147
128
}
148
129
149
130
pub 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 ,
160
141
}
161
142
162
143
impl < ' 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
+
163
159
fn next_segment ( & mut self ) -> Option < & ' a str > {
164
160
if self . line . is_empty ( ) {
165
161
loop {
@@ -279,10 +275,9 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
279
275
let mut active_name = false ;
280
276
let mut initial = true ;
281
277
282
- let mut out = String :: new ( ) ;
283
278
mappings. iter ( ) . fold (
284
- String :: with_capacity ( mappings. len ( ) * 4 ) ,
285
- |acc, mapping| {
279
+ String :: with_capacity ( mappings. len ( ) * 6 ) ,
280
+ |mut acc, mapping| {
286
281
if active_mapping && current_line == mapping. generated_line {
287
282
// A mapping is still active
288
283
if mapping. original . is_some_and ( |original| {
@@ -303,38 +298,37 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
303
298
}
304
299
}
305
300
306
- out. clear ( ) ;
307
301
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 ( ';' ) ) ;
309
303
current_line = mapping. generated_line ;
310
304
current_column = 0 ;
311
305
initial = false ;
312
306
} else if initial {
313
307
initial = false ;
314
308
} else {
315
- out . push ( ',' ) ;
309
+ acc . push ( ',' ) ;
316
310
}
317
311
318
- encode ( & mut out , mapping. generated_column , current_column) ;
312
+ encode ( & mut acc , mapping. generated_column , current_column) ;
319
313
current_column = mapping. generated_column ;
320
314
if let Some ( original) = & mapping. original {
321
315
active_mapping = true ;
322
316
if original. source_index == current_source_index {
323
- out . push ( 'A' ) ;
317
+ acc . push ( 'A' ) ;
324
318
} else {
325
- encode ( & mut out , original. source_index , current_source_index) ;
319
+ encode ( & mut acc , original. source_index , current_source_index) ;
326
320
current_source_index = original. source_index ;
327
321
}
328
- encode ( & mut out , original. original_line , current_original_line) ;
322
+ encode ( & mut acc , original. original_line , current_original_line) ;
329
323
current_original_line = original. original_line ;
330
324
if original. original_column == current_original_column {
331
- out . push ( 'A' ) ;
325
+ acc . push ( 'A' ) ;
332
326
} else {
333
- encode ( & mut out , original. original_column , current_original_column) ;
327
+ encode ( & mut acc , original. original_column , current_original_column) ;
334
328
current_original_column = original. original_column ;
335
329
}
336
330
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) ;
338
332
current_name_index = name_index;
339
333
active_name = true ;
340
334
} else {
@@ -343,7 +337,7 @@ fn encode_full_mappings(mappings: &[Mapping]) -> String {
343
337
} else {
344
338
active_mapping = false ;
345
339
}
346
- acc + & out
340
+ acc
347
341
} ,
348
342
)
349
343
}
0 commit comments