From 04b0f26182dd4db35de2eaea56cd7255519468b7 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Wed, 24 Feb 2021 16:09:07 +0800 Subject: [PATCH] fix golint issues in core/stringx (#516) --- core/stringx/random.go | 4 ++++ core/stringx/replacer.go | 2 ++ core/stringx/strings.go | 15 +++++++++++++-- core/stringx/trie.go | 4 ++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/stringx/random.go b/core/stringx/random.go index b00a9089..139775f9 100644 --- a/core/stringx/random.go +++ b/core/stringx/random.go @@ -42,10 +42,12 @@ func (ls *lockedSource) Seed(seed int64) { ls.source.Seed(seed) } +// Rand returns a random string. func Rand() string { return Randn(defaultRandLen) } +// RandId returns a random id string. func RandId() string { b := make([]byte, idLen) _, err := crand.Read(b) @@ -56,6 +58,7 @@ func RandId() string { return fmt.Sprintf("%x%x%x%x", b[0:2], b[2:4], b[4:6], b[6:8]) } +// Randn returns a random string with length n. func Randn(n int) string { b := make([]byte, n) // A src.Int63() generates 63 random bits, enough for letterIdxMax characters! @@ -74,6 +77,7 @@ func Randn(n int) string { return string(b) } +// Seed sets the seed to seed. func Seed(seed int64) { src.Seed(seed) } diff --git a/core/stringx/replacer.go b/core/stringx/replacer.go index e615753b..1baf8c4f 100644 --- a/core/stringx/replacer.go +++ b/core/stringx/replacer.go @@ -3,6 +3,7 @@ package stringx import "strings" type ( + // Replacer interface wraps the Replace method. Replacer interface { Replace(text string) string } @@ -13,6 +14,7 @@ type ( } ) +// NewReplacer returns a Replacer. func NewReplacer(mapping map[string]string) Replacer { var rep = &replacer{ mapping: mapping, diff --git a/core/stringx/strings.go b/core/stringx/strings.go index d28a71c8..0274b5b1 100644 --- a/core/stringx/strings.go +++ b/core/stringx/strings.go @@ -7,10 +7,13 @@ import ( ) var ( + // ErrInvalidStartPosition is an error that indicates the start position is invalid. ErrInvalidStartPosition = errors.New("start position is invalid") - ErrInvalidStopPosition = errors.New("stop position is invalid") + // ErrInvalidStopPosition is an error that indicates the stop position is invalid. + ErrInvalidStopPosition = errors.New("stop position is invalid") ) +// Contains checks if str is in list. func Contains(list []string, str string) bool { for _, each := range list { if each == str { @@ -21,6 +24,7 @@ func Contains(list []string, str string) bool { return false } +// Filter filters chars from s with given filter function. func Filter(s string, filter func(r rune) bool) string { var n int chars := []rune(s) @@ -36,6 +40,7 @@ func Filter(s string, filter func(r rune) bool) string { return string(chars[:n]) } +// HasEmpty checks if there are empty strings in args. func HasEmpty(args ...string) bool { for _, arg := range args { if len(arg) == 0 { @@ -46,10 +51,12 @@ func HasEmpty(args ...string) bool { return false } +// NotEmpty checks if all strings are not empty in args. func NotEmpty(args ...string) bool { return !HasEmpty(args...) } +// Remove removes given strs from strings. func Remove(strings []string, strs ...string) []string { out := append([]string(nil), strings...) @@ -67,6 +74,7 @@ func Remove(strings []string, strs ...string) []string { return out } +// Reverse reverses s. func Reverse(s string) string { runes := []rune(s) @@ -77,7 +85,7 @@ func Reverse(s string) string { return string(runes) } -// Substr returns runes between start and stop [start, stop) regardless of the chars are ascii or utf8 +// Substr returns runes between start and stop [start, stop) regardless of the chars are ascii or utf8. func Substr(str string, start int, stop int) (string, error) { rs := []rune(str) length := len(rs) @@ -93,6 +101,7 @@ func Substr(str string, start int, stop int) (string, error) { return string(rs[start:stop]), nil } +// TakeOne returns valid string if not empty or later one. func TakeOne(valid, or string) string { if len(valid) > 0 { return valid @@ -101,6 +110,7 @@ func TakeOne(valid, or string) string { return or } +// TakeWithPriority returns the first not empty result from fns. func TakeWithPriority(fns ...func() string) string { for _, fn := range fns { val := fn() @@ -112,6 +122,7 @@ func TakeWithPriority(fns ...func() string) string { return "" } +// Union merges the strings in first and second. func Union(first, second []string) []string { set := make(map[string]lang.PlaceholderType) diff --git a/core/stringx/trie.go b/core/stringx/trie.go index b2a2115a..7168c3ef 100644 --- a/core/stringx/trie.go +++ b/core/stringx/trie.go @@ -5,8 +5,10 @@ import "github.com/tal-tech/go-zero/core/lang" const defaultMask = '*' type ( + // TrieOption defines the method to customize a Trie. TrieOption func(trie *trieNode) + // A Trie is a tree implementation that used to find elements rapidly. Trie interface { Filter(text string) (string, []string, bool) FindKeywords(text string) []string @@ -23,6 +25,7 @@ type ( } ) +// NewTrie returns a Trie. func NewTrie(words []string, opts ...TrieOption) Trie { n := new(trieNode) @@ -130,6 +133,7 @@ func (n *trieNode) replaceWithAsterisk(chars []rune, start, stop int) { } } +// WithMask customizes a Trie with keywords masked as given mask char. func WithMask(mask rune) TrieOption { return func(n *trieNode) { n.mask = mask