Skip to main content

closest_valid_char_byte

Function closest_valid_char_byte 

Source
pub fn closest_valid_char_byte(string: &str, start_byte: usize) -> usize
Expand description

Finds the closest valid UTF-8 character boundary at or after the given byte position.

When working with byte indices in UTF-8 strings, you may land in the middle of a multi-byte character. This function finds the next valid character boundary.

§Arguments

  • string - The string to search in
  • start_byte - The byte index to start from

§Returns

Returns the byte index of the next valid character boundary (may be start_byte itself if it’s already at a boundary).

§Panics

Panics if start_byte is more than 3 bytes away from the next valid boundary, which should never happen with valid UTF-8 (max character size is 4 bytes).