Skip to content

Commit 177a092

Browse files
committed
u
1 parent bf0756a commit 177a092

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

crates/rolldown_utils/src/hash_placeholder.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ impl<'a> Iterator for HashPlaceholderIter<'a> {
5555
// Use byte-level slicing to avoid panics when `search_end` falls inside a
5656
// multi-byte UTF-8 character (e.g. Chinese/Japanese characters in the source).
5757
let search_end = (left_pos + MAX_HASH_SIZE + HASH_PLACEHOLDER_OVERHEAD).min(self.s.len());
58-
let search_bytes = &self.s.as_bytes()[left_pos..search_end];
59-
if let Some(right_pos) = memchr::memmem::find(search_bytes, HASH_PLACEHOLDER_RIGHT.as_bytes())
60-
{
61-
// `right_pos` points to the start of `}~` within `search_bytes`; advance
62-
// past those two ASCII bytes so the end index is always char-boundary-safe.
58+
if let Some(right_pos) = memchr::memmem::find(
59+
&self.s.as_bytes()[left_pos..search_end],
60+
HASH_PLACEHOLDER_RIGHT.as_bytes(),
61+
) {
6362
let right_pos = left_pos + right_pos + HASH_PLACEHOLDER_RIGHT.len();
6463
let placeholder = &self.s[left_pos..right_pos];
6564
self.start = right_pos;
@@ -253,25 +252,10 @@ fn test_find_hash_placeholders() {
253252
}
254253

255254
#[test]
256-
fn test_find_hash_placeholders_multibyte_chars() {
257-
// Regression test: Chinese/Japanese characters (multi-byte UTF-8) in the
258-
// surrounding source text must not cause a panic when `search_end` lands
259-
// inside a multi-byte character. The placeholder '!~{001}~' is still found
260-
// correctly.
255+
fn test_find_hash_placeholders_multi_byte_chars() {
256+
// Multi-byte UTF-8 chars near placeholders must not cause a panic.
261257
let s = "import{C as e}from\"./vue.runtime.esm-bundler-!~{001}~.js\";// 中文级别文字";
262258
let placeholders: Vec<_> = find_hash_placeholders(s, &HASH_PLACEHOLDER_LEFT_FINDER).collect();
263259
assert_eq!(placeholders.len(), 1);
264260
assert_eq!(placeholders[0].2, "!~{001}~");
265-
266-
// Test where the multi-byte character falls right at the search boundary
267-
// (26 bytes after `!~{`). Construct a string where a 3-byte Chinese char
268-
// straddles byte offset `left_pos + MAX_HASH_SIZE + OVERHEAD`.
269-
let prefix = "!~{001}~"; // placeholder itself
270-
let filler = "x".repeat(20); // padding so next `!~{` is far enough
271-
let chinese = "级"; // 3-byte UTF-8 char
272-
let s = format!("{prefix}{filler}!~{{002}}{chinese}");
273-
// No `}~` closing the second `!~{`, so only one placeholder should be found.
274-
let placeholders: Vec<_> = find_hash_placeholders(&s, &HASH_PLACEHOLDER_LEFT_FINDER).collect();
275-
assert_eq!(placeholders.len(), 1);
276-
assert_eq!(placeholders[0].2, "!~{001}~");
277261
}

0 commit comments

Comments
 (0)