github.com/kaydxh/golang@v0.0.131/go/strings/string_slice.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package strings
    23  
    24  import (
    25  	"strings"
    26  
    27  	set_ "github.com/kaydxh/golang/go/container/set"
    28  )
    29  
    30  func SliceUnique(s ...string) []string {
    31  	ss := set_.NewString()
    32  	for _, v := range s {
    33  		ss.Insert(v)
    34  	}
    35  
    36  	return ss.List()
    37  }
    38  
    39  func SliceIntersection(s1 []string, s2 []string) []string {
    40  	ss1 := set_.NewObject()
    41  	for _, s := range s1 {
    42  		ss1.Insert(s)
    43  	}
    44  
    45  	ss2 := set_.NewObject()
    46  	for _, s := range s2 {
    47  		ss2.Insert(s)
    48  	}
    49  
    50  	ss := []string{}
    51  	for _, v := range ss1.Intersection(ss2).List() {
    52  		s, ok := v.(string)
    53  		if ok {
    54  			ss = append(ss, s)
    55  		}
    56  	}
    57  	return ss
    58  }
    59  
    60  func SliceDifference(s1 []string, s2 []string) []string {
    61  	ss1 := set_.NewObject()
    62  	for _, s := range s1 {
    63  		ss1.Insert(s)
    64  	}
    65  
    66  	ss2 := set_.NewObject()
    67  	for _, s := range s2 {
    68  		ss2.Insert(s)
    69  	}
    70  
    71  	ss := []string{}
    72  	for _, v := range ss1.Difference(ss2).List() {
    73  		s, ok := v.(string)
    74  		if ok {
    75  			ss = append(ss, s)
    76  		}
    77  	}
    78  
    79  	return ss
    80  }
    81  
    82  func SliceWithCondition(s1 []string, cond func(s2 string) bool) []string {
    83  	ss1 := set_.NewObject()
    84  	for _, s := range s1 {
    85  		if cond(s) {
    86  			ss1.Insert(s)
    87  		}
    88  	}
    89  
    90  	ss := []string{}
    91  	for _, v := range ss1.List() {
    92  		s, ok := v.(string)
    93  		if ok {
    94  			ss = append(ss, s)
    95  		}
    96  	}
    97  
    98  	return ss
    99  }
   100  
   101  func RemoveEmpty(s []string) []string {
   102  	var ss []string
   103  	for _, v := range s {
   104  		if v != "" {
   105  			ss = append(ss, v)
   106  		}
   107  	}
   108  
   109  	return ss
   110  }
   111  
   112  // sliceContains reports whether the provided string is present in the given slice of strings.
   113  func SliceContainsCaseInSensitive(list []string, target string) bool {
   114  	return SliceContains(list, target, false)
   115  }
   116  
   117  func SliceContains(list []string, target string, caseSensitive bool) bool {
   118  	if !caseSensitive {
   119  		target = strings.ToLower(target)
   120  	}
   121  
   122  	for _, s := range list {
   123  		if !caseSensitive {
   124  			s = strings.ToLower(s)
   125  		}
   126  
   127  		if s == target {
   128  			return true
   129  		}
   130  	}
   131  	return false
   132  }
   133  
   134  /*
   135  func SliceIntersectionInt(s1 []int, s2 []int) []int {
   136  	ss1 := set_.NewObject(set_.GenerateArray([...]int(s1...)))
   137  	for _, s := range s1 {
   138  		ss1.Insert(s)
   139  	}
   140  
   141  	ss2 := set_.NewObject()
   142  	for _, s := range s2 {
   143  		ss2.Insert(s)
   144  	}
   145  
   146  	ss := []string{}
   147  	for _, v := range ss1.Intersection(ss2).List() {
   148  		s, ok := v.(string)
   149  		if ok {
   150  			ss = append(ss, s)
   151  		}
   152  	}
   153  	return ss
   154  }
   155  */