-
Notifications
You must be signed in to change notification settings - Fork 13.5k
make tidy-alphabetical
use a natural sort
#141311
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
base: master
Are you sure you want to change the base?
Conversation
@folkertdev We have a lot of redundant versions of this sort. See the algorithm documented in the style guide; is that algorithm sufficient here? If so, that algorithm is implemented in rustfmt. |
Oh, right, https://github.com/rust-lang/rustfmt/blob/master/src/sort.rs is probably more robust (e.g. it handles leading/repeated zeros, which my version does not). Can we achieve code sharing in a reasonable way between tidy and rustfmt? Or should I just copy-paste that implementation? |
Sharing the code is quite annoying due to how rustfmt is set up with its subtree, copying the code sounds better. It wouldn't be that bad if it diverges. |
I think because this algorithm was designed for
Contrary to alphabetical sorting;
So, idk, this algorithm has some nice features, but I think it needs some tweaking for this use case. |
1f057ed
to
f81c2f0
Compare
After looking at this a bit, I think for the sort of thing that So, I've updated the algorithm to 1) consider leading zeros and 2) perform a case-sensitive sort. In most cases that means your editor's sorting functionality will produce the correct result, except when there are numbers in the identifiers. |
// FEAT_SME_F8F32 | ||
("sme-f8f32", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]), | ||
// FEAT_SME_F16F16 | ||
("sme-f16f16", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]), | ||
// FEAT_SME_F64F64 | ||
("sme-f64f64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]), |
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.
Very nice improvement.
FWIW I don't use my editor to sort these things, I just do a decent enough job by eye then comply when tidy tells me to change something. So any change that looks more sensible and/or facilitates eyeball-binary-search makes me happy. |
f81c2f0
to
50f22f4
Compare
50f22f4
to
a7e76cb
Compare
There are changes to the cc @jieyouxu |
|
a7e76cb
to
1dfc840
Compare
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.
Thanks. I'm not sure if this is always better (e.g. the bootstrap dist
changes), but looking at the identifiers involving numbers, at least those are definitely better IMO.
@bors r+ rollup |
…eyouxu make `tidy-alphabetical` use a natural sort The idea here is that these lines should be correctly sorted, even though a naive string comparison would say they are not: ``` foo2 foo10 ``` This is the ["natural sort order"](https://en.wikipedia.org/wiki/Natural_sort_order). There is more discussion in [#t-compiler/help > tidy natural sort](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/tidy.20natural.20sort/with/519111079) Unfortunately, no standard sorting tools are smart enough to to this automatically (casting some doubt on whether we should make this change). Here are some sort outputs: ``` > cat foo.txt | sort foo foo1 foo10 foo2 mp mp1e2 np", np1e2", > cat foo.txt | sort -n foo foo1 foo10 foo2 mp mp1e2 np", np1e2", > cat foo.txt | sort -V foo foo1 foo2 foo10 mp mp1e2 np1e2", np", ``` Disappointingly, "numeric" sort does not actually have the behavior we want. It only sorts by numeric value if the line starts with a number. The "version" sort looks promising, but does something very unintuitive if you look at the final 4 values. None of the other options seem to have the desired behavior in all cases: ``` -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -g, --general-numeric-sort compare according to general numerical value -i, --ignore-nonprinting consider only printable characters -M, --month-sort compare (unknown) < 'JAN' < ... < 'DEC' -h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G) -n, --numeric-sort compare according to string numerical value -R, --random-sort shuffle, but group identical keys. See shuf(1) --random-source=FILE get random bytes from FILE -r, --reverse reverse the result of comparisons --sort=WORD sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V -V, --version-sort natural sort of (version) numbers within text ``` r? `@Noratrieb` (it sounded like you know this code?)
Rollup of 17 pull requests Successful merges: - #124595 (Suggest cloning `Arc` moved into closure) - #139594 (Simplify `ObligationCauseCode::IfExpression`) - #141311 (make `tidy-alphabetical` use a natural sort) - #141648 ([rustdoc] Do not emit redundant_explicit_links lint if the doc comment comes from expansion) - #142255 (Add edition checks for some tests that had divergent output) - #142285 (tests: Do not run afoul of asm.validity.non-exhaustive in input-stats) - #142549 (small iter.intersperse.fold() optimization) - #142637 (Remove some glob imports from the type system) - #142647 ([perf] Compute hard errors without diagnostics in impl_intersection_has_impossible_obligation) - #142700 (Remove incorrect comments in `Weak`) - #142884 (StableMIR: Add method to retrieve body of coroutine) - #142925 (Rewrite `.gitattributes` CRLF ui tests into run-make tests) - #143001 (Rename run always ) - #143010 (Update `browser-ui-test` version to `0.20.7`) - #143015 (Add `sym::macro_pin` diagnostic item for `core::pin::pin!()`) - #143020 (codegen_fn_attrs: make comment more precise) - #143033 (Expand const-stabilized API links in relnotes) r? `@ghost` `@rustbot` modify labels: rollup
…eyouxu make `tidy-alphabetical` use a natural sort The idea here is that these lines should be correctly sorted, even though a naive string comparison would say they are not: ``` foo2 foo10 ``` This is the ["natural sort order"](https://en.wikipedia.org/wiki/Natural_sort_order). There is more discussion in [#t-compiler/help > tidy natural sort](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/tidy.20natural.20sort/with/519111079) Unfortunately, no standard sorting tools are smart enough to to this automatically (casting some doubt on whether we should make this change). Here are some sort outputs: ``` > cat foo.txt | sort foo foo1 foo10 foo2 mp mp1e2 np", np1e2", > cat foo.txt | sort -n foo foo1 foo10 foo2 mp mp1e2 np", np1e2", > cat foo.txt | sort -V foo foo1 foo2 foo10 mp mp1e2 np1e2", np", ``` Disappointingly, "numeric" sort does not actually have the behavior we want. It only sorts by numeric value if the line starts with a number. The "version" sort looks promising, but does something very unintuitive if you look at the final 4 values. None of the other options seem to have the desired behavior in all cases: ``` -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -g, --general-numeric-sort compare according to general numerical value -i, --ignore-nonprinting consider only printable characters -M, --month-sort compare (unknown) < 'JAN' < ... < 'DEC' -h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G) -n, --numeric-sort compare according to string numerical value -R, --random-sort shuffle, but group identical keys. See shuf(1) --random-source=FILE get random bytes from FILE -r, --reverse reverse the result of comparisons --sort=WORD sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V -V, --version-sort natural sort of (version) numbers within text ``` r? ``@Noratrieb`` (it sounded like you know this code?)
Rollup of 9 pull requests Successful merges: - #124595 (Suggest cloning `Arc` moved into closure) - #139594 (Simplify `ObligationCauseCode::IfExpression`) - #141311 (make `tidy-alphabetical` use a natural sort) - #141648 ([rustdoc] Do not emit redundant_explicit_links lint if the doc comment comes from expansion) - #142285 (tests: Do not run afoul of asm.validity.non-exhaustive in input-stats) - #142393 (Don't give APITs names with macro expansion placeholder fragments in it) - #142884 (StableMIR: Add method to retrieve body of coroutine) - #142981 (Make missing lifetime suggestion verbose) - #143030 (Fix suggestion spans inside macros for the `unused_must_use` lint) r? `@ghost` `@rustbot` modify labels: rollup
The idea here is that these lines should be correctly sorted, even though a naive string comparison would say they are not:
This is the "natural sort order".
There is more discussion in #t-compiler/help > tidy natural sort
Unfortunately, no standard sorting tools are smart enough to to this automatically (casting some doubt on whether we should make this change). Here are some sort outputs:
Disappointingly, "numeric" sort does not actually have the behavior we want. It only sorts by numeric value if the line starts with a number. The "version" sort looks promising, but does something very unintuitive if you look at the final 4 values. None of the other options seem to have the desired behavior in all cases:
r? @Noratrieb (it sounded like you know this code?)