(
args: CodePageDecodeArgs,
vm: &VirtualMachine,
)
| 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 | } |
no test coverage detected