You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package util
  5. import (
  6. "net/url"
  7. "path"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // OptionalBool a boolean that can be "null"
  11. type OptionalBool byte
  12. const (
  13. // OptionalBoolNone a "null" boolean value
  14. OptionalBoolNone = iota
  15. // OptionalBoolTrue a "true" boolean value
  16. OptionalBoolTrue
  17. // OptionalBoolFalse a "false" boolean value
  18. OptionalBoolFalse
  19. )
  20. // IsTrue return true if equal to OptionalBoolTrue
  21. func (o OptionalBool) IsTrue() bool {
  22. return o == OptionalBoolTrue
  23. }
  24. // IsFalse return true if equal to OptionalBoolFalse
  25. func (o OptionalBool) IsFalse() bool {
  26. return o == OptionalBoolFalse
  27. }
  28. // IsNone return true if equal to OptionalBoolNone
  29. func (o OptionalBool) IsNone() bool {
  30. return o == OptionalBoolNone
  31. }
  32. // OptionalBoolOf get the corresponding OptionalBool of a bool
  33. func OptionalBoolOf(b bool) OptionalBool {
  34. if b {
  35. return OptionalBoolTrue
  36. }
  37. return OptionalBoolFalse
  38. }
  39. // Max max of two ints
  40. func Max(a, b int) int {
  41. if a < b {
  42. return b
  43. }
  44. return a
  45. }
  46. // URLJoin joins url components, like path.Join, but preserving contents
  47. func URLJoin(base string, elems ...string) string {
  48. u, err := url.Parse(base)
  49. if err != nil {
  50. log.Error(4, "URLJoin: Invalid base URL %s", base)
  51. return ""
  52. }
  53. joinArgs := make([]string, 0, len(elems)+1)
  54. joinArgs = append(joinArgs, u.Path)
  55. joinArgs = append(joinArgs, elems...)
  56. u.Path = path.Join(joinArgs...)
  57. return u.String()
  58. }
  59. // Min min of two ints
  60. func Min(a, b int) int {
  61. if a > b {
  62. return b
  63. }
  64. return a
  65. }