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

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