From 394d96960f2e21720a6c4ba695677f1dc467e816 Mon Sep 17 00:00:00 2001 From: Xuyang Zhang Date: Tue, 14 Jul 2026 15:50:21 +0800 Subject: [PATCH 1/2] tidy: handle large numeric sort keys --- src/tools/tidy/src/alphabetical.rs | 19 ++++++++++++------- src/tools/tidy/src/alphabetical/tests.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/tools/tidy/src/alphabetical.rs b/src/tools/tidy/src/alphabetical.rs index e3b5437bbc0..e04569ae6f2 100644 --- a/src/tools/tidy/src/alphabetical.rs +++ b/src/tools/tidy/src/alphabetical.rs @@ -285,7 +285,7 @@ fn consume_numeric_prefix>(it: &mut Peekable) -> Str let mut result = String::new(); while let Some(&c) = it.peek() { - if !c.is_numeric() { + if !c.is_ascii_digit() { break; } @@ -296,23 +296,28 @@ fn consume_numeric_prefix>(it: &mut Peekable) -> Str result } -// A sorting function that is case-sensitive, and sorts sequences of digits by their numeric value, -// so that `9` sorts before `12`. +// A sorting function that is case-sensitive, and sorts sequences of ASCII digits by their numeric +// value, so that `9` sorts before `12`. fn version_sort(a: &str, b: &str) -> Ordering { let mut it1 = a.chars().peekable(); let mut it2 = b.chars().peekable(); while let (Some(x), Some(y)) = (it1.peek(), it2.peek()) { - match (x.is_numeric(), y.is_numeric()) { + match (x.is_ascii_digit(), y.is_ascii_digit()) { (true, true) => { let num1: String = consume_numeric_prefix(it1.by_ref()); let num2: String = consume_numeric_prefix(it2.by_ref()); - let int1: u64 = num1.parse().unwrap(); - let int2: u64 = num2.parse().unwrap(); + let digits1 = num1.trim_start_matches('0'); + let digits2 = num2.trim_start_matches('0'); // Compare strings when the numeric value is equal to handle "00" versus "0". - match int1.cmp(&int2).then_with(|| num1.cmp(&num2)) { + match digits1 + .len() + .cmp(&digits2.len()) + .then_with(|| digits1.cmp(digits2)) + .then_with(|| num1.cmp(&num2)) + { Ordering::Equal => continue, different => return different, } diff --git a/src/tools/tidy/src/alphabetical/tests.rs b/src/tools/tidy/src/alphabetical/tests.rs index 96dad53fa35..a7b8ea09c76 100644 --- a/src/tools/tidy/src/alphabetical/tests.rs +++ b/src/tools/tidy/src/alphabetical/tests.rs @@ -330,6 +330,18 @@ fn test_numeric_good() { ); } +#[test] +fn test_large_numeric_values() { + good( + "\ + # tidy-alphabetical-start + item18446744073709551616 + item99999999999999999999 + # tidy-alphabetical-end + ", + ); +} + #[test] fn test_numeric_bad() { let lines = "\ From d4dff69f96bb11af31f0ed1dd143faafe0ea9183 Mon Sep 17 00:00:00 2001 From: Xuyang Zhang Date: Wed, 15 Jul 2026 22:31:47 +0800 Subject: [PATCH 2/2] tidy: document numeric sort key limit --- src/tools/tidy/src/alphabetical.rs | 21 +++++++++------------ src/tools/tidy/src/alphabetical/tests.rs | 12 ------------ 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/tools/tidy/src/alphabetical.rs b/src/tools/tidy/src/alphabetical.rs index e04569ae6f2..d9714c85428 100644 --- a/src/tools/tidy/src/alphabetical.rs +++ b/src/tools/tidy/src/alphabetical.rs @@ -9,6 +9,8 @@ //! // tidy-alphabetical-end //! ``` //! +//! Numeric sequences are parsed as `u64` values, so each sequence must fit within `u64`. +//! //! Empty lines and lines starting (ignoring spaces) with `//` or `#` (except those starting with //! `#!`) are considered comments are are sorted together with the next line (but do not affect //! sorting). @@ -285,7 +287,7 @@ fn consume_numeric_prefix>(it: &mut Peekable) -> Str let mut result = String::new(); while let Some(&c) = it.peek() { - if !c.is_ascii_digit() { + if !c.is_numeric() { break; } @@ -296,28 +298,23 @@ fn consume_numeric_prefix>(it: &mut Peekable) -> Str result } -// A sorting function that is case-sensitive, and sorts sequences of ASCII digits by their numeric -// value, so that `9` sorts before `12`. +// A sorting function that is case-sensitive, and sorts sequences of digits by their numeric value, +// so that `9` sorts before `12`. fn version_sort(a: &str, b: &str) -> Ordering { let mut it1 = a.chars().peekable(); let mut it2 = b.chars().peekable(); while let (Some(x), Some(y)) = (it1.peek(), it2.peek()) { - match (x.is_ascii_digit(), y.is_ascii_digit()) { + match (x.is_numeric(), y.is_numeric()) { (true, true) => { let num1: String = consume_numeric_prefix(it1.by_ref()); let num2: String = consume_numeric_prefix(it2.by_ref()); - let digits1 = num1.trim_start_matches('0'); - let digits2 = num2.trim_start_matches('0'); + let int1: u64 = num1.parse().unwrap(); + let int2: u64 = num2.parse().unwrap(); // Compare strings when the numeric value is equal to handle "00" versus "0". - match digits1 - .len() - .cmp(&digits2.len()) - .then_with(|| digits1.cmp(digits2)) - .then_with(|| num1.cmp(&num2)) - { + match int1.cmp(&int2).then_with(|| num1.cmp(&num2)) { Ordering::Equal => continue, different => return different, } diff --git a/src/tools/tidy/src/alphabetical/tests.rs b/src/tools/tidy/src/alphabetical/tests.rs index a7b8ea09c76..96dad53fa35 100644 --- a/src/tools/tidy/src/alphabetical/tests.rs +++ b/src/tools/tidy/src/alphabetical/tests.rs @@ -330,18 +330,6 @@ fn test_numeric_good() { ); } -#[test] -fn test_large_numeric_values() { - good( - "\ - # tidy-alphabetical-start - item18446744073709551616 - item99999999999999999999 - # tidy-alphabetical-end - ", - ); -} - #[test] fn test_numeric_bad() { let lines = "\