41 lines
690 B
Go
41 lines
690 B
Go
package buf2d
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
return func(value T) string {
|
|
return fmt.Sprintf("%v", value)
|
|
}
|
|
}
|