-
Notifications
You must be signed in to change notification settings - Fork 250
Add short representation generator for MeasureUnit #6685
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
Conversation
# Desctiption - Implemented `generate_short_representation` method in `MeasureUnit` to provide a concise string representation of measure units, including handling of constant denominators and scientific notation. - Added `ToString` implementation for `SiPrefix` and `SingleUnit` to facilitate their string representations, following a defined format for SI prefixes and unit IDs. - Updated documentation with examples for clarity on usage and expected outputs.
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback. |
| // add constant part to the negative part | ||
| if self.constant_denominator > 0 { | ||
| if !negative_part.is_empty() { | ||
| negative_part.insert(0, '#'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you need the #? C and R should be unambiguous without them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I was thinking about that. We only need C. In fact, we could even eliminate C if we declare the constant at the beginning, but let’s keep it for readability for now.
…SingleUnit - Updated `generate_short_representation` in `MeasureUnit` to utilize helper methods for appending short representations. - Introduced `append_short_representation` methods in `SiPrefix` and `SingleUnit` to streamline the process of generating their string representations. - Enhanced documentation with examples for better clarity on usage.
| /// | ||
| /// assert_eq!(short_representation, "C100I82P-1D3I85", "{}", "liter-per-100-kilometer"); | ||
| /// ``` | ||
| pub fn generate_short_representation(&self) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this actually intended to be a public API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, yes. Once we start using it internally, it will be private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what requires this to be public now? I'm worried that this will be forgotten and accidentally made public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will needed in the data gen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how will you make it private if it's used in datagen? should it be in provider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should have a field for the short unit id, but it is okay, the fn can be public for now
| /// | ||
| /// assert_eq!(short_representation, "C100I82P-1D3I85", "{}", "liter-per-100-kilometer"); | ||
| /// ``` | ||
| pub fn generate_short_representation(&self) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what requires this to be public now? I'm worried that this will be forgotten and accidentally made public
Co-authored-by: Robert Bastian <[email protected]>
Co-authored-by: Robert Bastian <[email protected]>
| // Divide by 10^8 | ||
| while n % 100_000_000 == 0 { | ||
| n /= 100_000_000; | ||
| zeros_count += 8; | ||
| } | ||
|
|
||
| // Divide by 10^4 | ||
| while n % 10_000 == 0 { | ||
| n /= 10_000; | ||
| zeros_count += 4; | ||
| } | ||
|
|
||
| // Divide by 10^2 | ||
| while n % 100 == 0 { | ||
| n /= 100; | ||
| zeros_count += 2; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the point of these? repeatedly dividing by 10 is sufficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for reducing the time complexity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the time complexity is constant because the input is fixed-length. I don't think this is worth the additional code complexity
| /// | ||
| /// assert_eq!(short_representation, "C100I82P-1D3I85", "{}", "liter-per-100-kilometer"); | ||
| /// ``` | ||
| pub fn generate_short_representation(&self) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how will you make it private if it's used in datagen? should it be in provider?
| Base::Decimal => 'D', | ||
| Base::Binary => 'B', | ||
| }); | ||
| write!(buff, "{}", self.power).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't unwrap, it adds panicking code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also in other places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
robertbastian
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
Desctiption
generate_short_representationmethod inMeasureUnitto provide a concise string representation of measure units, including handling of constant denominators and scientific notation.ToStringimplementation forSiPrefixandSingleUnitto facilitate their string representations, following a defined format for SI prefixes and unit IDs.