Skip to content

Commit 6e947aa

Browse files
committed
refactor(estree): use SliceIterExt in string serializer
1 parent c430288 commit 6e947aa

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

crates/oxc_estree/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ workspace = true
1919
doctest = false
2020

2121
[dependencies]
22-
oxc_data_structures = { workspace = true, features = ["code_buffer", "pointer_ext", "stack"], optional = true }
22+
oxc_data_structures = { workspace = true, features = ["code_buffer", "pointer_ext", "slice_iter_ext", "stack"], optional = true }
2323

2424
itoa = { workspace = true, optional = true }
2525
ryu-js = { workspace = true, optional = true }

crates/oxc_estree/src/serialize/strings.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{num::NonZeroU64, slice};
22

3-
use oxc_data_structures::{code_buffer::CodeBuffer, pointer_ext::PointerExt};
3+
use oxc_data_structures::{
4+
code_buffer::CodeBuffer, pointer_ext::PointerExt, slice_iter_ext::SliceIterExt,
5+
};
46

57
use super::{ESTree, Serializer};
68

@@ -279,13 +281,13 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
279281
escape = T::get_escape_for_byte(byte);
280282
// Consume bytes before this one.
281283
// SAFETY: `found_byte_index < 8` and there are at least 8 bytes remaining in `iter`
282-
unsafe { advance_unchecked(&mut iter, found_byte_index) };
284+
unsafe { iter.advance_unchecked(found_byte_index) };
283285
break 'inner;
284286
}
285287

286288
// Consume the whole batch.
287289
// SAFETY: There are at least `BATCH_SIZE` bytes remaining in `iter`.
288-
unsafe { advance_unchecked(&mut iter, 8) };
290+
unsafe { iter.advance_unchecked(8) };
289291

290292
// Go round `'inner` loop again to continue searching
291293
} else {
@@ -296,7 +298,7 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
296298
if escape != Escape::__ {
297299
// Consume bytes before this one.
298300
// SAFETY: `i` is an index of `iter`, so cannot be out of bounds.
299-
unsafe { advance_unchecked(&mut iter, i) };
301+
unsafe { iter.advance_unchecked(i) };
300302
break 'inner;
301303
}
302304
}
@@ -334,7 +336,7 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
334336

335337
// Consume the lossy replacement character.
336338
// SAFETY: Lossy replacement character is 3 bytes.
337-
unsafe { advance_unchecked(&mut iter, 3) };
339+
unsafe { iter.advance_unchecked(3) };
338340

339341
let hex = iter.as_slice().get(..4).unwrap();
340342
if hex == b"fffd" {
@@ -343,7 +345,7 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
343345

344346
// Consume `fffd`.
345347
// SAFETY: We know next 4 bytes are `fffd`.
346-
unsafe { advance_unchecked(&mut iter, 4) };
348+
unsafe { iter.advance_unchecked(4) };
347349

348350
// Set `chunk_start_ptr` to after `\u{FFFD}fffd`.
349351
// That's a complete UTF-8 sequence, so `chunk_start_ptr` is definitely
@@ -366,13 +368,13 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
366368
// if there weren't at least 4 bytes remaining in `iter`.
367369
// We haven't checked that the 4 following bytes are ASCII, but it doesn't matter
368370
// whether `iter` is left on a UTF-8 char boundary or not.
369-
unsafe { advance_unchecked(&mut iter, 4) }
371+
unsafe { iter.advance_unchecked(4) }
370372
}
371373
} else {
372374
// Some other unicode character starting with 0xEF.
373375
// Consume it and continue the loop.
374376
// SAFETY: `0xEF` is always 1st byte in a 3-byte UTF-8 character.
375-
unsafe { advance_unchecked(&mut iter, 3) };
377+
unsafe { iter.advance_unchecked(3) };
376378
}
377379
continue;
378380
}
@@ -393,7 +395,7 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
393395
write_char_escape(escape, byte, buffer);
394396

395397
// SAFETY: `'inner` loop above ensures `iter` is not at end of string
396-
unsafe { advance_unchecked(&mut iter, 1) };
398+
unsafe { iter.advance_unchecked(1) };
397399

398400
// Set `chunk_start_ptr` to be after this character.
399401
// `escape` is only non-zero for ASCII bytes, except `Escape::LO` which is handled above.
@@ -416,22 +418,6 @@ fn write_str<T: EscapeTable>(s: &str, buffer: &mut CodeBuffer) {
416418
buffer.print_ascii_byte(b'"');
417419
}
418420

419-
/// Advance bytes iterator by `count` bytes.
420-
///
421-
/// # SAFETY
422-
/// Caller must ensure there are at least `count` bytes remaining in `iter`.
423-
#[inline]
424-
unsafe fn advance_unchecked(iter: &mut slice::Iter<u8>, count: usize) {
425-
// Compiler boils this down to just adding `count` to `iter`'s pointer.
426-
// SAFETY: Caller guarantees there are at least `count` bytes remaining in `iter`.
427-
unsafe {
428-
let new_ptr = iter.as_slice().as_ptr().add(count);
429-
let new_len = iter.as_slice().len() - count;
430-
let slice = slice::from_raw_parts(new_ptr, new_len);
431-
*iter = slice.iter();
432-
};
433-
}
434-
435421
/// Write escape sequence to `buffer`.
436422
fn write_char_escape(escape: Escape, byte: u8, buffer: &mut CodeBuffer) {
437423
#[expect(clippy::if_not_else)]

0 commit comments

Comments
 (0)