Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

compare.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "sort"
  7. "strings"
  8. )
  9. // Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
  10. type Int64Slice []int64
  11. func (p Int64Slice) Len() int { return len(p) }
  12. func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
  13. func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  14. // IsSliceInt64Eq returns if the two slice has the same elements but different sequences.
  15. func IsSliceInt64Eq(a, b []int64) bool {
  16. if len(a) != len(b) {
  17. return false
  18. }
  19. sort.Sort(Int64Slice(a))
  20. sort.Sort(Int64Slice(b))
  21. for i := 0; i < len(a); i++ {
  22. if a[i] != b[i] {
  23. return false
  24. }
  25. }
  26. return true
  27. }
  28. // ExistsInSlice returns true if string exists in slice.
  29. func ExistsInSlice(target string, slice []string) bool {
  30. i := sort.Search(len(slice),
  31. func(i int) bool { return slice[i] == target })
  32. return i < len(slice)
  33. }
  34. // IsStringInSlice sequential searches if string exists in slice.
  35. func IsStringInSlice(target string, slice []string, insensitive ...bool) bool {
  36. caseInsensitive := false
  37. if len(insensitive) != 0 && insensitive[0] {
  38. caseInsensitive = true
  39. target = strings.ToLower(target)
  40. }
  41. for i := 0; i < len(slice); i++ {
  42. if caseInsensitive {
  43. if strings.ToLower(slice[i]) == target {
  44. return true
  45. }
  46. } else {
  47. if slice[i] == target {
  48. return true
  49. }
  50. }
  51. }
  52. return false
  53. }
  54. // IsInt64InSlice sequential searches if int64 exists in slice.
  55. func IsInt64InSlice(target int64, slice []int64) bool {
  56. for i := 0; i < len(slice); i++ {
  57. if slice[i] == target {
  58. return true
  59. }
  60. }
  61. return false
  62. }
  63. // IsEqualSlice returns true if slices are equal.
  64. func IsEqualSlice(target []string, source []string) bool {
  65. if len(target) != len(source) {
  66. return false
  67. }
  68. if (target == nil) != (source == nil) {
  69. return false
  70. }
  71. sort.Strings(target)
  72. sort.Strings(source)
  73. for i, v := range target {
  74. if v != source[i] {
  75. return false
  76. }
  77. }
  78. return true
  79. }