Skip to content

Commit a52a941

Browse files
committed
Enable 'const_char_encode_utf8' feature; Refactor 'encode_utf8_raw'; Add safety comments;
1 parent cc96551 commit a52a941

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

library/core/src/char/methods.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! impl char {}
22
33
use super::*;
4-
use crate::hint::unreachable_unchecked;
54
use crate::slice;
65
use crate::str::from_utf8_unchecked_mut;
76
use crate::unicode::printable::is_printable;
@@ -1767,32 +1766,29 @@ const fn len_utf8(code: u32) -> usize {
17671766
#[inline]
17681767
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
17691768
let len = len_utf8(code);
1770-
// Note that the original message is not const-compatible due to formatting.
1771-
assert!(
1772-
len <= dst.len(),
1773-
"encode_utf8: buffer does not have enough bytes to encode code point",
1774-
);
1775-
match len {
1776-
1 => {
1777-
dst[0x0] = code as u8;
1769+
match (len, &mut *dst) {
1770+
(1, [a, ..]) => {
1771+
*a = code as u8;
17781772
}
1779-
2 => {
1780-
dst[0x0] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
1781-
dst[0x1] = (code & 0x3F) as u8 | TAG_CONT;
1773+
(2, [a, b, ..]) => {
1774+
*a = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
1775+
*b = (code & 0x3F) as u8 | TAG_CONT;
17821776
}
1783-
3 => {
1784-
dst[0x0] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
1785-
dst[0x1] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1786-
dst[0x2] = (code & 0x3F) as u8 | TAG_CONT;
1777+
(3, [a, b, c, ..]) => {
1778+
*a = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
1779+
*b = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1780+
*c = (code & 0x3F) as u8 | TAG_CONT;
17871781
}
1788-
4 => {
1789-
dst[0x0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
1790-
dst[0x1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
1791-
dst[0x2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1792-
dst[0x3] = (code & 0x3F) as u8 | TAG_CONT;
1782+
(4, [a, b, c, d, ..]) => {
1783+
*a = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
1784+
*b = (code >> 12 & 0x3F) as u8 | TAG_CONT;
1785+
*c = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1786+
*d = (code & 0x3F) as u8 | TAG_CONT;
17931787
}
1794-
_ => unsafe { unreachable_unchecked() },
1788+
// Note that the original message is not const-compatible due to formatting.
1789+
_ => panic!("encode_utf8: buffer does not have enough bytes to encode code point"),
17951790
};
1791+
// SAFETY: `<&mut [u8]>::as_mut_ptr` returns a valid pointer and `len` has been tested to be valid.
17961792
unsafe { slice::from_raw_parts_mut(dst.as_mut_ptr(), len) }
17971793
}
17981794

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
#![feature(const_array_into_iter_constructors)]
119119
#![feature(const_bigint_helper_methods)]
120120
#![feature(const_black_box)]
121+
#![feature(const_char_encode_utf8)]
121122
#![feature(const_cell_into_inner)]
122123
#![feature(const_eval_select)]
123124
#![feature(const_exact_div)]

0 commit comments

Comments
 (0)