github.com/gdevillele/moby@v1.13.0/pkg/stringutils/stringutils.go (about)

     1  // Package stringutils provides helper functions for dealing with strings.
     2  package stringutils
     3  
     4  import (
     5  	"bytes"
     6  	"math/rand"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/pkg/random"
    10  )
    11  
    12  // GenerateRandomAlphaOnlyString generates an alphabetical random string with length n.
    13  func GenerateRandomAlphaOnlyString(n int) string {
    14  	// make a really long string
    15  	letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    16  	b := make([]byte, n)
    17  	for i := range b {
    18  		b[i] = letters[random.Rand.Intn(len(letters))]
    19  	}
    20  	return string(b)
    21  }
    22  
    23  // GenerateRandomASCIIString generates an ASCII random string with length n.
    24  func GenerateRandomASCIIString(n int) string {
    25  	chars := "abcdefghijklmnopqrstuvwxyz" +
    26  		"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
    27  		"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "
    28  	res := make([]byte, n)
    29  	for i := 0; i < n; i++ {
    30  		res[i] = chars[rand.Intn(len(chars))]
    31  	}
    32  	return string(res)
    33  }
    34  
    35  // Ellipsis truncates a string to fit within maxlen, and appends ellipsis (...).
    36  // For maxlen of 3 and lower, no ellipsis is appended.
    37  func Ellipsis(s string, maxlen int) string {
    38  	r := []rune(s)
    39  	if len(r) <= maxlen {
    40  		return s
    41  	}
    42  	if maxlen <= 3 {
    43  		return string(r[:maxlen])
    44  	}
    45  	return string(r[:maxlen-3]) + "..."
    46  }
    47  
    48  // Truncate truncates a string to maxlen.
    49  func Truncate(s string, maxlen int) string {
    50  	r := []rune(s)
    51  	if len(r) <= maxlen {
    52  		return s
    53  	}
    54  	return string(r[:maxlen])
    55  }
    56  
    57  // InSlice tests whether a string is contained in a slice of strings or not.
    58  // Comparison is case insensitive
    59  func InSlice(slice []string, s string) bool {
    60  	for _, ss := range slice {
    61  		if strings.ToLower(s) == strings.ToLower(ss) {
    62  			return true
    63  		}
    64  	}
    65  	return false
    66  }
    67  
    68  func quote(word string, buf *bytes.Buffer) {
    69  	// Bail out early for "simple" strings
    70  	if word != "" && !strings.ContainsAny(word, "\\'\"`${[|&;<>()~*?! \t\n") {
    71  		buf.WriteString(word)
    72  		return
    73  	}
    74  
    75  	buf.WriteString("'")
    76  
    77  	for i := 0; i < len(word); i++ {
    78  		b := word[i]
    79  		if b == '\'' {
    80  			// Replace literal ' with a close ', a \', and a open '
    81  			buf.WriteString("'\\''")
    82  		} else {
    83  			buf.WriteByte(b)
    84  		}
    85  	}
    86  
    87  	buf.WriteString("'")
    88  }
    89  
    90  // ShellQuoteArguments takes a list of strings and escapes them so they will be
    91  // handled right when passed as arguments to a program via a shell
    92  func ShellQuoteArguments(args []string) string {
    93  	var buf bytes.Buffer
    94  	for i, arg := range args {
    95  		if i != 0 {
    96  			buf.WriteByte(' ')
    97  		}
    98  		quote(arg, &buf)
    99  	}
   100  	return buf.String()
   101  }