summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/pkgver/mod.rs162
1 files changed, 116 insertions, 46 deletions
diff --git a/src/pkgver/mod.rs b/src/pkgver/mod.rs
index 75be308..e968256 100644
--- a/src/pkgver/mod.rs
+++ b/src/pkgver/mod.rs
@@ -87,66 +87,136 @@ pub fn vercmp(a: &str, b: &str) -> Ordering {
va.cmp_full(&vb)
}
-#[derive(Debug, PartialEq, Eq)]
+fn is_alnum(c: u8) -> bool {
+ c.is_ascii_alphanumeric()
+}
+
+/// The chunk starting at `k`, and the index just past it.
enum Chunk<'a> {
Num(u128),
Alpha(&'a str),
+ None,
}
-/// Split into maximal digit / alpha runs, ignoring everything else.
-fn chunks(s: &str) -> Vec<Chunk<'_>> {
- let b = s.as_bytes();
- let mut out = Vec::new();
- let mut i = 0;
- while i < b.len() {
- let c = b[i] as char;
- if c.is_ascii_digit() {
- let start = i;
- while i < b.len() && (b[i] as char).is_ascii_digit() {
- i += 1;
- }
- // Leading zeros are not significant.
- let digits = s[start..i].trim_start_matches('0');
- out.push(Chunk::Num(digits.parse::<u128>().unwrap_or(0)));
- } else if c.is_ascii_alphabetic() {
- let start = i;
- while i < b.len() && (b[i] as char).is_ascii_alphabetic() {
- i += 1;
- }
- out.push(Chunk::Alpha(&s[start..i]));
- } else {
+fn read_chunk(s: &[u8], k: usize) -> (Chunk<'_>, usize) {
+ if k >= s.len() {
+ return (Chunk::None, k);
+ }
+ if s[k].is_ascii_digit() {
+ let mut i = k;
+ while i < s.len() && s[i].is_ascii_digit() {
+ i += 1;
+ }
+ // Leading zeros are not significant.
+ let digits = std::str::from_utf8(&s[k..i]).unwrap_or("0").trim_start_matches('0');
+ (Chunk::Num(digits.parse::<u128>().unwrap_or(0)), i)
+ } else if s[k].is_ascii_alphabetic() {
+ let mut i = k;
+ while i < s.len() && s[i].is_ascii_alphabetic() {
i += 1;
}
+ (Chunk::Alpha(std::str::from_utf8(&s[k..i]).unwrap_or("")), i)
+ } else {
+ (Chunk::None, k)
}
- out
}
-/// ALPM's `vercmp_string`: chunk-wise comparison where digits precede letters.
+/// Was the character before `k` a separator? A chunk at the very start of a
+/// version counts as attached.
+fn preceded_by_separator(s: &[u8], k: usize) -> bool {
+ k > 0 && !is_alnum(s[k - 1])
+}
+
+/// ALPM's `vercmp`, character-wise.
+///
+/// Three rules do all the work, and each one is surprising the first time:
+///
+/// 1. A numeric chunk is newer than an alphabetic chunk at the same position, so
+/// `1.0.1` > `1.0.a`.
+/// 2. When one string runs out, the other side decides by its *next raw
+/// character*: a letter there makes the exhausted string newer (`1.0` >
+/// `1.0a`, the `1.0rc1 < 1.0` rule), anything else makes it older
+/// (`1.0` < `1.0.1`).
+/// 3. Separators are not merely skipped. An alphabetic chunk that follows a
+/// separator is newer than one glued to the previous chunk, whatever the
+/// letters are: `1.0.a` > `1.0z`. Two versions that differ only in where the
+/// dot sits are *not* equal, which is the part a chunk-splitting
+/// implementation gets wrong.
+///
+/// The cross-product test in `tests/versions.rs` checks this against pacman's own
+/// `vercmp` over ~1100 pairs, which is the only reason anyone can be confident
+/// about rule 3.
pub fn vercmp_string(a: &str, b: &str) -> Ordering {
- let ca = chunks(a);
- let cb = chunks(b);
- let mut i = 0;
+ let (a, b) = (a.as_bytes(), b.as_bytes());
+ let (mut i, mut j) = (0usize, 0usize);
loop {
- match (ca.get(i), cb.get(i)) {
- (None, None) => return Ordering::Equal,
- (None, Some(Chunk::Num(_))) => return Ordering::Less,
- (None, Some(Chunk::Alpha(_))) => return Ordering::Greater,
- (Some(Chunk::Num(_)), None) => return Ordering::Greater,
- (Some(Chunk::Alpha(_)), None) => return Ordering::Less,
- (Some(x), Some(y)) => {
- let o = match (x, y) {
- (Chunk::Num(x), Chunk::Num(y)) => x.cmp(y),
- (Chunk::Alpha(x), Chunk::Alpha(y)) => x.cmp(y),
- // A numeric chunk is always older than an alphabetic one.
- (Chunk::Num(_), Chunk::Alpha(_)) => Ordering::Less,
- (Chunk::Alpha(_), Chunk::Num(_)) => Ordering::Greater,
- };
- if o != Ordering::Equal {
- return o;
+ // End-of-string checks come before skipping separators, because whether
+ // the next character is glued or separated is exactly what decides them.
+ match (i >= a.len(), j >= b.len()) {
+ (true, true) => return Ordering::Equal,
+ (true, false) => {
+ return if j < b.len() && b[j].is_ascii_alphabetic() {
+ Ordering::Greater
+ } else {
+ Ordering::Less
+ }
+ }
+ (false, true) => {
+ return if i < a.len() && a[i].is_ascii_alphabetic() {
+ Ordering::Less
+ } else {
+ Ordering::Greater
+ }
+ }
+ (false, false) => {}
+ }
+ while i < a.len() && !is_alnum(a[i]) {
+ i += 1;
+ }
+ while j < b.len() && !is_alnum(b[j]) {
+ j += 1;
+ }
+ if i >= a.len() && j >= b.len() {
+ return Ordering::Equal;
+ }
+ if i >= a.len() || j >= b.len() {
+ let alpha = if i >= a.len() {
+ j < b.len() && b[j].is_ascii_alphabetic()
+ } else {
+ i < a.len() && a[i].is_ascii_alphabetic()
+ };
+ let a_first = i >= a.len();
+ return match (a_first, alpha) {
+ (true, true) => Ordering::Greater,
+ (true, false) => Ordering::Less,
+ (false, true) => Ordering::Less,
+ (false, false) => Ordering::Greater,
+ };
+ }
+ let (ca, na) = read_chunk(a, i);
+ let (cb, nb) = read_chunk(b, j);
+ match (ca, cb) {
+ (Chunk::Num(x), Chunk::Num(y)) => {
+ if x != y {
+ return x.cmp(&y);
+ }
+ }
+ (Chunk::Alpha(x), Chunk::Alpha(y)) => {
+ let (xa, yb) = (preceded_by_separator(a, i), preceded_by_separator(b, j));
+ if xa != yb {
+ return xa.cmp(&yb);
+ }
+ if x != y {
+ return x.cmp(y);
}
- i += 1;
}
+ // A numeric chunk is always newer than an alphabetic one.
+ (Chunk::Num(_), Chunk::Alpha(_)) => return Ordering::Greater,
+ (Chunk::Alpha(_), Chunk::Num(_)) => return Ordering::Less,
+ _ => {}
}
+ i = na;
+ j = nb;
}
}
@@ -287,7 +357,7 @@ mod tests {
assert_eq!(ord("1.0-1", "1.0-2"), Ordering::Less);
assert_eq!(ord("1.1-1", "1.0-1"), Ordering::Greater);
assert_eq!(ord("1.10-1", "1.9-1"), Ordering::Greater, "numeric chunks compare numerically");
- assert_eq!(ord("1.0.a-1", "1.0.1-1"), Ordering::Greater, "alpha sorts after numeric");
+ assert_eq!(ord("1.0.a-1", "1.0.1-1"), Ordering::Less, "a numeric chunk is newer than an alphabetic one");
assert_eq!(ord("1.5-1", "1.5.0-1"), Ordering::Less, "a trailing numeric chunk is newer");
assert_eq!(ord("1:1.0-1", "2.0-1"), Ordering::Greater, "epoch dominates");
assert_eq!(ord("1.0-1", "1.0_2-1"), Ordering::Less, "non-alnum separators are ignored");