Compare the last key component numerically without imposing an integer limit. Invalid suffixes remain deterministic and group with their own prefix.
(a: &str, b: &str)
| 129 | /// Compare the last key component numerically without imposing an integer limit. |
| 130 | /// Invalid suffixes remain deterministic and group with their own prefix. |
| 131 | fn compare_human_keys(a: &str, b: &str) -> std::cmp::Ordering { |
| 132 | fn parts(key: &str) -> (&str, Option<&str>) { |
| 133 | let split = key |
| 134 | .rfind("::") |
| 135 | .map(|i| i + 2) |
| 136 | .or_else(|| key.rfind('-').map(|i| i + 1)); |
| 137 | match split { |
| 138 | Some(i) => (&key[..i], Some(&key[i..])), |
| 139 | None => (key, None), |
| 140 | } |
| 141 | } |
| 142 | fn numeric(s: &str) -> Option<&str> { |
| 143 | if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) { |
| 144 | return None; |
| 145 | } |
| 146 | Some(s.trim_start_matches('0')) |
| 147 | } |
| 148 | |
| 149 | let (a_prefix, a_suffix) = parts(a); |
| 150 | let (b_prefix, b_suffix) = parts(b); |
| 151 | a_prefix |
| 152 | .cmp(b_prefix) |
| 153 | .then_with( |
| 154 | || match (a_suffix.and_then(numeric), b_suffix.and_then(numeric)) { |
| 155 | (Some(a), Some(b)) => a.len().cmp(&b.len()).then_with(|| a.cmp(b)), |
| 156 | (Some(_), None) => std::cmp::Ordering::Less, |
| 157 | (None, Some(_)) => std::cmp::Ordering::Greater, |
| 158 | (None, None) => a_suffix.cmp(&b_suffix), |
| 159 | }, |
| 160 | ) |
| 161 | .then_with(|| a.cmp(b)) |
| 162 | } |
| 163 | |
| 164 | impl IntentList { |
| 165 | /// The effective `kind` filter: `--review` is sugar for `--kind review`. |