diff --git a/README.md b/README.md index 2c2d007..8068cf3 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ - IsBlank 判断字符串是否为空白,或者由空白字符(空格、跳格、回车、换行等)组成。 - IsNotBlank 判断字符串是否不为空,或者不为空白,IsBlank的反逻辑。 - IsBlankChar 判断字符是否为空白字符。 + - MaxLen 获取字符串列表中的最大长度。 - [x] 本地缓存相关操作 - Set 设置缓存项,支持仅设置缓存,也支持同时给缓存添加一个过期时间。 - Get 获得缓存内容。 diff --git a/str.go b/str.go index ff66c9f..803628c 100644 --- a/str.go +++ b/str.go @@ -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 } diff --git a/str_test.go b/str_test.go index 3174edf..e8f1302 100644 --- a/str_test.go +++ b/str_test.go @@ -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) + } + } +}