Skip to content
Closed
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
22 changes: 17 additions & 5 deletions crates/perry-runtime/src/string/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ pub extern "C" fn js_string_split_n(
let (src_all_ascii, src_has_lone_surrogates) = unsafe {
let bytes = slice::from_raw_parts(string_data(s), (*s).byte_len as usize);
(
bytes.iter().all(|&b| b < 0x80),
// `is_ascii` is the same predicate as `all(|b| b < 0x80)`, but std
// tests a word at a time. The byte-at-a-time form was ~74% of this
// function's own time splitting a 211-byte JWT on "." (#10519).
bytes.is_ascii(),
(*s).flags & STRING_FLAG_HAS_LONE_SURROGATES != 0,
)
};
Expand Down Expand Up @@ -488,10 +491,19 @@ pub extern "C" fn js_string_split_n(
// stale address is the #5062 class. Offsets stay valid across a move; the
// source address is re-read from a rooted handle on every iteration.
let src_base = str_data.as_ptr() as usize;
let mut part_ranges: Vec<(usize, usize)> = str_data
.split(delim)
.map(|part| (part.as_ptr() as usize - src_base, part.len()))
.collect();
let range = |part: &str| (part.as_ptr() as usize - src_base, part.len());
// A one-byte delimiter is ASCII (WTF-8 spells every non-ASCII unit, lone
// surrogates included, in two or more bytes), so a `char` pattern names
// exactly the same occurrences as the `&str` one. It takes std's
// memchr-based searcher instead of the two-way `StrSearcher`, whose
// per-call setup is paid in full for a single-character needle -- the
// common `split(".")`, `split(",")`, `split(" ")` shape (#10519).
let mut part_ranges: Vec<(usize, usize)> = match delim.as_bytes() {
// `byte < 0x80` is not implied: storage can hold malformed bytes, and
// `0x80 as char` is U+0080, which is two bytes wide.
&[byte] if byte < 0x80 => str_data.split(byte as char).map(range).collect(),
_ => str_data.split(delim).map(range).collect(),
};
if limit > 0 && (part_ranges.len() as i64) > (limit as i64) {
part_ranges.truncate(limit as usize);
}
Expand Down
Loading