Documentation
¶
Overview ¶
Package digit provides generic functions for numeric digit operations.
This package offers utilities for converting between numeric types and strings, including base-62 encoding/decoding and various numeric conversions.
Base62 Encoding Comparison:
Feature FormatUint(62) big.Int.Text(62)
Value Range Limited to uint64 Unlimited
Byte Array Must convert to uint64 Direct (SetBytes)
(may overflow)
Performance Fast path for small General algorithm
integers
Interface Compatible with strconv Independent
Append Supported (zero-copy) Not supported
IMPORTANT: Size Limitations
- FormatUint(62) only supports uint64 range (0 to 18,446,744,073,709,551,615)
- For byte arrays larger than 8 bytes, must convert to uint64 first, but may overflow
- big.Int.Text(62) has no size limit and can handle arbitrarily large byte arrays
Use FormatUint when:
- Working with small to medium integers (<= uint64)
- Need strconv compatibility
- Need Append operations (zero-copy)
- Performance optimization for small integers is important
Use big.Int.Text(62) when:
- Working with byte arrays (especially large ones)
- Need to handle arbitrarily large integers
- Don't need strconv compatibility
Package digit provides generic functions for numeric digit operations.
This package offers utilities for converting between numeric types and strings, including base-62 encoding/decoding and various numeric conversions.
Examples ¶
// Convert int to base-62 string
str := digit.Itoa62(12345) // Returns base-62 encoded string
// Convert base-62 string to int
result := digit.Atoi62("dnh")
if result.IsOk() {
fmt.Println(result.Unwrap()) // Output: 12345
}
Index ¶
- Constants
- func Abs[T constraints.Digit](d T) T
- func AppendInt(dst []byte, i int64, base int) []byte
- func AppendUint(dst []byte, i uint64, base int) []byte
- func As[T constraints.Digit, D constraints.Digit](v T) result.Result[D]
- func Atoi(s string) (int, error)
- func CheckedAdd[T constraints.Integer](a, b T) option.Option[T]
- func CheckedMul[T constraints.Integer](a, b T) option.Option[T]
- func FormatByDict(dict []byte, num uint64) string
- func FormatInt(i int64, base int) string
- func FormatUint(i uint64, base int) string
- func FromBool[T ~bool, D constraints.Digit](v T) D
- func FromBools[T ~bool, D constraints.Digit](a []T) (b []D)
- func Itoa(i int) string
- func Max[T constraints.Integer]() T
- func ParseByDict[D constraints.Digit](dict []byte, numStr string) result.Result[D]
- func ParseInt(s string, base int, bitSize int) result.Result[int64]
- func ParseUint(s string, base int, bitSize int) result.Result[uint64]
- func SaturatingAdd[T constraints.Integer](a, b T) T
- func SaturatingSub[T constraints.Digit](a, b T) T
- func SliceAs[T constraints.Digit, D constraints.Digit](a []T) (b []D, err error)
- func ToBool[T constraints.Digit](v T) bool
- func ToBools[T constraints.Digit](a []T) []bool
- func TryFromString[T ~string, D constraints.Digit](v T, base int, bitSize int) result.Result[D]
- func TryFromStrings[T ~string, D constraints.Digit](a []T, base int, bitSize int) result.Result[[]D]
Constants ¶
const ( // Host64bit indicates whether the current host is a 64-bit architecture Host64bit = strconv.IntSize == 64 // Host32bit indicates whether the current host is a 32-bit architecture Host32bit = ^uint(0)>>32 == 0 )
const (
// Digits is the default digits for base62 encoding/decoding.
Digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
Variables ¶
This section is empty.
Functions ¶
func AppendInt ¶ added in v1.1.0
AppendInt appends the string form of the integer i, as generated by FormatInt, to dst and returns the extended buffer. NOTE:
Compatible with standard package strconv.
func AppendUint ¶ added in v1.1.0
AppendUint appends the string form of the unsigned integer i, as generated by FormatUint, to dst and returns the extended buffer. NOTE:
Compatible with standard package strconv.
func As ¶ added in v1.1.0
func As[T constraints.Digit, D constraints.Digit](v T) result.Result[D]
func CheckedAdd ¶ added in v1.0.0
func CheckedAdd[T constraints.Integer](a, b T) option.Option[T]
CheckedAdd performs checked addition Returns None if the result overflows, otherwise returns Some(result)
func CheckedMul ¶ added in v1.1.0
func CheckedMul[T constraints.Integer](a, b T) option.Option[T]
CheckedMul performs checked multiplication Returns None if the result overflows, otherwise returns Some(result)
func FormatByDict ¶ added in v1.1.0
FormatByDict convert num into corresponding string according to dict.
func FormatInt ¶ added in v1.1.0
FormatInt returns the string representation of i in the given base, for 2 <= base <= 62. NOTE:
Compatible with standard package strconv.
func FormatUint ¶ added in v1.1.0
FormatUint returns the string representation of i in the given base, for 2 <= base <= 62.
NOTE:
Compatible with standard package strconv.
Comparison with math/big.Int.Text(62) for base62 encoding:
Feature FormatUint(62) big.Int.Text(62)
Value Range Limited to uint64 Unlimited
Byte Array Must convert to uint64 Direct (SetBytes)
(may overflow)
Performance Fast path for small General algorithm
integers
Interface Compatible with strconv Independent
Append Supported (zero-copy) Not supported
IMPORTANT: Size Limitations
- FormatUint(62) only supports uint64 range (0 to 18,446,744,073,709,551,615)
- For byte arrays larger than 8 bytes, must convert to uint64 first, but may overflow
- big.Int.Text(62) has no size limit and can handle arbitrarily large byte arrays
Use FormatUint when:
- Working with small to medium integers (<= uint64)
- Need strconv compatibility
- Need Append operations (zero-copy)
- Performance optimization for small integers is important
Use big.Int.Text(62) when:
- Working with byte arrays (especially large ones)
- Need to handle arbitrarily large integers
- Don't need strconv compatibility
func FromBool ¶ added in v1.1.0
func FromBool[T ~bool, D constraints.Digit](v T) D
FromBool converts bool to digit.
func FromBools ¶ added in v1.1.0
func FromBools[T ~bool, D constraints.Digit](a []T) (b []D)
func Itoa ¶ added in v1.1.0
Itoa is equivalent to FormatInt(int64(i), 10). NOTE:
Compatible with standard package strconv.
func Max ¶ added in v1.0.0
func Max[T constraints.Integer]() T
Max returns the maximum value for integer type T
func ParseByDict ¶ added in v1.1.0
ParseByDict convert numStr into corresponding digit according to dict.
func ParseInt ¶ added in v1.1.0
ParseInt interprets a string s in the given base (0, 2 to 62) and bit size (0 to 64) and returns the corresponding value i.
If base == 0, the base is implied by the string's prefix: base 2 for "0b", base 8 for "0" or "0o", base 16 for "0x", and base 10 otherwise. Also, for base == 0 only, underscore characters are permitted per the Go integer literal syntax. If base is below 0, is 1, or is above 62, an error is returned.
The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. If bitSize is below 0 or above 64, an error is returned.
The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign. NOTE:
Compatible with standard package strconv.
func ParseUint ¶ added in v1.1.0
ParseUint is like ParseInt but for unsigned numbers. NOTE:
Compatible with standard package strconv.
func SaturatingAdd ¶ added in v1.0.0
func SaturatingAdd[T constraints.Integer](a, b T) T
SaturatingAdd performs saturating addition If the result overflows, it returns the maximum value of type T
func SaturatingSub ¶ added in v1.0.0
func SaturatingSub[T constraints.Digit](a, b T) T
SaturatingSub performs saturating subtraction If the result underflows (a < b), it returns 0
func SliceAs ¶ added in v1.1.0
func SliceAs[T constraints.Digit, D constraints.Digit](a []T) (b []D, err error)
SliceAs creates a copy of the digit slice.
func ToBools ¶ added in v1.1.0
func ToBools[T constraints.Digit](a []T) []bool
ToBools converts []D to []bool.
func TryFromString ¶ added in v1.1.0
TryFromString converts ~string to digit. If base == 0, the base is implied by the string's prefix: base 2 for "0b", base 8 for "0" or "0o", base 16 for "0x", and base 10 otherwise. Also, for base == 0 only, underscore characters are permitted per the Go integer literal syntax. If base is below 0, is 1, or is above 62, an error is returned.
func TryFromStrings ¶ added in v1.1.0
Types ¶
This section is empty.