github.com/yandex/pandora@v0.5.32/lib/str/string.go (about) 1 package str 2 3 import ( 4 "errors" 5 "math/rand" 6 "strings" 7 "time" 8 ) 9 10 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" 11 12 var randSource = rand.New(rand.NewSource(time.Now().UnixNano())) 13 14 func ParseStringFunc(shoot string) (string, []string, error) { 15 openIdx := strings.IndexRune(shoot, '(') 16 if openIdx == -1 { 17 closeIdx := strings.IndexRune(shoot, ')') 18 if closeIdx != -1 { 19 return "", nil, errors.New("invalid close bracket position") 20 } 21 return shoot, nil, nil 22 } 23 name := strings.TrimSpace(shoot[:openIdx]) 24 25 arg := strings.TrimSpace(shoot[openIdx+1:]) 26 closeIdx := strings.IndexRune(arg, ')') 27 if closeIdx != len(arg)-1 || closeIdx == -1 { 28 return "", nil, errors.New("invalid close bracket position") 29 } 30 arg = strings.TrimSpace(arg[:closeIdx]) 31 args := strings.Split(arg, ",") 32 for i := range args { 33 args[i] = strings.TrimSpace(args[i]) 34 } 35 return name, args, nil 36 } 37 38 func RandStringRunes(n int64, s string) string { 39 if len(s) == 0 { 40 s = letters 41 } 42 var letterRunes = []rune(s) 43 b := make([]rune, n) 44 for i := range b { 45 b[i] = letterRunes[randSource.Intn(len(letterRunes))] 46 } 47 return string(b) 48 }