(
ctx: Ctx,
errors: &E,
final_decode: bool,
)
| 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 | |
| 552 | pub mod latin_1 { |
no test coverage detected