Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
mars315 committed Jan 16, 2024
1 parent 1414afc commit d1a3bd7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/stringx/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import (
"github.com/mars315/autoflags/lib/builtin"
)

// SafeTokens 切分字符串,去掉空格
func SafeTokens(cmd string, sep string) []string {
if cmd = strings.TrimSpace(cmd); len(cmd) == 0 {
return nil
}

tokens := strings.Split(cmd, sep)
list := tokens[:0]
for _, token := range tokens {
str := strings.TrimSpace(token)
if len(str) > 0 {
list = append(list, str)
}
}
return list
}

func ToBool(s string) bool {
if len(s) == 0 {
return false
Expand All @@ -36,7 +53,11 @@ func Atoi[T builtin.SignedInteger](v string) T {

// AtoSlice string to signed integer slice
func AtoSlice[T builtin.SignedInteger](s string, sep string) []T {
ss := strings.Split(s, sep)
ss := SafeTokens(s, sep)
if len(ss) == 0 {
return nil
}

l := make([]T, 0, len(ss))
for _, v := range ss {
l = append(l, Atoi[T](v))
Expand Down

0 comments on commit d1a3bd7

Please sign in to comment.