From 27f465ac2258a26628e032cea4b35ce5985f7e8b Mon Sep 17 00:00:00 2001 From: MikeWang Date: Tue, 2 Jul 2024 00:15:43 +0800 Subject: [PATCH] feat: add Compare function (#163) * feat: add Compare function * fix comment --- util.go | 6 ++++++ uuid_test.go | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/util.go b/util.go index 5ea6c73..de4bfb1 100644 --- a/util.go +++ b/util.go @@ -5,6 +5,7 @@ package uuid import ( + "bytes" "io" ) @@ -41,3 +42,8 @@ func xtob(x1, x2 byte) (byte, bool) { b2 := xvalues[x2] return (b1 << 4) | b2, b1 != 255 && b2 != 255 } + +// Compare returns an integer comparing two uuids lexicographically. The result will be 0 if a == b, -1 if a < b, and +1 if a > b. +func Compare(a, b UUID) int { + return bytes.Compare(a[:], b[:]) +} diff --git a/uuid_test.go b/uuid_test.go index c1c6001..d848382 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -918,10 +918,10 @@ func TestVersion7MonotonicityStrict(t *testing.T) { defer SetRand(nil) length := 100000 // > 3906 - u1 := Must(NewV7()).String() + u1 := Must(NewV7()) for i := 0; i < length; i++ { - u2 := Must(NewV7()).String() - if u2 <= u1 { + u2 := Must(NewV7()) + if Compare(u1, u2) >= 0 { t.Errorf("monotonicity failed at #%d: %s(next) < %s(before)", i, u2, u1) break }