github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/utils/string.go (about)

     1  package utils
     2  
     3  import "strings"
     4  
     5  // IncludesPrefix checks if needle is in haystack
     6  func Includes(haystack []string, needle string) bool {
     7  	for _, name := range haystack {
     8  		if name == needle {
     9  			return true
    10  		}
    11  	}
    12  	return false
    13  }
    14  
    15  // IncludesPrefix checks if any item in haystack is a prefix of needle
    16  func IncludesPrefix(haystack []string, needle string) bool {
    17  	for _, prefix := range haystack {
    18  		if strings.HasPrefix(needle, prefix) {
    19  			return true
    20  		}
    21  	}
    22  	return false
    23  }
    24  
    25  // Remove removes val from slice
    26  func Remove(s []string, val string) []string {
    27  	for i, v := range s {
    28  		if v == val {
    29  			return append(s[:i], s[i+1:]...)
    30  		}
    31  	}
    32  	return s
    33  }
    34  
    35  // JSONPointerPath builds a JSON pointer path according to spec, see
    36  // https://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07#section-3.
    37  func JSONPointerPath(s string) string {
    38  	pointer := strings.Replace(s, "~", "~0", -1)
    39  	return strings.Replace(pointer, "/", "~1", -1)
    40  }