geom/ints/conv.go

98 lines
2.4 KiB
Go

package ints
import (
"bytes"
"strconv"
"strings"
)
// Itoa returns i as a string.
func Itoa(i int) string {
return strconv.Itoa(i)
}
// Itoa64 returns i as string.
func Itoa64(i int64) string {
return strconv.FormatInt(i, 10)
}
// FmtInts formats the integer slice as a string each item separated by sep.
func FmtInts(ints []int, sep string) string {
var out bytes.Buffer
for i, val := range ints {
if i > 0 {
out.WriteString(sep)
}
out.WriteString(Itoa(val))
}
return out.String()
}
// FmtInts64 formats the integer slice as a string each item separated by sep.
func FmtInts64(ints []int64, sep string) string {
var out bytes.Buffer
for i, val := range ints {
if i > 0 {
out.WriteString(sep)
}
out.WriteString(Itoa64(val))
}
return out.String()
}
// MustAtoi parses string s as an integer. Method panics if the conversion fails.
func MustAtoi(s string) int {
var i, err = strconv.Atoi(s)
if nil != err {
panic("error converting string to int")
}
return i
}
// MustAtoi64 parses string s as an integer. Method panics if the conversion fails.
func MustAtoi64(s string) int64 {
var i, err = strconv.ParseInt(s, 10, 64)
if nil != err {
panic("error converting string to int")
}
return i
}
// MustAstois parses the strings as integers. Method panics if one of the conversion fails.
func MustAstois(s ...string) []int {
var res = make([]int, len(s))
for i := range s {
res[i] = MustAtoi(s[i])
}
return res
}
// MustAstois64 parses the strings as integers. Method panics if one of the conversion fails.
func MustAstois64(s ...string) []int64 {
var res = make([]int64, len(s))
for i := range s {
res[i] = MustAtoi64(s[i])
}
return res
}
// SplitInts splits the string s by separator sep, converts every part to an integer and returns all the integers as a slice. Method panics if one of the conversion fails.
func SplitInts(s, sep string) []int {
var numbers = strings.Split(s, sep)
var ints = make([]int, len(numbers))
for i, n := range numbers {
ints[i] = MustAtoi(n)
}
return ints
}
// SplitInts64 splits the string s by separator sep, converts every part to an integer and returns all the integers as a slice. Method panics if one of the conversion fails.
func SplitInts64(s, sep string) []int64 {
var numbers = strings.Split(s, sep)
var ints = make([]int64, len(numbers))
for i, n := range numbers {
ints[i] = MustAtoi64(n)
}
return ints
}