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

Method replace

crates/vm/src/builtins/str.rs:1063–1085  ·  view source on GitHub ↗
(&self, args: ReplaceArgs)

Source from the content-addressed store, hash-verified

1061
1062 #[pymethod]
1063 fn replace(&self, args: ReplaceArgs) -> Wtf8Buf {
1064 use core::cmp::Ordering;
1065
1066 let s = self.as_wtf8();
1067 let ReplaceArgs { old, new, count } = args;
1068
1069 match count.cmp(&0) {
1070 Ordering::Less => s.replace(old.as_wtf8(), new.as_wtf8()),
1071 Ordering::Equal => s.to_owned(),
1072 Ordering::Greater => {
1073 let s_is_empty = s.is_empty();
1074 let old_is_empty = old.is_empty();
1075
1076 if s_is_empty && !old_is_empty {
1077 s.to_owned()
1078 } else if s_is_empty && old_is_empty {
1079 new.as_wtf8().to_owned()
1080 } else {
1081 s.replacen(old.as_wtf8(), new.as_wtf8(), count as usize)
1082 }
1083 }
1084 }
1085 }
1086
1087 #[pymethod]
1088 fn isprintable(&self) -> bool {

Callers

nothing calls this directly

Calls 5

replacenMethod · 0.80
as_wtf8Method · 0.45
cmpMethod · 0.45
to_ownedMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected