Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

format.gno

2.70 Kb · 109 lines
  1package utils
  2
  3import (
  4	"strconv"
  5
  6	ufmt "gno.land/p/nt/ufmt/v0"
  7)
  8
  9// FormatInt converts any signed integer type to its base-10 string
 10// representation. It accepts int8, int16, int32, int64 and int, and panics on
 11// any other type.
 12//
 13// Parameters:
 14//   - v: the value to format; it must have type int8, int16, int32, int64, or int
 15//
 16// Returns:
 17//   - decimal: the base-10 representation of the supported signed integer
 18func FormatInt(v any) string {
 19	switch v := v.(type) {
 20	case int8:
 21		return strconv.FormatInt(int64(v), 10)
 22	case int16:
 23		return strconv.FormatInt(int64(v), 10)
 24	case int32:
 25		return strconv.FormatInt(int64(v), 10)
 26	case int64:
 27		return strconv.FormatInt(v, 10)
 28	case int:
 29		return strconv.Itoa(v)
 30	default:
 31		panic(ufmt.Sprintf("invalid type for FormatInt: %T", v))
 32	}
 33}
 34
 35// FormatUint converts any unsigned integer type to its base-10 string
 36// representation. It accepts uint8, uint16, uint32, uint64 and uint, and panics
 37// on any other type.
 38//
 39// Parameters:
 40//   - v: the value to format; it must have type uint8, uint16, uint32, uint64, or uint
 41//
 42// Returns:
 43//   - decimal: the base-10 representation of the supported unsigned integer
 44func FormatUint(v any) string {
 45	switch v := v.(type) {
 46	case uint8:
 47		return strconv.FormatUint(uint64(v), 10)
 48	case uint16:
 49		return strconv.FormatUint(uint64(v), 10)
 50	case uint32:
 51		return strconv.FormatUint(uint64(v), 10)
 52	case uint64:
 53		return strconv.FormatUint(v, 10)
 54	case uint:
 55		return strconv.FormatUint(uint64(v), 10)
 56	default:
 57		panic(ufmt.Sprintf("invalid type for FormatUint: %T", v))
 58	}
 59}
 60
 61// FormatBool converts a boolean to "true" or "false".
 62//
 63// Parameters:
 64//   - v: the boolean to format
 65//
 66// Returns:
 67//   - text: "true" or "false" corresponding to v
 68func FormatBool(v bool) string {
 69	return strconv.FormatBool(v)
 70}
 71
 72// Uint64ToString returns the base-10 string representation of a uint64.
 73//
 74// Parameters:
 75//   - v: the unsigned 64-bit integer to format
 76//
 77// Returns:
 78//   - decimal: the base-10 representation of v
 79func Uint64ToString(v uint64) string {
 80	return strconv.FormatUint(v, 10)
 81}
 82
 83// Int64ToString returns the base-10 string representation of an int64.
 84//
 85// Parameters:
 86//   - v: the signed 64-bit integer to format
 87//
 88// Returns:
 89//   - decimal: the base-10 representation of v
 90func Int64ToString(v int64) string {
 91	return strconv.FormatInt(v, 10)
 92}
 93
 94// SafeParseInt64 parses a base-10 string into an int64, panicking when the
 95// input is not a valid int64.
 96//
 97// Parameters:
 98//   - value: the base-10 text to parse as an int64
 99//
100// Returns:
101//   - value: the parsed int64; invalid or out-of-range text panics
102func SafeParseInt64(value string) int64 {
103	parsed, err := strconv.ParseInt(value, 10, 64)
104	if err != nil {
105		panic(err)
106	}
107
108	return parsed
109}