Skip to content

Commit

Permalink
Merge pull request #59 from ccpwcn/dev
Browse files Browse the repository at this point in the history
feat: 新增MaxLen,获取字符串列表的最大长度
  • Loading branch information
ccpwcn authored Jan 8, 2025
2 parents 6508b68 + 112118c commit 9dd9a40
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- IsBlank 判断字符串是否为空白,或者由空白字符(空格、跳格、回车、换行等)组成。
- IsNotBlank 判断字符串是否不为空,或者不为空白,IsBlank的反逻辑。
- IsBlankChar 判断字符是否为空白字符。
- MaxLen 获取字符串列表中的最大长度。
- [x] 本地缓存相关操作
- Set 设置缓存项,支持仅设置缓存,也支持同时给缓存添加一个过期时间。
- Get 获得缓存内容。
Expand Down
23 changes: 17 additions & 6 deletions str.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,21 @@ func IsNotBlank(s string) bool {
// isBlankChar 判断字符是否为空白字符
func isBlankChar(c rune) bool {
return unicode.IsSpace(c) ||
c == '\ufeff' ||
c == '\u202a' ||
c == '\u0000' ||
c == '\u3164' ||
c == '\u2800' ||
c == '\u180e'
c == '\ufeff' || // 字节次序标记字符
c == '\u202a' || // 右到左标记
c == '\u0000' || // 空字符
c == '\u3164' || // 零宽度非连接符
c == '\u2800' || // 零宽度空间
c == '\u180e' // 蒙古文格式化字符
}

// MaxLen 获取字符串列表中最大长度
func MaxLen(strList ...string) int {
maxLen := 0
for _, str := range strList {
if len(str) > maxLen {
maxLen = len(str)
}
}
return maxLen
}
26 changes: 26 additions & 0 deletions str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,29 @@ func TestIsNotBlank(t *testing.T) {
}
}
}

func TestMaxLen(t *testing.T) {
type args struct {
ss []string
}
tests := []struct {
name string
args args
want int
}{
{name: "zero-1", args: args{ss: []string{}}, want: 0},
{name: "zero-2", args: args{ss: []string{""}}, want: 0},
{name: "zero-3", args: args{ss: []string{"", ""}}, want: 0},
{name: "one", args: args{ss: []string{"hello", "world"}}, want: 5},
{name: "two", args: args{ss: []string{"hello", "world", "hello world"}}, want: 11},
{name: "three", args: args{ss: []string{"hello", "world", "hello world", "hello world hello world"}}, want: 23},
{name: "four", args: args{ss: []string{"hello", "world", "hello world", "hello world hello world", "hello world hello world hello world"}}, want: 35},
{name: "five", args: args{ss: []string{"hello", "world", "hello world", "hello world hello world", "hello world hello world hello world", "hello world hello world hello world hello world"}}, want: 47},
}
for _, test := range tests {
actual := MaxLen(test.args.ss...)
if actual != test.want {
t.Errorf("预期 %v,实际值:%v", test.want, actual)
}
}
}

0 comments on commit 9dd9a40

Please sign in to comment.