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

Function decode

crates/common/src/encodings.rs:506–549  ·  view source on GitHub ↗
(
        ctx: Ctx,
        errors: &E,
        final_decode: bool,
    )

Source from the content-addressed store, hash-verified

504 }
505
506 pub fn decode<Ctx: DecodeContext, E: DecodeErrorHandler<Ctx>>(
507 ctx: Ctx,
508 errors: &E,
509 final_decode: bool,
510 ) -> Result<(Wtf8Buf, usize), Ctx::Error> {
511 decode_utf8_compatible(
512 ctx,
513 errors,
514 |v| {
515 core::str::from_utf8(v).map_err(|e| {
516 // SAFETY: as specified in valid_up_to's documentation, input[..e.valid_up_to()]
517 // is valid utf8
518 unsafe { make_decode_err(v, e.valid_up_to(), e.error_len()) }
519 })
520 },
521 |rest, err_len| {
522 let first_err = rest[0];
523 if matches!(first_err, 0x80..=0xc1 | 0xf5..=0xff) {
524 HandleResult::Error {
525 err_len: Some(1),
526 reason: "invalid start byte",
527 }
528 } else if err_len.is_none() {
529 // error_len() == None means unexpected eof
530 if final_decode {
531 HandleResult::Error {
532 err_len,
533 reason: "unexpected end of data",
534 }
535 } else {
536 HandleResult::Done
537 }
538 } else if !final_decode && matches!(rest, [0xed, 0xa0..=0xbf]) {
539 // truncated surrogate
540 HandleResult::Done
541 } else {
542 HandleResult::Error {
543 err_len,
544 reason: "invalid continuation byte",
545 }
546 }
547 },
548 )
549 }
550}
551
552pub mod latin_1 {

Callers 1

decode_utf8_compatibleFunction · 0.70

Calls 10

decode_utf8_compatibleFunction · 0.85
make_decode_errFunction · 0.85
collectMethod · 0.80
remaining_dataMethod · 0.80
SomeClass · 0.50
is_noneMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected