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

Function pystr_from_raw_cstr

crates/stdlib/src/locale.rs:102–140  ·  view source on GitHub ↗
(vm: &VirtualMachine, raw_ptr: *const libc::c_char)

Source from the content-addressed store, hash-verified

100 }
101
102 unsafe fn pystr_from_raw_cstr(vm: &VirtualMachine, raw_ptr: *const libc::c_char) -> PyResult {
103 let slice = unsafe { CStr::from_ptr(raw_ptr) };
104
105 // Fast path: ASCII/UTF-8
106 if let Ok(s) = slice.to_str() {
107 return Ok(vm.new_pyobj(s));
108 }
109
110 // On Windows, locale strings use the ANSI code page encoding
111 #[cfg(windows)]
112 {
113 use windows_sys::Win32::Globalization::{CP_ACP, MultiByteToWideChar};
114 let bytes = slice.to_bytes();
115 unsafe {
116 let len = MultiByteToWideChar(
117 CP_ACP,
118 0,
119 bytes.as_ptr(),
120 bytes.len() as i32,
121 ptr::null_mut(),
122 0,
123 );
124 if len > 0 {
125 let mut wide = vec![0u16; len as usize];
126 MultiByteToWideChar(
127 CP_ACP,
128 0,
129 bytes.as_ptr(),
130 bytes.len() as i32,
131 wide.as_mut_ptr(),
132 len,
133 );
134 return Ok(vm.new_pyobj(String::from_utf16_lossy(&wide)));
135 }
136 }
137 }
138
139 Ok(vm.new_pyobj(String::from_utf8_lossy(slice.to_bytes()).into_owned()))
140 }
141
142 #[pyattr(name = "Error", once)]
143 fn error(vm: &VirtualMachine) -> PyTypeRef {

Callers 1

setlocaleFunction · 0.85

Calls 7

to_strMethod · 0.80
new_pyobjMethod · 0.80
to_bytesMethod · 0.80
as_mut_ptrMethod · 0.80
into_ownedMethod · 0.80
as_ptrMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected