MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / compare_human_keys

Function compare_human_keys

atomic-cli/src/commands/intent/list.rs:131–162  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

129/// Compare the last key component numerically without imposing an integer limit.
130/// Invalid suffixes remain deterministic and group with their own prefix.
131fn 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
164impl IntentList {
165 /// The effective `kind` filter: `--review` is sugar for `--kind review`.

Callers 1

sort_rowsFunction · 0.85

Calls 3

partsFunction · 0.85
cmpMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected