From c5e41bd36d8c818a256b0823c9c2fd1fa011b06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20-=20=E3=82=A2=E3=83=AC=E3=83=83=E3=82=AF=E3=82=B9?= Date: Wed, 17 Jun 2026 11:52:55 +0200 Subject: [PATCH] feat: export UUID and Int64 suffix length constants Rename uuidSuffixLen/int64SuffixLen to UUIDSuffixLen/Int64SuffixLen so callers that length-classify a typeid string (e.g. peeling a fixed-width segment off a composite prefix) can reference the widths instead of hardcoding 26/13. The values are stable wire-format invariants; this is a pure API addition with no behavior change. --- anyint64.go | 2 +- anyuuid.go | 2 +- crockford.go | 14 +++++++------- int64.go | 2 +- typeid.go | 8 ++++++-- uuid.go | 2 +- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/anyint64.go b/anyint64.go index 0e8470b..c578184 100644 --- a/anyint64.go +++ b/anyint64.go @@ -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) diff --git a/anyuuid.go b/anyuuid.go index 750e78a..602d422 100644 --- a/anyuuid.go +++ b/anyuuid.go @@ -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) diff --git a/crockford.go b/crockford.go index 6b96099..594d68f 100644 --- a/crockford.go +++ b/crockford.go @@ -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] @@ -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)) } @@ -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] @@ -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)) } @@ -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 diff --git a/int64.go b/int64.go index 25b8088..5aec121 100644 --- a/int64.go +++ b/int64.go @@ -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 } diff --git a/typeid.go b/typeid.go index 424cd72..80f2e76 100644 --- a/typeid.go +++ b/typeid.go @@ -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. diff --git a/uuid.go b/uuid.go index 3e76acf..0774497 100644 --- a/uuid.go +++ b/uuid.go @@ -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 }