@@ -672,8 +672,9 @@ impl char {
672
672
/// 'ß'.encode_utf8(&mut b);
673
673
/// ```
674
674
#[ stable( feature = "unicode_encode_char" , since = "1.15.0" ) ]
675
+ #[ rustc_const_unstable( feature = "const_char_encode_utf8" , issue = "130512" ) ]
675
676
#[ inline]
676
- pub fn encode_utf8 ( self , dst : & mut [ u8 ] ) -> & mut str {
677
+ pub const fn encode_utf8 ( self , dst : & mut [ u8 ] ) -> & mut str {
677
678
// SAFETY: `char` is not a surrogate, so this is valid UTF-8.
678
679
unsafe { from_utf8_unchecked_mut ( encode_utf8_raw ( self as u32 , dst) ) }
679
680
}
@@ -1735,14 +1736,11 @@ impl EscapeDebugExtArgs {
1735
1736
1736
1737
#[ inline]
1737
1738
const fn len_utf8 ( code : u32 ) -> usize {
1738
- if code < MAX_ONE_B {
1739
- 1
1740
- } else if code < MAX_TWO_B {
1741
- 2
1742
- } else if code < MAX_THREE_B {
1743
- 3
1744
- } else {
1745
- 4
1739
+ match code {
1740
+ ..MAX_ONE_B => 1 ,
1741
+ ..MAX_TWO_B => 2 ,
1742
+ ..MAX_THREE_B => 3 ,
1743
+ _ => 4 ,
1746
1744
}
1747
1745
}
1748
1746
@@ -1760,11 +1758,12 @@ const fn len_utf8(code: u32) -> usize {
1760
1758
/// Panics if the buffer is not large enough.
1761
1759
/// A buffer of length four is large enough to encode any `char`.
1762
1760
#[ unstable( feature = "char_internals" , reason = "exposed only for libstd" , issue = "none" ) ]
1761
+ #[ rustc_const_unstable( feature = "const_char_encode_utf8" , issue = "130512" ) ]
1763
1762
#[ doc( hidden) ]
1764
1763
#[ inline]
1765
- pub fn encode_utf8_raw ( code : u32 , dst : & mut [ u8 ] ) -> & mut [ u8 ] {
1764
+ pub const fn encode_utf8_raw ( code : u32 , dst : & mut [ u8 ] ) -> & mut [ u8 ] {
1766
1765
let len = len_utf8 ( code) ;
1767
- match ( len, & mut dst[ .. ] ) {
1766
+ match ( len, & mut * dst) {
1768
1767
( 1 , [ a, ..] ) => {
1769
1768
* a = code as u8 ;
1770
1769
}
@@ -1783,14 +1782,11 @@ pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
1783
1782
* c = ( code >> 6 & 0x3F ) as u8 | TAG_CONT ;
1784
1783
* d = ( code & 0x3F ) as u8 | TAG_CONT ;
1785
1784
}
1786
- _ => panic ! (
1787
- "encode_utf8: need {} bytes to encode U+{:X}, but the buffer has {}" ,
1788
- len,
1789
- code,
1790
- dst. len( ) ,
1791
- ) ,
1785
+ // Note that we cannot format in constant expressions.
1786
+ _ => panic ! ( "encode_utf8: buffer does not have enough bytes to encode code point" ) ,
1792
1787
} ;
1793
- & mut dst[ ..len]
1788
+ // SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
1789
+ unsafe { slice:: from_raw_parts_mut ( dst. as_mut_ptr ( ) , len) }
1794
1790
}
1795
1791
1796
1792
/// Encodes a raw u32 value as UTF-16 into the provided `u16` buffer,
0 commit comments