mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
fix golint issues in core/stringx (#516)
This commit is contained in:
parent
acdaee0fb6
commit
04b0f26182
@ -42,10 +42,12 @@ func (ls *lockedSource) Seed(seed int64) {
|
|||||||
ls.source.Seed(seed)
|
ls.source.Seed(seed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rand returns a random string.
|
||||||
func Rand() string {
|
func Rand() string {
|
||||||
return Randn(defaultRandLen)
|
return Randn(defaultRandLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandId returns a random id string.
|
||||||
func RandId() string {
|
func RandId() string {
|
||||||
b := make([]byte, idLen)
|
b := make([]byte, idLen)
|
||||||
_, err := crand.Read(b)
|
_, 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])
|
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 {
|
func Randn(n int) string {
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
||||||
@ -74,6 +77,7 @@ func Randn(n int) string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Seed sets the seed to seed.
|
||||||
func Seed(seed int64) {
|
func Seed(seed int64) {
|
||||||
src.Seed(seed)
|
src.Seed(seed)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package stringx
|
|||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Replacer interface wraps the Replace method.
|
||||||
Replacer interface {
|
Replacer interface {
|
||||||
Replace(text string) string
|
Replace(text string) string
|
||||||
}
|
}
|
||||||
@ -13,6 +14,7 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewReplacer returns a Replacer.
|
||||||
func NewReplacer(mapping map[string]string) Replacer {
|
func NewReplacer(mapping map[string]string) Replacer {
|
||||||
var rep = &replacer{
|
var rep = &replacer{
|
||||||
mapping: mapping,
|
mapping: mapping,
|
||||||
|
@ -7,10 +7,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// ErrInvalidStartPosition is an error that indicates the start position is invalid.
|
||||||
ErrInvalidStartPosition = errors.New("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 {
|
func Contains(list []string, str string) bool {
|
||||||
for _, each := range list {
|
for _, each := range list {
|
||||||
if each == str {
|
if each == str {
|
||||||
@ -21,6 +24,7 @@ func Contains(list []string, str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filter filters chars from s with given filter function.
|
||||||
func Filter(s string, filter func(r rune) bool) string {
|
func Filter(s string, filter func(r rune) bool) string {
|
||||||
var n int
|
var n int
|
||||||
chars := []rune(s)
|
chars := []rune(s)
|
||||||
@ -36,6 +40,7 @@ func Filter(s string, filter func(r rune) bool) string {
|
|||||||
return string(chars[:n])
|
return string(chars[:n])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasEmpty checks if there are empty strings in args.
|
||||||
func HasEmpty(args ...string) bool {
|
func HasEmpty(args ...string) bool {
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if len(arg) == 0 {
|
if len(arg) == 0 {
|
||||||
@ -46,10 +51,12 @@ func HasEmpty(args ...string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotEmpty checks if all strings are not empty in args.
|
||||||
func NotEmpty(args ...string) bool {
|
func NotEmpty(args ...string) bool {
|
||||||
return !HasEmpty(args...)
|
return !HasEmpty(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove removes given strs from strings.
|
||||||
func Remove(strings []string, strs ...string) []string {
|
func Remove(strings []string, strs ...string) []string {
|
||||||
out := append([]string(nil), strings...)
|
out := append([]string(nil), strings...)
|
||||||
|
|
||||||
@ -67,6 +74,7 @@ func Remove(strings []string, strs ...string) []string {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reverse reverses s.
|
||||||
func Reverse(s string) string {
|
func Reverse(s string) string {
|
||||||
runes := []rune(s)
|
runes := []rune(s)
|
||||||
|
|
||||||
@ -77,7 +85,7 @@ func Reverse(s string) string {
|
|||||||
return string(runes)
|
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) {
|
func Substr(str string, start int, stop int) (string, error) {
|
||||||
rs := []rune(str)
|
rs := []rune(str)
|
||||||
length := len(rs)
|
length := len(rs)
|
||||||
@ -93,6 +101,7 @@ func Substr(str string, start int, stop int) (string, error) {
|
|||||||
return string(rs[start:stop]), nil
|
return string(rs[start:stop]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TakeOne returns valid string if not empty or later one.
|
||||||
func TakeOne(valid, or string) string {
|
func TakeOne(valid, or string) string {
|
||||||
if len(valid) > 0 {
|
if len(valid) > 0 {
|
||||||
return valid
|
return valid
|
||||||
@ -101,6 +110,7 @@ func TakeOne(valid, or string) string {
|
|||||||
return or
|
return or
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TakeWithPriority returns the first not empty result from fns.
|
||||||
func TakeWithPriority(fns ...func() string) string {
|
func TakeWithPriority(fns ...func() string) string {
|
||||||
for _, fn := range fns {
|
for _, fn := range fns {
|
||||||
val := fn()
|
val := fn()
|
||||||
@ -112,6 +122,7 @@ func TakeWithPriority(fns ...func() string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Union merges the strings in first and second.
|
||||||
func Union(first, second []string) []string {
|
func Union(first, second []string) []string {
|
||||||
set := make(map[string]lang.PlaceholderType)
|
set := make(map[string]lang.PlaceholderType)
|
||||||
|
|
||||||
|
@ -5,8 +5,10 @@ import "github.com/tal-tech/go-zero/core/lang"
|
|||||||
const defaultMask = '*'
|
const defaultMask = '*'
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// TrieOption defines the method to customize a Trie.
|
||||||
TrieOption func(trie *trieNode)
|
TrieOption func(trie *trieNode)
|
||||||
|
|
||||||
|
// A Trie is a tree implementation that used to find elements rapidly.
|
||||||
Trie interface {
|
Trie interface {
|
||||||
Filter(text string) (string, []string, bool)
|
Filter(text string) (string, []string, bool)
|
||||||
FindKeywords(text string) []string
|
FindKeywords(text string) []string
|
||||||
@ -23,6 +25,7 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewTrie returns a Trie.
|
||||||
func NewTrie(words []string, opts ...TrieOption) Trie {
|
func NewTrie(words []string, opts ...TrieOption) Trie {
|
||||||
n := new(trieNode)
|
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 {
|
func WithMask(mask rune) TrieOption {
|
||||||
return func(n *trieNode) {
|
return func(n *trieNode) {
|
||||||
n.mask = mask
|
n.mask = mask
|
||||||
|
Loading…
Reference in New Issue
Block a user