| 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 |