1313//! This module provides functionality to `str` that requires the Unicode methods provided by the
1414//! unicode parts of the CharExt trait.
1515
16+ use char:: { DecodeUtf16 , decode_utf16} ;
1617use core:: char;
17- use core:: iter:: Filter ;
18+ use core:: iter:: { Cloned , Filter } ;
1819use core:: slice;
1920use core:: str:: Split ;
2021
@@ -119,11 +120,18 @@ pub fn is_utf16(v: &[u16]) -> bool {
119120
120121/// An iterator that decodes UTF-16 encoded codepoints from a vector
121122/// of `u16`s.
123+ #[ deprecated( since = "1.4.0" , reason = "renamed to `char::DecodeUtf16`" ) ]
124+ #[ unstable( feature = "decode_utf16" , reason = "not exposed in std" , issue = "27830" ) ]
125+ #[ allow( deprecated) ]
122126#[ derive( Clone ) ]
123127pub struct Utf16Items < ' a > {
124- iter : slice:: Iter < ' a , u16 >
128+ decoder : DecodeUtf16 < Cloned < slice:: Iter < ' a , u16 > > >
125129}
130+
126131/// The possibilities for values decoded from a `u16` stream.
132+ #[ deprecated( since = "1.4.0" , reason = "`char::DecodeUtf16` uses `Result<char, u16>` instead" ) ]
133+ #[ unstable( feature = "decode_utf16" , reason = "not exposed in std" , issue = "27830" ) ]
134+ #[ allow( deprecated) ]
127135#[ derive( Copy , PartialEq , Eq , Clone , Debug ) ]
128136pub enum Utf16Item {
129137 /// A valid codepoint.
@@ -132,6 +140,7 @@ pub enum Utf16Item {
132140 LoneSurrogate ( u16 )
133141}
134142
143+ #[ allow( deprecated) ]
135144impl Utf16Item {
136145 /// Convert `self` to a `char`, taking `LoneSurrogate`s to the
137146 /// replacement character (U+FFFD).
@@ -144,49 +153,22 @@ impl Utf16Item {
144153 }
145154}
146155
156+ #[ deprecated( since = "1.4.0" , reason = "use `char::DecodeUtf16` instead" ) ]
157+ #[ unstable( feature = "decode_utf16" , reason = "not exposed in std" , issue = "27830" ) ]
158+ #[ allow( deprecated) ]
147159impl < ' a > Iterator for Utf16Items < ' a > {
148160 type Item = Utf16Item ;
149161
150162 fn next ( & mut self ) -> Option < Utf16Item > {
151- let u = match self . iter . next ( ) {
152- Some ( u) => * u,
153- None => return None
154- } ;
155-
156- if u < 0xD800 || 0xDFFF < u {
157- // not a surrogate
158- Some ( Utf16Item :: ScalarValue ( unsafe { char:: from_u32_unchecked ( u as u32 ) } ) )
159- } else if u >= 0xDC00 {
160- // a trailing surrogate
161- Some ( Utf16Item :: LoneSurrogate ( u) )
162- } else {
163- // preserve state for rewinding.
164- let old = self . iter . clone ( ) ;
165-
166- let u2 = match self . iter . next ( ) {
167- Some ( u2) => * u2,
168- // eof
169- None => return Some ( Utf16Item :: LoneSurrogate ( u) )
170- } ;
171- if u2 < 0xDC00 || u2 > 0xDFFF {
172- // not a trailing surrogate so we're not a valid
173- // surrogate pair, so rewind to redecode u2 next time.
174- self . iter = old. clone ( ) ;
175- return Some ( Utf16Item :: LoneSurrogate ( u) )
176- }
177-
178- // all ok, so lets decode it.
179- let c = ( ( ( u - 0xD800 ) as u32 ) << 10 | ( u2 - 0xDC00 ) as u32 ) + 0x1_0000 ;
180- Some ( Utf16Item :: ScalarValue ( unsafe { char:: from_u32_unchecked ( c) } ) )
181- }
163+ self . decoder . next ( ) . map ( |result| match result {
164+ Ok ( c) => Utf16Item :: ScalarValue ( c) ,
165+ Err ( s) => Utf16Item :: LoneSurrogate ( s) ,
166+ } )
182167 }
183168
184169 #[ inline]
185170 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
186- let ( low, high) = self . iter . size_hint ( ) ;
187- // we could be entirely valid surrogates (2 elements per
188- // char), or entirely non-surrogates (1 element per char)
189- ( low / 2 , high)
171+ self . decoder . size_hint ( )
190172 }
191173}
192174
@@ -196,7 +178,7 @@ impl<'a> Iterator for Utf16Items<'a> {
196178/// # Examples
197179///
198180/// ```
199- /// #![feature(unicode)]
181+ /// #![feature(unicode, decode_utf16 )]
200182///
201183/// extern crate rustc_unicode;
202184///
@@ -216,8 +198,11 @@ impl<'a> Iterator for Utf16Items<'a> {
216198/// LoneSurrogate(0xD834)]);
217199/// }
218200/// ```
201+ #[ deprecated( since = "1.4.0" , reason = "renamed to `char::decode_utf16`" ) ]
202+ #[ unstable( feature = "decode_utf16" , reason = "not exposed in std" , issue = "27830" ) ]
203+ #[ allow( deprecated) ]
219204pub fn utf16_items < ' a > ( v : & ' a [ u16 ] ) -> Utf16Items < ' a > {
220- Utf16Items { iter : v. iter ( ) }
205+ Utf16Items { decoder : decode_utf16 ( v. iter ( ) . cloned ( ) ) }
221206}
222207
223208/// Iterator adaptor for encoding `char`s to UTF-16.
0 commit comments