Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Compare function #163

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package uuid

import (
"bytes"
"io"
)

Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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.

Copy link
Contributor Author

@it512 it512 Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok !
thank you!

func Compare(a, b UUID) int {
return bytes.Compare(a[:], b[:])
}
6 changes: 3 additions & 3 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading