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.

87 lines
1.9 KiB

  1. // Copyright 2013 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com
  15. import (
  16. "strings"
  17. )
  18. // AppendStr appends string to slice with no duplicates.
  19. func AppendStr(strs []string, str string) []string {
  20. for _, s := range strs {
  21. if s == str {
  22. return strs
  23. }
  24. }
  25. return append(strs, str)
  26. }
  27. // CompareSliceStr compares two 'string' type slices.
  28. // It returns true if elements and order are both the same.
  29. func CompareSliceStr(s1, s2 []string) bool {
  30. if len(s1) != len(s2) {
  31. return false
  32. }
  33. for i := range s1 {
  34. if s1[i] != s2[i] {
  35. return false
  36. }
  37. }
  38. return true
  39. }
  40. // CompareSliceStr compares two 'string' type slices.
  41. // It returns true if elements are the same, and ignores the order.
  42. func CompareSliceStrU(s1, s2 []string) bool {
  43. if len(s1) != len(s2) {
  44. return false
  45. }
  46. for i := range s1 {
  47. for j := len(s2) - 1; j >= 0; j-- {
  48. if s1[i] == s2[j] {
  49. s2 = append(s2[:j], s2[j+1:]...)
  50. break
  51. }
  52. }
  53. }
  54. if len(s2) > 0 {
  55. return false
  56. }
  57. return true
  58. }
  59. // IsSliceContainsStr returns true if the string exists in given slice, ignore case.
  60. func IsSliceContainsStr(sl []string, str string) bool {
  61. str = strings.ToLower(str)
  62. for _, s := range sl {
  63. if strings.ToLower(s) == str {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. // IsSliceContainsInt64 returns true if the int64 exists in given slice.
  70. func IsSliceContainsInt64(sl []int64, i int64) bool {
  71. for _, s := range sl {
  72. if s == i {
  73. return true
  74. }
  75. }
  76. return false
  77. }