television_utils::strings

Function slice_at_char_boundaries

Source
pub fn slice_at_char_boundaries(
    s: &str,
    start_byte_index: usize,
    end_byte_index: usize,
) -> &str
Expand description

Returns a slice of the given string that starts and ends at character boundaries.

If the given start index is greater than the end index, or if either index is out of bounds, an empty string is returned.

ยงExamples

use television_utils::strings::slice_at_char_boundaries;

let s = "Hello, World!";
assert_eq!(slice_at_char_boundaries(s, 0, 0), "");
assert_eq!(slice_at_char_boundaries(s, 0, 1), "H");

let s = "๐Ÿ‘‹๐ŸŒ!";
assert_eq!(slice_at_char_boundaries(s, 0, 0), "");
assert_eq!(slice_at_char_boundaries(s, 0, 2), "๐Ÿ‘‹");
assert_eq!(slice_at_char_boundaries(s, 0, 5), "๐Ÿ‘‹๐ŸŒ");