Skip to main content

Module text

Module text 

Source
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 <= max that is a char boundary in s.
truncate_at_boundary
&s[..max], backed off to the nearest char boundary at or before max.