From 4bad2e0b7637e2a361b746392a559f7298fc74a5 Mon Sep 17 00:00:00 2001 From: it512 Date: Sun, 30 Jun 2024 22:56:12 +0800 Subject: [PATCH 1/2] feat: add Compare function --- util.go | 6 ++++++ uuid_test.go | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/util.go b/util.go index 5ea6c73..6475740 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 compare two uuids +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 } From 43222358cf8a7ae01b8c4db4e95c15174fcae0bd Mon Sep 17 00:00:00 2001 From: it512 Date: Mon, 1 Jul 2024 22:19:29 +0800 Subject: [PATCH 2/2] fix comment --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 6475740..de4bfb1 100644 --- a/util.go +++ b/util.go @@ -43,7 +43,7 @@ func xtob(x1, x2 byte) (byte, bool) { return (b1 << 4) | b2, b1 != 255 && b2 != 255 } -// Compare compare two uuids +// 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[:]) }