Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(common): Simplify StringInput implementation #9071

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions crates/swc_common/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub type SourceFileInput<'a> = StringInput<'a>;
/// Implementation of [Input].
#[derive(Clone)]
pub struct StringInput<'a> {
start_pos_of_iter: BytePos,
last_pos: BytePos,
/// Current cursor
iter: str::CharIndices<'a>,
Expand All @@ -31,7 +30,6 @@ impl<'a> StringInput<'a> {
assert!(start <= end);

StringInput {
start_pos_of_iter: start,
last_pos: start,
orig: src,
iter: src.char_indices(),
Expand Down Expand Up @@ -82,8 +80,8 @@ impl<'a> Input for StringInput<'a> {

#[inline]
unsafe fn bump(&mut self) {
if let Some((i, c)) = self.iter.next() {
self.last_pos = self.start_pos_of_iter + BytePos((i + c.len_utf8()) as u32);
if let Some((_, c)) = self.iter.next() {
self.last_pos = self.last_pos + BytePos((c.len_utf8()) as u32);
} else {
unsafe {
debug_unreachable!("bump should not be called when cur() == None");
Expand Down Expand Up @@ -131,7 +129,6 @@ impl<'a> Input for StringInput<'a> {

self.iter = unsafe { s.get_unchecked(end_idx..) }.char_indices();
self.last_pos = end;
self.start_pos_of_iter = end;

ret
}
Expand All @@ -155,7 +152,6 @@ impl<'a> Input for StringInput<'a> {
let ret = unsafe { s.get_unchecked(..last) };

self.last_pos = self.last_pos + BytePos(last as _);
self.start_pos_of_iter = self.last_pos;
self.iter = unsafe { s.get_unchecked(last..) }.char_indices();

ret
Expand All @@ -181,7 +177,6 @@ impl<'a> Input for StringInput<'a> {
debug_assert!(last <= s.len());

self.last_pos = self.last_pos + BytePos(last as _);
self.start_pos_of_iter = self.last_pos;
self.iter = unsafe { s.get_unchecked(last..) }.char_indices();

Some(self.last_pos)
Expand All @@ -195,7 +190,6 @@ impl<'a> Input for StringInput<'a> {
debug_assert!(idx <= orig.len());
let s = unsafe { orig.get_unchecked(idx..) };
self.iter = s.char_indices();
self.start_pos_of_iter = to;
self.last_pos = to;
}

Expand All @@ -217,15 +211,8 @@ impl<'a> Input for StringInput<'a> {
#[inline]
fn eat_byte(&mut self, c: u8) -> bool {
if self.is_byte(c) {
if let Some((i, _)) = self.iter.next() {
self.last_pos = self.start_pos_of_iter + BytePos((i + 1) as u32);
} else {
unsafe {
debug_unreachable!(
"We can't enter here as we already checked the state using `is_byte`"
)
}
}
self.iter.next();
self.last_pos = self.last_pos + BytePos(1_u32);
true
} else {
false
Expand Down Expand Up @@ -340,13 +327,11 @@ mod tests {
with_test_sess("foo/d", |mut i| {
assert_eq!(unsafe { i.slice(BytePos(1), BytePos(2)) }, "f");
assert_eq!(i.last_pos, BytePos(2));
assert_eq!(i.start_pos_of_iter, BytePos(2));
assert_eq!(i.cur(), Some('o'));

assert_eq!(unsafe { i.slice(BytePos(2), BytePos(4)) }, "oo");
assert_eq!(unsafe { i.slice(BytePos(1), BytePos(4)) }, "foo");
assert_eq!(i.last_pos, BytePos(4));
assert_eq!(i.start_pos_of_iter, BytePos(4));
assert_eq!(i.cur(), Some('/'));
});
}
Expand All @@ -356,13 +341,11 @@ mod tests {
with_test_sess("load", |mut i| {
assert_eq!(unsafe { i.slice(BytePos(1), BytePos(3)) }, "lo");
assert_eq!(i.last_pos, BytePos(3));
assert_eq!(i.start_pos_of_iter, BytePos(3));
assert_eq!(i.cur(), Some('a'));
unsafe { i.reset_to(BytePos(1)) };

assert_eq!(i.cur(), Some('l'));
assert_eq!(i.last_pos, BytePos(1));
assert_eq!(i.start_pos_of_iter, BytePos(1));
});
}

Expand All @@ -371,12 +354,10 @@ mod tests {
with_test_sess("foo/d", |mut i| {
assert_eq!(i.cur_pos(), BytePos(1));
assert_eq!(i.last_pos, BytePos(1));
assert_eq!(i.start_pos_of_iter, BytePos(1));
assert_eq!(i.uncons_while(|c| c.is_alphabetic()), "foo");

// assert_eq!(i.cur_pos(), BytePos(4));
assert_eq!(i.last_pos, BytePos(4));
assert_eq!(i.start_pos_of_iter, BytePos(4));
assert_eq!(i.cur(), Some('/'));

unsafe {
Expand All @@ -398,10 +379,8 @@ mod tests {
with_test_sess("foo/d", |mut i| {
assert_eq!(i.cur_pos(), BytePos(1));
assert_eq!(i.last_pos, BytePos(1));
assert_eq!(i.start_pos_of_iter, BytePos(1));

assert_eq!(i.find(|c| c == '/'), Some(BytePos(5)));
assert_eq!(i.start_pos_of_iter, BytePos(5));
assert_eq!(i.last_pos, BytePos(5));
assert_eq!(i.cur(), Some('d'));
});
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
};

mod comments_buffer;
#[deprecated = "Directly use swc_common::input instead"]
pub mod input;
mod jsx;
mod number;
Expand Down
Loading