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

Method ensure_valid_utf8

crates/vm/src/builtins/str.rs:492–509  ·  view source on GitHub ↗
(&self, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

490 }
491
492 pub(crate) fn ensure_valid_utf8(&self, vm: &VirtualMachine) -> PyResult<()> {
493 if self.is_utf8() {
494 Ok(())
495 } else {
496 let start = self
497 .as_wtf8()
498 .code_points()
499 .position(|c| c.to_char().is_none())
500 .unwrap();
501 Err(vm.new_unicode_encode_error_real(
502 identifier!(vm, utf_8).to_owned(),
503 vm.ctx.new_str(self.data.clone()),
504 start,
505 start + 1,
506 vm.ctx.new_str("surrogates not allowed"),
507 ))
508 }
509 }
510
511 pub fn to_string_lossy(&self) -> Cow<'_, str> {
512 self.to_str()

Callers 4

try_as_utf8Method · 0.80
try_into_utf8Method · 0.80
try_downcast_fromMethod · 0.80
slot_newMethod · 0.80

Calls 12

code_pointsMethod · 0.80
ErrClass · 0.50
is_utf8Method · 0.45
unwrapMethod · 0.45
positionMethod · 0.45
as_wtf8Method · 0.45
is_noneMethod · 0.45
to_charMethod · 0.45
to_ownedMethod · 0.45
new_strMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected