Skip to main content

floor_char_boundary

Function floor_char_boundary 

Source
pub fn floor_char_boundary(src: &str, byte: usize) -> usize
Expand description

The largest char boundary at or below byte, clamped to src.len().

Every consumer of a Span that wants to slice the source needs this: a span’s byte offsets come from the lexer and are boundaries by construction, but a caller may have widened, clamped or defaulted one, and slicing a str off a boundary panics. Rounding down rather than panicking is the useful behaviour on both sides — a diagnostic covering one extra character beats no diagnostic at all.