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.

102 lines
2.1 KiB

  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. "strings"
  8. )
  9. // OptionalBool a boolean that can be "null"
  10. type OptionalBool byte
  11. const (
  12. // OptionalBoolNone a "null" boolean value
  13. OptionalBoolNone = iota
  14. // OptionalBoolTrue a "true" boolean value
  15. OptionalBoolTrue
  16. // OptionalBoolFalse a "false" boolean value
  17. OptionalBoolFalse
  18. )
  19. // IsTrue return true if equal to OptionalBoolTrue
  20. func (o OptionalBool) IsTrue() bool {
  21. return o == OptionalBoolTrue
  22. }
  23. // IsFalse return true if equal to OptionalBoolFalse
  24. func (o OptionalBool) IsFalse() bool {
  25. return o == OptionalBoolFalse
  26. }
  27. // IsNone return true if equal to OptionalBoolNone
  28. func (o OptionalBool) IsNone() bool {
  29. return o == OptionalBoolNone
  30. }
  31. // OptionalBoolOf get the corresponding OptionalBool of a bool
  32. func OptionalBoolOf(b bool) OptionalBool {
  33. if b {
  34. return OptionalBoolTrue
  35. }
  36. return OptionalBoolFalse
  37. }
  38. // Max max of two ints
  39. func Max(a, b int) int {
  40. if a < b {
  41. return b
  42. }
  43. return a
  44. }
  45. // Min min of two ints
  46. func Min(a, b int) int {
  47. if a > b {
  48. return b
  49. }
  50. return a
  51. }
  52. // IsEmptyString checks if the provided string is empty
  53. func IsEmptyString(s string) bool {
  54. return len(strings.TrimSpace(s)) == 0
  55. }
  56. // NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)
  57. func NormalizeEOL(input []byte) []byte {
  58. var right, left, pos int
  59. if right = bytes.IndexByte(input, '\r'); right == -1 {
  60. return input
  61. }
  62. length := len(input)
  63. tmp := make([]byte, length)
  64. // We know that left < length because otherwise right would be -1 from IndexByte.
  65. copy(tmp[pos:pos+right], input[left:left+right])
  66. pos += right
  67. tmp[pos] = '\n'
  68. left += right + 1
  69. pos++
  70. for left < length {
  71. if input[left] == '\n' {
  72. left++
  73. }
  74. right = bytes.IndexByte(input[left:], '\r')
  75. if right == -1 {
  76. copy(tmp[pos:], input[left:])
  77. pos += length - left
  78. break
  79. }
  80. copy(tmp[pos:pos+right], input[left:left+right])
  81. pos += right
  82. tmp[pos] = '\n'
  83. left += right + 1
  84. pos++
  85. }
  86. return tmp[:pos]
  87. }