Playground
codec.gno
package utils
import (
u256 "gno.land/p/gnoswap/uint256/v1"
"gno.land/p/nt/ufmt/v0"
)
// EncodeUint16 converts a uint16 to a fixed-width two-byte big-endian string.
//
// Parameters:
// - value: Unsigned 16-bit integer to encode.
//
// Returns:
// - encoded: Exactly two bytes in big-endian order; lexicographic order matches numeric order.
func EncodeUint16(value uint16) string {
var buf [2]byte
buf[0] = byte(value >> 8)
buf[1] = byte(value)
return string(buf[:])
}
// DecodeUint16 decodes a fixed-width two-byte big-endian string.
//
// Parameters:
// - key: Exactly two encoded bytes, with the most-significant byte first.
//
// Returns:
// - value: The uint16 represented by key.
//
// Panics if key is not exactly two bytes long.
func DecodeUint16(key string) uint16 {
if len(key) != 2 {
panic(ufmt.Sprintf("invalid uint16 key length: %d", len(key)))
}
return uint16(key[0])<<8 | uint16(key[1])
}
// EncodeInt32 converts an int32 to a fixed-width four-byte big-endian string.
// Flipping the sign bit makes lexicographic order match signed numeric order.
//
// Parameters:
// - value: Signed 32-bit integer to encode.
//
// Returns:
// - encoded: Exactly four order-preserving big-endian bytes.
func EncodeInt32(value int32) string {
ordered := uint32(value) ^ (uint32(1) << 31)
var buf [4]byte
buf[0] = byte(ordered >> 24)
buf[1] = byte(ordered >> 16)
buf[2] = byte(ordered >> 8)
buf[3] = byte(ordered)
return string(buf[:])
}
// DecodeInt32 decodes a four-byte order-preserving big-endian string.
//
// Parameters:
// - key: Exactly four bytes produced by EncodeInt32.
//
// Returns:
// - value: The signed int32 recovered after reversing the sign-bit transform.
//
// Panics if key is not exactly four bytes long.
func DecodeInt32(key string) int32 {
if len(key) != 4 {
panic(ufmt.Sprintf("invalid int32 key length: %d", len(key)))
}
ordered := uint32(key[0])<<24 |
uint32(key[1])<<16 |
uint32(key[2])<<8 |
uint32(key[3])
return int32(ordered ^ (uint32(1) << 31))
}
// EncodeUint64 converts a uint64 to a fixed-width eight-byte big-endian string.
//
// Parameters:
// - value: Unsigned 64-bit integer to encode.
//
// Returns:
// - encoded: Exactly eight bytes in big-endian order.
func EncodeUint64(value uint64) string {
var buf [8]byte
buf[0] = byte(value >> 56)
buf[1] = byte(value >> 48)
buf[2] = byte(value >> 40)
buf[3] = byte(value >> 32)
buf[4] = byte(value >> 24)
buf[5] = byte(value >> 16)
buf[6] = byte(value >> 8)
buf[7] = byte(value)
return string(buf[:])
}
// DecodeUint64 decodes a fixed-width eight-byte big-endian string.
//
// Parameters:
// - key: Exactly eight encoded bytes, with the most-significant byte first.
//
// Returns:
// - value: The uint64 represented by key.
//
// Panics if key is not exactly eight bytes long.
func DecodeUint64(key string) uint64 {
if len(key) != 8 {
panic(ufmt.Sprintf("invalid uint64 key length: %d", len(key)))
}
return uint64(key[0])<<56 |
uint64(key[1])<<48 |
uint64(key[2])<<40 |
uint64(key[3])<<32 |
uint64(key[4])<<24 |
uint64(key[5])<<16 |
uint64(key[6])<<8 |
uint64(key[7])
}
// EncodeUint256 converts a 256-bit unsigned integer to a fixed-width 32-byte
// big-endian string, emitting the most-significant limb first.
//
// Parameters:
// - value: Non-nil *u256.Uint value to encode.
//
// Returns:
// - encoded: Exactly 32 big-endian bytes representing value.
func EncodeUint256(value *u256.Uint) string {
return EncodeUint64(value[3]) + EncodeUint64(value[2]) +
EncodeUint64(value[1]) + EncodeUint64(value[0])
}
// DecodeUint256 decodes a fixed-width 32-byte big-endian uint256 encoding.
//
// Parameters:
// - encoded: Exactly 32 bytes containing the most-significant byte first.
//
// Returns:
// - value: A new *u256.Uint populated from encoded.
//
// Panics if encoded is not exactly 32 bytes long.
func DecodeUint256(encoded string) *u256.Uint {
if len(encoded) != 32 {
panic(ufmt.Sprintf("invalid uint256 encoding length: %d", len(encoded)))
}
return u256.Zero().SetBytes([]byte(encoded))
}
format.gno
package utils
import (
"strconv"
ufmt "gno.land/p/nt/ufmt/v0"
)
// FormatInt converts any signed integer type to its base-10 string
// representation. It accepts int8, int16, int32, int64 and int, and panics on
// any other type.
//
// Parameters:
// - v: the value to format; it must have type int8, int16, int32, int64, or int
//
// Returns:
// - decimal: the base-10 representation of the supported signed integer
func FormatInt(v any) string {
switch v := v.(type) {
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case int:
return strconv.Itoa(v)
default:
panic(ufmt.Sprintf("invalid type for FormatInt: %T", v))
}
}
// FormatUint converts any unsigned integer type to its base-10 string
// representation. It accepts uint8, uint16, uint32, uint64 and uint, and panics
// on any other type.
//
// Parameters:
// - v: the value to format; it must have type uint8, uint16, uint32, uint64, or uint
//
// Returns:
// - decimal: the base-10 representation of the supported unsigned integer
func FormatUint(v any) string {
switch v := v.(type) {
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
default:
panic(ufmt.Sprintf("invalid type for FormatUint: %T", v))
}
}
// FormatBool converts a boolean to "true" or "false".
//
// Parameters:
// - v: the boolean to format
//
// Returns:
// - text: "true" or "false" corresponding to v
func FormatBool(v bool) string {
return strconv.FormatBool(v)
}
// Uint64ToString returns the base-10 string representation of a uint64.
//
// Parameters:
// - v: the unsigned 64-bit integer to format
//
// Returns:
// - decimal: the base-10 representation of v
func Uint64ToString(v uint64) string {
return strconv.FormatUint(v, 10)
}
// Int64ToString returns the base-10 string representation of an int64.
//
// Parameters:
// - v: the signed 64-bit integer to format
//
// Returns:
// - decimal: the base-10 representation of v
func Int64ToString(v int64) string {
return strconv.FormatInt(v, 10)
}
// SafeParseInt64 parses a base-10 string into an int64, panicking when the
// input is not a valid int64.
//
// Parameters:
// - value: the base-10 text to parse as an int64
//
// Returns:
// - value: the parsed int64; invalid or out-of-range text panics
func SafeParseInt64(value string) int64 {
parsed, err := strconv.ParseInt(value, 10, 64)
if err != nil {
panic(err)
}
return parsed
}
gnomod.toml
module = "gno.land/p/gnoswap/utils/v1" gno = "0.9" [addpkg] creator = "g1q5tjjv3kkslzwf2na9y9wk7e5a8ugph8xfsk04" height = 557737 max_deposit = "100000000ugnot"