utils.go 595 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "math"
  4. "strings"
  5. )
  6. func maxInt(a, b int) int {
  7. if a > b {
  8. return a
  9. }
  10. return b
  11. }
  12. func minInt(a, b int) int {
  13. if a < b {
  14. return a
  15. }
  16. return b
  17. }
  18. func minNonZeroInt(a, b int) int {
  19. switch {
  20. case a == 0:
  21. return b
  22. case b == 0:
  23. return a
  24. }
  25. return minInt(a, b)
  26. }
  27. func roundToInt(a float64) int {
  28. return int(math.Round(a))
  29. }
  30. func scaleInt(a int, scale float64) int {
  31. if a == 0 {
  32. return 0
  33. }
  34. return roundToInt(float64(a) * scale)
  35. }
  36. func trimAfter(s string, sep byte) string {
  37. i := strings.IndexByte(s, sep)
  38. if i < 0 {
  39. return s
  40. }
  41. return s[:i]
  42. }