@@ -560,11 +560,19 @@ pub trait Char {
560
560
561
561
/// Encodes this character as UTF-8 into the provided byte buffer.
562
562
///
563
- /// The buffer must be at least 4 bytes long or a runtime failure will
563
+ /// The buffer must be at least 4 bytes long or a runtime failure may
564
564
/// occur.
565
565
///
566
- /// This will then return the number of characters written to the slice.
566
+ /// This will then return the number of bytes written to the slice.
567
567
fn encode_utf8 ( & self , dst : & mut [ u8 ] ) -> uint ;
568
+
569
+ /// Encodes this character as UTF-16 into the provided `u16` buffer.
570
+ ///
571
+ /// The buffer must be at least 2 elements long or a runtime failure may
572
+ /// occur.
573
+ ///
574
+ /// This will then return the number of `u16`s written to the slice.
575
+ fn encode_utf16 ( & self , dst : & mut [ u16 ] ) -> uint ;
568
576
}
569
577
570
578
impl Char for char {
@@ -602,7 +610,7 @@ impl Char for char {
602
610
603
611
fn len_utf8_bytes ( & self ) -> uint { len_utf8_bytes ( * self ) }
604
612
605
- fn encode_utf8 < ' a > ( & self , dst : & ' a mut [ u8 ] ) -> uint {
613
+ fn encode_utf8 ( & self , dst : & mut [ u8 ] ) -> uint {
606
614
let code = * self as uint ;
607
615
if code < MAX_ONE_B {
608
616
dst[ 0 ] = code as u8 ;
@@ -624,6 +632,24 @@ impl Char for char {
624
632
return 4 ;
625
633
}
626
634
}
635
+
636
+ fn encode_utf16 ( & self , dst : & mut [ u16 ] ) -> uint {
637
+ let mut ch = * self as uint ;
638
+ if ( ch & 0xFFFF_ u) == ch {
639
+ // The BMP falls through (assuming non-surrogate, as it
640
+ // should)
641
+ assert ! ( ch <= 0xD7FF_ u || ch >= 0xE000_ u) ;
642
+ dst[ 0 ] = ch as u16 ;
643
+ 1
644
+ } else {
645
+ // Supplementary planes break into surrogates.
646
+ assert ! ( ch >= 0x1_0000_ u && ch <= 0x10_FFFF_ u) ;
647
+ ch -= 0x1_0000_ u;
648
+ dst[ 0 ] = 0xD800_u16 | ( ( ch >> 10 ) as u16 ) ;
649
+ dst[ 1 ] = 0xDC00_u16 | ( ( ch as u16 ) & 0x3FF_u16 ) ;
650
+ 2
651
+ }
652
+ }
627
653
}
628
654
629
655
#[ cfg( not( test) ) ]
0 commit comments