]> git.lizzy.rs Git - rust.git/commitdiff
Merge unstable Utf16Encoder into EncodeUtf16
authorSimon Sapin <simon.sapin@exyr.org>
Fri, 6 Apr 2018 08:24:01 +0000 (10:24 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Wed, 11 Apr 2018 22:13:53 +0000 (00:13 +0200)
src/liballoc/str.rs
src/liballoc/tests/str.rs
src/libcore/unicode/mod.rs

index 0b961c2c1861285f89a5fecead61c64630715307..65df93bd3bb54da1845a9e61e363471ed1a52e56 100644 (file)
@@ -45,7 +45,6 @@
 use core::mem;
 use core::ptr;
 use core::iter::FusedIterator;
-use core::unicode::Utf16Encoder;
 
 use vec_deque::VecDeque;
 use borrow::{Borrow, ToOwned};
@@ -146,7 +145,8 @@ fn connect(&self, sep: &str) -> String {
 #[derive(Clone)]
 #[stable(feature = "encode_utf16", since = "1.8.0")]
 pub struct EncodeUtf16<'a> {
-    encoder: Utf16Encoder<Chars<'a>>,
+    chars: Chars<'a>,
+    extra: u16,
 }
 
 #[stable(feature = "collection_debug", since = "1.17.0")]
@@ -162,12 +162,29 @@ impl<'a> Iterator for EncodeUtf16<'a> {
 
     #[inline]
     fn next(&mut self) -> Option<u16> {
-        self.encoder.next()
+        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>) {
-        self.encoder.size_hint()
+        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)))
     }
 }
 
@@ -870,7 +887,7 @@ pub fn lines_any(&self) -> LinesAny {
     /// ```
     #[stable(feature = "encode_utf16", since = "1.8.0")]
     pub fn encode_utf16(&self) -> EncodeUtf16 {
-        EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) }
+        EncodeUtf16 { chars: self[..].chars(), extra: 0 }
     }
 
     /// Returns `true` if the given pattern matches a sub-slice of
index 2df8ca63a3eeb4b5a570afe3c4ccf49285f9f925..a3f4c385fe23b731b52f329312790a1ba494253b 100644 (file)
@@ -1204,8 +1204,7 @@ fn test_rev_split_char_iterator_no_trailing() {
 
 #[test]
 fn test_utf16_code_units() {
-    use core::unicode::Utf16Encoder;
-    assert_eq!(Utf16Encoder::new(vec!['é', '\u{1F4A9}'].into_iter()).collect::<Vec<u16>>(),
+    assert_eq!("é\u{1F4A9}".encode_utf16().collect::<Vec<u16>>(),
                [0xE9, 0xD83D, 0xDCA9])
 }
 
index 3413476fd2288bb9b83fb3b9678e0188f8647d0c..9ab8cb748b10d37bfc5c195c58a3644c33ab322a 100644 (file)
@@ -24,61 +24,3 @@ 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> {}