Function clamp_boundary

Source
pub fn clamp_boundary(text: &str, byte: usize) -> usize
Expand description

Clamps a byte index backward to a valid UTF-8 boundary in text.

Inputs beyond the byte length become text.len(). An input in the middle of a multibyte scalar walks backward to that scalar’s leading byte. Zero and all already valid boundaries are unchanged.

§Examples

use ailloli_ui_text::edit::clamp_boundary;
let text = "aé";
assert_eq!(clamp_boundary(text, 2), 1);
assert_eq!(clamp_boundary(text, usize::MAX), text.len());