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