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

Function encode_utf8_compatible

crates/common/src/encodings.rs:211–257  ·  view source on GitHub ↗
(
    mut ctx: Ctx,
    errors: &E,
    err_reason: &str,
    target_kind: StrKind,
)

Source from the content-addressed store, hash-verified

209
210#[inline]
211fn encode_utf8_compatible<Ctx, E>(
212 mut ctx: Ctx,
213 errors: &E,
214 err_reason: &str,
215 target_kind: StrKind,
216) -> Result<Vec<u8>, Ctx::Error>
217where
218 Ctx: EncodeContext,
219 E: EncodeErrorHandler<Ctx>,
220{
221 // let mut data = s.as_ref();
222 // let mut char_data_index = 0;
223 let mut out = Vec::<u8>::with_capacity(ctx.remaining_data().len());
224 loop {
225 let data = ctx.remaining_data();
226 let mut iter = iter_code_points(data);
227 let Some((i, _)) = iter.find(|(_, c)| !target_kind.can_encode(*c)) else {
228 break;
229 };
230
231 out.extend_from_slice(&ctx.remaining_data().as_bytes()[..i.bytes]);
232
233 let err_start = ctx.position() + i;
234 // number of non-compatible chars between the first non-compatible char and the next compatible char
235 let err_end = match { iter }.find(|(_, c)| target_kind.can_encode(*c)) {
236 Some((i, _)) => ctx.position() + i,
237 None => ctx.data_len(),
238 };
239
240 let range = err_start..err_end;
241 let replace = ctx.handle_error(errors, range.clone(), Some(err_reason))?;
242 match replace {
243 EncodeReplace::Str(s) => {
244 if s.is_compatible_with(target_kind) {
245 out.extend_from_slice(s.as_ref().as_bytes());
246 } else {
247 return Err(ctx.error_encoding(range, Some(err_reason)));
248 }
249 }
250 EncodeReplace::Bytes(b) => {
251 out.extend_from_slice(b.as_ref());
252 }
253 }
254 }
255 out.extend_from_slice(ctx.remaining_data().as_bytes());
256 Ok(out)
257}
258
259pub mod errors {
260 use crate::str::UnicodeEscapeCodepoint;

Callers 1

encodeFunction · 0.85

Calls 15

iter_code_pointsFunction · 0.85
remaining_dataMethod · 0.80
can_encodeMethod · 0.80
data_lenMethod · 0.80
error_encodingMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
lenMethod · 0.45
findMethod · 0.45
as_bytesMethod · 0.45
positionMethod · 0.45
handle_errorMethod · 0.45

Tested by

no test coverage detected