Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增MaxLen,获取字符串列表的最大长度 #59

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading