Coverage Report

Created: 2026-04-12 06:23

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