Skip to main content

truncate_utf8_prefix

Function truncate_utf8_prefix 

Source
pub fn truncate_utf8_prefix(text: &str, max_bytes: usize) -> &str
Expand description

Borrow the longest prefix of text that is at most max_bytes bytes, rounded down to the nearest UTF-8 char boundary.

This is the canonical byte-bounded prefix used by provider previews, PTY capture limits, and bounded WebMCP prompts; do not fork the boundary loop.

assert_eq!(truncate_utf8_prefix("hello world", 5), "hello");
assert_eq!(truncate_utf8_prefix("hi", 10), "hi");
// Never splits a multi-byte char: 4 bytes lands mid-`日`, so only 2 survive.
assert_eq!(truncate_utf8_prefix("AB日", 4), "AB");