Skip to content

Commit 51507bc

Browse files
authored
perf(swc_common): Remove char_indices calls (#10541)
1 parent d76fbc0 commit 51507bc

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

.changeset/silly-humans-design.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_common: patch
4+
---
5+
6+
perf(swc/common): rm char_indices

crates/swc_common/src/input.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,19 @@ impl<'a> Input<'a> for StringInput<'a> {
155155
where
156156
F: FnMut(char) -> bool,
157157
{
158-
let s = self.iter.as_str();
159-
let mut last = 0;
160-
161-
for (i, c) in s.char_indices() {
162-
if pred(c) {
163-
last = i + c.len_utf8();
164-
} else {
165-
break;
158+
let last = {
159+
let mut last = 0;
160+
for c in self.iter.clone() {
161+
if pred(c) {
162+
last += c.len_utf8();
163+
} else {
164+
break;
165+
}
166166
}
167-
}
167+
last
168+
};
169+
170+
let s = self.iter.as_str();
168171
debug_assert!(last <= s.len());
169172
let ret = unsafe { s.get_unchecked(..last) };
170173

@@ -178,19 +181,22 @@ impl<'a> Input<'a> for StringInput<'a> {
178181
where
179182
F: FnMut(char) -> bool,
180183
{
181-
let s = self.iter.as_str();
182-
let mut last = 0;
183-
184-
for (i, c) in s.char_indices() {
185-
if pred(c) {
186-
last = i + c.len_utf8();
187-
break;
184+
let last = {
185+
let mut last = 0;
186+
for c in self.iter.clone() {
187+
last += c.len_utf8();
188+
if pred(c) {
189+
break;
190+
}
188191
}
189-
}
192+
last
193+
};
194+
190195
if last == 0 {
191196
return None;
192197
}
193198

199+
let s = self.iter.as_str();
194200
debug_assert!(last <= s.len());
195201

196202
self.last_pos = self.last_pos + BytePos(last as _);

0 commit comments

Comments
 (0)