github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/util/strutil.go (about)

     1  /*
     2  Copyright 2018 The OpenEBS Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"regexp"
    21  	"strings"
    22  )
    23  
    24  // Contains is a util function which returns true if one key is present in array
    25  // else it returns false
    26  func Contains(s []string, k string) bool {
    27  	for _, e := range s {
    28  		if e == k {
    29  			return true
    30  		}
    31  	}
    32  	return false
    33  }
    34  
    35  // ContainsIgnoredCase is a util function which returns true if one key is present
    36  // in array else it returns false. This function is not case sensitive.
    37  func ContainsIgnoredCase(s []string, k string) bool {
    38  	for _, e := range s {
    39  		if strings.EqualFold(e, k) {
    40  			return true
    41  		}
    42  	}
    43  	return false
    44  }
    45  
    46  // MatchIgnoredCase is a util function which returns true if any of the keys
    47  // are present as a string in given string - s
    48  // This function is not case sensitive.
    49  func MatchIgnoredCase(keys []string, s string) bool {
    50  	for _, k := range keys {
    51  		if strings.Contains(strings.ToLower(s), strings.ToLower(k)) {
    52  			return true
    53  		}
    54  	}
    55  	return false
    56  }
    57  
    58  // RemoveString removes all occurrences of a string from slice
    59  func RemoveString(slice []string, s string) (result []string) {
    60  	for _, item := range slice {
    61  		if item != s {
    62  			result = append(result, item)
    63  		}
    64  	}
    65  	return result
    66  }
    67  
    68  // IsMatchRegex is a utility function which returns true if the string -s
    69  // matches with the regex specified.
    70  func IsMatchRegex(regex, s string) bool {
    71  	r := regexp.MustCompile(regex)
    72  	return r.MatchString(s)
    73  }
    74  
    75  // AddUniqueStringtoSlice ensures there are no repeated devices added to a slice
    76  func AddUniqueStringtoSlice(names []string, name string) []string {
    77  
    78  	if len(names) == 0 {
    79  		names = append(names, name)
    80  		return names
    81  	}
    82  	shouldAppend := false
    83  	for _, n := range names {
    84  		if strings.Compare(n, name) == 0 {
    85  			shouldAppend = false
    86  			break
    87  		} else {
    88  			shouldAppend = true
    89  		}
    90  	}
    91  	if shouldAppend {
    92  		names = append(names, name)
    93  	}
    94  	return names
    95  }