MCPcopy Index your code
hub / github.com/RustPython/RustPython / encode_utf8_raw

Function encode_utf8_raw

crates/wtf8/src/core_char.rs:50–80  ·  view source on GitHub ↗
(code: u32, dst: &mut [u8])

Source from the content-addressed store, hash-verified

48#[doc(hidden)]
49#[inline]
50pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
51 let len = len_utf8(code);
52 match (len, &mut *dst) {
53 (1, [a, ..]) => {
54 *a = code as u8;
55 }
56 (2, [a, b, ..]) => {
57 *a = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
58 *b = (code & 0x3F) as u8 | TAG_CONT;
59 }
60 (3, [a, b, c, ..]) => {
61 *a = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
62 *b = (code >> 6 & 0x3F) as u8 | TAG_CONT;
63 *c = (code & 0x3F) as u8 | TAG_CONT;
64 }
65 (4, [a, b, c, d, ..]) => {
66 *a = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
67 *b = (code >> 12 & 0x3F) as u8 | TAG_CONT;
68 *c = (code >> 6 & 0x3F) as u8 | TAG_CONT;
69 *d = (code & 0x3F) as u8 | TAG_CONT;
70 }
71 _ => {
72 panic!(
73 "encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}",
74 dst_len = dst.len(),
75 )
76 }
77 };
78 // SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
79 unsafe { slice::from_raw_parts_mut(dst.as_mut_ptr(), len) }
80}
81
82/// Encodes a raw `u32` value as UTF-16 into the provided `u16` buffer,
83/// and then returns the subslice of the buffer that contains the encoded character.

Callers 1

encode_wtf8Method · 0.85

Calls 2

len_utf8Function · 0.85
as_mut_ptrMethod · 0.80

Tested by

no test coverage detected