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:
parent
1005be095f
commit
55484436fa
1 changed files with 21 additions and 1 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue