Documentation ¶
Overview ¶
Package strutil 字符串工具包
Index ¶
- Variables
- func Atoi64(s string) (int64, error)
- func Center(s string, length int) string
- func CollapseWhitespace(s string) string
- func Concat(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 FlatErrors(errs []error, sep string) error
- 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 IsValidPrjOrAppName(repo string) bool
- func Join(ss []string, sep string, omitEmptyOpt ...bool) string
- func JoinPath(ss ...string) string
- func Lines(s string, omitEmptyOpt ...bool) []string
- func Map(ss []string, fs ...func(s string) string) []string
- func NormalizeNewlines(d []byte) []byte
- func ParseVersion(version string) string
- func RandStr(size int) string
- func RemoveSlice(ss []string, removes ...string) []string
- func Repeat(s string, count int) string
- func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, s string, repl func([]string) string) string
- func ReverseSlice(ss []string)
- func Split(s string, sep string, omitEmptyOpt ...bool) []string
- func SplitIfEmptyString(s, sep string) []string
- func String(i interface{}) string
- func Title(s string) string
- func ToLower(s string) string
- func ToTitle(s string) string
- func ToUpper(s string) string
- func Trim(s string, cutset ...string) string
- func TrimLeft(s string, cutset ...string) string
- func TrimPrefixes(s string, prefixes ...string) string
- func TrimRight(s string, cutset ...string) string
- func TrimSlice(ss []string, cutset ...string) []string
- func TrimSliceLeft(ss []string, cutset ...string) []string
- func TrimSlicePrefixes(ss []string, prefixes ...string) []string
- func TrimSliceRight(ss []string, cutset ...string) []string
- func TrimSliceSuffixes(ss []string, suffixes ...string) []string
- func TrimSuffixes(s string, suffixes ...string) string
- func Truncate(s string, length int) string
- func Validate(s string, validators ...Validator) error
- type Validator
Constants ¶
This section is empty.
Variables ¶
var EnvValueLenValidator = MaxLenValidator(128 * 1024)
EnvValueLenValidator 校验 `s` 是否超过 linux env value 最大长度
var K8sNodeSelectorMatchValidator = AlphaNumericDashUnderscoreValidator
K8sNodeSelectorKeyValidator k8s 节点选择正则表达式校验
Functions ¶
func Center ¶
Center 居中 `s`
Center("a", 5) => " a "
Center("ab", 5) => " ab "
Center("abc", 1) => "abc"
func CollapseWhitespace ¶
CollapseWhitespace 转化连续的 space 为 _一个_ 空格
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 检查 `s` 中是否存在 `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 返回不含重复元素的 slice,各元素按第一次出现顺序排序。如果 omitEmptyOpt = true, 忽略空字符串
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 返回不含重复元素的 slice,各元素按第一次出现顺序排序。
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 判断 `s` 和 `other` 是否相同,如果 ignorecase = true, 忽略大小写
Equal("aaa", "AAA") => false
Equal("aaa", "AaA", true) => true
func HasPrefixes ¶
HasPrefixes `prefixes` 中是否存在 `s` 的前缀
HasPrefixes("asd", "ddd", "uuu") => false
HasPrefixes("asd", "sd", "as") => true
HasPrefixes("asd", "asd") => true
func HasSuffixes ¶
HasSuffixes `suffixes` 中是否存在 `s` 的后缀
HasSuffixes("asd", "ddd", "d") => true
HasSuffixes("asd", "sd") => true
HasSuffixes("asd", "iid", "as") => false
func IntersectionInt64Slice ¶
IntersectionIn64Slice 返回两个 int64 slice 的交集,复杂度 O(m * log(m))
IntersectionIn64Slice([]int64{3, 1, 2, 0}, []int64{0, 3}) => []int64{3, 0}
IntersectionIn64Slice([]int64{3, 1, 2, 1, 0}, []int64{1, 2, 0}) => []int64{1, 2, 1, 0}
func IntersectionUin64Slice ¶
IntersectionUin64Slice 返回两个 uint64 slice 的交集,复杂度 O(m * n),待优化
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 IsValidPrjOrAppName ¶
IsValidPrjOrAppName 是否是一个合法的项目或应用名 需要满足docker repository的规则,和ingress域名的规则
func Lines ¶
Lines 将 `s` 按 newline 切分成 string slice, omitEmptyOpt=true 时,忽略结果中的空字符串
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 对 `ss` 中的每个元素执行 `f`, 返回f返回的结果列表
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 ParseVersion ¶
ParseVersion 序列化版本 "1.05.1" --> "1.5.1",
func RemoveSlice ¶
Remove 删除 slice 在 removes 中存在的元素。
RemoveSlice([]string{"a", "b", "c", "a"}, "a") => []string{"b", "c"})
RemoveSlice([]string{"a", "b", "c", "a"}, "b", "c") => []string{"a", "a"})
func ReverseSlice ¶
func ReverseSlice(ss []string)
ReverseSlice 反转 slice
ReverseSlice([]string{"s1", "s2", "s3"} => []string{"s3", "s2", "s1}
func Split ¶
Split 根据 `sep` 来切分 `s`, `omitEmptyOpt`=true 时,忽略结果中的空字符串
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 SplitIfEmptyString ¶
func Trim ¶
Trim 两边裁剪 `s`, 如果不指定 `cutset`, 默认cutset=space
Trim("trim ") => "trim"
Trim(" this ") => "this"
Trim("athisb", "abs") => "this"
func TrimLeft ¶
TrimLeft 裁剪 `s` 左边, 如果不指定 `cutset`, 默认cutset=space
TrimLeft("trim ") => "trim "
TrimLeft(" this") => "this"
TrimLeft("athisa", "a") => "thisa"
func TrimPrefixes ¶
TrimPrefixes 裁剪 `s` 的前缀
TrimPrefixes("/tmp/file", "/tmp") => "/file"
TrimPrefixes("/tmp/tmp/file", "/tmp", "/tmp/tmp") => "/tmp/file"
func TrimRight ¶
TrimRight 裁剪 `s` 右边,如果不指定 `cutset`, 默认cutset=space
TrimRight("trim ") => "trim"
TrimRight(" this") => " this"
TrimRight("athisa", "a") => "athis"
func TrimSlice ¶
TrimSlice Trim 的 Slice 版本
TrimSlice([]string{"trim ", " trim", " trim "}) => []string{"trim", "trim", "trim"}
func TrimSliceLeft ¶
TrimSliceLeft TrimLeft 的 Slice 版本
TrimSliceLeft([]string{"trim ", " trim", " trim "}) => []string{"trim ", "trim", "trim "}
func TrimSlicePrefixes ¶
TrimSlicePrefixes TrimPrefixes 的 Slice 版本
TrimSlicePrefixes([]string{"/tmp/file", "/tmp/tmp/file"}, "/tmp", "/tmp/tmp") => []string{"/file", "/tmp/file"}
func TrimSliceRight ¶
TrimSliceRight TrimRight 的 Slice 版本
TrimSliceRight([]string{"trim ", " trim", " trim "}) => []string{"trim", " trim", " trim"}
func TrimSliceSuffixes ¶
TrimSliceSuffixes TrimSuffixes 的 Slice 版本
TrimSliceSuffixes([]string{"test.go", "test.go.tmp"}, ".go", ".tmp") => []string{"test", "test.go"}
func TrimSuffixes ¶
TrimSuffixes 裁剪 `s` 的后缀
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 正则表达式校验,只能以大小写字母或数字开头,支持大小写字母、数字、中划线、下划线、点
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 检验 `s` 是否符合 linux env key 规范
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 charactors: %s", len(chineseCharacters), Join(chineseCharacters, " ", true)) } return nil }
NoChineseValidator 校验 `s` 是否包含中文字符