vitess.io/vitess@v0.16.2/go/textutil/strings.go (about) 1 /* 2 Copyright 2020 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package textutil 18 19 import ( 20 "net/url" 21 "regexp" 22 "strings" 23 ) 24 25 var ( 26 delimitedListRegexp = regexp.MustCompile(`[ ,;]+`) 27 ) 28 29 // SplitDelimitedList splits a given string by comma, semi-colon or space, and returns non-empty strings 30 func SplitDelimitedList(s string) (list []string) { 31 tokens := delimitedListRegexp.Split(s, -1) 32 for _, token := range tokens { 33 token = strings.TrimSpace(token) 34 if token == "" { 35 continue 36 } 37 list = append(list, token) 38 } 39 return list 40 } 41 42 // EscapeJoin acts like strings.Join, except it first escapes elements via net/url 43 func EscapeJoin(elems []string, sep string) string { 44 escapedElems := []string{} 45 for i := range elems { 46 escapedElems = append(escapedElems, url.QueryEscape(elems[i])) 47 } 48 return strings.Join(escapedElems, sep) 49 } 50 51 // SplitUnescape acts like strings.Split, except it then unescapes tokens via net/url 52 func SplitUnescape(s string, sep string) ([]string, error) { 53 if s == "" { 54 return nil, nil 55 } 56 elems := strings.Split(s, sep) 57 unescapedElems := []string{} 58 for i := range elems { 59 d, err := url.QueryUnescape(elems[i]) 60 if err != nil { 61 return unescapedElems, err 62 } 63 unescapedElems = append(unescapedElems, d) 64 } 65 return unescapedElems, nil 66 } 67 68 // SingleWordCamel takes a single word and returns is in Camel case; basically 69 // just capitalizing the first letter and making sure the rest are lower case. 70 func SingleWordCamel(w string) string { 71 if w == "" { 72 return w 73 } 74 return strings.ToUpper(w[0:1]) + strings.ToLower(w[1:]) 75 }