buf2d/utils.go

41 lines
690 B
Go
Raw Permalink Normal View History

2020-10-01 13:14:36 +02:00
package buf2d
2022-03-31 12:37:35 +02:00
import (
"fmt"
"reflect"
)
2020-10-01 13:14:36 +02:00
func limit(v, min, max int) int {
return getmax(getmin(v, max), min)
}
func getmax(x, y int) int {
if x > y {
return x
}
return y
}
func getmin(x, y int) int {
if x < y {
return x
}
return y
}
2022-03-31 12:37:35 +02:00
func MakeDefaultStringFunc[T any]() func(T) string {
if reflect.TypeOf(new(T)).Elem() == reflect.TypeOf(new(rune)).Elem() {
return func(value T) string {
return string((interface{}(value)).(rune))
}
} else if reflect.TypeOf(new(T)).Elem() == reflect.TypeOf(new(string)).Elem() {
return func(value T) string {
return (interface{}(value)).(string)
}
}
2022-03-31 12:37:35 +02:00
return func(value T) string {
return fmt.Sprintf("%v", value)
}
}