github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/strings.go (about)

     1  package util
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  	"unicode"
     7  )
     8  
     9  var cleanFileRegex = regexp.MustCompile(`[^a-zA-Z0-9_\-\.]`)
    10  
    11  // Truncate returns a string of at most the given length.
    12  func Truncate(input string, outputLength int) string {
    13  	if len(input) <= outputLength {
    14  		return input
    15  	}
    16  	return input[:outputLength]
    17  }
    18  
    19  func CleanForPath(name string) string {
    20  	return cleanFileRegex.ReplaceAllLiteralString(name, "_")
    21  }
    22  
    23  // CleanName returns a name with spaces and dashes replaced with safe underscores
    24  func CleanName(name string) string {
    25  	name = strings.Replace(name, "-", "_", -1)
    26  	name = strings.Replace(name, " ", "_", -1)
    27  	return name
    28  }
    29  
    30  // IndexWhiteSpace returns the first index of white space in the given string.
    31  // Returns -1 if no white space exists.
    32  func IndexWhiteSpace(s string) int {
    33  	for i, r := range s {
    34  		if unicode.IsSpace(r) {
    35  			return i
    36  		}
    37  	}
    38  	return -1
    39  }