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

util.go 2.3 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "bytes"
  7. "strconv"
  8. "strings"
  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. // Min min of two ints
  47. func Min(a, b int) int {
  48. if a > b {
  49. return b
  50. }
  51. return a
  52. }
  53. // IsEmptyString checks if the provided string is empty
  54. func IsEmptyString(s string) bool {
  55. return len(strings.TrimSpace(s)) == 0
  56. }
  57. // NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)
  58. func NormalizeEOL(input []byte) []byte {
  59. var right, left, pos int
  60. if right = bytes.IndexByte(input, '\r'); right == -1 {
  61. return input
  62. }
  63. length := len(input)
  64. tmp := make([]byte, length)
  65. // We know that left < length because otherwise right would be -1 from IndexByte.
  66. copy(tmp[pos:pos+right], input[left:left+right])
  67. pos += right
  68. tmp[pos] = '\n'
  69. left += right + 1
  70. pos++
  71. for left < length {
  72. if input[left] == '\n' {
  73. left++
  74. }
  75. right = bytes.IndexByte(input[left:], '\r')
  76. if right == -1 {
  77. copy(tmp[pos:], input[left:])
  78. pos += length - left
  79. break
  80. }
  81. copy(tmp[pos:pos+right], input[left:left+right])
  82. pos += right
  83. tmp[pos] = '\n'
  84. left += right + 1
  85. pos++
  86. }
  87. return tmp[:pos]
  88. }
  89. func AddZero(t int64) (m string) {
  90. if t < 10 {
  91. m = "0" + strconv.FormatInt(t, 10)
  92. return m
  93. } else {
  94. return strconv.FormatInt(t, 10)
  95. }
  96. }