digit

package
v1.20.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 10 Imported by: 1

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

View Source
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
)
View Source
const (
	// Digits is the default digits for base62 encoding/decoding.
	Digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.Digit](d T) T

Abs returns the absolute value of a number

func AppendInt added in v1.1.0

func AppendInt(dst []byte, i int64, base int) []byte

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

func AppendUint(dst []byte, i uint64, base int) []byte

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 Atoi added in v1.1.0

func Atoi(s string) (int, error)

Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.

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

func FormatByDict(dict []byte, num uint64) string

FormatByDict convert num into corresponding string according to dict.

func FormatInt added in v1.1.0

func FormatInt(i int64, base int) string

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

func FormatUint(i uint64, base int) string

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

func Itoa(i int) string

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

func ParseByDict[D constraints.Digit](dict []byte, numStr string) result.Result[D]

ParseByDict convert numStr into corresponding digit according to dict.

func ParseInt added in v1.1.0

func ParseInt(s string, base int, bitSize int) result.Result[int64]

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

func ParseUint(s string, base int, bitSize int) result.Result[uint64]

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 ToBool added in v1.1.0

func ToBool[T constraints.Digit](v T) bool

ToBool converts D to bool.

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

func TryFromString[T ~string, D constraints.Digit](v T, base int, bitSize int) result.Result[D]

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

func TryFromStrings[T ~string, D constraints.Digit](a []T, base int, bitSize int) result.Result[[]D]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL