@@ -787,12 +787,12 @@ impl String {
787
787
#[ cfg( not( no_global_oom_handling) ) ]
788
788
#[ unstable( feature = "str_from_utf16_endian" , issue = "116258" ) ]
789
789
pub fn from_utf16le ( v : & [ u8 ] ) -> Result < String , FromUtf16Error > {
790
- if v . len ( ) % 2 != 0 {
790
+ let ( chunks , [ ] ) = v . as_chunks :: < 2 > ( ) else {
791
791
return Err ( FromUtf16Error ( ( ) ) ) ;
792
- }
792
+ } ;
793
793
match ( cfg ! ( target_endian = "little" ) , unsafe { v. align_to :: < u16 > ( ) } ) {
794
794
( true , ( [ ] , v, [ ] ) ) => Self :: from_utf16 ( v) ,
795
- _ => char:: decode_utf16 ( v . array_chunks :: < 2 > ( ) . copied ( ) . map ( u16:: from_le_bytes) )
795
+ _ => char:: decode_utf16 ( chunks . iter ( ) . copied ( ) . map ( u16:: from_le_bytes) )
796
796
. collect :: < Result < _ , _ > > ( )
797
797
. map_err ( |_| FromUtf16Error ( ( ) ) ) ,
798
798
}
@@ -830,11 +830,11 @@ impl String {
830
830
( true , ( [ ] , v, [ ] ) ) => Self :: from_utf16_lossy ( v) ,
831
831
( true , ( [ ] , v, [ _remainder] ) ) => Self :: from_utf16_lossy ( v) + "\u{FFFD} " ,
832
832
_ => {
833
- let mut iter = v. array_chunks :: < 2 > ( ) ;
834
- let string = char:: decode_utf16 ( iter . by_ref ( ) . copied ( ) . map ( u16:: from_le_bytes) )
833
+ let ( chunks , remainder ) = v. as_chunks :: < 2 > ( ) ;
834
+ let string = char:: decode_utf16 ( chunks . iter ( ) . copied ( ) . map ( u16:: from_le_bytes) )
835
835
. map ( |r| r. unwrap_or ( char:: REPLACEMENT_CHARACTER ) )
836
836
. collect ( ) ;
837
- if iter . remainder ( ) . is_empty ( ) { string } else { string + "\u{FFFD} " }
837
+ if remainder. is_empty ( ) { string } else { string + "\u{FFFD} " }
838
838
}
839
839
}
840
840
}
@@ -862,12 +862,12 @@ impl String {
862
862
#[ cfg( not( no_global_oom_handling) ) ]
863
863
#[ unstable( feature = "str_from_utf16_endian" , issue = "116258" ) ]
864
864
pub fn from_utf16be ( v : & [ u8 ] ) -> Result < String , FromUtf16Error > {
865
- if v . len ( ) % 2 != 0 {
865
+ let ( chunks , [ ] ) = v . as_chunks :: < 2 > ( ) else {
866
866
return Err ( FromUtf16Error ( ( ) ) ) ;
867
- }
867
+ } ;
868
868
match ( cfg ! ( target_endian = "big" ) , unsafe { v. align_to :: < u16 > ( ) } ) {
869
869
( true , ( [ ] , v, [ ] ) ) => Self :: from_utf16 ( v) ,
870
- _ => char:: decode_utf16 ( v . array_chunks :: < 2 > ( ) . copied ( ) . map ( u16:: from_be_bytes) )
870
+ _ => char:: decode_utf16 ( chunks . iter ( ) . copied ( ) . map ( u16:: from_be_bytes) )
871
871
. collect :: < Result < _ , _ > > ( )
872
872
. map_err ( |_| FromUtf16Error ( ( ) ) ) ,
873
873
}
@@ -905,11 +905,11 @@ impl String {
905
905
( true , ( [ ] , v, [ ] ) ) => Self :: from_utf16_lossy ( v) ,
906
906
( true , ( [ ] , v, [ _remainder] ) ) => Self :: from_utf16_lossy ( v) + "\u{FFFD} " ,
907
907
_ => {
908
- let mut iter = v. array_chunks :: < 2 > ( ) ;
909
- let string = char:: decode_utf16 ( iter . by_ref ( ) . copied ( ) . map ( u16:: from_be_bytes) )
908
+ let ( chunks , remainder ) = v. as_chunks :: < 2 > ( ) ;
909
+ let string = char:: decode_utf16 ( chunks . iter ( ) . copied ( ) . map ( u16:: from_be_bytes) )
910
910
. map ( |r| r. unwrap_or ( char:: REPLACEMENT_CHARACTER ) )
911
911
. collect ( ) ;
912
- if iter . remainder ( ) . is_empty ( ) { string } else { string + "\u{FFFD} " }
912
+ if remainder. is_empty ( ) { string } else { string + "\u{FFFD} " }
913
913
}
914
914
}
915
915
}
0 commit comments