1 // Copyright 2012-2014 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.
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.
11 // ignore-lexer-test FIXME #15679
13 //! Unicode-intensive string manipulations.
15 //! This module provides functionality to `str` that requires the Unicode methods provided by the
16 //! unicode parts of the CharExt trait.
18 use self::GraphemeState::*;
23 use core::iter::{Filter, AdditiveIterator};
28 use tables::grapheme::GraphemeCat;
30 /// An iterator over the words of a string, separated by a sequence of whitespace
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub struct Words<'a> {
33 inner: Filter<Split<'a, fn(char) -> bool>, fn(&&str) -> bool>,
36 /// Methods for Unicode string slices
37 #[allow(missing_docs)] // docs in libcollections
38 pub trait UnicodeStr {
39 fn graphemes<'a>(&'a self, is_extended: bool) -> Graphemes<'a>;
40 fn grapheme_indices<'a>(&'a self, is_extended: bool) -> GraphemeIndices<'a>;
41 fn words<'a>(&'a self) -> Words<'a>;
42 fn is_whitespace(&self) -> bool;
43 fn is_alphanumeric(&self) -> bool;
44 fn width(&self, is_cjk: bool) -> usize;
45 fn trim<'a>(&'a self) -> &'a str;
46 fn trim_left<'a>(&'a self) -> &'a str;
47 fn trim_right<'a>(&'a self) -> &'a str;
50 impl UnicodeStr for str {
52 fn graphemes(&self, is_extended: bool) -> Graphemes {
53 Graphemes { string: self, extended: is_extended, cat: None, catb: None }
57 fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices {
58 GraphemeIndices { start_offset: self.as_ptr() as usize, iter: self.graphemes(is_extended) }
62 fn words(&self) -> Words {
63 fn is_not_empty(s: &&str) -> bool { !s.is_empty() }
64 let is_not_empty: fn(&&str) -> bool = is_not_empty; // coerce to fn pointer
66 fn is_whitespace(c: char) -> bool { c.is_whitespace() }
67 let is_whitespace: fn(char) -> bool = is_whitespace; // coerce to fn pointer
69 Words { inner: self.split(is_whitespace).filter(is_not_empty) }
73 fn is_whitespace(&self) -> bool { self.chars().all(|c| c.is_whitespace()) }
76 fn is_alphanumeric(&self) -> bool { self.chars().all(|c| c.is_alphanumeric()) }
79 fn width(&self, is_cjk: bool) -> usize {
80 self.chars().map(|c| c.width(is_cjk).unwrap_or(0)).sum()
84 fn trim(&self) -> &str {
85 self.trim_matches(|c: char| c.is_whitespace())
89 fn trim_left(&self) -> &str {
90 self.trim_left_matches(|c: char| c.is_whitespace())
94 fn trim_right(&self) -> &str {
95 self.trim_right_matches(|c: char| c.is_whitespace())
99 /// External iterator for grapheme clusters and byte offsets.
101 pub struct GraphemeIndices<'a> {
106 impl<'a> Iterator for GraphemeIndices<'a> {
107 type Item = (usize, &'a str);
110 fn next(&mut self) -> Option<(usize, &'a str)> {
111 self.iter.next().map(|s| (s.as_ptr() as usize - self.start_offset, s))
115 fn size_hint(&self) -> (usize, Option<usize>) {
116 self.iter.size_hint()
120 impl<'a> DoubleEndedIterator for GraphemeIndices<'a> {
122 fn next_back(&mut self) -> Option<(usize, &'a str)> {
123 self.iter.next_back().map(|s| (s.as_ptr() as usize - self.start_offset, s))
127 /// External iterator for a string's
128 /// [grapheme clusters](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries).
130 pub struct Graphemes<'a> {
133 cat: Option<GraphemeCat>,
134 catb: Option<GraphemeCat>,
137 // state machine for cluster boundary rules
138 #[derive(PartialEq,Eq)]
148 impl<'a> Iterator for Graphemes<'a> {
152 fn size_hint(&self) -> (usize, Option<usize>) {
153 let slen = self.string.len();
154 (cmp::min(slen, 1), Some(slen))
158 fn next(&mut self) -> Option<&'a str> {
159 use tables::grapheme as gr;
160 if self.string.len() == 0 {
164 let mut take_curr = true;
166 let mut state = Start;
167 let mut cat = gr::GC_Any;
168 for (curr, ch) in self.string.char_indices() {
171 // retrieve cached category, if any
172 // We do this because most of the time we would end up
173 // looking up each character twice.
174 cat = match self.cat {
175 None => gr::grapheme_category(ch),
176 _ => self.cat.take().unwrap()
180 gr::GC_Extend => true,
181 gr::GC_SpacingMark if self.extended => true,
184 state = FindExtend; // rule GB9/GB9a
188 state = match state {
189 Start if '\r' == ch => {
190 let slen = self.string.len();
192 if nidx != slen && self.string.char_at(nidx) == '\n' {
193 idx = nidx; // rule GB3
198 gr::GC_Control => break,
200 gr::GC_LV | gr::GC_V => HangulLV,
201 gr::GC_LVT | gr::GC_T => HangulLVT,
202 gr::GC_RegionalIndicator => Regional,
205 FindExtend => { // found non-extending when looking for extending
209 HangulL => match cat { // rule GB6: L x (L|V|LV|LVT)
210 gr::GC_L => continue,
211 gr::GC_LV | gr::GC_V => HangulLV,
212 gr::GC_LVT => HangulLVT,
218 HangulLV => match cat { // rule GB7: (LV|V) x (V|T)
219 gr::GC_V => continue,
220 gr::GC_T => HangulLVT,
226 HangulLVT => match cat { // rule GB8: (LVT|T) x T
227 gr::GC_T => continue,
233 Regional => match cat { // rule GB8a
234 gr::GC_RegionalIndicator => continue,
243 self.cat = if take_curr {
244 idx = idx + self.string.char_at(idx).len_utf8();
250 let retstr = &self.string[..idx];
251 self.string = &self.string[idx..];
256 impl<'a> DoubleEndedIterator for Graphemes<'a> {
258 fn next_back(&mut self) -> Option<&'a str> {
259 use tables::grapheme as gr;
260 if self.string.len() == 0 {
264 let mut take_curr = true;
265 let mut idx = self.string.len();
266 let mut previdx = idx;
267 let mut state = Start;
268 let mut cat = gr::GC_Any;
269 for (curr, ch) in self.string.char_indices().rev() {
273 // cached category, if any
274 cat = match self.catb {
275 None => gr::grapheme_category(ch),
276 _ => self.catb.take().unwrap()
279 // a matching state machine that runs *backwards* across an input string
280 // note that this has some implications for the Hangul matching, since
281 // we now need to know what the rightward letter is:
283 // Right to left, we have:
287 // HangulL means the letter to the right is L
288 // HangulLV means the letter to the right is V
289 // HangulLVT means the letter to the right is T
290 state = match state {
291 Start if '\n' == ch => {
292 if idx > 0 && '\r' == self.string.char_at_reverse(idx) {
293 idx -= 1; // rule GB3
297 Start | FindExtend => match cat {
298 gr::GC_Extend => FindExtend,
299 gr::GC_SpacingMark if self.extended => FindExtend,
300 gr::GC_L | gr::GC_LV | gr::GC_LVT => HangulL,
301 gr::GC_V => HangulLV,
302 gr::GC_T => HangulLVT,
303 gr::GC_RegionalIndicator => Regional,
305 take_curr = Start == state;
310 HangulL => match cat { // char to right is an L
311 gr::GC_L => continue, // L x L is the only legal match
317 HangulLV => match cat { // char to right is a V
318 gr::GC_V => continue, // V x V, right char is still V
319 gr::GC_L | gr::GC_LV => HangulL, // (L|V) x V, right char is now L
325 HangulLVT => match cat { // char to right is a T
326 gr::GC_T => continue, // T x T, right char is still T
327 gr::GC_V => HangulLV, // V x T, right char is now V
328 gr::GC_LV | gr::GC_LVT => HangulL, // (LV|LVT) x T, right char is now L
334 Regional => match cat { // rule GB8a
335 gr::GC_RegionalIndicator => continue,
344 self.catb = if take_curr {
351 let retstr = &self.string[idx..];
352 self.string = &self.string[..idx];
357 // https://tools.ietf.org/html/rfc3629
358 static UTF8_CHAR_WIDTH: [u8; 256] = [
359 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
360 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
361 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
362 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
363 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
364 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
365 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
366 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
367 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
368 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
369 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
370 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
371 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
372 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
373 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
374 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
377 /// Given a first byte, determine how many bytes are in this UTF-8 character
379 pub fn utf8_char_width(b: u8) -> usize {
380 return UTF8_CHAR_WIDTH[b as usize] as usize;
383 /// Determines if a vector of `u16` contains valid UTF-16
384 pub fn is_utf16(v: &[u16]) -> bool {
385 let mut it = v.iter();
386 macro_rules! next { ($ret:expr) => {
387 match it.next() { Some(u) => *u, None => return $ret }
393 match char::from_u32(u as u32) {
396 let u2 = next!(false);
397 if u < 0xD7FF || u > 0xDBFF ||
398 u2 < 0xDC00 || u2 > 0xDFFF { return false; }
404 /// An iterator that decodes UTF-16 encoded codepoints from a vector
407 pub struct Utf16Items<'a> {
408 iter: slice::Iter<'a, u16>
410 /// The possibilities for values decoded from a `u16` stream.
411 #[derive(Copy, PartialEq, Eq, Clone, Debug)]
413 /// A valid codepoint.
415 /// An invalid surrogate without its pair.
420 /// Convert `self` to a `char`, taking `LoneSurrogate`s to the
421 /// replacement character (U+FFFD).
423 pub fn to_char_lossy(&self) -> char {
425 Utf16Item::ScalarValue(c) => c,
426 Utf16Item::LoneSurrogate(_) => '\u{FFFD}'
431 impl<'a> Iterator for Utf16Items<'a> {
432 type Item = Utf16Item;
434 fn next(&mut self) -> Option<Utf16Item> {
435 let u = match self.iter.next() {
440 if u < 0xD800 || 0xDFFF < u {
442 Some(Utf16Item::ScalarValue(unsafe {mem::transmute(u as u32)}))
443 } else if u >= 0xDC00 {
444 // a trailing surrogate
445 Some(Utf16Item::LoneSurrogate(u))
447 // preserve state for rewinding.
448 let old = self.iter.clone();
450 let u2 = match self.iter.next() {
453 None => return Some(Utf16Item::LoneSurrogate(u))
455 if u2 < 0xDC00 || u2 > 0xDFFF {
456 // not a trailing surrogate so we're not a valid
457 // surrogate pair, so rewind to redecode u2 next time.
458 self.iter = old.clone();
459 return Some(Utf16Item::LoneSurrogate(u))
462 // all ok, so lets decode it.
463 let c = (((u - 0xD800) as u32) << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
464 Some(Utf16Item::ScalarValue(unsafe {mem::transmute(c)}))
469 fn size_hint(&self) -> (usize, Option<usize>) {
470 let (low, high) = self.iter.size_hint();
471 // we could be entirely valid surrogates (2 elements per
472 // char), or entirely non-surrogates (1 element per char)
477 /// Create an iterator over the UTF-16 encoded codepoints in `v`,
478 /// returning invalid surrogates as `LoneSurrogate`s.
483 /// # #![feature(unicode)]
484 /// extern crate unicode;
486 /// use unicode::str::Utf16Item::{ScalarValue, LoneSurrogate};
489 /// // 𝄞mus<invalid>ic<invalid>
490 /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
491 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
494 /// assert_eq!(unicode::str::utf16_items(&v).collect::<Vec<_>>(),
495 /// vec![ScalarValue('𝄞'),
496 /// ScalarValue('m'), ScalarValue('u'), ScalarValue('s'),
497 /// LoneSurrogate(0xDD1E),
498 /// ScalarValue('i'), ScalarValue('c'),
499 /// LoneSurrogate(0xD834)]);
502 pub fn utf16_items<'a>(v: &'a [u16]) -> Utf16Items<'a> {
503 Utf16Items { iter : v.iter() }
506 /// Iterator adaptor for encoding `char`s to UTF-16.
508 pub struct Utf16Encoder<I> {
513 impl<I> Utf16Encoder<I> {
514 /// Create an UTF-16 encoder from any `char` iterator.
515 pub fn new(chars: I) -> Utf16Encoder<I> where I: Iterator<Item=char> {
516 Utf16Encoder { chars: chars, extra: 0 }
520 impl<I> Iterator for Utf16Encoder<I> where I: Iterator<Item=char> {
524 fn next(&mut self) -> Option<u16> {
526 let tmp = self.extra;
531 let mut buf = [0; 2];
532 self.chars.next().map(|ch| {
533 let n = CharExt::encode_utf16(ch, &mut buf).unwrap_or(0);
534 if n == 2 { self.extra = buf[1]; }
540 fn size_hint(&self) -> (usize, Option<usize>) {
541 let (low, high) = self.chars.size_hint();
542 // every char gets either one u16 or two u16,
543 // so this iterator is between 1 or 2 times as
544 // long as the underlying iterator.
545 (low, high.and_then(|n| n.checked_mul(2)))
549 impl<'a> Iterator for Words<'a> {
552 fn next(&mut self) -> Option<&'a str> { self.inner.next() }
554 impl<'a> DoubleEndedIterator for Words<'a> {
555 fn next_back(&mut self) -> Option<&'a str> { self.inner.next_back() }