strutil

package
v1.0.0-RC Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2021 License: AGPL-3.0 Imports: 9 Imported by: 30

Documentation

Overview

Package strutil 字符串工具包

Index

Constants

This section is empty.

Variables

View Source
var EnvValueLenValidator = MaxLenValidator(128 * 1024)

EnvValueLenValidator 校验 `s` 是否超过 linux env value 最大长度

View Source
var K8sNodeSelectorMatchValidator = AlphaNumericDashUnderscoreValidator

K8sNodeSelectorKeyValidator k8s 节点选择正则表达式校验

Functions

func Atoi64

func Atoi64(s string) (int64, error)

Atoi64 parse string to int64

Atoi64("6") => (6, nil)

func Center

func Center(s string, length int) string

Center 居中 `s`

Center("a", 5) => " a "

Center("ab", 5) => " ab "

Center("abc", 1) => "abc"

func CollapseWhitespace

func CollapseWhitespace(s string) string

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 Concat

func Concat(s ...string) string

Concat 合并字符串

func Contains

func Contains(s string, substrs ...string) bool

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

func DedupInt64Slice(ii []int64, omitZeroOpt ...bool) []int64

DedupInt64Slice ([]int64{3, 3, 1, 2, 1, 2, 3, 3, 2, 1, 0, 1, 2}, true) => []int64{3, 1, 2}

func DedupSlice

func DedupSlice(ss []string, omitEmptyOpt ...bool) []string

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

func DedupUint64Slice(ii []uint64, omitZeroOpt ...bool) []uint64

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

func Equal(s, other string, ignorecase ...bool) bool

Equal 判断 `s` 和 `other` 是否相同,如果 ignorecase = true, 忽略大小写

Equal("aaa", "AAA") => false

Equal("aaa", "AaA", true) => true

func Exist

func Exist(slice []string, val string) bool

func FlatErrors

func FlatErrors(errs []error, sep string) error

FlatErrors 将 errors 打平为一个 error

func HasPrefixes

func HasPrefixes(s string, prefixes ...string) bool

HasPrefixes `prefixes` 中是否存在 `s` 的前缀

HasPrefixes("asd", "ddd", "uuu") => false

HasPrefixes("asd", "sd", "as") => true

HasPrefixes("asd", "asd") => true

func HasSuffixes

func HasSuffixes(s string, suffixes ...string) bool

HasSuffixes `suffixes` 中是否存在 `s` 的后缀

HasSuffixes("asd", "ddd", "d") => true

HasSuffixes("asd", "sd") => true

HasSuffixes("asd", "iid", "as") => false

func IntersectionInt64Slice

func IntersectionInt64Slice(s1, s2 []int64) []int64

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

func IntersectionUin64Slice(s1, s2 []uint64) []uint64

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

func IsValidPrjOrAppName(repo string) bool

IsValidPrjOrAppName 是否是一个合法的项目或应用名 需要满足docker repository的规则,和ingress域名的规则

func Join

func Join(ss []string, sep string, omitEmptyOpt ...bool) string

Join see also strings.Join, omitEmptyOpt = true 时,不拼接 `ss` 中空字符串

func JoinPath

func JoinPath(ss ...string) string

JoinPath see also filepath.Join

func Lines

func Lines(s string, omitEmptyOpt ...bool) []string

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

func Map(ss []string, fs ...func(s string) string) []string

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

func NormalizeNewlines(d []byte) []byte

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

func ParseVersion(version string) string

ParseVersion 序列化版本 "1.05.1" --> "1.5.1",

func RandStr

func RandStr(size int) string

RandStr 获取随机字符串

func RemoveSlice

func RemoveSlice(ss []string, removes ...string) []string

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 Repeat

func Repeat(s string, count int) string

Repeat see also strings.Repeat

func ReplaceAllStringSubmatchFunc

func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, s string, repl func([]string) string) string

func ReverseSlice

func ReverseSlice(ss []string)

ReverseSlice 反转 slice

ReverseSlice([]string{"s1", "s2", "s3"} => []string{"s3", "s2", "s1}

func Split

func Split(s string, sep string, omitEmptyOpt ...bool) []string

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 SplitIfEmptyString(s, sep string) []string

func String

func String(i interface{}) string

String convert interface to string

func Title

func Title(s string) string

Title see also strings.Title

func ToLower

func ToLower(s string) string

ToLower see also strings.ToLower

func ToTitle

func ToTitle(s string) string

ToTitle see also strings.ToTitle

func ToUpper

func ToUpper(s string) string

ToUpper see also strings.ToUpper

func Trim

func Trim(s string, cutset ...string) string

Trim 两边裁剪 `s`, 如果不指定 `cutset`, 默认cutset=space

Trim("trim ") => "trim"

Trim(" this ") => "this"

Trim("athisb", "abs") => "this"

func TrimLeft

func TrimLeft(s string, cutset ...string) string

TrimLeft 裁剪 `s` 左边, 如果不指定 `cutset`, 默认cutset=space

TrimLeft("trim ") => "trim "

TrimLeft(" this") => "this"

TrimLeft("athisa", "a") => "thisa"

func TrimPrefixes

func TrimPrefixes(s string, prefixes ...string) string

TrimPrefixes 裁剪 `s` 的前缀

TrimPrefixes("/tmp/file", "/tmp") => "/file"

TrimPrefixes("/tmp/tmp/file", "/tmp", "/tmp/tmp") => "/tmp/file"

func TrimRight

func TrimRight(s string, cutset ...string) string

TrimRight 裁剪 `s` 右边,如果不指定 `cutset`, 默认cutset=space

TrimRight("trim ") => "trim"

TrimRight(" this") => " this"

TrimRight("athisa", "a") => "athis"

func TrimSlice

func TrimSlice(ss []string, cutset ...string) []string

TrimSlice Trim 的 Slice 版本

TrimSlice([]string{"trim ", " trim", " trim "}) => []string{"trim", "trim", "trim"}

func TrimSliceLeft

func TrimSliceLeft(ss []string, cutset ...string) []string

TrimSliceLeft TrimLeft 的 Slice 版本

TrimSliceLeft([]string{"trim ", " trim", " trim "}) => []string{"trim ", "trim", "trim "}

func TrimSlicePrefixes

func TrimSlicePrefixes(ss []string, prefixes ...string) []string

TrimSlicePrefixes TrimPrefixes 的 Slice 版本

TrimSlicePrefixes([]string{"/tmp/file", "/tmp/tmp/file"}, "/tmp", "/tmp/tmp") => []string{"/file", "/tmp/file"}

func TrimSliceRight

func TrimSliceRight(ss []string, cutset ...string) []string

TrimSliceRight TrimRight 的 Slice 版本

TrimSliceRight([]string{"trim ", " trim", " trim "}) => []string{"trim", " trim", " trim"}

func TrimSliceSuffixes

func TrimSliceSuffixes(ss []string, suffixes ...string) []string

TrimSliceSuffixes TrimSuffixes 的 Slice 版本

TrimSliceSuffixes([]string{"test.go", "test.go.tmp"}, ".go", ".tmp") => []string{"test", "test.go"}

func TrimSuffixes

func TrimSuffixes(s string, suffixes ...string) string

TrimSuffixes 裁剪 `s` 的后缀

TrimSuffixes("test.go", ".go") => "test"

TrimSuffixes("test.go", ".md", ".go", ".sh") => "test"

TrimSuffixes("test.go.tmp", ".go", ".tmp") => "test.go"

func Truncate

func Truncate(s string, length int) string

Truncate 截断 `s` 到 `length`-3 的长度,末尾增加 "..."

Truncate("it is too long", 6) => "it ..."

Truncate("it is too long", 13) => "it is too ..."

Truncate("but it is not", 16) => "but it is not"

func Validate

func Validate(s string, validators ...Validator) error

Validate validate `s` with composed validators and return error if have

Types

type Validator

type Validator func(s string) error

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` 是否包含中文字符

func MaxLenValidator

func MaxLenValidator(maxLen int) Validator

MaxLenValidator 校验 `s` 是否超过最大长度

func MaxRuneCountValidator

func MaxRuneCountValidator(maxLen int) Validator

func MinLenValidator

func MinLenValidator(minLen int) Validator

MinLenValidator 校验 `s` 是否符合最小长度要求

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL