1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-05 03:55:58 +01:00

XX Fingerprint: adjust the compare operation to produce the same order as sequoia-wot, for hex-shaped fingerprints

This commit is contained in:
Heiko Schaefer 2023-07-10 18:41:19 +02:00
parent 1005be095f
commit 55484436fa
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D

View file

@ -13,7 +13,27 @@ class Fingerprint(fingerprint: String) : Comparable<Fingerprint> {
}
override fun compareTo(other: Fingerprint): Int {
return fingerprint.compareTo(other.fingerprint)
// The strings are equal
if (this == other) return 0
// Compare char by char.
// This ordering has the property that Hex-encoded Fingerprints are
// ordered in the same way as in sequoia-wot.
// (As they would be, if they were byte arrays).
//
// For other formats of "Fingerprint", the specific ordering doesn't matter to us.
//
// Following the sequoia-wot ordering means that we get the exact same output for the test suite.
val a = this.fingerprint.toCharArray()
val b = other.fingerprint.toCharArray()
a.zip(b).forEach { (x, y) ->
if (x != y) return x.compareTo(y)
}
// If prefix is the same, but one string is longer, we consider the longer one "bigger"
if (a.size != b.size) return a.size.compareTo(b.size)
throw RuntimeException("This should be unreachable")
}
override fun equals(other: Any?): Boolean {