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

Method repeat

crates/vm/src/builtins/str.rs:541–558  ·  view source on GitHub ↗
(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

539 }
540
541 fn repeat(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
542 if value == 0 && zelf.class().is(vm.ctx.types.str_type) {
543 // Special case: when some `str` is multiplied by `0`,
544 // returns the empty `str`.
545 return Ok(vm.ctx.empty_str.to_owned());
546 }
547 if (value == 1 || zelf.is_empty()) && zelf.class().is(vm.ctx.types.str_type) {
548 // Special case: when some `str` is multiplied by `1` or is the empty `str`,
549 // nothing really happens, we need to return an object itself
550 // with the same `id()` to be compatible with CPython.
551 // This only works for `str` itself, not its subclasses.
552 return Ok(zelf);
553 }
554 zelf.as_wtf8()
555 .as_bytes()
556 .mul(vm, value)
557 .map(|x| Self::from(unsafe { Wtf8Buf::from_bytes_unchecked(x) }).into_ref(&vm.ctx))
558 }
559
560 pub fn try_as_utf8<'a>(&'a self, vm: &VirtualMachine) -> PyResult<&'a PyUtf8Str> {
561 // Check if the string contains surrogates

Callers

nothing calls this directly

Calls 9

isMethod · 0.80
classMethod · 0.45
to_ownedMethod · 0.45
is_emptyMethod · 0.45
mapMethod · 0.45
mulMethod · 0.45
as_bytesMethod · 0.45
as_wtf8Method · 0.45
into_refMethod · 0.45

Tested by

no test coverage detected