Skip to content

Commit d4ed1e6

Browse files
committed
Merge unstable Utf16Encoder into EncodeUtf16
1 parent 0d9afcd commit d4ed1e6

File tree

3 files changed

+23
-65
lines changed

3 files changed

+23
-65
lines changed

src/liballoc/str.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
4545
use core::mem;
4646
use core::ptr;
4747
use core::iter::FusedIterator;
48-
use core::unicode::Utf16Encoder;
4948

5049
use vec_deque::VecDeque;
5150
use borrow::{Borrow, ToOwned};
@@ -146,7 +145,8 @@ impl<S: Borrow<str>> SliceConcatExt<str> for [S] {
146145
#[derive(Clone)]
147146
#[stable(feature = "encode_utf16", since = "1.8.0")]
148147
pub struct EncodeUtf16<'a> {
149-
encoder: Utf16Encoder<Chars<'a>>,
148+
chars: Chars<'a>,
149+
extra: u16,
150150
}
151151

152152
#[stable(feature = "collection_debug", since = "1.17.0")]
@@ -162,12 +162,29 @@ impl<'a> Iterator for EncodeUtf16<'a> {
162162

163163
#[inline]
164164
fn next(&mut self) -> Option<u16> {
165-
self.encoder.next()
165+
if self.extra != 0 {
166+
let tmp = self.extra;
167+
self.extra = 0;
168+
return Some(tmp);
169+
}
170+
171+
let mut buf = [0; 2];
172+
self.chars.next().map(|ch| {
173+
let n = ch.encode_utf16(&mut buf).len();
174+
if n == 2 {
175+
self.extra = buf[1];
176+
}
177+
buf[0]
178+
})
166179
}
167180

168181
#[inline]
169182
fn size_hint(&self) -> (usize, Option<usize>) {
170-
self.encoder.size_hint()
183+
let (low, high) = self.chars.size_hint();
184+
// every char gets either one u16 or two u16,
185+
// so this iterator is between 1 or 2 times as
186+
// long as the underlying iterator.
187+
(low, high.and_then(|n| n.checked_mul(2)))
171188
}
172189
}
173190

@@ -870,7 +887,7 @@ impl str {
870887
/// ```
871888
#[stable(feature = "encode_utf16", since = "1.8.0")]
872889
pub fn encode_utf16(&self) -> EncodeUtf16 {
873-
EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) }
890+
EncodeUtf16 { chars: self[..].chars(), extra: 0 }
874891
}
875892

876893
/// Returns `true` if the given pattern matches a sub-slice of

src/liballoc/tests/str.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,7 @@ fn test_rev_split_char_iterator_no_trailing() {
12041204

12051205
#[test]
12061206
fn test_utf16_code_units() {
1207-
use core::unicode::Utf16Encoder;
1208-
assert_eq!(Utf16Encoder::new(vec!['é', '\u{1F4A9}'].into_iter()).collect::<Vec<u16>>(),
1207+
assert_eq!(\u{1F4A9}".encode_utf16().collect::<Vec<u16>>(),
12091208
[0xE9, 0xD83D, 0xDCA9])
12101209
}
12111210

src/libcore/unicode/mod.rs

-58
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,3 @@ pub mod derived_property {
2424
pub mod property {
2525
pub use unicode::tables::property::Pattern_White_Space;
2626
}
27-
28-
use iter::FusedIterator;
29-
30-
/// Iterator adaptor for encoding `char`s to UTF-16.
31-
#[derive(Clone)]
32-
#[allow(missing_debug_implementations)]
33-
pub struct Utf16Encoder<I> {
34-
chars: I,
35-
extra: u16,
36-
}
37-
38-
impl<I> Utf16Encoder<I> {
39-
/// Create a UTF-16 encoder from any `char` iterator.
40-
pub fn new(chars: I) -> Utf16Encoder<I>
41-
where I: Iterator<Item = char>
42-
{
43-
Utf16Encoder {
44-
chars,
45-
extra: 0,
46-
}
47-
}
48-
}
49-
50-
impl<I> Iterator for Utf16Encoder<I>
51-
where I: Iterator<Item = char>
52-
{
53-
type Item = u16;
54-
55-
#[inline]
56-
fn next(&mut self) -> Option<u16> {
57-
if self.extra != 0 {
58-
let tmp = self.extra;
59-
self.extra = 0;
60-
return Some(tmp);
61-
}
62-
63-
let mut buf = [0; 2];
64-
self.chars.next().map(|ch| {
65-
let n = ch.encode_utf16(&mut buf).len();
66-
if n == 2 {
67-
self.extra = buf[1];
68-
}
69-
buf[0]
70-
})
71-
}
72-
73-
#[inline]
74-
fn size_hint(&self) -> (usize, Option<usize>) {
75-
let (low, high) = self.chars.size_hint();
76-
// every char gets either one u16 or two u16,
77-
// so this iterator is between 1 or 2 times as
78-
// long as the underlying iterator.
79-
(low, high.and_then(|n| n.checked_mul(2)))
80-
}
81-
}
82-
83-
impl<I> FusedIterator for Utf16Encoder<I>
84-
where I: FusedIterator<Item = char> {}

0 commit comments

Comments
 (0)