-
Notifications
You must be signed in to change notification settings - Fork 136
Serialize number to no more than necessary precision #173
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
r? @SimonSapin |
After experimenting a bit, I believe that the standard library’s default float formatting already uses the shortest representation that parses to the same float. The issue in servo/servo#17205 is specifically with percentages. I can reproduce it with the given test case, but not when changing
Float formatting can be very subtle, and I’m not at all familiar with the details. Responsibility for maintaining code in this repository is de-facto mine, so I’d much rather find another solution than adding a bunch of float formatting code. What do you think of not dividing percentage token value in the parser and only doing so in an accessor for layout code? |
That's true, but the "same float" is not the important thing here. I'll follow up in servo/servo#17205, though I'll be largely repeating things I already said there.... |
Alright, it sounds like we do want to round to 6 decimal places (from the first significant digit, not from the fraction separator). Still, I’d feel better if that code were not in |
I'm okay with maintaining it in a separate crate, although I don't have a good sense of how should I name that crate. |
☔ The latest upstream changes (presumably #178) made this pull request unmergeable. Please resolve the merge conflicts. |
c730099
to
a5123a6
Compare
I created a new crate |
src/serializer.rs
Outdated
write!(dest, "{}", value)? | ||
} | ||
let mut result = String::new(); | ||
dtoa_short::write(&mut result, value)?; |
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.
Why can’t this write write to dest
directly? Can we find some way to avoid allocating a 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.
Oh, I see contains
below. Could something be added to dtoa_short
so that it tells us that information?
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.
In the initial commit here I added a "has_exp" out parameter, but that feels pretty broken as a public interface... Probably I can add a return value for dtoa_short::write
to return the form of the number serialized.
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.
Unless you need to extend and existing value, out parameters are not idiomatic in Rust and can be replaced by returning a tuple. Or this case, replacing Result<(), Error>
with Result<bool, Error>
, or maybe a custom enum instead of bool
.
I read through |
I got |
a5123a6
to
f838e1a
Compare
Updated the patch with updated dtoa-short. |
r=me Presumably you’ll also want a version number bump to get this on crates.io? |
Oh, hmm, yep. |
f838e1a
to
2181f2f
Compare
@bors-servo r=SimonSapin |
📌 Commit 2181f2f has been approved by |
Serialize number to no more than necessary precision I'm not sure whether it can be simplified... and I probably need some more idea for how should I test it... But things look fine so far. This should probably goes to an independent crate at some point. It should fix servo/servo#17205. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/173) <!-- Reviewable:end -->
☀️ Test successful - status-travis |
Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205
Update cssparser to 0.19.2 Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205.
Update cssparser to 0.19.2 Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18201) <!-- Reviewable:end -->
Update cssparser to 0.19.2 Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18201) <!-- Reviewable:end -->
…rup); r=upsuper Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. Source-Repo: https://github.com/servo/servo Source-Revision: 2e775abfa4563fc5db467c5c79f9d2507f6bb2e7 --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 77c9f3763f12450066f6695a98f7267c1c3f7d2a
…rup); r=upsuper Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. Source-Repo: https://github.com/servo/servo Source-Revision: 2e775abfa4563fc5db467c5c79f9d2507f6bb2e7
…rup); r=upsuper Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. Source-Repo: https://github.com/servo/servo Source-Revision: 2e775abfa4563fc5db467c5c79f9d2507f6bb2e7
…rup); r=upsuper Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. Source-Repo: https://github.com/servo/servo Source-Revision: 2e775abfa4563fc5db467c5c79f9d2507f6bb2e7 UltraBlame original commit: 4e4a40befc4d0c078f0d4900934da9d51064a60c
…rup); r=upsuper Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. Source-Repo: https://github.com/servo/servo Source-Revision: 2e775abfa4563fc5db467c5c79f9d2507f6bb2e7 UltraBlame original commit: 4e4a40befc4d0c078f0d4900934da9d51064a60c
…rup); r=upsuper Pull in servo/rust-cssparser#173, which together with #18159 fixes #17205. Source-Repo: https://github.com/servo/servo Source-Revision: 2e775abfa4563fc5db467c5c79f9d2507f6bb2e7 UltraBlame original commit: 4e4a40befc4d0c078f0d4900934da9d51064a60c
I want to call out @bzbarsky's comment from servo/servo#17205:
I ran into an issue today where I have some SCSS being run through Parcel – whose Lightning CSS transformer relies on rust-cssparser – and the calculated values in the compiled CSS were all being rounded to 6 digits. This resulted in some values that weren't exactly what I was expecting. This project is well over my head, but the quoted comment from @bzbarsky resonates with me – doing this sort of rounding in some cases results in "lossy" conversions from Sass/SCSS to CSS. Is there anything that can be done? |
I'm not sure whether it can be simplified... and I probably need some more idea for how should I test it... But things look fine so far.
This should probably goes to an independent crate at some point.
It should fix servo/servo#17205.
This change is