2016-05-28 15:30:18 +00:00
|
|
|
package common
|
|
|
|
|
2016-05-28 18:24:39 +00:00
|
|
|
import "strconv"
|
|
|
|
|
2016-05-28 15:30:18 +00:00
|
|
|
// In picks x if y < x, picks z if y > z, or if none of the previous
|
|
|
|
// conditions is satisfies, it simply picks y.
|
2016-05-28 18:24:39 +00:00
|
|
|
func In(x, y, z int) int {
|
2016-05-28 15:30:18 +00:00
|
|
|
switch {
|
|
|
|
case y < x:
|
|
|
|
return x
|
|
|
|
case y > z:
|
|
|
|
return z
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
2016-05-28 18:24:39 +00:00
|
|
|
|
|
|
|
// InString takes y as a string, also allows for a default value should y be
|
|
|
|
// invalid as a number.
|
|
|
|
func InString(x int, y string, z, def int) int {
|
|
|
|
num, err := strconv.Atoi(y)
|
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return In(x, num, z)
|
|
|
|
}
|