Skip to content

Commit

Permalink
Merge pull request #57 from ccpwcn/dev
Browse files Browse the repository at this point in the history
feat: 新增字符处理
  • Loading branch information
ccpwcn authored Jan 2, 2025
2 parents 751c3ff + a345211 commit ca309e8
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
- ReverseString 反转字符串。
- EnglishWordsCount 统计英文单词的数量,对于英文单词的切分,业内有很多讨论,此函数的实现结果仅供参考,本人不参与这些争论。
- ArabicToChinese 将一个阿拉伯数字转为中文数字,虽然目前仅支持正整数,但是已满足绝大多数业务场景了,实测很好用。
- IsBlank 判断字符串是否为空。
- IsNotBlank 判断字符串是否不为空。
- IsBlankChar 判断字符是否为空白字符。
- [x] 本地缓存相关操作
- Set 设置缓存项,支持仅设置缓存,也支持同时给缓存添加一个过期时间。
- Get 获得缓存内容。
Expand Down
30 changes: 30 additions & 0 deletions str.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"regexp"
"strings"
"unicode"
"unicode/utf8"
"unsafe"
)
Expand Down Expand Up @@ -239,3 +240,32 @@ func EnglishWordsCount(s string) int {
words := englishWordsPattern.FindAllString(s, -1)
return len(words)
}

// IsBlank 判断字符串是否为空
func IsBlank(s string) bool {
if len(strings.TrimSpace(s)) == 0 {
return true
}
for _, c := range s {
if !isBlankChar(c) {
return false
}
}
return true
}

// IsNotBlank 判断字符串是否不为空
func IsNotBlank(s string) bool {
return !IsBlank(s)
}

// isBlankChar 判断字符是否为空白字符
func isBlankChar(c rune) bool {
return unicode.IsSpace(c) ||
c == '\ufeff' ||
c == '\u202a' ||
c == '\u0000' ||
c == '\u3164' ||
c == '\u2800' ||
c == '\u180e'
}
113 changes: 113 additions & 0 deletions str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,116 @@ func TestEnglishWordsCount(t *testing.T) {
}
}
}

func TestIsNotBlank(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "hello_word",
args: args{s: "hello_word"},
want: true,
},
{
name: "hello word",
args: args{s: "hello word"},
want: true,
},
{
name: "<SPACE>",
args: args{s: " "},
want: false,
},
{
name: "<SPACE2>",
args: args{s: " "},
want: false,
},
{
name: "<SPACE3>",
args: args{s: " "},
want: false,
},
{
name: "<RETURN>",
args: args{s: "\n"},
want: false,
},
{
name: "<RETURN2>",
args: args{s: "\n\n"},
want: false,
},
{
name: "<RETURN3>",
args: args{s: "\n\n"},
want: false,
},
{
name: "<RT>",
args: args{s: "\r"},
want: false,
},
{
name: "<RT2>",
args: args{s: "\r\r"},
want: false,
},
{
name: "<RT3>",
args: args{s: "\r\r\r"},
want: false,
},
{
name: "<RETURN_ENTER>",
args: args{s: "\r\n"},
want: false,
},
{
name: "\\ufeff",
args: args{s: "\ufeff"},
want: false,
},
{
name: "\\u202a",
args: args{s: "\u202a"},
want: false,
},
{
name: "\\u0000",
args: args{s: "\u0000"},
want: false,
},
{
name: "\\u3164",
args: args{s: "\u3164"},
want: false,
},
{
name: "\\u2800",
args: args{s: "\u2800"},
want: false,
},
{
name: "\\u180e",
args: args{s: "\u180e"},
want: false,
},
{
name: "<TAB>",
args: args{s: "\t"},
want: false,
},
}
for _, test := range tests {
actual := IsNotBlank(test.args.s)
if actual != test.want {
t.Errorf("测试 %s,预期 %v,实际值:%v", test.name, test.want, actual)
}
}
}

0 comments on commit ca309e8

Please sign in to comment.