Skip to main content

Module text

Module text 

Source
Expand description

UTF-8-safe string truncation helpers.

Error, log, and audit paths routinely truncate user-controlled query text for display. Slicing a &str at a fixed byte offset (&s[..100]) panics when that offset lands inside a multi-byte UTF-8 character — a remotely-triggerable abort if the truncated string is attacker-influenced (audit H20). These helpers truncate on character boundaries instead.

Functions§

truncate_at_char_boundary
Borrow the longest prefix of s that is at most max_bytes long and ends on a UTF-8 character boundary.
truncate_for_display
Render s for inclusion in an error/log message, truncated to max_bytes.