Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for padding to a minimum number of decimal digits #1482

Merged
merged 14 commits into from
Jan 11, 2022

Conversation

Manishearth
Copy link
Member

Fixes #1476

I'm not sure what to name this. Honestly I think that we should name this API minimum_decimal_digits instead (and maybe get rid of the padding API?). I don't see truncation as particularly useful. Overall from talking to the WearOS users they do prefer to think in terms of "minimum" and "maximum" digits.

utils/fixed_decimal/src/decimal.rs Show resolved Hide resolved
utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
@Manishearth Manishearth requested a review from sffc January 8, 2022 00:03
utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
utils/fixed_decimal/src/decimal.rs Show resolved Hide resolved
@Manishearth Manishearth requested a review from sffc January 10, 2022 22:03
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like the FixedDecimal class to be robust against integer overflow because we have actually had problems with this in ICU. Whenever you add, subtract, or cast these things, you should handle the overflow. See my suggestions. Ideally we also have test cases for these.

utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
Manishearth and others added 3 commits January 11, 2022 10:57
Co-authored-by: Shane F. Carr <shane@unicode.org>
Co-authored-by: Shane F. Carr <shane@unicode.org>
@Manishearth Manishearth requested a review from sffc January 11, 2022 18:58
utils/fixed_decimal/src/decimal.rs Outdated Show resolved Hide resolved
}

// how many digits need to be deleted from the start.
let cut = self.magnitude - self.upper_magnitude;
self.upper_magnitude = magnitude;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Please add here, too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not here yet. Please add the check_invariants in all three functions (pad left, pad right, truncate left).

Also please fix the clippy warning. :)

utils/fixed_decimal/src/decimal.rs Show resolved Hide resolved
sffc
sffc previously approved these changes Jan 11, 2022
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after adding the debug assertions.

Also optionally add unit tests for magnitudes near i16::MAX to test the new branches, or open an issue to add these in a follow-up.

@Manishearth
Copy link
Member Author

Filed #1496 for now

@Manishearth Manishearth requested a review from sffc January 11, 2022 21:26
@Manishearth Manishearth merged commit c6a9402 into unicode-org:main Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-consider arguments for FixedDecimal::pad_or_truncate_left
2 participants