unicode_segmentation/lib.rs
1// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Iterators which split strings on Grapheme Cluster, Word or Sentence boundaries, according
12//! to the [Unicode Standard Annex #29](http://www.unicode.org/reports/tr29/) rules.
13//!
14//! ```rust
15//! extern crate unicode_segmentation;
16//!
17//! use unicode_segmentation::UnicodeSegmentation;
18//!
19//! fn main() {
20//! let s = "a̐éö̲\r\n";
21//! let g = UnicodeSegmentation::graphemes(s, true).collect::<Vec<&str>>();
22//! let b: &[_] = &["a̐", "é", "ö̲", "\r\n"];
23//! assert_eq!(g, b);
24//!
25//! let s = "The quick (\"brown\") fox can't jump 32.3 feet, right?";
26//! let w = s.unicode_words().collect::<Vec<&str>>();
27//! let b: &[_] = &["The", "quick", "brown", "fox", "can't", "jump", "32.3", "feet", "right"];
28//! assert_eq!(w, b);
29//!
30//! let s = "The quick (\"brown\") fox";
31//! let w = s.split_word_bounds().collect::<Vec<&str>>();
32//! let b: &[_] = &["The", " ", "quick", " ", "(", "\"", "brown", "\"", ")", " ", "fox"];
33//! assert_eq!(w, b);
34//! }
35//! ```
36//!
37//! # no_std
38//!
39//! unicode-segmentation does not depend on libstd, so it can be used in crates
40//! with the `#![no_std]` attribute.
41//!
42//! # crates.io
43//!
44//! You can use this package in your project by adding the following
45//! to your `Cargo.toml`:
46//!
47//! ```toml
48//! [dependencies]
49//! unicode-segmentation = "1.9.0"
50//! ```
51
52#![deny(missing_docs, unsafe_code)]
53#![doc(
54 html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
55 html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png"
56)]
57#![no_std]
58
59#[cfg(test)]
60extern crate std;
61
62pub use grapheme::{GraphemeCursor, GraphemeIncomplete};
63pub use grapheme::{GraphemeIndices, Graphemes};
64pub use sentence::{USentenceBoundIndices, USentenceBounds, UnicodeSentences};
65pub use tables::UNICODE_VERSION;
66pub use word::{UWordBoundIndices, UWordBounds, UnicodeWordIndices, UnicodeWords};
67
68mod grapheme;
69mod sentence;
70#[rustfmt::skip]
71mod tables;
72mod word;
73
74/// Methods for segmenting strings according to
75/// [Unicode Standard Annex #29](http://www.unicode.org/reports/tr29/).
76pub trait UnicodeSegmentation {
77 /// Returns an iterator over the [grapheme clusters][graphemes] of `self`.
78 ///
79 /// [graphemes]: http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries
80 ///
81 /// If `is_extended` is true, the iterator is over the
82 /// *extended grapheme clusters*;
83 /// otherwise, the iterator is over the *legacy grapheme clusters*.
84 /// [UAX#29](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries)
85 /// recommends extended grapheme cluster boundaries for general processing.
86 ///
87 /// # Examples
88 ///
89 /// ```
90 /// # use self::unicode_segmentation::UnicodeSegmentation;
91 /// let gr1 = UnicodeSegmentation::graphemes("a\u{310}e\u{301}o\u{308}\u{332}", true)
92 /// .collect::<Vec<&str>>();
93 /// let b: &[_] = &["a\u{310}", "e\u{301}", "o\u{308}\u{332}"];
94 ///
95 /// assert_eq!(&gr1[..], b);
96 ///
97 /// let gr2 = UnicodeSegmentation::graphemes("a\r\nb🇷🇺🇸🇹", true).collect::<Vec<&str>>();
98 /// let b: &[_] = &["a", "\r\n", "b", "🇷🇺", "🇸🇹"];
99 ///
100 /// assert_eq!(&gr2[..], b);
101 /// ```
102 fn graphemes(&self, is_extended: bool) -> Graphemes<'_>;
103
104 /// Returns an iterator over the grapheme clusters of `self` and their
105 /// byte offsets. See `graphemes()` for more information.
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// # use self::unicode_segmentation::UnicodeSegmentation;
111 /// let gr_inds = UnicodeSegmentation::grapheme_indices("a̐éö̲\r\n", true)
112 /// .collect::<Vec<(usize, &str)>>();
113 /// let b: &[_] = &[(0, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")];
114 ///
115 /// assert_eq!(&gr_inds[..], b);
116 /// ```
117 fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices<'_>;
118
119 /// Returns an iterator over the words of `self`, separated on
120 /// [UAX#29 word boundaries](http://www.unicode.org/reports/tr29/#Word_Boundaries).
121 ///
122 /// Here, "words" are just those substrings which, after splitting on
123 /// UAX#29 word boundaries, contain any alphanumeric characters. That is, the
124 /// substring must contain at least one character with the
125 /// [Alphabetic](http://unicode.org/reports/tr44/#Alphabetic)
126 /// property, or with
127 /// [General_Category=Number](http://unicode.org/reports/tr44/#General_Category_Values).
128 ///
129 /// # Example
130 ///
131 /// ```
132 /// # use self::unicode_segmentation::UnicodeSegmentation;
133 /// let uws = "The quick (\"brown\") fox can't jump 32.3 feet, right?";
134 /// let uw1 = uws.unicode_words().collect::<Vec<&str>>();
135 /// let b: &[_] = &["The", "quick", "brown", "fox", "can't", "jump", "32.3", "feet", "right"];
136 ///
137 /// assert_eq!(&uw1[..], b);
138 /// ```
139 fn unicode_words(&self) -> UnicodeWords<'_>;
140
141 /// Returns an iterator over the words of `self`, separated on
142 /// [UAX#29 word boundaries](http://www.unicode.org/reports/tr29/#Word_Boundaries), and their
143 /// offsets.
144 ///
145 /// Here, "words" are just those substrings which, after splitting on
146 /// UAX#29 word boundaries, contain any alphanumeric characters. That is, the
147 /// substring must contain at least one character with the
148 /// [Alphabetic](http://unicode.org/reports/tr44/#Alphabetic)
149 /// property, or with
150 /// [General_Category=Number](http://unicode.org/reports/tr44/#General_Category_Values).
151 ///
152 /// # Example
153 ///
154 /// ```
155 /// # use self::unicode_segmentation::UnicodeSegmentation;
156 /// let uwis = "The quick (\"brown\") fox can't jump 32.3 feet, right?";
157 /// let uwi1 = uwis.unicode_word_indices().collect::<Vec<(usize, &str)>>();
158 /// let b: &[_] = &[(0, "The"), (4, "quick"), (12, "brown"), (20, "fox"), (24, "can't"),
159 /// (30, "jump"), (35, "32.3"), (40, "feet"), (46, "right")];
160 ///
161 /// assert_eq!(&uwi1[..], b);
162 /// ```
163 fn unicode_word_indices(&self) -> UnicodeWordIndices<'_>;
164
165 /// Returns an iterator over substrings of `self` separated on
166 /// [UAX#29 word boundaries](http://www.unicode.org/reports/tr29/#Word_Boundaries).
167 ///
168 /// The concatenation of the substrings returned by this function is just the original string.
169 ///
170 /// # Example
171 ///
172 /// ```
173 /// # use self::unicode_segmentation::UnicodeSegmentation;
174 /// let swu1 = "The quick (\"brown\") fox".split_word_bounds().collect::<Vec<&str>>();
175 /// let b: &[_] = &["The", " ", "quick", " ", "(", "\"", "brown", "\"", ")", " ", "fox"];
176 ///
177 /// assert_eq!(&swu1[..], b);
178 /// ```
179 fn split_word_bounds(&self) -> UWordBounds<'_>;
180
181 /// Returns an iterator over substrings of `self`, split on UAX#29 word boundaries,
182 /// and their offsets. See `split_word_bounds()` for more information.
183 ///
184 /// # Example
185 ///
186 /// ```
187 /// # use self::unicode_segmentation::UnicodeSegmentation;
188 /// let swi1 = "Brr, it's 29.3°F!".split_word_bound_indices().collect::<Vec<(usize, &str)>>();
189 /// let b: &[_] = &[(0, "Brr"), (3, ","), (4, " "), (5, "it's"), (9, " "), (10, "29.3"),
190 /// (14, "°"), (16, "F"), (17, "!")];
191 ///
192 /// assert_eq!(&swi1[..], b);
193 /// ```
194 fn split_word_bound_indices(&self) -> UWordBoundIndices<'_>;
195
196 /// Returns an iterator over substrings of `self` separated on
197 /// [UAX#29 sentence boundaries](http://www.unicode.org/reports/tr29/#Sentence_Boundaries).
198 ///
199 /// Here, "sentences" are just those substrings which, after splitting on
200 /// UAX#29 sentence boundaries, contain any alphanumeric characters. That is, the
201 /// substring must contain at least one character with the
202 /// [Alphabetic](http://unicode.org/reports/tr44/#Alphabetic)
203 /// property, or with
204 /// [General_Category=Number](http://unicode.org/reports/tr44/#General_Category_Values).
205 ///
206 /// # Example
207 ///
208 /// ```
209 /// # use self::unicode_segmentation::UnicodeSegmentation;
210 /// let uss = "Mr. Fox jumped. [...] The dog was too lazy.";
211 /// let us1 = uss.unicode_sentences().collect::<Vec<&str>>();
212 /// let b: &[_] = &["Mr. ", "Fox jumped. ", "The dog was too lazy."];
213 ///
214 /// assert_eq!(&us1[..], b);
215 /// ```
216 fn unicode_sentences(&self) -> UnicodeSentences<'_>;
217
218 /// Returns an iterator over substrings of `self` separated on
219 /// [UAX#29 sentence boundaries](http://www.unicode.org/reports/tr29/#Sentence_Boundaries).
220 ///
221 /// The concatenation of the substrings returned by this function is just the original string.
222 ///
223 /// # Example
224 ///
225 /// ```
226 /// # use self::unicode_segmentation::UnicodeSegmentation;
227 /// let ssbs = "Mr. Fox jumped. [...] The dog was too lazy.";
228 /// let ssb1 = ssbs.split_sentence_bounds().collect::<Vec<&str>>();
229 /// let b: &[_] = &["Mr. ", "Fox jumped. ", "[...] ", "The dog was too lazy."];
230 ///
231 /// assert_eq!(&ssb1[..], b);
232 /// ```
233 fn split_sentence_bounds(&self) -> USentenceBounds<'_>;
234
235 /// Returns an iterator over substrings of `self`, split on UAX#29 sentence boundaries,
236 /// and their offsets. See `split_sentence_bounds()` for more information.
237 ///
238 /// # Example
239 ///
240 /// ```
241 /// # use self::unicode_segmentation::UnicodeSegmentation;
242 /// let ssis = "Mr. Fox jumped. [...] The dog was too lazy.";
243 /// let ssi1 = ssis.split_sentence_bound_indices().collect::<Vec<(usize, &str)>>();
244 /// let b: &[_] = &[(0, "Mr. "), (4, "Fox jumped. "), (16, "[...] "),
245 /// (22, "The dog was too lazy.")];
246 ///
247 /// assert_eq!(&ssi1[..], b);
248 /// ```
249 fn split_sentence_bound_indices(&self) -> USentenceBoundIndices<'_>;
250}
251
252impl UnicodeSegmentation for str {
253 #[inline]
254 fn graphemes(&self, is_extended: bool) -> Graphemes<'_> {
255 grapheme::new_graphemes(self, is_extended)
256 }
257
258 #[inline]
259 fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices<'_> {
260 grapheme::new_grapheme_indices(self, is_extended)
261 }
262
263 #[inline]
264 fn unicode_words(&self) -> UnicodeWords<'_> {
265 word::new_unicode_words(self)
266 }
267
268 #[inline]
269 fn unicode_word_indices(&self) -> UnicodeWordIndices<'_> {
270 word::new_unicode_word_indices(self)
271 }
272
273 #[inline]
274 fn split_word_bounds(&self) -> UWordBounds<'_> {
275 word::new_word_bounds(self)
276 }
277
278 #[inline]
279 fn split_word_bound_indices(&self) -> UWordBoundIndices<'_> {
280 word::new_word_bound_indices(self)
281 }
282
283 #[inline]
284 fn unicode_sentences(&self) -> UnicodeSentences<'_> {
285 sentence::new_unicode_sentences(self)
286 }
287
288 #[inline]
289 fn split_sentence_bounds(&self) -> USentenceBounds<'_> {
290 sentence::new_sentence_bounds(self)
291 }
292
293 #[inline]
294 fn split_sentence_bound_indices(&self) -> USentenceBoundIndices<'_> {
295 sentence::new_sentence_bound_indices(self)
296 }
297}