github.com/wuhuizuo/gomplate@v3.5.0+incompatible/strings/strings.go (about)

     1  // Package strings contains functions to manipulate strings
     2  package strings
     3  
     4  import (
     5  	"regexp"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/Masterminds/goutils"
    10  )
    11  
    12  // Indent - indent each line of the string with the given indent string
    13  func Indent(width int, indent, s string) string {
    14  	if width == 0 {
    15  		return s
    16  	}
    17  	if width > 1 {
    18  		indent = strings.Repeat(indent, width)
    19  	}
    20  	var res []byte
    21  	bol := true
    22  	for i := 0; i < len(s); i++ {
    23  		c := s[i]
    24  		if bol && c != '\n' {
    25  			res = append(res, indent...)
    26  		}
    27  		res = append(res, c)
    28  		bol = c == '\n'
    29  	}
    30  	return string(res)
    31  }
    32  
    33  // Trunc - truncate a string to the given length
    34  func Trunc(length int, s string) string {
    35  	if length < 0 {
    36  		return s
    37  	}
    38  	if len(s) <= length {
    39  		return s
    40  	}
    41  	return s[0:length]
    42  }
    43  
    44  // Sort - return an alphanumerically-sorted list of strings
    45  //
    46  // Deprecated: use coll.Sort instead
    47  func Sort(list []string) []string {
    48  	sorted := sort.StringSlice(list)
    49  	sorted.Sort()
    50  	return sorted
    51  }
    52  
    53  var (
    54  	spaces      = regexp.MustCompile(`\s+`)
    55  	nonAlphaNum = regexp.MustCompile(`[^\pL\pN]+`)
    56  )
    57  
    58  // SnakeCase -
    59  func SnakeCase(in string) string {
    60  	s := casePrepare(in)
    61  	return spaces.ReplaceAllString(s, "_")
    62  }
    63  
    64  // KebabCase -
    65  func KebabCase(in string) string {
    66  	s := casePrepare(in)
    67  	return spaces.ReplaceAllString(s, "-")
    68  }
    69  
    70  func casePrepare(in string) string {
    71  	in = strings.TrimSpace(in)
    72  	s := strings.ToLower(in)
    73  	// make sure the first letter remains lower- or upper-cased
    74  	s = strings.Replace(s, string(s[0]), string(in[0]), 1)
    75  	s = nonAlphaNum.ReplaceAllString(s, " ")
    76  	return strings.TrimSpace(s)
    77  }
    78  
    79  // CamelCase -
    80  func CamelCase(in string) string {
    81  	in = strings.TrimSpace(in)
    82  	s := strings.Title(in)
    83  	// make sure the first letter remains lower- or upper-cased
    84  	s = strings.Replace(s, string(s[0]), string(in[0]), 1)
    85  	return nonAlphaNum.ReplaceAllString(s, "")
    86  }
    87  
    88  // WordWrapOpts defines the options to apply to the WordWrap function
    89  type WordWrapOpts struct {
    90  	// The desired maximum line length in characters (defaults to 80)
    91  	Width uint
    92  
    93  	// Line-break sequence to insert (defaults to "\n")
    94  	LBSeq string
    95  }
    96  
    97  // applies default options
    98  func wwDefaults(opts WordWrapOpts) WordWrapOpts {
    99  	if opts.Width == 0 {
   100  		opts.Width = 80
   101  	}
   102  	if opts.LBSeq == "" {
   103  		opts.LBSeq = "\n"
   104  	}
   105  	return opts
   106  }
   107  
   108  // WordWrap - insert line-breaks into the string, before it reaches the given
   109  // width.
   110  func WordWrap(in string, opts WordWrapOpts) string {
   111  	opts = wwDefaults(opts)
   112  	return goutils.WrapCustom(in, int(opts.Width), opts.LBSeq, false)
   113  }