mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 08:02:59 +00:00
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.
21 lines
611 B
Rust
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))
|
|
}
|