Files
moli/moli-parser/src/input_prefix.rs
T
ldm0 18622bb9d7 refactor(parser): share UTF-8 input prefix splitting
HTML and streaming XML use identical owned-string chunk splitting. Share it inside the parser crate, keeping zero-budget handling, UTF-8 boundaries, the oversized first character case and optional remainder ownership.
2026-11-09 18:53:28 +08:00

21 lines
611 B
Rust

pub(crate) fn split_parser_input_prefix(
input: String,
max_bytes: usize,
) -> (String, Option<String>) {
if max_bytes == 0 || input.len() <= max_bytes {
return (input, None);
}
let mut split = input.len();
for (index, character) in input.char_indices() {
let end = index + character.len_utf8();
if end > max_bytes {
split = if index == 0 { end } else { index };
break;
}
}
let remainder = input[split..].to_owned();
let prefix = input[..split].to_owned();
(prefix, (!remainder.is_empty()).then_some(remainder))
}