Skip to content

Commit 6d5afb7

Browse files
committed
Remove saturating ops when possible
1 parent 7442caa commit 6d5afb7

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/arraystring.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ where
490490
pub fn pop(&mut self) -> Option<char> {
491491
debug!("Pop");
492492
self.as_str().chars().last().map(|ch| {
493-
self.size = self.size.saturating_sub(ch.len_utf8().into_lossy());
493+
self.size -= ch.len_utf8().into_lossy();
494494
ch
495495
})
496496
}
@@ -534,9 +534,8 @@ where
534534
end = pos;
535535
}
536536

537+
let _ = self.replace_range(end.., "");
537538
let _ = self.replace_range(..start, "");
538-
// Note: Garanteed to not overflow but that should not be the bottleneck
539-
let _ = self.replace_range(end.saturating_sub(start).., "");
540539
}
541540

542541
/// Removes specified char from `ArrayString`
@@ -877,19 +876,14 @@ where
877876
let ptr = self.array.as_mut_ptr();
878877
core::ptr::copy(
879878
ptr.add(end),
880-
ptr.add(str.len().saturating_add(start)),
881-
this_len.saturating_sub(end),
879+
ptr.add(str.len()).add(start),
880+
this_len - end,
882881
);
883882
if !str.is_empty() {
884883
core::ptr::copy(str.as_ptr(), ptr.add(start), str.len());
885884
}
886885
}
887-
self.size = self
888-
.len()
889-
.saturating_add(str.len())
890-
.saturating_add(start)
891-
.saturating_sub(end)
892-
.into_lossy();
886+
self.size += str.len() + start + end;
893887
Ok(())
894888
}
895889
}

0 commit comments

Comments
 (0)