conv

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT, MIT Imports: 6 Imported by: 1

README

conv

forked from tidwall/conv, forked from tidwall/box

GoDoc

A Go package for converting various primitive types.

Designed to follow ECMA-262, therefore it differs slightly from how the built in strconv package works.

Please see the docs for more details.

Functions

Ttoi(t bool) int64      // bool -> int64
Ttou(t bool) uint64     // bool -> uint64
Ttof(t bool) float64    // bool -> float64
Ttoa(t bool) string     // bool -> string
Ttov(t bool) any        // bool -> any

Ftot(f float64) bool    // float64 -> bool
Ftoi(f float64) int64   // float64 -> int64
Ftou(f float64) uint64  // float64 -> uint64
Ftoa(f float64) string  // float64 -> string
Ftov(f float64) any     // float64 -> any

Itot(i int64) bool      // int64 -> bool
Itof(i int64) float64   // int64 -> float64
Itou(i int64) uint64    // int64 -> uint64
Itoa(i int64) string    // int64 -> string
Itov(i int64) any       // int64 -> any

Utot(u uint64) bool     // uint64 -> bool
Utof(u uint64) float64  // uint64 -> float64
Utoi(u uint64) int64    // uint64 -> int64
Utoa(u uint64) string   // uint64 -> string
Utov(u uint64) any      // uint64 -> any

Atot(a string) bool     // string -> bool
Atof(a string) float64  // string -> float64
Atoi(a string) int64    // string -> int64
Atou(a string) uint64   // string -> uint64
Atov(a string) any      // string -> any

Vtot(v any) bool        // any -> bool
Vtof(v any) float64     // any -> float64
Vtoi(v any) int64       // any -> int64
Vtou(v any) uint64      // any -> uint64
Vtoa(v any) string      // any -> string

box

GoDoc

experimental

Box is a Go package for wrapping value types. Works similar to an interface{} and is optimized for primitives, strings, and byte slices.

Features

  • Zero new allocations for wrapping primitives, strings, and []byte slices.
  • Uses a 128 bit structure. Same as interface{} on 64-bit architectures.
  • Allows for auto convertions between value types. No panics on assertions.
  • Pretty decent performance.

Examples

// A boxed value can hold various types, just like an interface{}.
var v box.Value

// box an int
v = box.Int(123)

// unbox the value
println(v.Int())
println(v.String())
println(v.Bool())

// box a string
v = box.String("hello")
println(v.String())

// Auto conversions between types
println(box.String("123.45").Float64())
println(box.Bool(false).String())
println(box.String("hello").IsString())

// output
// 123
// 123
// true
// hello
// +1.234500e+002
// false
// true

Performance

Below are some benchmarks comparing interface{} to box.Value.

  • Iface*/to: Convert a value to an interface{}.
  • Iface*/from: Convert an interface{} back to its original value.
  • Box*/to: Convert a value to box.Value.
  • Box*/from: Convert a box.Value back to its original value.
goos: darwin
goarch: arm64
pkg: github.com/tidwall/box
BenchmarkIfaceInt/to-10        10000000    8.921 ns/op    7 B/op   0 allocs/op
BenchmarkIfaceInt/from-10      10000000    0.6289 ns/op   0 B/op   0 allocs/op
BenchmarkBoxInt/to-10          10000000    1.334 ns/op    0 B/op   0 allocs/op
BenchmarkBoxInt/from-10        10000000    0.6823 ns/op   0 B/op   0 allocs/op
BenchmarkIfaceString/to-10     10000000   18.17 ns/op    16 B/op   1 allocs/op
BenchmarkIfaceString/from-10   10000000    0.8010 ns/op   0 B/op   0 allocs/op
BenchmarkBoxString/to-10       10000000    3.705 ns/op    0 B/op   0 allocs/op
BenchmarkBoxString/from-10     10000000    2.421 ns/op    0 B/op   0 allocs/op
BenchmarkIfaceBytes/to-10      10000000   21.62 ns/op    24 B/op   1 allocs/op
BenchmarkIfaceBytes/from-10    10000000    0.8104 ns/op   0 B/op   0 allocs/op
BenchmarkBoxBytes/to-10        10000000    2.881 ns/op    0 B/op   0 allocs/op
BenchmarkBoxBytes/from-10      10000000    2.366 ns/op    0 B/op   0 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Atof

func Atof(a string) float64

Atof converts string to float64 For infinte numbers use 'Infinity' or '-Infinity', not 'Inf' or '-Inf'. Returns NaN for invalid syntax

func Atoi

func Atoi(a string) int64

Atoi converts string to int64 Returns 0 for invalid syntax

func Atot

func Atot(a string) bool

Atot converts string to bool Always returns true unless string is empty.

func Atou

func Atou(a string) uint64

Atou converts string to uint64 Returns 0 for invalid syntax

func Atov

func Atov(a string) interface{}

Atov converts string to any

func Ftoa

func Ftoa(f float64) string

Ftoa converts float64 to string Returns 'Infinity' or '-Infinity', not 'Inf' or '-Inf', for infinite numbers. Returns 'NaN' for not-a-number.

func Ftoi

func Ftoi(f float64) int64

Ftoi converts float64 to int64

func Ftot

func Ftot(f float64) bool

Ftot converts float64 to bool

func Ftou

func Ftou(f float64) uint64

Ftou converts float64 to uint64

func Ftov

func Ftov(f float64) interface{}

Ftov converts float64 to any

func Itoa

func Itoa(i int64) string

Itoa converts int64 to string

func Itof

func Itof(i int64) float64

Itof converts int64 to float64

func Itot

func Itot(i int64) bool

Itot converts int64 to bool

func Itou

func Itou(i int64) uint64

Itou converts int64 to uint64

func Itov

func Itov(i int64) interface{}

Itov converts int64 to any

func Ttoa

func Ttoa(t bool) string

Ttoa converts bool to string

func Ttof

func Ttof(t bool) float64

Ttof converts bool to float64

func Ttoi

func Ttoi(t bool) int64

Ttoi converts bool to int64

func Ttou

func Ttou(t bool) uint64

Ttou converts bool to uint64

func Ttov

func Ttov(t bool) interface{}

Ttov converts bool to any

func Utoa

func Utoa(u uint64) string

Utoa converts uint64 to string

func Utof

func Utof(u uint64) float64

Utof converts uint64 to float64

func Utoi

func Utoi(u uint64) int64

Utoi converts uint64 to int64

func Utot

func Utot(u uint64) bool

Utot converts uint64 to bool

func Utov

func Utov(u uint64) interface{}

Utov converts uint64 to any

func Vtoa

func Vtoa(v interface{}) string

Vtoa converts any to string

func Vtof

func Vtof(v interface{}) float64

Vtof converts any to float64

func Vtoi

func Vtoi(v interface{}) int64

Vtoi converts any to int64

func Vtot

func Vtot(v interface{}) bool

Vtot converts any to bool

func Vtou

func Vtou(v interface{}) uint64

Vtou converts any to uint64

Types

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is a boxed value

func Any

func Any(v interface{}) Value

Any boxes anything

func Bool

func Bool(t bool) Value

Bool boxes a bool

func Byte

func Byte(x byte) Value

Byte boxes a byte

func Bytes

func Bytes(b []byte) Value

Bytes boxes a byte slice

func CustomBits

func CustomBits(x uint64) Value

CustomBits boxes a custom value.

func Float32

func Float32(x float32) Value

Float32 boxes a float32

func Float64

func Float64(f float64) Value

Float64 boxes a float64

func Int

func Int(x int) Value

Int boxes an int

func Int16

func Int16(x int16) Value

Int16 boxes an int16

func Int32

func Int32(x int32) Value

Int32 boxes an int32

func Int64

func Int64(x int64) Value

Int64 boxes an int64

func Int8

func Int8(x int8) Value

Int8 boxes an int8

func Nil

func Nil() Value

Nil boxes a nil. This is the same as the default `box.Value{}`.

func String

func String(s string) Value

String boxes a string value

func StringWithTag

func StringWithTag(s string, tag uint16) Value

StringWithTag boxes a string value and adds a custom tag.

func Uint

func Uint(x uint) Value

Uint boxes a uint

func Uint16

func Uint16(x uint16) Value

Uint16 boxes a uint16

func Uint32

func Uint32(x uint32) Value

Uint32 boxes a uint32

func Uint64

func Uint64(x uint64) Value

Uint64 boxes a uint64

func Uint8

func Uint8(x uint8) Value

Uint8 boxes a uint8

func (Value) Any

func (v Value) Any() interface{}

Any returns the value as an `any/interface{}` type.

func (Value) Bool

func (v Value) Bool() bool

Bool returns the value as a bool

func (Value) Byte

func (v Value) Byte() byte

Byte returns the value as a byte

func (Value) Bytes

func (v Value) Bytes() []byte

Bytes returns the value as a byte slice. When the boxed value is a `[]byte` then those original bytes are returned. Otherwise, the string representation of the value is returned, which will be equivalent to `[]byte(value.String())`.

func (Value) Float32

func (v Value) Float32() float32

Float32 returns the value as a float32

func (Value) Float64

func (v Value) Float64() float64

Float64 returns the value as a float64

func (Value) Int

func (v Value) Int() int

Int returns the value as an int

func (Value) Int16

func (v Value) Int16() int16

Int16 returns the value as an int16

func (Value) Int32

func (v Value) Int32() int32

Int32 returns the value as an int32

func (Value) Int64

func (v Value) Int64() int64

Int64 returns the value as an int64

func (Value) Int8

func (v Value) Int8() int8

Int8 returns the value as an int8

func (Value) IsBool

func (v Value) IsBool() bool

IsBool returns true if the boxed value is a bool primitive.

func (Value) IsBytes

func (v Value) IsBytes() bool

IsBytes returns true if the boxed value is a []byte.

func (Value) IsCustomBits

func (v Value) IsCustomBits() bool

IsCustomBits returns true if the boxed value was created using box.CustomBits.

func (Value) IsFloat

func (v Value) IsFloat() bool

IsFloat returns true if the boxed value is a float-like primitive: float32, float64

func (Value) IsInt

func (v Value) IsInt() bool

IsInt returns true if the boxed value is an int-like primitive: int, int8, int16, int32, int64, byte

func (Value) IsNil

func (v Value) IsNil() bool

IsNil returns true if the boxed value is nil.

func (Value) IsNumber

func (v Value) IsNumber() bool

IsNumber returns true if the boxed value is an numeric-like primitive: int, int8, int16, int32, int64, byte, uint, uint8, uint16, uint32, uint64, float32, float64

func (Value) IsString

func (v Value) IsString() bool

IsString returns true if the boxed value is a string.

func (Value) IsUint

func (v Value) IsUint() bool

IsUint returns true if the boxed value is an uint-like primitive: uint, uint8, uint16, uint32, uint64

func (Value) String

func (v Value) String() string

String returns the value as a string.

func (Value) Tag

func (v Value) Tag() uint16

Tag returns the tag from a value created by box.StringWithTag

func (Value) Uint

func (v Value) Uint() uint

Uint returns the value as a uint

func (Value) Uint16

func (v Value) Uint16() uint16

Uint16 returns the value as a uint16

func (Value) Uint32

func (v Value) Uint32() uint32

Uint32 returns the value as a uint32

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 returns the value as a uint64

func (Value) Uint8

func (v Value) Uint8() uint8

Uint8 returns the value as a uint8

Jump to

Keyboard shortcuts

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