package geom import ( "bytes" "strconv" "strings" ) // AbsInt returns the absolute value of i. func AbsInt(i int) int { if 0 > i { return -i } return i } // 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() } // MinInt returns the minimum of a and b. func MinInt(a, b int) int { if a < b { return a } return b } // MinInt64 returns the minimum of a and b. func MinInt64(a, b int64) int64 { if a < b { return a } return b } // MaxInt returns the maximum of a and b. func MaxInt(a, b int) int { if a > b { return a } return b } // MaxInt64 returns the maximum of a and b. func MaxInt64(a, b int64) int64 { if a > b { return a } return b } // 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 } // NormInt returns the normalized value of i (-1, 0 or 1). func NormInt(i int) int { if i < 0 { return -1 } if i > 0 { return 1 } return 0 } // NormInt64 returns the normalized value of i (-1, 0 or 1). func NormInt64(i int64) int64 { if i < 0 { return -1 } if i > 0 { return 1 } return 0 } // 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 } // SubAbsInt subtracts a from b and returns the absolute value of the result. func SubAbsInt(a, b int) int { if a > b { return a - b } return b - a }