Expand description
Cutting a &str at a byte offset without splitting a character.
Rust panics on &s[..n] when n lands inside a multi-byte character, and
this workspace slices strings at fixed byte budgets in a lot of places:
script-tool I/O caps, ACP frame chunking, region seed truncation, dashboard
column fitting, log previews. Every one of those had grown its own
while !s.is_char_boundary(end) { end -= 1 } loop with its own comment
explaining why - and two sites (lev test’s response preview and lev setup’s key redactor) never grew one at all and panicked on any emoji.
That failure has happened for real: a byte cut-off through a flag emoji
inside a Rhai host function double-panicked and aborted the whole daemon.
Keeping the walk-back in one tested place means a new
truncation site cannot forget it. The workspace also denies
clippy::string_slice, so reaching for a raw &s[..n] instead of these
helpers is a compile error unless the boundary is proven at the call site.
Functions§
- estimate_
tokens - The workspace’s one generic token estimate: bytes divided by four, rounded up.
- floor_
char_ boundary - Largest byte index
<= maxthat is a char boundary ins. - truncate_
at_ boundary &s[..max], backed off to the nearest char boundary at or beforemax.