1
- use arrayvec:: ArrayVec ;
2
1
use std:: {
3
2
borrow:: { BorrowMut , Cow } ,
4
3
cell:: RefCell ,
5
4
rc:: Rc ,
6
5
} ;
7
6
7
+ use memchr:: Memchr2 ;
8
8
use rustc_hash:: FxHashMap as HashMap ;
9
9
10
10
use crate :: {
@@ -13,7 +13,7 @@ use crate::{
13
13
source:: { Mapping , OriginalLocation } ,
14
14
vlq:: decode,
15
15
with_indices:: WithIndices ,
16
- MapOptions , SourceMap ,
16
+ Error , MapOptions , SourceMap ,
17
17
} ;
18
18
19
19
// Adding this type because sourceContentLine not happy
@@ -123,81 +123,78 @@ pub fn decode_mappings(
123
123
124
124
pub struct SegmentIter < ' a > {
125
125
mapping_str : & ' a [ u8 ] ,
126
+ mapping_iter : Memchr2 < ' a > ,
126
127
generated_line : usize ,
127
128
generated_column : u32 ,
128
129
source_index : u32 ,
129
130
original_line : u32 ,
130
131
original_column : u32 ,
131
132
name_index : u32 ,
132
- nums : ArrayVec < i64 , 5 > ,
133
133
tracing_index : usize ,
134
- current_index : usize ,
134
+ tracing_newline : bool ,
135
135
}
136
136
137
137
impl < ' a > SegmentIter < ' a > {
138
138
pub fn new ( mapping_str : & ' a str ) -> Self {
139
+ let mapping_str = mapping_str. as_bytes ( ) ;
140
+ let mapping_iter = memchr:: memchr2_iter ( b',' , b';' , mapping_str) ;
139
141
SegmentIter {
140
- mapping_str : mapping_str. as_bytes ( ) ,
142
+ mapping_str,
143
+ mapping_iter,
141
144
source_index : 0 ,
142
145
original_line : 1 ,
143
146
original_column : 0 ,
144
147
name_index : 0 ,
145
148
generated_line : 1 ,
146
149
generated_column : 0 ,
147
- nums : ArrayVec :: new ( ) ,
148
150
tracing_index : 0 ,
149
- current_index : 0 ,
151
+ tracing_newline : false ,
150
152
}
151
153
}
152
154
153
155
fn next_segment ( & mut self ) -> Option < & ' a [ u8 ] > {
154
156
let mapping_str_len = self . mapping_str . len ( ) ;
155
- if self . current_index == mapping_str_len {
156
- return None ;
157
- }
158
157
159
158
loop {
160
- match self . mapping_str [ self . current_index ] {
161
- b',' => {
162
- if self . tracing_index != self . current_index {
163
- let segment =
164
- & self . mapping_str [ self . tracing_index ..self . current_index ] ;
165
- self . tracing_index = self . current_index ;
166
- return Some ( segment) ;
159
+ if self . tracing_newline {
160
+ self . generated_line += 1 ;
161
+ self . generated_column = 0 ;
162
+ self . tracing_newline = false ;
163
+ }
164
+
165
+ match self . mapping_iter . next ( ) {
166
+ Some ( index) => match self . mapping_str [ index] {
167
+ b',' => {
168
+ if self . tracing_index != index {
169
+ let segment = & self . mapping_str [ self . tracing_index ..index] ;
170
+ self . tracing_index = index + 1 ;
171
+ return Some ( segment) ;
172
+ }
173
+ self . tracing_index = index + 1 ;
167
174
}
168
- self . current_index += 1 ;
169
- self . tracing_index = self . current_index ;
170
- }
171
- b';' => {
172
- if self . tracing_index != self . current_index {
175
+ b';' => {
176
+ self . tracing_newline = true ;
177
+ if self . tracing_index != index {
178
+ let segment = & self . mapping_str [ self . tracing_index ..index] ;
179
+ self . tracing_index = index + 1 ;
180
+ return Some ( segment) ;
181
+ }
182
+ self . tracing_index = index + 1 ;
183
+ }
184
+ _ => unreachable ! ( ) ,
185
+ } ,
186
+ None => {
187
+ if self . tracing_index != mapping_str_len {
173
188
let segment =
174
- & self . mapping_str [ self . tracing_index ..self . current_index ] ;
175
- self . tracing_index = self . current_index ;
189
+ & self . mapping_str [ self . tracing_index ..mapping_str_len ] ;
190
+ self . tracing_index = mapping_str_len ;
176
191
return Some ( segment) ;
177
192
}
178
- self . generated_line += 1 ;
179
- self . generated_column = 0 ;
180
- self . current_index += 1 ;
181
- self . tracing_index = self . current_index ;
182
193
}
183
- _ => match memchr:: memchr2 (
184
- b',' ,
185
- b';' ,
186
- & self . mapping_str [ self . current_index ..] ,
187
- ) {
188
- Some ( index) => self . current_index += index,
189
- None => self . current_index = mapping_str_len,
190
- } ,
191
194
}
192
195
193
- if self . current_index == mapping_str_len {
194
- if self . tracing_index != self . current_index {
195
- let segment =
196
- & self . mapping_str [ self . tracing_index ..self . current_index ] ;
197
- return Some ( segment) ;
198
- } else {
199
- return None ;
200
- }
196
+ if self . tracing_index == mapping_str_len {
197
+ return None ;
201
198
}
202
199
}
203
200
}
@@ -209,30 +206,38 @@ impl<'a> Iterator for SegmentIter<'a> {
209
206
fn next ( & mut self ) -> Option < Self :: Item > {
210
207
match self . next_segment ( ) {
211
208
Some ( segment) => {
212
- self . nums . clear ( ) ;
213
- decode ( segment, & mut self . nums ) . unwrap ( ) ;
214
- self . generated_column =
215
- ( i64:: from ( self . generated_column ) + self . nums [ 0 ] ) as u32 ;
209
+ let mut vlq = decode ( segment) ;
210
+ self . generated_column = ( i64:: from ( self . generated_column )
211
+ + vlq
212
+ . next ( )
213
+ . unwrap_or_else ( || Err ( Error :: VlqNoValues ) )
214
+ . unwrap ( ) ) as u32 ;
216
215
217
216
let mut src = None ;
218
217
let mut name = None ;
219
218
220
- if self . nums . len ( ) > 1 {
221
- if self . nums . len ( ) != 4 && self . nums . len ( ) != 5 {
222
- panic ! ( "got {} segments, expected 4 or 5" , self . nums. len( ) ) ;
223
- }
219
+ if let Some ( source_index ) = vlq . next ( ) {
220
+ // if self.nums.len() != 4 && self.nums.len() != 5 {
221
+ // panic!("got {} segments, expected 4 or 5", self.nums.len());
222
+ // }
224
223
self . source_index =
225
- ( i64:: from ( self . source_index ) + self . nums [ 1 ] ) as u32 ;
224
+ ( i64:: from ( self . source_index ) + source_index . unwrap ( ) ) as u32 ;
226
225
src = Some ( self . source_index ) ;
227
- self . original_line =
228
- ( i64:: from ( self . original_line ) + self . nums [ 2 ] ) as u32 ;
229
- self . original_column =
230
- ( i64:: from ( self . original_column ) + self . nums [ 3 ] ) as u32 ;
231
-
232
- if self . nums . len ( ) > 4 {
226
+ self . original_line = ( i64:: from ( self . original_line )
227
+ + vlq
228
+ . next ( )
229
+ . unwrap_or_else ( || Err ( Error :: VlqNoValues ) )
230
+ . unwrap ( ) ) as u32 ;
231
+ self . original_column = ( i64:: from ( self . original_column )
232
+ + vlq
233
+ . next ( )
234
+ . unwrap_or_else ( || Err ( Error :: VlqNoValues ) )
235
+ . unwrap ( ) ) as u32 ;
236
+
237
+ if let Some ( name_index) = vlq. next ( ) {
233
238
self . name_index =
234
- ( i64:: from ( self . name_index ) + self . nums [ 4 ] ) as u32 ;
235
- name = Some ( self . name_index ) ;
239
+ ( i64:: from ( self . name_index ) + name_index . unwrap ( ) ) as u32 ;
240
+ name = Some ( self . name_index )
236
241
}
237
242
}
238
243
0 commit comments