Reject out-of-range and mid-codepoint chunks in GraphemeCursor (#193) Validate chunk bounds and UTF-8 codepoint boundaries across `GraphemeCursor` so out-of-range, mid-character, or empty-chunk calls return `GraphemeIncomplete::InvalidOffset` (or ignore empty pre-context) instead of panicking: - `next_boundary` / `prev_boundary`: return `InvalidOffset` when `chunk_start + chunk.len()` overflows, `offset` lies past `chunk_end`, or `offset - chunk_start` is not a char boundary in `chunk`. - `is_boundary`: reject `offset > len`, overflowing `chunk_start + chunk.len()`, and `offset < chunk_start` even when `cat_after` is already cached. - `provide_context`: ignore empty chunks and overflowing `chunk_start + chunk.len()` without clearing a pending `PreContext` request or unwrapping an empty iterator. Fixes #170
Iterators which split strings on Grapheme Cluster, Word, or Sentence boundaries, according to the Unicode Standard Annex #29 rules.
use unicode_segmentation::UnicodeSegmentation; fn main() { let s = "a̐éö̲\r\n"; let g = s.graphemes(true).collect::<Vec<&str>>(); let b: &[_] = &["a̐", "é", "ö̲", "\r\n"]; assert_eq!(g, b); let s = "The quick (\"brown\") fox can't jump 32.3 feet, right?"; let w = s.unicode_words().collect::<Vec<&str>>(); let b: &[_] = &["The", "quick", "brown", "fox", "can't", "jump", "32.3", "feet", "right"]; assert_eq!(w, b); let s = "The quick (\"brown\") fox"; let w = s.split_word_bounds().collect::<Vec<&str>>(); let b: &[_] = &["The", " ", "quick", " ", "(", "\"", "brown", "\"", ")", " ", "fox"]; assert_eq!(w, b); }
unicode-segmentation does not depend on libstd, so it can be used in crates with the #![no_std] attribute.
You can use this package in your project by adding the following to your Cargo.toml:
[dependencies] unicode-segmentation = "1"
Yanked due to accidental breakage and MSRV mistag.
#[inline] opportunities, resulting in 15-40% performance improvement.GraphemeCursor API allows random access and bidirectional iteration.as_str methods to the iterator types.