/src/unicode-segmentation/src/lib.rs
Line | Count | Source (jump to first uncovered line) |
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)] |
60 | | extern crate std; |
61 | | |
62 | | pub use grapheme::{GraphemeCursor, GraphemeIncomplete}; |
63 | | pub use grapheme::{GraphemeIndices, Graphemes}; |
64 | | pub use sentence::{USentenceBoundIndices, USentenceBounds, UnicodeSentences}; |
65 | | pub use tables::UNICODE_VERSION; |
66 | | pub use word::{UWordBoundIndices, UWordBounds}; |
67 | | |
68 | | use crate::word::{UnicodeWordIndices, UnicodeWords}; |
69 | | |
70 | | mod grapheme; |
71 | | mod sentence; |
72 | | #[rustfmt::skip] |
73 | | mod tables; |
74 | | mod word; |
75 | | |
76 | | /// Methods for segmenting strings according to |
77 | | /// [Unicode Standard Annex #29](http://www.unicode.org/reports/tr29/). |
78 | | pub trait UnicodeSegmentation { |
79 | | /// Returns an iterator over the [grapheme clusters][graphemes] of `self`. |
80 | | /// |
81 | | /// [graphemes]: http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries |
82 | | /// |
83 | | /// If `is_extended` is true, the iterator is over the |
84 | | /// *extended grapheme clusters*; |
85 | | /// otherwise, the iterator is over the *legacy grapheme clusters*. |
86 | | /// [UAX#29](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) |
87 | | /// recommends extended grapheme cluster boundaries for general processing. |
88 | | /// |
89 | | /// # Examples |
90 | | /// |
91 | | /// ``` |
92 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
93 | | /// let gr1 = UnicodeSegmentation::graphemes("a\u{310}e\u{301}o\u{308}\u{332}", true) |
94 | | /// .collect::<Vec<&str>>(); |
95 | | /// let b: &[_] = &["a\u{310}", "e\u{301}", "o\u{308}\u{332}"]; |
96 | | /// |
97 | | /// assert_eq!(&gr1[..], b); |
98 | | /// |
99 | | /// let gr2 = UnicodeSegmentation::graphemes("a\r\nb🇷🇺🇸🇹", true).collect::<Vec<&str>>(); |
100 | | /// let b: &[_] = &["a", "\r\n", "b", "🇷🇺", "🇸🇹"]; |
101 | | /// |
102 | | /// assert_eq!(&gr2[..], b); |
103 | | /// ``` |
104 | | fn graphemes(&self, is_extended: bool) -> Graphemes<'_>; |
105 | | |
106 | | /// Returns an iterator over the grapheme clusters of `self` and their |
107 | | /// byte offsets. See `graphemes()` for more information. |
108 | | /// |
109 | | /// # Examples |
110 | | /// |
111 | | /// ``` |
112 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
113 | | /// let gr_inds = UnicodeSegmentation::grapheme_indices("a̐éö̲\r\n", true) |
114 | | /// .collect::<Vec<(usize, &str)>>(); |
115 | | /// let b: &[_] = &[(0, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")]; |
116 | | /// |
117 | | /// assert_eq!(&gr_inds[..], b); |
118 | | /// ``` |
119 | | fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices<'_>; |
120 | | |
121 | | /// Returns an iterator over the words of `self`, separated on |
122 | | /// [UAX#29 word boundaries](http://www.unicode.org/reports/tr29/#Word_Boundaries). |
123 | | /// |
124 | | /// Here, "words" are just those substrings which, after splitting on |
125 | | /// UAX#29 word boundaries, contain any alphanumeric characters. That is, the |
126 | | /// substring must contain at least one character with the |
127 | | /// [Alphabetic](http://unicode.org/reports/tr44/#Alphabetic) |
128 | | /// property, or with |
129 | | /// [General_Category=Number](http://unicode.org/reports/tr44/#General_Category_Values). |
130 | | /// |
131 | | /// # Example |
132 | | /// |
133 | | /// ``` |
134 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
135 | | /// let uws = "The quick (\"brown\") fox can't jump 32.3 feet, right?"; |
136 | | /// let uw1 = uws.unicode_words().collect::<Vec<&str>>(); |
137 | | /// let b: &[_] = &["The", "quick", "brown", "fox", "can't", "jump", "32.3", "feet", "right"]; |
138 | | /// |
139 | | /// assert_eq!(&uw1[..], b); |
140 | | /// ``` |
141 | | fn unicode_words(&self) -> UnicodeWords<'_>; |
142 | | |
143 | | /// Returns an iterator over the words of `self`, separated on |
144 | | /// [UAX#29 word boundaries](http://www.unicode.org/reports/tr29/#Word_Boundaries), and their |
145 | | /// offsets. |
146 | | /// |
147 | | /// Here, "words" are just those substrings which, after splitting on |
148 | | /// UAX#29 word boundaries, contain any alphanumeric characters. That is, the |
149 | | /// substring must contain at least one character with the |
150 | | /// [Alphabetic](http://unicode.org/reports/tr44/#Alphabetic) |
151 | | /// property, or with |
152 | | /// [General_Category=Number](http://unicode.org/reports/tr44/#General_Category_Values). |
153 | | /// |
154 | | /// # Example |
155 | | /// |
156 | | /// ``` |
157 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
158 | | /// let uwis = "The quick (\"brown\") fox can't jump 32.3 feet, right?"; |
159 | | /// let uwi1 = uwis.unicode_word_indices().collect::<Vec<(usize, &str)>>(); |
160 | | /// let b: &[_] = &[(0, "The"), (4, "quick"), (12, "brown"), (20, "fox"), (24, "can't"), |
161 | | /// (30, "jump"), (35, "32.3"), (40, "feet"), (46, "right")]; |
162 | | /// |
163 | | /// assert_eq!(&uwi1[..], b); |
164 | | /// ``` |
165 | | fn unicode_word_indices(&self) -> UnicodeWordIndices<'_>; |
166 | | |
167 | | /// Returns an iterator over substrings of `self` separated on |
168 | | /// [UAX#29 word boundaries](http://www.unicode.org/reports/tr29/#Word_Boundaries). |
169 | | /// |
170 | | /// The concatenation of the substrings returned by this function is just the original string. |
171 | | /// |
172 | | /// # Example |
173 | | /// |
174 | | /// ``` |
175 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
176 | | /// let swu1 = "The quick (\"brown\") fox".split_word_bounds().collect::<Vec<&str>>(); |
177 | | /// let b: &[_] = &["The", " ", "quick", " ", "(", "\"", "brown", "\"", ")", " ", "fox"]; |
178 | | /// |
179 | | /// assert_eq!(&swu1[..], b); |
180 | | /// ``` |
181 | | fn split_word_bounds(&self) -> UWordBounds<'_>; |
182 | | |
183 | | /// Returns an iterator over substrings of `self`, split on UAX#29 word boundaries, |
184 | | /// and their offsets. See `split_word_bounds()` for more information. |
185 | | /// |
186 | | /// # Example |
187 | | /// |
188 | | /// ``` |
189 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
190 | | /// let swi1 = "Brr, it's 29.3°F!".split_word_bound_indices().collect::<Vec<(usize, &str)>>(); |
191 | | /// let b: &[_] = &[(0, "Brr"), (3, ","), (4, " "), (5, "it's"), (9, " "), (10, "29.3"), |
192 | | /// (14, "°"), (16, "F"), (17, "!")]; |
193 | | /// |
194 | | /// assert_eq!(&swi1[..], b); |
195 | | /// ``` |
196 | | fn split_word_bound_indices(&self) -> UWordBoundIndices<'_>; |
197 | | |
198 | | /// Returns an iterator over substrings of `self` separated on |
199 | | /// [UAX#29 sentence boundaries](http://www.unicode.org/reports/tr29/#Sentence_Boundaries). |
200 | | /// |
201 | | /// Here, "sentences" are just those substrings which, after splitting on |
202 | | /// UAX#29 sentence boundaries, contain any alphanumeric characters. That is, the |
203 | | /// substring must contain at least one character with the |
204 | | /// [Alphabetic](http://unicode.org/reports/tr44/#Alphabetic) |
205 | | /// property, or with |
206 | | /// [General_Category=Number](http://unicode.org/reports/tr44/#General_Category_Values). |
207 | | /// |
208 | | /// # Example |
209 | | /// |
210 | | /// ``` |
211 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
212 | | /// let uss = "Mr. Fox jumped. [...] The dog was too lazy."; |
213 | | /// let us1 = uss.unicode_sentences().collect::<Vec<&str>>(); |
214 | | /// let b: &[_] = &["Mr. ", "Fox jumped. ", "The dog was too lazy."]; |
215 | | /// |
216 | | /// assert_eq!(&us1[..], b); |
217 | | /// ``` |
218 | | fn unicode_sentences(&self) -> UnicodeSentences<'_>; |
219 | | |
220 | | /// Returns an iterator over substrings of `self` separated on |
221 | | /// [UAX#29 sentence boundaries](http://www.unicode.org/reports/tr29/#Sentence_Boundaries). |
222 | | /// |
223 | | /// The concatenation of the substrings returned by this function is just the original string. |
224 | | /// |
225 | | /// # Example |
226 | | /// |
227 | | /// ``` |
228 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
229 | | /// let ssbs = "Mr. Fox jumped. [...] The dog was too lazy."; |
230 | | /// let ssb1 = ssbs.split_sentence_bounds().collect::<Vec<&str>>(); |
231 | | /// let b: &[_] = &["Mr. ", "Fox jumped. ", "[...] ", "The dog was too lazy."]; |
232 | | /// |
233 | | /// assert_eq!(&ssb1[..], b); |
234 | | /// ``` |
235 | | fn split_sentence_bounds(&self) -> USentenceBounds<'_>; |
236 | | |
237 | | /// Returns an iterator over substrings of `self`, split on UAX#29 sentence boundaries, |
238 | | /// and their offsets. See `split_sentence_bounds()` for more information. |
239 | | /// |
240 | | /// # Example |
241 | | /// |
242 | | /// ``` |
243 | | /// # use self::unicode_segmentation::UnicodeSegmentation; |
244 | | /// let ssis = "Mr. Fox jumped. [...] The dog was too lazy."; |
245 | | /// let ssi1 = ssis.split_sentence_bound_indices().collect::<Vec<(usize, &str)>>(); |
246 | | /// let b: &[_] = &[(0, "Mr. "), (4, "Fox jumped. "), (16, "[...] "), |
247 | | /// (22, "The dog was too lazy.")]; |
248 | | /// |
249 | | /// assert_eq!(&ssi1[..], b); |
250 | | /// ``` |
251 | | fn split_sentence_bound_indices(&self) -> USentenceBoundIndices<'_>; |
252 | | } |
253 | | |
254 | | impl UnicodeSegmentation for str { |
255 | | #[inline] |
256 | 2.80k | fn graphemes(&self, is_extended: bool) -> Graphemes<'_> { |
257 | 2.80k | grapheme::new_graphemes(self, is_extended) |
258 | 2.80k | } <str as unicode_segmentation::UnicodeSegmentation>::graphemes Line | Count | Source | 256 | 2.80k | fn graphemes(&self, is_extended: bool) -> Graphemes<'_> { | 257 | 2.80k | grapheme::new_graphemes(self, is_extended) | 258 | 2.80k | } |
Unexecuted instantiation: <str as unicode_segmentation::UnicodeSegmentation>::graphemes |
259 | | |
260 | | #[inline] |
261 | 0 | fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices { |
262 | 0 | grapheme::new_grapheme_indices(self, is_extended) |
263 | 0 | } |
264 | | |
265 | | #[inline] |
266 | 2.80k | fn unicode_words(&self) -> UnicodeWords<'_> { |
267 | 2.80k | word::new_unicode_words(self) |
268 | 2.80k | } <str as unicode_segmentation::UnicodeSegmentation>::unicode_words Line | Count | Source | 266 | 2.80k | fn unicode_words(&self) -> UnicodeWords<'_> { | 267 | 2.80k | word::new_unicode_words(self) | 268 | 2.80k | } |
Unexecuted instantiation: <str as unicode_segmentation::UnicodeSegmentation>::unicode_words |
269 | | |
270 | | #[inline] |
271 | 0 | fn unicode_word_indices(&self) -> UnicodeWordIndices<'_> { |
272 | 0 | word::new_unicode_word_indices(self) |
273 | 0 | } |
274 | | |
275 | | #[inline] |
276 | 2.80k | fn split_word_bounds(&self) -> UWordBounds<'_> { |
277 | 2.80k | word::new_word_bounds(self) |
278 | 2.80k | } <str as unicode_segmentation::UnicodeSegmentation>::split_word_bounds Line | Count | Source | 276 | 2.80k | fn split_word_bounds(&self) -> UWordBounds<'_> { | 277 | 2.80k | word::new_word_bounds(self) | 278 | 2.80k | } |
Unexecuted instantiation: <str as unicode_segmentation::UnicodeSegmentation>::split_word_bounds |
279 | | |
280 | | #[inline] |
281 | 0 | fn split_word_bound_indices(&self) -> UWordBoundIndices<'_> { |
282 | 0 | word::new_word_bound_indices(self) |
283 | 0 | } |
284 | | |
285 | | #[inline] |
286 | 0 | fn unicode_sentences(&self) -> UnicodeSentences<'_> { |
287 | 0 | sentence::new_unicode_sentences(self) |
288 | 0 | } |
289 | | |
290 | | #[inline] |
291 | 0 | fn split_sentence_bounds(&self) -> USentenceBounds<'_> { |
292 | 0 | sentence::new_sentence_bounds(self) |
293 | 0 | } |
294 | | |
295 | | #[inline] |
296 | 0 | fn split_sentence_bound_indices(&self) -> USentenceBoundIndices { |
297 | 0 | sentence::new_sentence_bound_indices(self) |
298 | 0 | } |
299 | | } |