pub fn slice_up_to_char_boundary(s: &str, byte_index: usize) -> &str
Expand description
Returns a slice of the given string that starts at the beginning and ends at a character boundary.
If the given index is out of bounds, the whole string is returned. If the given index is already a character boundary, the string up to that index is returned.
ยงExamples
use television_utils::strings::slice_up_to_char_boundary;
let s = "Hello, World!";
assert_eq!(slice_up_to_char_boundary(s, 0), "");
assert_eq!(slice_up_to_char_boundary(s, 1), "H");
assert_eq!(slice_up_to_char_boundary(s, 13), "Hello, World!");
let s = "๐\n๐!";
assert_eq!(slice_up_to_char_boundary(s, 0), "");
assert_eq!(slice_up_to_char_boundary(s, 1), "๐");
assert_eq!(slice_up_to_char_boundary(s, 4), "๐");
assert_eq!(slice_up_to_char_boundary(s, 7), "๐\n๐");