-
Notifications
You must be signed in to change notification settings - Fork 183
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
Implement MeasureUnit #4360
Implement MeasureUnit #4360
Conversation
let (num_part, den_part) = identifier | ||
.split_once("-per-") | ||
.or_else(|| identifier.split_once("per-")) | ||
.unwrap_or((identifier, "")); |
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.
I realized we actually only need one search:
let (num_part, den_part) = identifier | |
.split_once("-per-") | |
.or_else(|| identifier.split_once("per-")) | |
.unwrap_or((identifier, "")); | |
let (num_part, den_part) = identifier | |
.split_once("per-") | |
.map(|(n,d)| (n.strip_suffix("-").unwrap_or(n), d)) | |
.unwrap_or((identifier, "")); |
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.
okay, done, but I feel we over complicated it
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.
much better
if let Some(unit_id) = trie.get(part.as_bytes()) { | ||
Some(unit_id) | ||
} else { | ||
None | ||
} |
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.
if let Some(unit_id) = trie.get(part.as_bytes()) { | |
Some(unit_id) | |
} else { | |
None | |
} | |
trie.get(part.as_bytes()) |
or just inline
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
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.
Praise: Good work; seems about where we want to be for the measure units parser
// TODO(#4369): split this struct to two structs: MeasureUnitParser for parsing the identifier and MeasureUnit to represent the unit. | ||
pub struct MeasureUnit { | ||
/// Contains the processed units. | ||
pub contained_units: Vec<MeasureUnitItem>, |
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.
Suggestion: this should be either SmallVec
(as in fixed_decimal) or ShortBoxSlice
(as in icu_locid). The common case shouldn't need to allocate a Vec
.
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
/// Get the power of the unit. | ||
/// NOTE: | ||
/// if the power is found, the function will return (power, part without the power). | ||
/// if the power is not found, the function will return (1, part). |
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.
Nit: These docs seem out of date
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
/// NOTE: | ||
/// if the power is found, the function will return (power, part without the power). | ||
/// if the power is not found, the function will return (1, part). | ||
fn get_power(part: &str) -> Option<i8> { |
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.
Nit (optional): I prefer free functions instead of static functions in Rust except for constructors.
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.
I was thinking about this too. I have moved them to si+prefix.rs and power.rs
/// Get the SI prefix. | ||
/// NOTE: | ||
/// if the prefix is found, the function will return (power, base, part without the prefix). | ||
/// if the prefix is not found, the function will return (0, Base::NotExist, part). |
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.
Nit: Docs seem out of date
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
No description provided.