//! is the same as `&[u8]`.
#![doc(primitive = "str")]
+#![stable]
-use self::MaybeOwned::*;
use self::RecompositionState::*;
use self::DecompositionType::*;
-use core::borrow::{BorrowFrom, Cow, ToOwned};
-use core::char::Char;
+use core::borrow::{BorrowFrom, ToOwned};
+use core::char::CharExt;
use core::clone::Clone;
-use core::cmp::{Equiv, PartialEq, Eq, PartialOrd, Ord, Ordering};
-use core::cmp;
-use core::default::Default;
-use core::fmt;
-use core::hash;
use core::iter::AdditiveIterator;
-use core::iter::{mod, range, Iterator, IteratorExt};
-use core::kinds::Sized;
+use core::iter::{range, Iterator, IteratorExt};
use core::ops;
-use core::option::Option::{mod, Some, None};
+use core::option::Option::{self, Some, None};
use core::slice::AsSlice;
use core::str as core_str;
use unicode::str::{UnicodeStr, Utf16Encoder};
use vec::Vec;
use slice::SliceConcatExt;
-pub use core::str::{from_utf8, CharEq, Chars, CharIndices};
-pub use core::str::{Bytes, CharSplits, is_utf8};
-pub use core::str::{CharSplitsN, Lines, LinesAny, MatchIndices, StrSplits, SplitStr};
-pub use core::str::{CharRange};
-pub use core::str::{FromStr, from_str, Utf8Error};
-pub use core::str::Str;
-pub use core::str::{from_utf8_unchecked, from_c_str};
-pub use unicode::str::{Words, Graphemes, GraphemeIndices};
+pub use core::str::{FromStr, Utf8Error, Str};
+pub use core::str::{Lines, LinesAny, MatchIndices, SplitStr, CharRange};
pub use core::str::{Split, SplitTerminator};
pub use core::str::{SplitN, RSplitN};
+pub use core::str::{from_utf8, CharEq, Chars, CharIndices, Bytes};
+pub use core::str::{from_utf8_unchecked, from_c_str};
+pub use unicode::str::{Words, Graphemes, GraphemeIndices};
/*
Section: Creating a string
}
}
-#[deriving(Clone)]
+#[derive(Clone)]
enum DecompositionType {
Canonical,
Compatible
/// External iterator for a string's decomposition's characters.
/// Use with the `std::iter` module.
-#[deriving(Clone)]
+#[derive(Clone)]
+#[unstable]
pub struct Decompositions<'a> {
kind: DecompositionType,
iter: Chars<'a>,
sorted: bool
}
-impl<'a> Iterator<char> for Decompositions<'a> {
+#[stable]
+impl<'a> Iterator for Decompositions<'a> {
+ type Item = char;
+
#[inline]
fn next(&mut self) -> Option<char> {
match self.buffer.first() {
}
}
-#[deriving(Clone)]
+#[derive(Clone)]
enum RecompositionState {
Composing,
Purging,
/// External iterator for a string's recomposition's characters.
/// Use with the `std::iter` module.
-#[deriving(Clone)]
+#[derive(Clone)]
+#[unstable]
pub struct Recompositions<'a> {
iter: Decompositions<'a>,
state: RecompositionState,
last_ccc: Option<u8>
}
-impl<'a> Iterator<char> for Recompositions<'a> {
+#[stable]
+impl<'a> Iterator for Recompositions<'a> {
+ type Item = char;
+
#[inline]
fn next(&mut self) -> Option<char> {
loop {
/// External iterator for a string's UTF16 codeunits.
/// Use with the `std::iter` module.
-#[deriving(Clone)]
+#[derive(Clone)]
+#[unstable]
pub struct Utf16Units<'a> {
encoder: Utf16Encoder<Chars<'a>>
}
-impl<'a> Iterator<u16> for Utf16Units<'a> {
+#[stable]
+impl<'a> Iterator for Utf16Units<'a> {
+ type Item = u16;
+
#[inline]
fn next(&mut self) -> Option<u16> { self.encoder.next() }
fn size_hint(&self) -> (uint, Option<uint>) { self.encoder.size_hint() }
}
-/// Replaces all occurrences of one string with another.
-///
-/// # Arguments
-///
-/// * s - The string containing substrings to replace
-/// * from - The string to replace
-/// * to - The replacement string
-///
-/// # Return value
-///
-/// The original string with all occurrences of `from` replaced with `to`.
-///
-/// # Examples
-///
-/// ```rust
-/// # #![allow(deprecated)]
-/// use std::str;
-/// let string = "orange";
-/// let new_string = str::replace(string, "or", "str");
-/// assert_eq!(new_string.as_slice(), "strange");
-/// ```
-#[deprecated = "call the inherent method instead"]
-pub fn replace(s: &str, from: &str, to: &str) -> String {
- s.replace(from, to)
-}
-
/*
Section: Misc
*/
($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63u8) as u32)
}
-/*
-Section: MaybeOwned
-*/
-
-/// A string type that can hold either a `String` or a `&str`.
-/// This can be useful as an optimization when an allocation is sometimes
-/// needed but not always.
-#[deprecated = "use std::string::CowString"]
-pub enum MaybeOwned<'a> {
- /// A borrowed string.
- Slice(&'a str),
- /// An owned string.
- Owned(String)
-}
-
-/// A specialization of `CowString` to be sendable.
-#[deprecated = "use std::string::CowString<'static>"]
-pub type SendStr = CowString<'static>;
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> MaybeOwned<'a> {
- /// Returns `true` if this `MaybeOwned` wraps an owned string.
- ///
- /// # Examples
- ///
- /// ``` ignore
- /// let string = String::from_str("orange");
- /// let maybe_owned_string = string.into_maybe_owned();
- /// assert_eq!(true, maybe_owned_string.is_owned());
- /// ```
- #[inline]
- pub fn is_owned(&self) -> bool {
- match *self {
- Slice(_) => false,
- Owned(_) => true
- }
- }
-
- /// Returns `true` if this `MaybeOwned` wraps a borrowed string.
- ///
- /// # Examples
- ///
- /// ``` ignore
- /// let string = "orange";
- /// let maybe_owned_string = string.as_slice().into_maybe_owned();
- /// assert_eq!(true, maybe_owned_string.is_slice());
- /// ```
- #[inline]
- pub fn is_slice(&self) -> bool {
- match *self {
- Slice(_) => true,
- Owned(_) => false
- }
- }
-
- /// Return the number of bytes in this string.
- #[inline]
- #[allow(deprecated)]
- pub fn len(&self) -> uint { self.as_slice().len() }
-
- /// Returns true if the string contains no bytes
- #[allow(deprecated)]
- #[inline]
- pub fn is_empty(&self) -> bool { self.len() == 0 }
-}
-
-#[deprecated = "use std::borrow::IntoCow"]
-/// Trait for moving into a `MaybeOwned`.
-pub trait IntoMaybeOwned<'a> {
- /// Moves `self` into a `MaybeOwned`.
- fn into_maybe_owned(self) -> MaybeOwned<'a>;
-}
-
-#[deprecated = "use std::borrow::IntoCow"]
-#[allow(deprecated)]
-impl<'a> IntoMaybeOwned<'a> for String {
- /// # Examples
- ///
- /// ``` ignore
- /// let owned_string = String::from_str("orange");
- /// let maybe_owned_string = owned_string.into_maybe_owned();
- /// assert_eq!(true, maybe_owned_string.is_owned());
- /// ```
- #[allow(deprecated)]
- #[inline]
- fn into_maybe_owned(self) -> MaybeOwned<'a> {
- Owned(self)
- }
-}
-
-#[deprecated = "use std::borrow::IntoCow"]
-#[allow(deprecated)]
-impl<'a> IntoMaybeOwned<'a> for &'a str {
- /// # Examples
- ///
- /// ``` ignore
- /// let string = "orange";
- /// let maybe_owned_str = string.as_slice().into_maybe_owned();
- /// assert_eq!(false, maybe_owned_str.is_owned());
- /// ```
- #[allow(deprecated)]
- #[inline]
- fn into_maybe_owned(self) -> MaybeOwned<'a> { Slice(self) }
-}
-
-#[allow(deprecated)]
-#[deprecated = "use std::borrow::IntoCow"]
-impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> {
- /// # Examples
- ///
- /// ``` ignore
- /// let str = "orange";
- /// let maybe_owned_str = str.as_slice().into_maybe_owned();
- /// let maybe_maybe_owned_str = maybe_owned_str.into_maybe_owned();
- /// assert_eq!(false, maybe_maybe_owned_str.is_owned());
- /// ```
- #[inline]
- fn into_maybe_owned(self) -> MaybeOwned<'a> { self }
-}
-
-#[deprecated = "use std::string::CowString"]
-#[allow(deprecated)]
-impl<'a> PartialEq for MaybeOwned<'a> {
- #[inline]
- fn eq(&self, other: &MaybeOwned) -> bool {
- self.as_slice() == other.as_slice()
- }
-}
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> Eq for MaybeOwned<'a> {}
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> PartialOrd for MaybeOwned<'a> {
- #[inline]
- fn partial_cmp(&self, other: &MaybeOwned) -> Option<Ordering> {
- Some(self.cmp(other))
- }
-}
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> Ord for MaybeOwned<'a> {
- #[inline]
- #[allow(deprecated)]
- fn cmp(&self, other: &MaybeOwned) -> Ordering {
- self.as_slice().cmp(other.as_slice())
- }
-}
-
-#[allow(deprecated)]
-#[deprecated = "use std::string::CowString"]
-impl<'a, S: Str> Equiv<S> for MaybeOwned<'a> {
- #[inline]
- fn equiv(&self, other: &S) -> bool {
- self.as_slice() == other.as_slice()
- }
-}
-
-#[deprecated = "use std::string::CowString"]
-#[allow(deprecated)]
-impl<'a> Str for MaybeOwned<'a> {
- #[inline]
- fn as_slice<'b>(&'b self) -> &'b str {
- match *self {
- Slice(s) => s,
- Owned(ref s) => s.as_slice()
- }
- }
-}
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> Clone for MaybeOwned<'a> {
- #[allow(deprecated)]
- #[inline]
- fn clone(&self) -> MaybeOwned<'a> {
- match *self {
- Slice(s) => Slice(s),
- Owned(ref s) => Owned(String::from_str(s.as_slice()))
- }
- }
-}
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> Default for MaybeOwned<'a> {
- #[allow(deprecated)]
- #[inline]
- fn default() -> MaybeOwned<'a> { Slice("") }
-}
-
-#[deprecated = "use std::string::CowString"]
-#[allow(deprecated)]
-impl<'a, H: hash::Writer> hash::Hash<H> for MaybeOwned<'a> {
- #[inline]
- fn hash(&self, hasher: &mut H) {
- self.as_slice().hash(hasher)
- }
-}
-
-#[deprecated = "use std::string::CowString"]
-impl<'a> fmt::Show for MaybeOwned<'a> {
- #[inline]
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match *self {
- Slice(ref s) => s.fmt(f),
- Owned(ref s) => s.fmt(f)
- }
- }
-}
-
#[unstable = "trait is unstable"]
impl BorrowFrom<String> for str {
fn borrow_from(owned: &String) -> &str { owned[] }
}
}
-/// Unsafe string operations.
-#[deprecated]
-pub mod raw {
- pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes};
- pub use core::str::raw::{slice_unchecked};
-}
-
/*
Section: CowString
*/
-/// A clone-on-write string
-#[deprecated = "use std::string::CowString instead"]
-pub type CowString<'a> = Cow<'a, String, str>;
-
/*
Section: Trait implementations
*/
/// Any string that can be represented as a slice.
-pub trait StrExt for Sized?: ops::Slice<uint, str> {
+#[stable]
+pub trait StrExt: ops::Slice<uint, str> {
/// Escapes each char in `s` with `char::escape_default`.
#[unstable = "return type may change to be an iterator"]
fn escape_default(&self) -> String {
result
}
- /// Given a string, makes a new string with repeated copies of it.
- #[deprecated = "use repeat(self).take(n).collect() instead"]
- fn repeat(&self, nn: uint) -> String {
- iter::repeat(self[]).take(nn).collect()
- }
-
- /// Returns the Levenshtein Distance between two strings.
- #[deprecated = "this function will be removed"]
- fn lev_distance(&self, t: &str) -> uint {
- let me = self[];
- if me.is_empty() { return t.chars().count(); }
- if t.is_empty() { return me.chars().count(); }
-
- let mut dcol: Vec<_> = range(0, t.len() + 1).collect();
- let mut t_last = 0;
-
- for (i, sc) in me.chars().enumerate() {
-
- let mut current = i;
- dcol[0] = current + 1;
-
- for (j, tc) in t.chars().enumerate() {
-
- let next = dcol[j + 1];
-
- if sc == tc {
- dcol[j + 1] = current;
- } else {
- dcol[j + 1] = cmp::min(current, next);
- dcol[j + 1] = cmp::min(dcol[j + 1], dcol[j]) + 1;
- }
-
- current = next;
- t_last = j;
- }
- }
-
- dcol[t_last + 1]
- }
-
/// Returns an iterator over the string in Unicode Normalization Form D
/// (canonical decomposition).
#[inline]
/// assert_eq!(v, vec!["1", "", "2"]);
/// ```
#[unstable = "might get removed in the future in favor of a more generic split()"]
- fn split_str<'a>(&'a self, pat: &'a str) -> StrSplits<'a> {
+ fn split_str<'a>(&'a self, pat: &'a str) -> SplitStr<'a> {
core_str::StrExt::split_str(self[], pat)
}
core_str::StrExt::lines_any(self[])
}
- /// Returns the number of Unicode code points (`char`) that a
- /// string holds.
- ///
- /// This does not perform any normalization, and is `O(n)`, since
- /// UTF-8 is a variable width encoding of code points.
- ///
- /// *Warning*: The number of code points in a string does not directly
- /// correspond to the number of visible characters or width of the
- /// visible text due to composing characters, and double- and
- /// zero-width ones.
- ///
- /// See also `.len()` for the byte length.
- ///
- /// # Example
- ///
- /// ```rust
- /// # #![allow(deprecated)]
- /// // composed forms of `ö` and `é`
- /// let c = "Löwe 老虎 Léopard"; // German, Simplified Chinese, French
- /// // decomposed forms of `ö` and `é`
- /// let d = "Lo\u{0308}we 老虎 Le\u{0301}opard";
- ///
- /// assert_eq!(c.char_len(), 15);
- /// assert_eq!(d.char_len(), 17);
- ///
- /// assert_eq!(c.len(), 21);
- /// assert_eq!(d.len(), 23);
- ///
- /// // the two strings *look* the same
- /// println!("{}", c);
- /// println!("{}", d);
- /// ```
- #[deprecated = "call .chars().count() instead"]
- fn char_len(&self) -> uint {
- core_str::StrExt::char_len(self[])
- }
-
/// Returns a slice of the given string from the byte range
/// [`begin`..`end`).
///
core_str::StrExt::trim_matches(self[], pat)
}
- /// Deprecated
- #[deprecated = "Replaced by `trim_matches`"]
- fn trim_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str {
- self.trim_matches(to_trim)
- }
-
/// Returns a string with all prefixes that match
/// the pattern `pat` repeatedly removed.
///
core_str::StrExt::trim_left_matches(self[], pat)
}
- /// Deprecated
- #[deprecated = "Replaced by `trim_left_matches`"]
- fn trim_left_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str {
- self.trim_left_matches(to_trim)
- }
-
/// Returns a string with all suffixes that match
/// the pattern `pat` repeatedly removed.
///
core_str::StrExt::trim_right_matches(self[], pat)
}
- /// Deprecated
- #[deprecated = "Replaced by `trim_right_matches`"]
- fn trim_right_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str {
- self.trim_right_matches(to_trim)
- }
-
/// Check that `index`-th byte lies at the start and/or end of a
/// UTF-8 code point sequence.
///
#[inline]
#[unstable = "this method was just created"]
fn parse<F: FromStr>(&self) -> Option<F> {
- FromStr::from_str(self[])
+ core_str::StrExt::parse(self[])
}
/// Returns an iterator over the
UnicodeStr::words(self[])
}
- /// Returns true if the string contains only whitespace.
- ///
- /// Whitespace characters are determined by `char::is_whitespace`.
- ///
- /// # Example
- ///
- /// ```rust
- /// # #![allow(deprecated)]
- /// assert!(" \t\n".is_whitespace());
- /// assert!("".is_whitespace());
- ///
- /// assert!( !"abc".is_whitespace());
- /// ```
- #[deprecated = "use .chars().all(|c| c.is_whitespace())"]
- fn is_whitespace(&self) -> bool {
- UnicodeStr::is_whitespace(self[])
- }
-
- /// Returns true if the string contains only alphanumeric code
- /// points.
- ///
- /// Alphanumeric characters are determined by `char::is_alphanumeric`.
- ///
- /// # Example
- ///
- /// ```rust
- /// # #![allow(deprecated)]
- /// assert!("Löwe老虎Léopard123".is_alphanumeric());
- /// assert!("".is_alphanumeric());
- ///
- /// assert!( !" &*~".is_alphanumeric());
- /// ```
- #[deprecated = "use .chars().all(|c| c.is_alphanumeric())"]
- fn is_alphanumeric(&self) -> bool {
- UnicodeStr::is_alphanumeric(self[])
- }
-
/// Returns a string's displayed width in columns, treating control
/// characters as zero-width.
///
fn trim_right(&self) -> &str {
UnicodeStr::trim_right(self[])
}
-
- /// Deprecated, call `.to_owned()` instead from the `std::borrow::ToOwned`
- /// trait.
- #[deprecated = "call `.to_owned()` on `std::borrow::ToOwned` instead"]
- fn into_string(&self) -> String {
- self[].to_owned()
- }
}
+#[stable]
impl StrExt for str {}
#[cfg(test)]
mod tests {
use prelude::*;
- use core::default::Default;
use core::iter::AdditiveIterator;
- use super::{from_utf8, is_utf8, raw};
- use super::MaybeOwned::{Owned, Slice};
+ use super::from_utf8;
use super::Utf8Error;
#[test]
assert_eq!("\u{2620}".len(), 3u);
assert_eq!("\u{1d11e}".len(), 4u);
- assert_eq!("".char_len(), 0u);
- assert_eq!("hello world".char_len(), 11u);
- assert_eq!("\x63".char_len(), 1u);
- assert_eq!("\u{a2}".char_len(), 1u);
- assert_eq!("\u{3c0}".char_len(), 1u);
- assert_eq!("\u{2620}".char_len(), 1u);
- assert_eq!("\u{1d11e}".char_len(), 1u);
- assert_eq!("ประเทศไทย中华Việt Nam".char_len(), 19u);
+ assert_eq!("".chars().count(), 0u);
+ assert_eq!("hello world".chars().count(), 11u);
+ assert_eq!("\x63".chars().count(), 1u);
+ assert_eq!("\u{a2}".chars().count(), 1u);
+ assert_eq!("\u{3c0}".chars().count(), 1u);
+ assert_eq!("\u{2620}".chars().count(), 1u);
+ assert_eq!("\u{1d11e}".chars().count(), 1u);
+ assert_eq!("ประเทศไทย中华Việt Nam".chars().count(), 19u);
assert_eq!("hello".width(false), 10u);
assert_eq!("hello".width(true), 10u);
#[test]
fn test_slice_chars() {
fn t(a: &str, b: &str, start: uint) {
- assert_eq!(a.slice_chars(start, start + b.char_len()), b);
+ assert_eq!(a.slice_chars(start, start + b.chars().count()), b);
}
t("", "", 0);
t("hello", "llo", 2);
assert_eq!("ะเทศไท", "ประเทศไทย中华Việt Nam".slice_chars(2, 8));
}
- fn s(x: &str) -> String { x.into_string() }
+ fn s(x: &str) -> String { x.to_string() }
macro_rules! test_concat {
($expected: expr, $string: expr) => {
#[test]
fn test_connect_for_different_types() {
test_connect!("a-b", ["a", "b"], "-");
- let hyphen = "-".into_string();
+ let hyphen = "-".to_string();
test_connect!("a-b", [s("a"), s("b")], hyphen.as_slice());
test_connect!("a-b", vec!["a", "b"], hyphen.as_slice());
test_connect!("a-b", vec!["a", "b"].as_slice(), "-");
test_connect!("-a-bc", ["", "a", "bc"], "-");
}
- #[test]
- fn test_repeat() {
- assert_eq!("x".repeat(4), String::from_str("xxxx"));
- assert_eq!("hi".repeat(4), String::from_str("hihihihi"));
- assert_eq!("ไท华".repeat(3), String::from_str("ไท华ไท华ไท华"));
- assert_eq!("".repeat(4), String::from_str(""));
- assert_eq!("hi".repeat(0), String::from_str(""));
- }
-
#[test]
fn test_unsafe_slice() {
- assert_eq!("ab", unsafe {raw::slice_bytes("abc", 0, 2)});
- assert_eq!("bc", unsafe {raw::slice_bytes("abc", 1, 3)});
- assert_eq!("", unsafe {raw::slice_bytes("abc", 1, 1)});
+ assert_eq!("ab", unsafe {"abc".slice_unchecked(0, 2)});
+ assert_eq!("bc", unsafe {"abc".slice_unchecked(1, 3)});
+ assert_eq!("", unsafe {"abc".slice_unchecked(1, 1)});
fn a_million_letter_a() -> String {
let mut i = 0u;
let mut rs = String::new();
}
let letters = a_million_letter_a();
assert!(half_a_million_letter_a() ==
- unsafe {String::from_str(raw::slice_bytes(letters.as_slice(),
+ unsafe {String::from_str(letters.slice_unchecked(
0u,
500000))});
}
}
#[test]
- fn test_trim_left_chars() {
+ fn test_trim_left_matches() {
let v: &[char] = &[];
- assert_eq!(" *** foo *** ".trim_left_chars(v), " *** foo *** ");
+ assert_eq!(" *** foo *** ".trim_left_matches(v), " *** foo *** ");
let chars: &[char] = &['*', ' '];
- assert_eq!(" *** foo *** ".trim_left_chars(chars), "foo *** ");
- assert_eq!(" *** *** ".trim_left_chars(chars), "");
- assert_eq!("foo *** ".trim_left_chars(chars), "foo *** ");
+ assert_eq!(" *** foo *** ".trim_left_matches(chars), "foo *** ");
+ assert_eq!(" *** *** ".trim_left_matches(chars), "");
+ assert_eq!("foo *** ".trim_left_matches(chars), "foo *** ");
- assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11");
+ assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
let chars: &[char] = &['1', '2'];
- assert_eq!("12foo1bar12".trim_left_chars(chars), "foo1bar12");
- assert_eq!("123foo1bar123".trim_left_chars(|&: c: char| c.is_numeric()), "foo1bar123");
+ assert_eq!("12foo1bar12".trim_left_matches(chars), "foo1bar12");
+ assert_eq!("123foo1bar123".trim_left_matches(|&: c: char| c.is_numeric()), "foo1bar123");
}
#[test]
- fn test_trim_right_chars() {
+ fn test_trim_right_matches() {
let v: &[char] = &[];
- assert_eq!(" *** foo *** ".trim_right_chars(v), " *** foo *** ");
+ assert_eq!(" *** foo *** ".trim_right_matches(v), " *** foo *** ");
let chars: &[char] = &['*', ' '];
- assert_eq!(" *** foo *** ".trim_right_chars(chars), " *** foo");
- assert_eq!(" *** *** ".trim_right_chars(chars), "");
- assert_eq!(" *** foo".trim_right_chars(chars), " *** foo");
+ assert_eq!(" *** foo *** ".trim_right_matches(chars), " *** foo");
+ assert_eq!(" *** *** ".trim_right_matches(chars), "");
+ assert_eq!(" *** foo".trim_right_matches(chars), " *** foo");
- assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar");
+ assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
let chars: &[char] = &['1', '2'];
- assert_eq!("12foo1bar12".trim_right_chars(chars), "12foo1bar");
- assert_eq!("123foo1bar123".trim_right_chars(|&: c: char| c.is_numeric()), "123foo1bar");
+ assert_eq!("12foo1bar12".trim_right_matches(chars), "12foo1bar");
+ assert_eq!("123foo1bar123".trim_right_matches(|&: c: char| c.is_numeric()), "123foo1bar");
}
#[test]
- fn test_trim_chars() {
+ fn test_trim_matches() {
let v: &[char] = &[];
- assert_eq!(" *** foo *** ".trim_chars(v), " *** foo *** ");
+ assert_eq!(" *** foo *** ".trim_matches(v), " *** foo *** ");
let chars: &[char] = &['*', ' '];
- assert_eq!(" *** foo *** ".trim_chars(chars), "foo");
- assert_eq!(" *** *** ".trim_chars(chars), "");
- assert_eq!("foo".trim_chars(chars), "foo");
+ assert_eq!(" *** foo *** ".trim_matches(chars), "foo");
+ assert_eq!(" *** *** ".trim_matches(chars), "");
+ assert_eq!("foo".trim_matches(chars), "foo");
- assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar");
+ assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
let chars: &[char] = &['1', '2'];
- assert_eq!("12foo1bar12".trim_chars(chars), "foo1bar");
- assert_eq!("123foo1bar123".trim_chars(|&: c: char| c.is_numeric()), "foo1bar");
+ assert_eq!("12foo1bar12".trim_matches(chars), "foo1bar");
+ assert_eq!("123foo1bar123".trim_matches(|&: c: char| c.is_numeric()), "foo1bar");
}
#[test]
#[test]
fn test_is_whitespace() {
- assert!("".is_whitespace());
- assert!(" ".is_whitespace());
- assert!("\u{2009}".is_whitespace()); // Thin space
- assert!(" \n\t ".is_whitespace());
- assert!(!" _ ".is_whitespace());
+ assert!("".chars().all(|c| c.is_whitespace()));
+ assert!(" ".chars().all(|c| c.is_whitespace()));
+ assert!("\u{2009}".chars().all(|c| c.is_whitespace())); // Thin space
+ assert!(" \n\t ".chars().all(|c| c.is_whitespace()));
+ assert!(!" _ ".chars().all(|c| c.is_whitespace()));
}
#[test]
#[test]
fn test_is_utf8() {
// deny overlong encodings
- assert!(!is_utf8(&[0xc0, 0x80]));
- assert!(!is_utf8(&[0xc0, 0xae]));
- assert!(!is_utf8(&[0xe0, 0x80, 0x80]));
- assert!(!is_utf8(&[0xe0, 0x80, 0xaf]));
- assert!(!is_utf8(&[0xe0, 0x81, 0x81]));
- assert!(!is_utf8(&[0xf0, 0x82, 0x82, 0xac]));
- assert!(!is_utf8(&[0xf4, 0x90, 0x80, 0x80]));
+ assert!(from_utf8(&[0xc0, 0x80]).is_err());
+ assert!(from_utf8(&[0xc0, 0xae]).is_err());
+ assert!(from_utf8(&[0xe0, 0x80, 0x80]).is_err());
+ assert!(from_utf8(&[0xe0, 0x80, 0xaf]).is_err());
+ assert!(from_utf8(&[0xe0, 0x81, 0x81]).is_err());
+ assert!(from_utf8(&[0xf0, 0x82, 0x82, 0xac]).is_err());
+ assert!(from_utf8(&[0xf4, 0x90, 0x80, 0x80]).is_err());
// deny surrogates
- assert!(!is_utf8(&[0xED, 0xA0, 0x80]));
- assert!(!is_utf8(&[0xED, 0xBF, 0xBF]));
+ assert!(from_utf8(&[0xED, 0xA0, 0x80]).is_err());
+ assert!(from_utf8(&[0xED, 0xBF, 0xBF]).is_err());
- assert!(is_utf8(&[0xC2, 0x80]));
- assert!(is_utf8(&[0xDF, 0xBF]));
- assert!(is_utf8(&[0xE0, 0xA0, 0x80]));
- assert!(is_utf8(&[0xED, 0x9F, 0xBF]));
- assert!(is_utf8(&[0xEE, 0x80, 0x80]));
- assert!(is_utf8(&[0xEF, 0xBF, 0xBF]));
- assert!(is_utf8(&[0xF0, 0x90, 0x80, 0x80]));
- assert!(is_utf8(&[0xF4, 0x8F, 0xBF, 0xBF]));
+ assert!(from_utf8(&[0xC2, 0x80]).is_ok());
+ assert!(from_utf8(&[0xDF, 0xBF]).is_ok());
+ assert!(from_utf8(&[0xE0, 0xA0, 0x80]).is_ok());
+ assert!(from_utf8(&[0xED, 0x9F, 0xBF]).is_ok());
+ assert!(from_utf8(&[0xEE, 0x80, 0x80]).is_ok());
+ assert!(from_utf8(&[0xEF, 0xBF, 0xBF]).is_ok());
+ assert!(from_utf8(&[0xF0, 0x90, 0x80, 0x80]).is_ok());
+ assert!(from_utf8(&[0xF4, 0x8F, 0xBF, 0xBF]).is_ok());
}
#[test]
fn test_is_utf16() {
use unicode::str::is_utf16;
- macro_rules! pos ( ($($e:expr),*) => { { $(assert!(is_utf16($e));)* } });
+ macro_rules! pos {
+ ($($e:expr),*) => { { $(assert!(is_utf16($e));)* } }
+ }
// non-surrogates
pos!(&[0x0000],
&[0x0067, 0xd8ff, 0xddb7, 0x000f, 0xd900, 0xdc80]);
// negative tests
- macro_rules! neg ( ($($e:expr),*) => { { $(assert!(!is_utf16($e));)* } });
+ macro_rules! neg {
+ ($($e:expr),*) => { { $(assert!(!is_utf16($e));)* } }
+ }
neg!(
// surrogate + regular unit
let mut pos = 0;
for ch in v.iter() {
assert!(s.char_at(pos) == *ch);
- pos += String::from_char(1, *ch).len();
+ pos += ch.to_string().len();
}
}
let mut pos = s.len();
for ch in v.iter().rev() {
assert!(s.char_at_reverse(pos) == *ch);
- pos -= String::from_char(1, *ch).len();
+ pos -= ch.to_string().len();
}
}
use core::iter::order;
// official Unicode test data
// from http://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.txt
- let test_same: [(_, &[_]), .. 325] = [
+ let test_same: [(_, &[_]); 325] = [
("\u{20}\u{20}", &["\u{20}", "\u{20}"]),
("\u{20}\u{308}\u{20}", &["\u{20}\u{308}", "\u{20}"]),
("\u{20}\u{D}", &["\u{20}", "\u{D}"]),
("\u{646}\u{200D}\u{20}", &["\u{646}\u{200D}", "\u{20}"]),
];
- let test_diff: [(_, &[_], &[_]), .. 23] = [
+ let test_diff: [(_, &[_], &[_]); 23] = [
("\u{20}\u{903}", &["\u{20}\u{903}"], &["\u{20}", "\u{903}"]), ("\u{20}\u{308}\u{903}",
&["\u{20}\u{308}\u{903}"], &["\u{20}\u{308}", "\u{903}"]), ("\u{D}\u{308}\u{903}",
&["\u{D}", "\u{308}\u{903}"], &["\u{D}", "\u{308}", "\u{903}"]), ("\u{A}\u{308}\u{903}",
let xs = b"hello\xFF";
assert_eq!(from_utf8(xs), Err(Utf8Error::TooShort));
}
-
- #[test]
- fn test_maybe_owned_traits() {
- let s = Slice("abcde");
- assert_eq!(s.len(), 5);
- assert_eq!(s.as_slice(), "abcde");
- assert_eq!(String::from_str(s.as_slice()).as_slice(), "abcde");
- assert_eq!(format!("{}", s).as_slice(), "abcde");
- assert!(s.lt(&Owned(String::from_str("bcdef"))));
- assert_eq!(Slice(""), Default::default());
-
- let o = Owned(String::from_str("abcde"));
- assert_eq!(o.len(), 5);
- assert_eq!(o.as_slice(), "abcde");
- assert_eq!(String::from_str(o.as_slice()).as_slice(), "abcde");
- assert_eq!(format!("{}", o).as_slice(), "abcde");
- assert!(o.lt(&Slice("bcdef")));
- assert_eq!(Owned(String::from_str("")), Default::default());
-
- assert!(s.cmp(&o) == Equal);
- assert!(s.equiv(&o));
-
- assert!(o.cmp(&s) == Equal);
- assert!(o.equiv(&s));
- }
-
- #[test]
- fn test_maybe_owned_methods() {
- let s = Slice("abcde");
- assert!(s.is_slice());
- assert!(!s.is_owned());
-
- let o = Owned(String::from_str("abcde"));
- assert!(!o.is_slice());
- assert!(o.is_owned());
- }
-
- #[test]
- fn test_maybe_owned_clone() {
- assert_eq!(Owned(String::from_str("abcde")), Slice("abcde").clone());
- assert_eq!(Owned(String::from_str("abcde")), Owned(String::from_str("abcde")).clone());
- assert_eq!(Slice("abcde"), Slice("abcde").clone());
- assert_eq!(Slice("abcde"), Owned(String::from_str("abcde")).clone());
- }
-
- #[test]
- fn test_maybe_owned_into_string() {
- assert_eq!(Slice("abcde").to_string(), String::from_str("abcde"));
- assert_eq!(Owned(String::from_str("abcde")).to_string(),
- String::from_str("abcde"));
- }
-
- #[test]
- fn test_into_maybe_owned() {
- assert_eq!("abcde".into_maybe_owned(), Slice("abcde"));
- assert_eq!((String::from_str("abcde")).into_maybe_owned(), Slice("abcde"));
- assert_eq!("abcde".into_maybe_owned(), Owned(String::from_str("abcde")));
- assert_eq!((String::from_str("abcde")).into_maybe_owned(),
- Owned(String::from_str("abcde")));
- }
}
#[cfg(test)]
mod bench {
use super::*;
- use prelude::{SliceExt, IteratorExt, DoubleEndedIteratorExt, SliceConcatExt};
+ use prelude::{SliceExt, IteratorExt, SliceConcatExt};
use test::Bencher;
use test::black_box;
#[bench]
fn char_indicesator(b: &mut Bencher) {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
- let len = s.char_len();
+ let len = s.chars().count();
b.iter(|| assert_eq!(s.char_indices().count(), len));
}
#[bench]
fn char_indicesator_rev(b: &mut Bencher) {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
- let len = s.char_len();
+ let len = s.chars().count();
b.iter(|| assert_eq!(s.char_indices().rev().count(), len));
}
b.iter(|| assert_eq!(s.split(c).count(), len));
}
- #[bench]
- fn is_utf8_100_ascii(b: &mut Bencher) {
-
- let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
- Lorem ipsum dolor sit amet, consectetur. ";
-
- assert_eq!(100, s.len());
- b.iter(|| {
- is_utf8(s)
- });
- }
-
- #[bench]
- fn is_utf8_100_multibyte(b: &mut Bencher) {
- let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
- assert_eq!(100, s.len());
- b.iter(|| {
- is_utf8(s)
- });
- }
-
#[bench]
fn bench_connect(b: &mut Bencher) {
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";