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 //! Unicode-intensive string manipulations.
13 //! This module provides functionality to `str` that requires the Unicode
14 //! methods provided by the unicode parts of the CharExt trait.
17 use core::iter::Filter;
20 /// An iterator over the non-whitespace substrings of a string,
21 /// separated by any amount of whitespace.
22 #[stable(feature = "split_whitespace", since = "1.1.0")]
23 pub struct SplitWhitespace<'a> {
24 inner: Filter<Split<'a, fn(char) -> bool>, fn(&&str) -> bool>,
27 /// Methods for Unicode string slices
28 #[allow(missing_docs)] // docs in libcollections
29 pub trait UnicodeStr {
30 fn split_whitespace<'a>(&'a self) -> SplitWhitespace<'a>;
31 fn is_whitespace(&self) -> bool;
32 fn is_alphanumeric(&self) -> bool;
33 fn trim(&self) -> &str;
34 fn trim_left(&self) -> &str;
35 fn trim_right(&self) -> &str;
38 impl UnicodeStr for str {
40 fn split_whitespace(&self) -> SplitWhitespace {
41 fn is_not_empty(s: &&str) -> bool {
44 let is_not_empty: fn(&&str) -> bool = is_not_empty; // coerce to fn pointer
46 fn is_whitespace(c: char) -> bool {
49 let is_whitespace: fn(char) -> bool = is_whitespace; // coerce to fn pointer
51 SplitWhitespace { inner: self.split(is_whitespace).filter(is_not_empty) }
55 fn is_whitespace(&self) -> bool {
56 self.chars().all(|c| c.is_whitespace())
60 fn is_alphanumeric(&self) -> bool {
61 self.chars().all(|c| c.is_alphanumeric())
65 fn trim(&self) -> &str {
66 self.trim_matches(|c: char| c.is_whitespace())
70 fn trim_left(&self) -> &str {
71 self.trim_left_matches(|c: char| c.is_whitespace())
75 fn trim_right(&self) -> &str {
76 self.trim_right_matches(|c: char| c.is_whitespace())
80 // https://tools.ietf.org/html/rfc3629
81 static UTF8_CHAR_WIDTH: [u8; 256] = [
82 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
83 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
84 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
85 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
86 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
87 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
88 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
89 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
90 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
91 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
92 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
93 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
94 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
95 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
96 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
97 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
100 /// Given a first byte, determine how many bytes are in this UTF-8 character
102 pub fn utf8_char_width(b: u8) -> usize {
103 return UTF8_CHAR_WIDTH[b as usize] as usize;
106 /// Determines if a vector of `u16` contains valid UTF-16
107 pub fn is_utf16(v: &[u16]) -> bool {
108 let mut it = v.iter();
109 macro_rules! next { ($ret:expr) => {
110 match it.next() { Some(u) => *u, None => return $ret }
116 match char::from_u32(u as u32) {
119 let u2 = next!(false);
120 if u < 0xD7FF || u > 0xDBFF || u2 < 0xDC00 || u2 > 0xDFFF {
128 /// Iterator adaptor for encoding `char`s to UTF-16.
130 pub struct Utf16Encoder<I> {
135 impl<I> Utf16Encoder<I> {
136 /// Create a UTF-16 encoder from any `char` iterator.
137 pub fn new(chars: I) -> Utf16Encoder<I>
138 where I: Iterator<Item = char>
147 impl<I> Iterator for Utf16Encoder<I>
148 where I: Iterator<Item = char>
153 fn next(&mut self) -> Option<u16> {
155 let tmp = self.extra;
160 self.chars.next().map(|ch| {
161 let n = CharExt::encode_utf16(ch);
162 let n = n.as_slice();
171 fn size_hint(&self) -> (usize, Option<usize>) {
172 let (low, high) = self.chars.size_hint();
173 // every char gets either one u16 or two u16,
174 // so this iterator is between 1 or 2 times as
175 // long as the underlying iterator.
176 (low, high.and_then(|n| n.checked_mul(2)))
180 impl<'a> Iterator for SplitWhitespace<'a> {
183 fn next(&mut self) -> Option<&'a str> {
187 impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
188 fn next_back(&mut self) -> Option<&'a str> {
189 self.inner.next_back()