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

utils.gno

2.53 Kb · 112 lines
  1package governance
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	ufmt "gno.land/p/nt/ufmt/v0"
  8)
  9
 10// numberKind represents the type of number to parse.
 11type numberKind int
 12
 13const (
 14	kindInt numberKind = iota
 15	kindInt64
 16	kindUint64
 17)
 18
 19// parseNumber parses a string to a number (int, int64, or uint64) with proper validation.
 20func parseNumber(s string, kind numberKind) any {
 21	if len(strings.TrimSpace(s)) == 0 {
 22		panic(ufmt.Sprint("invalid number value: empty or whitespace string"))
 23	}
 24
 25	switch kind {
 26	case kindInt:
 27		num, err := strconv.ParseInt(s, 10, 64)
 28		if err != nil {
 29			panic(ufmt.Sprintf("invalid int value: %s", s))
 30		}
 31		return int(num)
 32	case kindInt64:
 33		num, err := strconv.ParseInt(s, 10, 64)
 34		if err != nil {
 35			panic(ufmt.Sprintf("invalid int64 value: %s", s))
 36		}
 37		return num
 38	case kindUint64:
 39		num, err := strconv.ParseUint(s, 10, 64)
 40		if err != nil {
 41			panic(ufmt.Sprintf("invalid uint64 value: %s", s))
 42		}
 43		return num
 44	default:
 45		panic(ufmt.Sprintf("unsupported number kind: %v", kind))
 46	}
 47}
 48
 49// parseBool parses a string to a boolean.
 50func parseBool(s string) bool {
 51	if len(strings.TrimSpace(s)) == 0 {
 52		panic(ufmt.Sprint("invalid bool value: empty or whitespace string"))
 53	}
 54
 55	switch s {
 56	case "true":
 57		return true
 58	case "false":
 59		return false
 60	default:
 61		panic(ufmt.Sprintf("invalid bool value: %s", s))
 62	}
 63}
 64
 65// parseInt parses a string to int with proper validation and overflow checking.
 66func parseInt(s string) int {
 67	if len(strings.TrimSpace(s)) == 0 {
 68		panic(ufmt.Sprint("invalid int value: empty or whitespace string"))
 69	}
 70
 71	num, err := strconv.ParseInt(s, 10, 64)
 72	if err != nil {
 73		panic(ufmt.Sprintf("invalid int value: %s", s))
 74	}
 75
 76	const maxInt = int(^uint(0) >> 1)
 77	const minInt = -maxInt - 1
 78
 79	if num > int64(maxInt) || num < int64(minInt) {
 80		panic(ufmt.Sprintf("int overflow: value %d exceeds int range [%d, %d]", num, minInt, maxInt))
 81	}
 82
 83	return int(num)
 84}
 85
 86// parseInt64 parses a string to int64 with proper validation.
 87func parseInt64(s string) int64 {
 88	if len(strings.TrimSpace(s)) == 0 {
 89		panic(ufmt.Sprint("invalid int64 value: empty or whitespace string"))
 90	}
 91
 92	num, err := strconv.ParseInt(s, 10, 64)
 93	if err != nil {
 94		panic(ufmt.Sprintf("invalid int64 value: %s", s))
 95	}
 96
 97	return num
 98}
 99
100// parseUint64 parses a string to uint64 with proper validation.
101func parseUint64(s string) uint64 {
102	if len(strings.TrimSpace(s)) == 0 {
103		panic(ufmt.Sprint("invalid uint64 value: empty or whitespace string"))
104	}
105
106	num, err := strconv.ParseUint(s, 10, 64)
107	if err != nil {
108		panic(ufmt.Sprintf("invalid uint64 value: %s", s))
109	}
110
111	return num
112}