Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion anyint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func AnyInt64From(prefix string, v int64) (AnyInt64, error) {
func ParseAnyInt64(s string) (AnyInt64, error) {
j := strings.LastIndex(s, "_") + 1
pref, suffix := s[:max(0, j-1)], s[j:]
if len(suffix) != int64SuffixLen {
if len(suffix) != Int64SuffixLen {
return AnyInt64{}, fmt.Errorf("typeid: invalid format: %q", s)
}
v, err := decodeBase32Int64(suffix)
Expand Down
2 changes: 1 addition & 1 deletion anyuuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func AnyUUIDFrom(prefix string, u uuid.UUID) (AnyUUID, error) {
func ParseAnyUUID(s string) (AnyUUID, error) {
j := strings.LastIndex(s, "_") + 1
pref, suffix := s[:max(0, j-1)], s[j:]
if len(suffix) != uuidSuffixLen {
if len(suffix) != UUIDSuffixLen {
return AnyUUID{}, fmt.Errorf("typeid: invalid format: %q", s)
}
b, err := decodeBase32UUID(suffix)
Expand Down
14 changes: 7 additions & 7 deletions crockford.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func decodeChar(c byte) (byte, error) {

// appendBase32UUID appends an optional prefix and underscore, then the 26-char typeid suffix for u.
func appendBase32UUID(dst []byte, prefix string, u uuid.UUID) []byte {
dst = slices.Grow(dst, uuidSuffixLen+len(prefix)+min(1, len(prefix)))
dst = slices.Grow(dst, UUIDSuffixLen+len(prefix)+min(1, len(prefix)))
if len(prefix) > 0 {
dst = append(append(dst, prefix...), '_')
}
hi := binary.BigEndian.Uint64(u[:8])
lo := binary.BigEndian.Uint64(u[8:])

var buf [uuidSuffixLen]byte
var buf [UUIDSuffixLen]byte

for i := 25; i >= 14; i-- {
buf[i] = alphabet[lo&0x1F]
Expand All @@ -63,7 +63,7 @@ func appendBase32UUID(dst []byte, prefix string, u uuid.UUID) []byte {

// UUID decoding (26 chars -> 128 bits)
func decodeBase32UUID(s string) ([16]byte, error) {
if len(s) != uuidSuffixLen {
if len(s) != UUIDSuffixLen {
return [16]byte{}, fmt.Errorf("typeid: invalid suffix length %d", len(s))
}

Expand Down Expand Up @@ -108,12 +108,12 @@ func decodeBase32UUID(s string) ([16]byte, error) {

// appendBase32Int64 appends an optional prefix and underscore, then the 13-char typeid suffix for n.
func appendBase32Int64(dst []byte, prefix string, n int64) []byte {
dst = slices.Grow(dst, int64SuffixLen+len(prefix)+min(1, len(prefix)))
dst = slices.Grow(dst, Int64SuffixLen+len(prefix)+min(1, len(prefix)))
if len(prefix) > 0 {
dst = append(append(dst, prefix...), '_')
}
u := uint64(n)
var buf [int64SuffixLen]byte
var buf [Int64SuffixLen]byte

for i := 12; i >= 1; i-- {
buf[i] = alphabet[u&0x1F]
Expand All @@ -126,7 +126,7 @@ func appendBase32Int64(dst []byte, prefix string, n int64) []byte {

// Int64 decoding (13 chars -> 63 bits)
func decodeBase32Int64(s string) (int64, error) {
if len(s) != int64SuffixLen {
if len(s) != Int64SuffixLen {
return 0, fmt.Errorf("typeid: invalid suffix length %d", len(s))
}

Expand All @@ -139,7 +139,7 @@ func decodeBase32Int64(s string) (int64, error) {
}
val := uint64(v)

for i := 1; i < int64SuffixLen; i++ {
for i := 1; i < Int64SuffixLen; i++ {
v, err = decodeChar(s[i])
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Int64From[P Prefixer](v int64) (Int64[P], error) {
}

func ParseInt64[P Prefixer](s string) (Int64[P], error) {
suffix, err := splitTypeid[P](s, int64SuffixLen)
suffix, err := splitTypeid[P](s, Int64SuffixLen)
if err != nil {
return Int64[P]{}, err
}
Expand Down
8 changes: 6 additions & 2 deletions typeid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ var (
ErrOverflowInt64 = errors.New("typeid: value overflows int64")
)

// Suffix lengths of the two id encodings, in Crockford base32 characters —
// stable wire-format invariants (a UUID is 128 bits, an Int64 is 63 bits).
// Exposed so callers that length-classify a typeid string need not hardcode the
// widths.
const (
uuidSuffixLen = 26 // 130-bit capacity, 128 used
int64SuffixLen = 13 // 65-bit capacity, 63 used
UUIDSuffixLen = 26 // 130-bit capacity, 128 used
Int64SuffixLen = 13 // 65-bit capacity, 63 used
)

// CBOR constants for hand-encoded marshal/unmarshal.
Expand Down
2 changes: 1 addition & 1 deletion uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func UUIDFrom[P Prefixer](u uuid.UUID) (UUID[P], error) {
}

func ParseUUID[P Prefixer](s string) (UUID[P], error) {
suffix, err := splitTypeid[P](s, uuidSuffixLen)
suffix, err := splitTypeid[P](s, UUIDSuffixLen)
if err != nil {
return UUID[P]{}, err
}
Expand Down