pub trait RoundCharBoundaryExt: Sealed {
// Required methods
fn floor_char_boundary_ib(&self, index: usize) -> usize;
fn ceil_char_boundary_ib(&self, index: usize) -> usize;
}Expand description
Polyfill for unstable #![feature(round_char_boundary)]
Required Methods§
Sourcefn floor_char_boundary_ib(&self, index: usize) -> usize
fn floor_char_boundary_ib(&self, index: usize) -> usize
Finds the closest x not exceeding index where is_char_boundary(x) is true.
This method can help you truncate a string so that it’s still valid UTF-8, but doesn’t exceed a given number of bytes. Note that this is done purely at the character level and can still visually split graphemes, even though the underlying characters aren’t split. For example, the emoji 🧑🔬 (scientist) could be split so that the string only includes 🧑 (person) instead.
§Examples
use ib_unicode::str::RoundCharBoundaryExt;
let s = "❤️🧡💛💚💙💜";
assert_eq!(s.len(), 26);
assert!(!s.is_char_boundary(13));
let closest = s.floor_char_boundary_ib(13);
assert_eq!(closest, 10);
assert_eq!(&s[..closest], "❤️🧡");Sourcefn ceil_char_boundary_ib(&self, index: usize) -> usize
fn ceil_char_boundary_ib(&self, index: usize) -> usize
Finds the closest x not below index where is_char_boundary(x) is true.
If index is greater than the length of the string, this returns the length of the string.
This method is the natural complement to floor_char_boundary. See that method
for more details.
§Examples
use ib_unicode::str::RoundCharBoundaryExt;
let s = "❤️🧡💛💚💙💜";
assert_eq!(s.len(), 26);
assert!(!s.is_char_boundary(13));
let closest = s.ceil_char_boundary_ib(13);
assert_eq!(closest, 14);
assert_eq!(&s[..closest], "❤️🧡💛");