Skip to content

Commit

Permalink
Merge pull request #1 from twharmon/string-doc
Browse files Browse the repository at this point in the history
Fix String documentation
  • Loading branch information
twharmon authored Jan 31, 2022
2 parents 6cdfbd1 + 4c26a7d commit 8d29f1d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions gouid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ var (
MixedCaseAlpha = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
)

// String returns a string with the given size. Ids are
// made with cryptographically secure random bytes and
// umatch /^[a-z0-9]$/.
// String returns a string with the given size made up of
// characters from the given charset. Ids are made with
// cryptographically secure random bytes. The length of
// charset must not exceed 256.
func String(size int, charset []byte) string {
b := make([]byte, size, size)
b := make([]byte, size)
rand.Read(b)
for i := 0; i < size; i++ {
charCnt := byte(len(charset))
index := (b[i] / (255 / charCnt)) % charCnt
charCnt := len(charset)
index := (b[i] / byte(256/charCnt)) % byte(charCnt)
b[i] = charset[index]
}
return *(*string)(unsafe.Pointer(&b))
}

// Bytes returns cryptographically secure random bytes.
func Bytes(size int) GOUID {
b := make([]byte, size, size)
b := make([]byte, size)
rand.Read(b)
return b
}
Expand Down
4 changes: 2 additions & 2 deletions gouid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestBytes(t *testing.T) {
if len(id) != length {
t.Error("lengh of id was not 16")
}
if bytes.Compare(id, gouid.Bytes(length)) == 0 {
if bytes.Equal(id, gouid.Bytes(length)) {
t.Error("collision")
}
}
Expand All @@ -96,7 +96,7 @@ func TestBytesUnmarshal(t *testing.T) {
t.Error(err)
t.FailNow()
}
if bytes.Compare(m["id"], []byte{1}) != 0 {
if !bytes.Equal(m["id"], []byte{1}) {
t.Errorf("bad json unmarshal: %v != %v", m["id"], []byte{1})
}
}
Expand Down

0 comments on commit 8d29f1d

Please sign in to comment.