gopkg.in/easygen.v4@v4.1.0/tf-strings.go (about)

     1  package easygen
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  var (
     9  	re  *regexp.Regexp
    10  	rec *regexp.Regexp
    11  )
    12  
    13  ////////////////////////////////////////////////////////////////////////////
    14  // Regexp Function Definitions
    15  
    16  func regexpFindAllString(s string, regExp string, n int) []string {
    17  	return regexp.MustCompile(regExp).FindAllString(s, n)
    18  }
    19  
    20  func regexpFindAllStringIndex(s string, regExp string, n int) [][]int {
    21  	return regexp.MustCompile(regExp).FindAllStringIndex(s, n)
    22  }
    23  
    24  func regexpFindAllStringSubmatch(s string, regExp string, n int) [][]string {
    25  	return regexp.MustCompile(regExp).FindAllStringSubmatch(s, n)
    26  }
    27  
    28  func regexpFindAllStringSubmatchIndex(s string, regExp string, n int) [][]int {
    29  	return regexp.MustCompile(regExp).FindAllStringSubmatchIndex(s, n)
    30  }
    31  
    32  func regexpFindString(s string, regExp string) string {
    33  	return regexp.MustCompile(regExp).FindString(s)
    34  }
    35  
    36  func regexpFindStringIndex(s string, regExp string) (loc []int) {
    37  	return regexp.MustCompile(regExp).FindStringIndex(s)
    38  }
    39  
    40  func regexpFindStringSubmatch(s string, regExp string) []string {
    41  	return regexp.MustCompile(regExp).FindStringSubmatch(s)
    42  }
    43  
    44  func regexpFindStringSubmatchIndex(s string, regExp string) []int {
    45  	return regexp.MustCompile(regExp).FindStringSubmatchIndex(s)
    46  }
    47  
    48  func regexpMatchString(s string, regExp string) bool {
    49  	return regexp.MustCompile(regExp).MatchString(s)
    50  }
    51  
    52  func regexpReplaceAllLiteralString(src, regExp string, repl string) string {
    53  	return regexp.MustCompile(regExp).ReplaceAllLiteralString(src, repl)
    54  }
    55  
    56  func regexpReplaceAllString(src, regExp string, repl string) string {
    57  	return regexp.MustCompile(regExp).ReplaceAllString(src, repl)
    58  }
    59  
    60  func regexpReplaceAllStringFunc(src string, regExp string, repl func(string) string) string {
    61  	return regexp.MustCompile(regExp).ReplaceAllStringFunc(src, repl)
    62  }
    63  
    64  func regexpSplit(s string, regExp string, n int) []string {
    65  	return regexp.MustCompile(regExp).Split(s, n)
    66  }
    67  
    68  ////////////////////////////////////////////////////////////////////////////
    69  // Misc
    70  
    71  // coalesce function takes two or more string arguments and returns the first argument that is not empty.
    72  // The result is empty only if all the arguments are empty.
    73  func coalesce(s ...string) string {
    74  	for _, str := range s {
    75  		if len(str) != 0 && str != "<no value>" {
    76  			return str
    77  		}
    78  	}
    79  	return ""
    80  }
    81  
    82  // quote4shell -- quote file name for shell.
    83  // So "%bob's file" will be quoted as '%bob'\''s file'
    84  func quote4shell(s string) string {
    85  	return "'" + strings.Join(strings.Split(s, "'"), `'\''`) + "'"
    86  }