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

Method _wchar_bytes_to_string

crates/stdlib/src/array.rs:776–806  ·  view source on GitHub ↗
(
            bytes: &[u8],
            item_size: usize,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

774 }
775
776 fn _wchar_bytes_to_string(
777 bytes: &[u8],
778 item_size: usize,
779 vm: &VirtualMachine,
780 ) -> PyResult<Wtf8Buf> {
781 if item_size == 2 {
782 // safe because every configuration of bytes for the types we support are valid
783 let utf16 = unsafe {
784 core::slice::from_raw_parts(
785 bytes.as_ptr() as *const u16,
786 bytes.len() / core::mem::size_of::<u16>(),
787 )
788 };
789 Ok(Wtf8Buf::from_wide(utf16))
790 } else {
791 // safe because every configuration of bytes for the types we support are valid
792 let chars = unsafe {
793 core::slice::from_raw_parts(
794 bytes.as_ptr() as *const u32,
795 bytes.len() / core::mem::size_of::<u32>(),
796 )
797 };
798 chars
799 .iter()
800 .map(|&ch| {
801 // cpython issue 17223
802 u32_to_char(ch).map_err(|msg| vm.new_value_error(msg))
803 })
804 .try_collect()
805 }
806 }
807
808 fn _unicode_to_wchar_bytes(wtf8: &Wtf8, item_size: usize) -> Vec<u8> {
809 if item_size == 2 {

Callers

nothing calls this directly

Calls 5

u32_to_charFunction · 0.85
as_ptrMethod · 0.45
lenMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected