]> git.lizzy.rs Git - rust.git/commitdiff
Merge core::unicode::str into core::str
authorSimon Sapin <simon.sapin@exyr.org>
Thu, 5 Apr 2018 17:00:48 +0000 (19:00 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Wed, 11 Apr 2018 22:13:52 +0000 (00:13 +0200)
And the UnicodeStr trait into StrExt

src/liballoc/str.rs
src/liballoc/tests/str.rs
src/libcore/str/mod.rs
src/libcore/unicode/mod.rs
src/libcore/unicode/str.rs [deleted file]

index eaca9eb49f9f0916bbf62b8f414f22376d8f523b..0b961c2c1861285f89a5fecead61c64630715307 100644 (file)
@@ -45,7 +45,7 @@
 use core::mem;
 use core::ptr;
 use core::iter::FusedIterator;
-use core::unicode::str::{UnicodeStr, Utf16Encoder};
+use core::unicode::Utf16Encoder;
 
 use vec_deque::VecDeque;
 use borrow::{Borrow, ToOwned};
@@ -74,7 +74,7 @@
 #[stable(feature = "rust1", since = "1.0.0")]
 pub use core::str::{from_utf8_unchecked, from_utf8_unchecked_mut, ParseBoolError};
 #[stable(feature = "rust1", since = "1.0.0")]
-pub use core::unicode::str::SplitWhitespace;
+pub use core::str::SplitWhitespace;
 #[stable(feature = "rust1", since = "1.0.0")]
 pub use core::str::pattern;
 
@@ -800,7 +800,7 @@ pub fn bytes(&self) -> Bytes {
     #[stable(feature = "split_whitespace", since = "1.1.0")]
     #[inline]
     pub fn split_whitespace(&self) -> SplitWhitespace {
-        UnicodeStr::split_whitespace(self)
+        StrExt::split_whitespace(self)
     }
 
     /// An iterator over the lines of a string, as string slices.
@@ -1570,7 +1570,7 @@ pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a,
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn trim(&self) -> &str {
-        UnicodeStr::trim(self)
+        StrExt::trim(self)
     }
 
     /// Returns a string slice with leading whitespace removed.
@@ -1606,7 +1606,7 @@ pub fn trim(&self) -> &str {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn trim_left(&self) -> &str {
-        UnicodeStr::trim_left(self)
+        StrExt::trim_left(self)
     }
 
     /// Returns a string slice with trailing whitespace removed.
@@ -1642,7 +1642,7 @@ pub fn trim_left(&self) -> &str {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn trim_right(&self) -> &str {
-        UnicodeStr::trim_right(self)
+        StrExt::trim_right(self)
     }
 
     /// Returns a string slice with all prefixes and suffixes that match a
@@ -2141,7 +2141,7 @@ pub fn repeat(&self, n: usize) -> String {
     #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
     #[inline]
     pub fn is_whitespace(&self) -> bool {
-        UnicodeStr::is_whitespace(self)
+        StrExt::is_whitespace(self)
     }
 
     /// Returns true if this `str` is entirely alphanumeric, and false otherwise.
@@ -2160,7 +2160,7 @@ pub fn is_whitespace(&self) -> bool {
     #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
     #[inline]
     pub fn is_alphanumeric(&self) -> bool {
-        UnicodeStr::is_alphanumeric(self)
+        StrExt::is_alphanumeric(self)
     }
 
     /// Checks if all characters in this string are within the ASCII range.
index 763dbe675b91d63a46cde708bc4c64b5666cf3e0..2df8ca63a3eeb4b5a570afe3c4ccf49285f9f925 100644 (file)
@@ -1204,7 +1204,7 @@ fn test_rev_split_char_iterator_no_trailing() {
 
 #[test]
 fn test_utf16_code_units() {
-    use core::unicode::str::Utf16Encoder;
+    use core::unicode::Utf16Encoder;
     assert_eq!(Utf16Encoder::new(vec!['é', '\u{1F4A9}'].into_iter()).collect::<Vec<u16>>(),
                [0xE9, 0xD83D, 0xDCA9])
 }
index 7a97d89dcf9678ab2e8e968ad763c5505af883af..f1fe23092de93924f2c1549758bcb84462cfc08b 100644 (file)
@@ -19,7 +19,7 @@
 
 use char;
 use fmt;
-use iter::{Map, Cloned, FusedIterator, TrustedLen};
+use iter::{Map, Cloned, FusedIterator, TrustedLen, Filter};
 use iter_private::TrustedRandomAccess;
 use slice::{self, SliceIndex};
 use mem;
@@ -2216,6 +2216,18 @@ fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
     fn is_empty(&self) -> bool;
     #[stable(feature = "core", since = "1.6.0")]
     fn parse<T: FromStr>(&self) -> Result<T, T::Err>;
+    #[stable(feature = "split_whitespace", since = "1.1.0")]
+    fn split_whitespace<'a>(&'a self) -> SplitWhitespace<'a>;
+    #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
+    fn is_whitespace(&self) -> bool;
+    #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
+    fn is_alphanumeric(&self) -> bool;
+    #[stable(feature = "rust1", since = "1.0.0")]
+    fn trim(&self) -> &str;
+    #[stable(feature = "rust1", since = "1.0.0")]
+    fn trim_left(&self) -> &str;
+    #[stable(feature = "rust1", since = "1.0.0")]
+    fn trim_right(&self) -> &str;
 }
 
 // truncate `&str` to length at most equal to `max`
@@ -2536,6 +2548,36 @@ fn is_empty(&self) -> bool { self.len() == 0 }
 
     #[inline]
     fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
+
+    #[inline]
+    fn split_whitespace(&self) -> SplitWhitespace {
+        SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
+    }
+
+    #[inline]
+    fn is_whitespace(&self) -> bool {
+        self.chars().all(|c| c.is_whitespace())
+    }
+
+    #[inline]
+    fn is_alphanumeric(&self) -> bool {
+        self.chars().all(|c| c.is_alphanumeric())
+    }
+
+    #[inline]
+    fn trim(&self) -> &str {
+        self.trim_matches(|c: char| c.is_whitespace())
+    }
+
+    #[inline]
+    fn trim_left(&self) -> &str {
+        self.trim_left_matches(|c: char| c.is_whitespace())
+    }
+
+    #[inline]
+    fn trim_right(&self) -> &str {
+        self.trim_right_matches(|c: char| c.is_whitespace())
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -2551,3 +2593,75 @@ impl<'a> Default for &'a str {
     /// Creates an empty str
     fn default() -> &'a str { "" }
 }
+
+/// An iterator over the non-whitespace substrings of a string,
+/// separated by any amount of whitespace.
+///
+/// This struct is created by the [`split_whitespace`] method on [`str`].
+/// See its documentation for more.
+///
+/// [`split_whitespace`]: ../../std/primitive.str.html#method.split_whitespace
+/// [`str`]: ../../std/primitive.str.html
+#[stable(feature = "split_whitespace", since = "1.1.0")]
+#[derive(Clone, Debug)]
+pub struct SplitWhitespace<'a> {
+    inner: Filter<Split<'a, IsWhitespace>, IsNotEmpty>,
+}
+
+#[derive(Clone)]
+struct IsWhitespace;
+
+impl FnOnce<(char, )> for IsWhitespace {
+    type Output = bool;
+
+    #[inline]
+    extern "rust-call" fn call_once(mut self, arg: (char, )) -> bool {
+        self.call_mut(arg)
+    }
+}
+
+impl FnMut<(char, )> for IsWhitespace {
+    #[inline]
+    extern "rust-call" fn call_mut(&mut self, arg: (char, )) -> bool {
+        arg.0.is_whitespace()
+    }
+}
+
+#[derive(Clone)]
+struct IsNotEmpty;
+
+impl<'a, 'b> FnOnce<(&'a &'b str, )> for IsNotEmpty {
+    type Output = bool;
+
+    #[inline]
+    extern "rust-call" fn call_once(mut self, arg: (&&str, )) -> bool {
+        self.call_mut(arg)
+    }
+}
+
+impl<'a, 'b> FnMut<(&'a &'b str, )> for IsNotEmpty {
+    #[inline]
+    extern "rust-call" fn call_mut(&mut self, arg: (&&str, )) -> bool {
+        !arg.0.is_empty()
+    }
+}
+
+
+#[stable(feature = "split_whitespace", since = "1.1.0")]
+impl<'a> Iterator for SplitWhitespace<'a> {
+    type Item = &'a str;
+
+    fn next(&mut self) -> Option<&'a str> {
+        self.inner.next()
+    }
+}
+
+#[stable(feature = "split_whitespace", since = "1.1.0")]
+impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
+    fn next_back(&mut self) -> Option<&'a str> {
+        self.inner.next_back()
+    }
+}
+
+#[stable(feature = "fused", since = "1.26.0")]
+impl<'a> FusedIterator for SplitWhitespace<'a> {}
index 060c55286fecf592ba120330e174d789c09454c6..3413476fd2288bb9b83fb3b9678e0188f8647d0c 100644 (file)
@@ -15,8 +15,6 @@
 pub(crate) mod tables;
 pub(crate) mod version;
 
-pub mod str;
-
 // For use in liballoc, not re-exported in libstd.
 pub mod derived_property {
     pub use unicode::tables::derived_property::{Case_Ignorable, Cased};
@@ -26,3 +24,61 @@ pub mod derived_property {
 pub mod property {
     pub use unicode::tables::property::Pattern_White_Space;
 }
+
+use iter::FusedIterator;
+
+/// Iterator adaptor for encoding `char`s to UTF-16.
+#[derive(Clone)]
+#[allow(missing_debug_implementations)]
+pub struct Utf16Encoder<I> {
+    chars: I,
+    extra: u16,
+}
+
+impl<I> Utf16Encoder<I> {
+    /// Create a UTF-16 encoder from any `char` iterator.
+    pub fn new(chars: I) -> Utf16Encoder<I>
+        where I: Iterator<Item = char>
+    {
+        Utf16Encoder {
+            chars,
+            extra: 0,
+        }
+    }
+}
+
+impl<I> Iterator for Utf16Encoder<I>
+    where I: Iterator<Item = char>
+{
+    type Item = u16;
+
+    #[inline]
+    fn next(&mut self) -> Option<u16> {
+        if self.extra != 0 {
+            let tmp = self.extra;
+            self.extra = 0;
+            return Some(tmp);
+        }
+
+        let mut buf = [0; 2];
+        self.chars.next().map(|ch| {
+            let n = ch.encode_utf16(&mut buf).len();
+            if n == 2 {
+                self.extra = buf[1];
+            }
+            buf[0]
+        })
+    }
+
+    #[inline]
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        let (low, high) = self.chars.size_hint();
+        // every char gets either one u16 or two u16,
+        // so this iterator is between 1 or 2 times as
+        // long as the underlying iterator.
+        (low, high.and_then(|n| n.checked_mul(2)))
+    }
+}
+
+impl<I> FusedIterator for Utf16Encoder<I>
+    where I: FusedIterator<Item = char> {}
diff --git a/src/libcore/unicode/str.rs b/src/libcore/unicode/str.rs
deleted file mode 100644 (file)
index 0882984..0000000
+++ /dev/null
@@ -1,186 +0,0 @@
-// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Unicode-intensive string manipulations.
-
-use char;
-use iter::{Filter, FusedIterator};
-use str::Split;
-
-/// An iterator over the non-whitespace substrings of a string,
-/// separated by any amount of whitespace.
-///
-/// This struct is created by the [`split_whitespace`] method on [`str`].
-/// See its documentation for more.
-///
-/// [`split_whitespace`]: ../../std/primitive.str.html#method.split_whitespace
-/// [`str`]: ../../std/primitive.str.html
-#[stable(feature = "split_whitespace", since = "1.1.0")]
-#[derive(Clone, Debug)]
-pub struct SplitWhitespace<'a> {
-    inner: Filter<Split<'a, IsWhitespace>, IsNotEmpty>,
-}
-
-/// Methods for Unicode string slices
-#[allow(missing_docs)] // docs in liballoc
-pub trait UnicodeStr {
-    fn split_whitespace<'a>(&'a self) -> SplitWhitespace<'a>;
-    fn is_whitespace(&self) -> bool;
-    fn is_alphanumeric(&self) -> bool;
-    fn trim(&self) -> &str;
-    fn trim_left(&self) -> &str;
-    fn trim_right(&self) -> &str;
-}
-
-impl UnicodeStr for str {
-    #[inline]
-    fn split_whitespace(&self) -> SplitWhitespace {
-        SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
-    }
-
-    #[inline]
-    fn is_whitespace(&self) -> bool {
-        self.chars().all(|c| c.is_whitespace())
-    }
-
-    #[inline]
-    fn is_alphanumeric(&self) -> bool {
-        self.chars().all(|c| c.is_alphanumeric())
-    }
-
-    #[inline]
-    fn trim(&self) -> &str {
-        self.trim_matches(|c: char| c.is_whitespace())
-    }
-
-    #[inline]
-    fn trim_left(&self) -> &str {
-        self.trim_left_matches(|c: char| c.is_whitespace())
-    }
-
-    #[inline]
-    fn trim_right(&self) -> &str {
-        self.trim_right_matches(|c: char| c.is_whitespace())
-    }
-}
-
-/// Iterator adaptor for encoding `char`s to UTF-16.
-#[derive(Clone)]
-#[allow(missing_debug_implementations)]
-pub struct Utf16Encoder<I> {
-    chars: I,
-    extra: u16,
-}
-
-impl<I> Utf16Encoder<I> {
-    /// Create a UTF-16 encoder from any `char` iterator.
-    pub fn new(chars: I) -> Utf16Encoder<I>
-        where I: Iterator<Item = char>
-    {
-        Utf16Encoder {
-            chars,
-            extra: 0,
-        }
-    }
-}
-
-impl<I> Iterator for Utf16Encoder<I>
-    where I: Iterator<Item = char>
-{
-    type Item = u16;
-
-    #[inline]
-    fn next(&mut self) -> Option<u16> {
-        if self.extra != 0 {
-            let tmp = self.extra;
-            self.extra = 0;
-            return Some(tmp);
-        }
-
-        let mut buf = [0; 2];
-        self.chars.next().map(|ch| {
-            let n = ch.encode_utf16(&mut buf).len();
-            if n == 2 {
-                self.extra = buf[1];
-            }
-            buf[0]
-        })
-    }
-
-    #[inline]
-    fn size_hint(&self) -> (usize, Option<usize>) {
-        let (low, high) = self.chars.size_hint();
-        // every char gets either one u16 or two u16,
-        // so this iterator is between 1 or 2 times as
-        // long as the underlying iterator.
-        (low, high.and_then(|n| n.checked_mul(2)))
-    }
-}
-
-impl<I> FusedIterator for Utf16Encoder<I>
-    where I: FusedIterator<Item = char> {}
-
-#[derive(Clone)]
-struct IsWhitespace;
-
-impl FnOnce<(char, )> for IsWhitespace {
-    type Output = bool;
-
-    #[inline]
-    extern "rust-call" fn call_once(mut self, arg: (char, )) -> bool {
-        self.call_mut(arg)
-    }
-}
-
-impl FnMut<(char, )> for IsWhitespace {
-    #[inline]
-    extern "rust-call" fn call_mut(&mut self, arg: (char, )) -> bool {
-        arg.0.is_whitespace()
-    }
-}
-
-#[derive(Clone)]
-struct IsNotEmpty;
-
-impl<'a, 'b> FnOnce<(&'a &'b str, )> for IsNotEmpty {
-    type Output = bool;
-
-    #[inline]
-    extern "rust-call" fn call_once(mut self, arg: (&&str, )) -> bool {
-        self.call_mut(arg)
-    }
-}
-
-impl<'a, 'b> FnMut<(&'a &'b str, )> for IsNotEmpty {
-    #[inline]
-    extern "rust-call" fn call_mut(&mut self, arg: (&&str, )) -> bool {
-        !arg.0.is_empty()
-    }
-}
-
-
-#[stable(feature = "split_whitespace", since = "1.1.0")]
-impl<'a> Iterator for SplitWhitespace<'a> {
-    type Item = &'a str;
-
-    fn next(&mut self) -> Option<&'a str> {
-        self.inner.next()
-    }
-}
-
-#[stable(feature = "split_whitespace", since = "1.1.0")]
-impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
-    fn next_back(&mut self) -> Option<&'a str> {
-        self.inner.next_back()
-    }
-}
-
-#[stable(feature = "fused", since = "1.26.0")]
-impl<'a> FusedIterator for SplitWhitespace<'a> {}