1
1
//! impl char {}
2
2
3
3
use super :: * ;
4
- use crate :: intrinsics :: const_eval_select ;
4
+ use crate :: macros :: const_panic ;
5
5
use crate :: slice;
6
6
use crate :: str:: from_utf8_unchecked_mut;
7
7
use crate :: unicode:: printable:: is_printable;
@@ -1773,17 +1773,7 @@ const fn len_utf16(code: u32) -> usize {
1773
1773
#[ cfg_attr( bootstrap, rustc_const_stable( feature = "const_char_encode_utf8" , since = "1.83.0" ) ) ]
1774
1774
#[ doc( hidden) ]
1775
1775
#[ inline]
1776
- #[ rustc_allow_const_fn_unstable( const_eval_select) ]
1777
1776
pub const fn encode_utf8_raw ( code : u32 , dst : & mut [ u8 ] ) -> & mut [ u8 ] {
1778
- const fn panic_at_const ( _code : u32 , _len : usize , _dst_len : usize ) {
1779
- // Note that we cannot format in constant expressions.
1780
- panic ! ( "encode_utf8: buffer does not have enough bytes to encode code point" ) ;
1781
- }
1782
- fn panic_at_rt ( code : u32 , len : usize , dst_len : usize ) {
1783
- panic ! (
1784
- "encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1785
- ) ;
1786
- }
1787
1777
let len = len_utf8 ( code) ;
1788
1778
match ( len, & mut * dst) {
1789
1779
( 1 , [ a, ..] ) => {
@@ -1804,8 +1794,15 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
1804
1794
* c = ( code >> 6 & 0x3F ) as u8 | TAG_CONT ;
1805
1795
* d = ( code & 0x3F ) as u8 | TAG_CONT ;
1806
1796
}
1807
- // FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
1808
- _ => const_eval_select ( ( code, len, dst. len ( ) ) , panic_at_const, panic_at_rt) ,
1797
+ _ => {
1798
+ const_panic ! (
1799
+ "encode_utf8: buffer does not have enough bytes to encode code point" ,
1800
+ "encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1801
+ code: u32 = code,
1802
+ len: usize = len,
1803
+ dst_len: usize = dst. len( ) ,
1804
+ )
1805
+ }
1809
1806
} ;
1810
1807
// SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
1811
1808
unsafe { slice:: from_raw_parts_mut ( dst. as_mut_ptr ( ) , len) }
@@ -1826,15 +1823,6 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
1826
1823
#[ doc( hidden) ]
1827
1824
#[ inline]
1828
1825
pub const fn encode_utf16_raw ( mut code : u32 , dst : & mut [ u16 ] ) -> & mut [ u16 ] {
1829
- const fn panic_at_const ( _code : u32 , _len : usize , _dst_len : usize ) {
1830
- // Note that we cannot format in constant expressions.
1831
- panic ! ( "encode_utf16: buffer does not have enough bytes to encode code point" ) ;
1832
- }
1833
- fn panic_at_rt ( code : u32 , len : usize , dst_len : usize ) {
1834
- panic ! (
1835
- "encode_utf16: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1836
- ) ;
1837
- }
1838
1826
let len = len_utf16 ( code) ;
1839
1827
match ( len, & mut * dst) {
1840
1828
( 1 , [ a, ..] ) => {
@@ -1845,8 +1833,15 @@ pub const fn encode_utf16_raw(mut code: u32, dst: &mut [u16]) -> &mut [u16] {
1845
1833
* a = ( code >> 10 ) as u16 | 0xD800 ;
1846
1834
* b = ( code & 0x3FF ) as u16 | 0xDC00 ;
1847
1835
}
1848
- // FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
1849
- _ => const_eval_select ( ( code, len, dst. len ( ) ) , panic_at_const, panic_at_rt) ,
1836
+ _ => {
1837
+ const_panic ! (
1838
+ "encode_utf16: buffer does not have enough bytes to encode code point" ,
1839
+ "encode_utf16: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}" ,
1840
+ code: u32 = code,
1841
+ len: usize = len,
1842
+ dst_len: usize = dst. len( ) ,
1843
+ )
1844
+ }
1850
1845
} ;
1851
1846
// SAFETY: `<&mut [u16]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
1852
1847
unsafe { slice:: from_raw_parts_mut ( dst. as_mut_ptr ( ) , len) }
0 commit comments