television_utils::strings

Function slice_up_to_char_boundary

Source
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๐ŸŒ");