Skip to content

Commit f9b8821

Browse files
committed
slice_unchecked
1 parent 346b38a commit f9b8821

File tree

4 files changed

+5
-17
lines changed

4 files changed

+5
-17
lines changed

crates/swc_ecma_fast_parser/src/lexer/cursor.rs

-12
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,6 @@ impl<'a> Cursor<'a> {
149149
unsafe { self.input.get_unchecked(self.pos as usize..) }
150150
}
151151

152-
/// Get a slice of the input
153-
#[inline(always)]
154-
pub fn slice(&self, start: u32, end: u32) -> &'a [u8] {
155-
let real_start = start.min(self.len);
156-
let real_end = end.min(self.len);
157-
// SAFETY: We've validated bounds
158-
unsafe {
159-
self.input
160-
.get_unchecked(real_start as usize..real_end as usize)
161-
}
162-
}
163-
164152
/// Get a slice of the input without bounds checking.
165153
///
166154
/// # Safety

crates/swc_ecma_fast_parser/src/lexer/jsx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Lexer<'_> {
125125
let end = self.cursor.position();
126126

127127
if end > start {
128-
let slice = self.cursor.slice(start, end);
128+
let slice = unsafe { self.cursor.slice_unchecked(start, end) };
129129
text.push_str(unsafe { std::str::from_utf8_unchecked(slice) });
130130
}
131131
}
@@ -149,7 +149,7 @@ impl Lexer<'_> {
149149

150150
// Extract the raw text
151151
let end_idx = self.cursor.position();
152-
let raw_bytes = self.cursor.slice(start_idx, end_idx);
152+
let raw_bytes = unsafe { self.cursor.slice_unchecked(start_idx, end_idx) };
153153
let raw_str = unsafe { std::str::from_utf8_unchecked(raw_bytes) };
154154

155155
let span = self.span();

crates/swc_ecma_fast_parser/src/lexer/operators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ impl Lexer<'_> {
624624
}
625625
let end = self.cursor.position();
626626
let shebang_str =
627-
unsafe { std::str::from_utf8_unchecked(self.cursor.slice(start, end)) };
627+
unsafe { std::str::from_utf8_unchecked(self.cursor.slice_unchecked(start, end)) };
628628

629629
return Ok(Token::new(
630630
TokenType::Shebang,

crates/swc_ecma_fast_parser/src/lexer/template.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl Lexer<'_> {
238238
// Add all these characters at once
239239
let end = self.cursor.position();
240240
if end > start {
241-
let slice = self.cursor.slice(start, end);
241+
let slice = unsafe { self.cursor.slice_unchecked(start, end) };
242242
value.push_str(unsafe { std::str::from_utf8_unchecked(slice) });
243243
}
244244
} else {
@@ -252,7 +252,7 @@ impl Lexer<'_> {
252252

253253
// Extract the raw template (including backticks)
254254
let end_idx = self.cursor.position();
255-
let raw_bytes = self.cursor.slice(start_idx, end_idx);
255+
let raw_bytes = unsafe { self.cursor.slice_unchecked(start_idx, end_idx) };
256256
let raw_str = unsafe { std::str::from_utf8_unchecked(raw_bytes) };
257257

258258
let span = self.span();

0 commit comments

Comments
 (0)