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

Implement MeasureUnit #4360

Merged
merged 56 commits into from
Dec 7, 2023
Merged

Implement MeasureUnit #4360

merged 56 commits into from
Dec 7, 2023

Conversation

younies
Copy link
Member

@younies younies commented Nov 23, 2023

No description provided.

@younies younies requested a review from a team as a code owner November 23, 2023 12:21
@younies younies marked this pull request as draft November 23, 2023 12:21
@younies younies marked this pull request as ready for review November 24, 2023 21:52
experimental/unitsconversion/src/measureunit.rs Outdated Show resolved Hide resolved
experimental/unitsconversion/src/measureunit.rs Outdated Show resolved Hide resolved
experimental/unitsconversion/src/measureunit.rs Outdated Show resolved Hide resolved
experimental/unitsconversion/src/measureunit.rs Outdated Show resolved Hide resolved
Comment on lines 259 to 262
let (num_part, den_part) = identifier
.split_once("-per-")
.or_else(|| identifier.split_once("per-"))
.unwrap_or((identifier, ""));
Copy link
Member

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:

Suggested change
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, ""));

Copy link
Member Author

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

Copy link
Member

@robertbastian robertbastian left a comment

Choose a reason for hiding this comment

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

much better

Comment on lines 193 to 197
if let Some(unit_id) = trie.get(part.as_bytes()) {
Some(unit_id)
} else {
None
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if let Some(unit_id) = trie.get(part.as_bytes()) {
Some(unit_id)
} else {
None
}
trie.get(part.as_bytes())

or just inline

Copy link
Member Author

Choose a reason for hiding this comment

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

done

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.

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>,
Copy link
Member

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.

Copy link
Member Author

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).
Copy link
Member

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

Copy link
Member Author

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> {
Copy link
Member

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.

Copy link
Member Author

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).
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@younies younies requested a review from sffc December 1, 2023 01:04
@younies younies merged commit cb242cb into unicode-org:main Dec 7, 2023
29 checks passed
@younies younies deleted the units-trie branch June 27, 2024 20:02
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.

3 participants