(
&self,
ctx: &mut PyEncodeContext<'a>,
range: Range<StrSize>,
reason: Option<&str>,
)
| 493 | |
| 494 | impl<'a> EncodeErrorHandler<PyEncodeContext<'a>> for SurrogatePass { |
| 495 | fn handle_encode_error( |
| 496 | &self, |
| 497 | ctx: &mut PyEncodeContext<'a>, |
| 498 | range: Range<StrSize>, |
| 499 | reason: Option<&str>, |
| 500 | ) -> PyResult<(EncodeReplace<PyEncodeContext<'a>>, StrSize)> { |
| 501 | let standard_encoding = StandardEncoding::parse(ctx.encoding) |
| 502 | .ok_or_else(|| ctx.error_encoding(range.clone(), reason))?; |
| 503 | let err_str = &ctx.full_data()[range.start.bytes..range.end.bytes]; |
| 504 | let num_chars = range.end.chars - range.start.chars; |
| 505 | let mut out: Vec<u8> = Vec::with_capacity(num_chars * 4); |
| 506 | for ch in err_str.code_points() { |
| 507 | let c = ch.to_u32(); |
| 508 | let 0xd800..=0xdfff = c else { |
| 509 | // Not a surrogate, fail with original exception |
| 510 | return Err(ctx.error_encoding(range, reason)); |
| 511 | }; |
| 512 | match standard_encoding { |
| 513 | StandardEncoding::Utf8 => out.extend(ch.encode_wtf8(&mut [0; 4]).as_bytes()), |
| 514 | StandardEncoding::Utf16Le => out.extend((c as u16).to_le_bytes()), |
| 515 | StandardEncoding::Utf16Be => out.extend((c as u16).to_be_bytes()), |
| 516 | StandardEncoding::Utf32Le => out.extend(c.to_le_bytes()), |
| 517 | StandardEncoding::Utf32Be => out.extend(c.to_be_bytes()), |
| 518 | } |
| 519 | } |
| 520 | Ok((EncodeReplace::Bytes(ctx.bytes(out)), range.end)) |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | impl<'a> DecodeErrorHandler<PyDecodeContext<'a>> for SurrogatePass { |
no test coverage detected