Documentation ¶
Overview ¶
Package strutil is string util package
Index ¶
- Variables
- func Center(s string, length int) string
- func CollapseWhitespace(s string) string
- func Contains(s string, substrs ...string) bool
- func DedupInt64Slice(ii []int64, omitZeroOpt ...bool) []int64
- func DedupSlice(ss []string, omitEmptyOpt ...bool) []string
- func DedupUint64Slice(ii []uint64, omitZeroOpt ...bool) []uint64
- func Equal(s, other string, ignorecase ...bool) bool
- func Exist(slice []string, val string) bool
- func HasPrefixes(s string, prefixes ...string) bool
- func HasSuffixes(s string, suffixes ...string) bool
- func IntersectionInt64Slice(s1, s2 []int64) []int64
- func IntersectionUin64Slice(s1, s2 []uint64) []uint64
- func Join(ss []string, sep string, omitEmptyOpt ...bool) string
- func Lines(s string, omitEmptyOpt ...bool) []string
- func Map(ss []string, fs ...func(s string) string) []string
- func NormalizeNewlines(d []byte) []byte
- func RandStr(size int) string
- func RemoveSlice(ss []string, removes ...string) []string
- func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, s string, repl func([]string) string) string
- func ReverseSlice(ss []string)
- func SnakeToUpCamel(name string) string
- func Split(s string, sep string, omitEmptyOpt ...bool) []string
- func String(i interface{}) string
- func Trim(s string, cutset ...string) string
- func TrimPrefixes(s string, prefixes ...string) string
- func TrimSlice(ss []string, cutset ...string) []string
- func TrimSlicePrefixes(ss []string, prefixes ...string) []string
- func TrimSliceSuffixes(ss []string, suffixes ...string) []string
- func TrimSuffixes(s string, suffixes ...string) string
- func Validate(s string, validators ...Validator) error
- type Validator
Constants ¶
This section is empty.
Variables ¶
var EnvValueLenValidator = MaxLenValidator(128 * 1024)
EnvValueLenValidator check whether `s` exceeds the maximum length of linux env value.
Functions ¶
func Center ¶
Center centering `s` according to total length.
Center("a", 5) => " a "
Center("ab", 5) => " ab "
Center("abc", 1) => "abc"
func CollapseWhitespace ¶
CollapseWhitespace replace continues space(collapseWhitespaceRegex) to one blank.
CollapseWhitespace("only one space") => "only one space"
CollapseWhitespace("collapse \n all \t sorts of \r \n \r\n whitespace") => "collapse all sorts of whitespace"
func Contains ¶
Contains check if `s` contains one of `substrs`.
Contains("test contains.", "t c", "iii") => true
Contains("test contains.", "t cc", "test ") => false
Contains("test contains.", "iii", "uuu", "ont") => true
func DedupInt64Slice ¶
DedupInt64Slice ([]int64{3, 3, 1, 2, 1, 2, 3, 3, 2, 1, 0, 1, 2}, true) => []int64{3, 1, 2} .
func DedupSlice ¶
DedupSlice return a slice without repeating elements, and the elements are sorted in the order of their first appearance. If omitEmptyOpt = true, ignore empty string.
DedupSlice([]string{"c", "", "b", "a", "", "a", "b", "c", "", "d"}) => []string{"c", "", "b", "a", "d"}
DedupSlice([]string{"c", "", "b", "a", "", "a", "b", "c", "", "d"}, true) => []string{"c", "b", "a", "d"}
func DedupUint64Slice ¶
DedupUint64Slice return a slice without repeating elements, and the elements are sorted in the order of their first appearance. If omitZeroOpt = true, ignore zero value elem.
DedupUint64Slice([]uint64{3, 3, 1, 2, 1, 2, 3, 3, 2, 1, 0, 1, 2}) => []uint64{3, 1, 2, 0}
DedupUint64Slice([]uint64{3, 3, 1, 2, 1, 2, 3, 3, 2, 1, 0, 1, 2}, true) => []uint64{3, 1, 2}
func Equal ¶
Equal judge whether `s` is equal to `other`. If ignorecase=true, judge without case.
Equal("aaa", "AAA") => false
Equal("aaa", "AaA", true) => true
func HasPrefixes ¶
HasPrefixes judge if `s` have at least one of elem in `prefixes` as prefix.
HasPrefixes("asd", "ddd", "uuu") => false
HasPrefixes("asd", "sd", "as") => true
HasPrefixes("asd", "asd") => true
func HasSuffixes ¶
HasSuffixes judge if `s` have at least one of elem in `suffixes` as suffix.
HasSuffixes("asd", "ddd", "d") => true
HasSuffixes("asd", "sd") => true
HasSuffixes("asd", "iid", "as") => false
func IntersectionInt64Slice ¶
IntersectionInt64Slice return the intersection of two int64 slices, complexity O(m * log(m)) .
IntersectionInt64Slice([]int64{3, 1, 2, 0}, []int64{0, 3}) => []int64{3, 0}
IntersectionInt64Slice([]int64{3, 1, 2, 1, 0}, []int64{1, 2, 0}) => []int64{1, 2, 1, 0}
func IntersectionUin64Slice ¶
IntersectionUin64Slice return the intersection of two uint64 slices, complexity O(m * n), to be optimized.
IntersectionUin64Slice([]uint64{3, 1, 2, 0}, []uint64{0, 3}) => []uint64{3, 0}
IntersectionUin64Slice([]uint64{3, 1, 2, 1, 0}, []uint64{1, 2, 0}) => []uint64{1, 2, 1, 0}
func Lines ¶
Lines split `s` by newline. If `omitEmptyOpt`=true, ignore empty string.
Lines("abc\ndef\nghi") => []string{"abc", "def", "ghi"}
Lines("abc\rdef\rghi") => []string{"abc", "def", "ghi"}
Lines("abc\r\ndef\r\nghi\n") => []string{"abc", "def", "ghi", ""}
Lines("abc\r\ndef\r\nghi\n", true) => []string{"abc", "def", "ghi"}
func Map ¶
Map apply each funcs to each elem of `ss`.
Map([]string{"1", "2", "3"}, func(s string) string {return Concat("X", s)}) => []string{"X1", "X2", "X3"}
Map([]string{"Aa", "bB", "cc"}, ToLower, Title) => []string{"Aa", "Bb", "Cc"}
func NormalizeNewlines ¶
NormalizeNewlines normalizes \r\n (windows) and \r (mac) into \n (unix).
There are 3 ways to represent a newline.
Unix: using single character LF, which is byte 10 (0x0a), represented as “” in Go string literal. Windows: using 2 characters: CR LF, which is bytes 13 10 (0x0d, 0x0a), represented as “” in Go string literal. Mac OS: using 1 character CR (byte 13 (0x0d)), represented as “” in Go string literal. This is the least popular.
func RemoveSlice ¶
RemoveSlice delete the elements of slice in `removes`.
RemoveSlice([]string{"a", "b", "c", "a"}, "a") => []string{"b", "c"})
RemoveSlice([]string{"a", "b", "c", "a"}, "b", "c") => []string{"a", "a"})
func ReplaceAllStringSubmatchFunc ¶
ReplaceAllStringSubmatchFunc apply handleFunc to all matched substring.
func ReverseSlice ¶
func ReverseSlice(ss []string)
ReverseSlice reverse slice.
ReverseSlice([]string{"s1", "s2", "s3"} => []string{"s3", "s2", "s1}
func SnakeToUpCamel ¶
SnakeToUpCamel make a snake style name to up-camel style
func Split ¶
Split split `s` by `sep`. If `omitEmptyOpt`=true, ignore empty string.
Split("a|bc|12||3", "|") => []string{"a", "bc", "12", "", "3"}
Split("a|bc|12||3", "|", true) => []string{"a", "bc", "12", "3"}
Split("a,b,c", ":") => []string{"a,b,c"}
func Trim ¶
Trim trim `s`'s prefix and suffix. If `cutset` not specified, `cutset` = space.
Trim("trim ") => "trim"
Trim(" this ") => "this"
Trim("athisb", "abs") => "this"
func TrimPrefixes ¶
TrimPrefixes trim `s`'s prefixes.
TrimPrefixes("/tmp/file", "/tmp") => "/file"
TrimPrefixes("/tmp/tmp/file", "/tmp", "/tmp/tmp") => "/tmp/file"
func TrimSlice ¶
TrimSlice is the slice version of Trim.
TrimSlice([]string{"trim ", " trim", " trim "}) => []string{"trim", "trim", "trim"}
func TrimSlicePrefixes ¶
TrimSlicePrefixes is the slice version of TrimPrefixes.
TrimSlicePrefixes([]string{"/tmp/file", "/tmp/tmp/file"}, "/tmp", "/tmp/tmp") => []string{"/file", "/tmp/file"}
func TrimSliceSuffixes ¶
TrimSliceSuffixes is the slice version of TrimSuffixes.
TrimSliceSuffixes([]string{"test.go", "test.go.tmp"}, ".go", ".tmp") => []string{"test", "test.go"}
func TrimSuffixes ¶
TrimSuffixes trim `s`'s suffixes.
TrimSuffixes("test.go", ".go") => "test"
TrimSuffixes("test.go", ".md", ".go", ".sh") => "test"
TrimSuffixes("test.go.tmp", ".go", ".tmp") => "test.go"
Types ¶
type Validator ¶
Validator defines a validator function. User can extend validator in their own packages.
var AlphaNumericDashUnderscoreValidator Validator = func(s string) error { exp := `^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$` valid := regexp.MustCompile(exp).MatchString(s) if !valid { return fmt.Errorf("valid regexp: %s", exp) } return nil }
AlphaNumericDashUnderscoreValidator regular expression verification, can only: - start with uppercase and lowercase letters or numbers - supports uppercase and lowercase letters, numbers, underscores, underscores, and dots
var EnvKeyValidator Validator = func(s string) error { valid := envKeyRegexp.MatchString(s) if !valid { return fmt.Errorf("illegal env key, validated by regexp: %s", envKeyRegexp.String()) } return nil }
EnvKeyValidator check whether `s` meets the linux env key specification.
var NoChineseValidator Validator = func(s string) error { var chineseCharacters []string for _, runeValue := range s { if unicode.Is(unicode.Han, runeValue) { chineseCharacters = append(chineseCharacters, string(runeValue)) } } if len(chineseCharacters) > 0 { return fmt.Errorf("found %d chinese characters: %s", len(chineseCharacters), Join(chineseCharacters, " ", true)) } return nil }
NoChineseValidator check whether `s` contains Chinese characters.
func MaxLenValidator ¶
MaxLenValidator check whether `s` exceeds the maximum length.
func MaxRuneCountValidator ¶
MaxRuneCountValidator check max rune count.
func MinLenValidator ¶
MinLenValidator verify if `s` meets the minimum length requirement.