strutil

package
v1.0.7-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: Apache-2.0 Imports: 8 Imported by: 5

Documentation

Overview

Package strutil is string util package

Index

Constants

This section is empty.

Variables

View Source
var EnvValueLenValidator = MaxLenValidator(128 * 1024)

EnvValueLenValidator check whether `s` exceeds the maximum length of linux env value.

Functions

func Center

func Center(s string, length int) string

Center centering `s` according to total length.

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

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

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

func CollapseWhitespace

func CollapseWhitespace(s string) string

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

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

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

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 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

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

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

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

Equal judge whether `s` is equal to `other`. If ignorecase=true, judge without case.

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

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

func Exist

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

Exist check if elem exist in slice.

func HasPrefixes

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

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

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

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

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

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

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

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 Join

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

Join see also strings.Join, If omitEmptyOpt = true, ignore empty string inside `ss`.

func Lines

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

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

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

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

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 RandStr

func RandStr(size int) string

RandStr get random string.

func RemoveSlice

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

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

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

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

func SnakeToUpCamel(name string) string

SnakeToUpCamel make a snake style name to up-camel style

func Split

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

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 String

func String(i interface{}) string

String convert interface to string

func Trim

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

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

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

TrimPrefixes trim `s`'s prefixes.

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

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

func TrimSlice

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

TrimSlice is the slice version of Trim.

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

func TrimSlicePrefixes

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

TrimSlicePrefixes is the slice version of TrimPrefixes.

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

func TrimSliceSuffixes

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

TrimSliceSuffixes is the slice version of TrimSuffixes.

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

func TrimSuffixes

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

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"

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 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

func MaxLenValidator(maxLen int) Validator

MaxLenValidator check whether `s` exceeds the maximum length.

func MaxRuneCountValidator

func MaxRuneCountValidator(maxLen int) Validator

MaxRuneCountValidator check max rune count.

func MinLenValidator

func MinLenValidator(minLen int) Validator

MinLenValidator verify if `s` meets the minimum length requirement.

Jump to

Keyboard shortcuts

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