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.7 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "math/rand"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // OptionalBool a boolean that can be "null"
  13. type OptionalBool byte
  14. const (
  15. // OptionalBoolNone a "null" boolean value
  16. OptionalBoolNone = iota
  17. // OptionalBoolTrue a "true" boolean value
  18. OptionalBoolTrue
  19. // OptionalBoolFalse a "false" boolean value
  20. OptionalBoolFalse
  21. )
  22. // IsTrue return true if equal to OptionalBoolTrue
  23. func (o OptionalBool) IsTrue() bool {
  24. return o == OptionalBoolTrue
  25. }
  26. // IsFalse return true if equal to OptionalBoolFalse
  27. func (o OptionalBool) IsFalse() bool {
  28. return o == OptionalBoolFalse
  29. }
  30. // IsNone return true if equal to OptionalBoolNone
  31. func (o OptionalBool) IsNone() bool {
  32. return o == OptionalBoolNone
  33. }
  34. // OptionalBoolOf get the corresponding OptionalBool of a bool
  35. func OptionalBoolOf(b bool) OptionalBool {
  36. if b {
  37. return OptionalBoolTrue
  38. }
  39. return OptionalBoolFalse
  40. }
  41. // Max max of two ints
  42. func Max(a, b int) int {
  43. if a < b {
  44. return b
  45. }
  46. return a
  47. }
  48. // Min min of two ints
  49. func Min(a, b int) int {
  50. if a > b {
  51. return b
  52. }
  53. return a
  54. }
  55. // IsEmptyString checks if the provided string is empty
  56. func IsEmptyString(s string) bool {
  57. return len(strings.TrimSpace(s)) == 0
  58. }
  59. // NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)
  60. func NormalizeEOL(input []byte) []byte {
  61. var right, left, pos int
  62. if right = bytes.IndexByte(input, '\r'); right == -1 {
  63. return input
  64. }
  65. length := len(input)
  66. tmp := make([]byte, length)
  67. // We know that left < length because otherwise right would be -1 from IndexByte.
  68. copy(tmp[pos:pos+right], input[left:left+right])
  69. pos += right
  70. tmp[pos] = '\n'
  71. left += right + 1
  72. pos++
  73. for left < length {
  74. if input[left] == '\n' {
  75. left++
  76. }
  77. right = bytes.IndexByte(input[left:], '\r')
  78. if right == -1 {
  79. copy(tmp[pos:], input[left:])
  80. pos += length - left
  81. break
  82. }
  83. copy(tmp[pos:pos+right], input[left:left+right])
  84. pos += right
  85. tmp[pos] = '\n'
  86. left += right + 1
  87. pos++
  88. }
  89. return tmp[:pos]
  90. }
  91. func AddZero(t int64) (m string) {
  92. if t < 10 {
  93. m = "0" + strconv.FormatInt(t, 10)
  94. return m
  95. } else {
  96. return strconv.FormatInt(t, 10)
  97. }
  98. }
  99. func ConvertDisplayJobNameToJobName(DisplayName string) (JobName string) {
  100. t := time.Now()
  101. JobName = strings.ToLower(cutNameString(DisplayName, 15)) + "t" + t.Format("20060102150405")[10:] + strconv.Itoa(int(rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(100000)))
  102. return JobName
  103. }
  104. func cutNameString(str string, lens int) string {
  105. if len(str) < lens {
  106. return str
  107. }
  108. return str[:lens]
  109. }