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

Function encode_utf16_raw

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

Source from the content-addressed store, hash-verified

92#[doc(hidden)]
93#[inline]
94pub fn encode_utf16_raw(mut code: u32, dst: &mut [u16]) -> &mut [u16] {
95 let len = len_utf16(code);
96 match (len, &mut *dst) {
97 (1, [a, ..]) => {
98 *a = code as u16;
99 }
100 (2, [a, b, ..]) => {
101 code -= 0x1_0000;
102 *a = (code >> 10) as u16 | 0xD800;
103 *b = (code & 0x3FF) as u16 | 0xDC00;
104 }
105 _ => {
106 panic!(
107 "encode_utf16: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}",
108 dst_len = dst.len(),
109 )
110 }
111 };
112 // SAFETY: `<&mut [u16]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
113 unsafe { slice::from_raw_parts_mut(dst.as_mut_ptr(), len) }
114}

Callers 1

nextMethod · 0.85

Calls 2

len_utf16Function · 0.85
as_mut_ptrMethod · 0.80

Tested by

no test coverage detected