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

Function code_page_decode

crates/vm/src/stdlib/_codecs.rs:1361–1394  ·  view source on GitHub ↗
(
        args: CodePageDecodeArgs,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

1359
1360 #[pyfunction]
1361 fn code_page_decode(
1362 args: CodePageDecodeArgs,
1363 vm: &VirtualMachine,
1364 ) -> PyResult<(PyStrRef, usize)> {
1365 use crate::common::wtf8::Wtf8Buf;
1366
1367 if args.code_page < 0 {
1368 return Err(vm.new_value_error("invalid code page number"));
1369 }
1370 let errors = args.errors.as_ref().map(|s| s.as_str()).unwrap_or("strict");
1371 let code_page = args.code_page as u32;
1372 let data = args.data.borrow_buf();
1373 let is_final = args.r#final;
1374
1375 if data.is_empty() {
1376 return Ok((vm.ctx.empty_str.to_owned(), 0));
1377 }
1378
1379 let encoding_name = code_page_encoding_name(code_page);
1380
1381 // Fast path: try to decode the whole buffer with strict flags
1382 match try_decode_code_page_strict(code_page, &data, vm)? {
1383 Some(wide) => {
1384 let s = Wtf8Buf::from_wide(&wide);
1385 return Ok((vm.ctx.new_str(s), data.len()));
1386 }
1387 None => {
1388 // Decode error - fall through to slow path
1389 }
1390 }
1391
1392 // Slow path: byte by byte with error handling
1393 decode_code_page_errors(code_page, &data, errors, is_final, &encoding_name, vm)
1394 }
1395}

Callers 3

decodeFunction · 0.90
_buffer_decodeMethod · 0.90
decodeMethod · 0.90

Calls 12

code_page_encoding_nameFunction · 0.85
decode_code_page_errorsFunction · 0.85
ErrClass · 0.50
mapMethod · 0.45
as_refMethod · 0.45
as_strMethod · 0.45
borrow_bufMethod · 0.45
is_emptyMethod · 0.45
to_ownedMethod · 0.45
new_strMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected