Skip to content

Commit

Permalink
Inline trunc_internal
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Jul 6, 2023
1 parent 11d5efe commit c583e2d
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions utils/fixed_decimal/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,12 +1046,6 @@ impl FixedDecimal {
/// assert_eq!("1", dec.to_string());
/// ```
pub fn trunc(&mut self, position: i16) {
self.trunc_internal(position, true);
#[cfg(debug_assertions)]
self.check_invariants();
}

fn trunc_internal(&mut self, position: i16, strip_trailing_zeros: bool) {
self.lower_magnitude = cmp::min(position, 0);
if position == i16::MIN {
// Nothing more to do
Expand All @@ -1065,13 +1059,14 @@ impl FixedDecimal {
if magnitude <= self.magnitude {
self.digits
.truncate(crate::ops::i16_abs_sub(self.magnitude, magnitude) as usize);
if strip_trailing_zeros {
self.remove_trailing_zeros_from_digits_list();
}
self.remove_trailing_zeros_from_digits_list();
} else {
self.digits.clear();
self.magnitude = 0;
}

#[cfg(debug_assertions)]
self.check_invariants();
}

/// Half Truncates the number on the right to a particular position, deleting
Expand Down Expand Up @@ -1176,7 +1171,16 @@ impl FixedDecimal {
let before_truncate_is_zero = self.is_zero();
let before_truncate_bottom_magnitude = self.nonzero_magnitude_end();
let before_truncate_magnitude = self.magnitude;
self.trunc_internal(position, false);

self.lower_magnitude = cmp::min(position, 0);
if position == i16::MIN {
// Nothing more to do
#[cfg(debug_assertions)]
self.check_invariants();
return;
}
let magnitude = position - 1;
self.upper_magnitude = cmp::max(self.upper_magnitude, magnitude);

if before_truncate_is_zero || position <= before_truncate_bottom_magnitude {
#[cfg(debug_assertions)]
Expand All @@ -1185,6 +1189,8 @@ impl FixedDecimal {
}

if position <= before_truncate_magnitude {
self.digits
.truncate(crate::ops::i16_abs_sub(self.magnitude, magnitude) as usize);
let result = self.increment_abs_by_one();
if result.is_err() {
// Do nothing for now.
Expand All @@ -1195,7 +1201,7 @@ impl FixedDecimal {
return;
}

debug_assert!(self.digits.is_empty());
self.digits.clear();
self.digits.push(1);
self.magnitude = position;
self.upper_magnitude = cmp::max(self.upper_magnitude, position);
Expand Down

0 comments on commit c583e2d

Please sign in to comment.