1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

feat: add IsPrintable

This commit is contained in:
dudaodong
2023-03-31 12:00:32 +08:00
parent 217350042b
commit bc25e7a037
5 changed files with 132 additions and 3 deletions

View File

@@ -58,8 +58,8 @@ func IsAllLower(str string) bool {
return str != ""
}
// IsASCII checks if string is ASCII char.
// Play:
// IsASCII checks if string is all ASCII char.
// Play: todo
func IsASCII(str string) bool {
for i := 0; i < len(str); i++ {
if str[i] > unicode.MaxASCII {
@@ -69,6 +69,20 @@ func IsASCII(str string) bool {
return true
}
// IsPrintable checks if string is all printable chars.
// Play: todo
func IsPrintable(str string) bool {
for _, r := range str {
if !unicode.IsPrint(r) {
if r == '\n' || r == '\r' || r == '\t' || r == '`' {
continue
}
return false
}
}
return true
}
// ContainUpper check if the string contain at least one upper case letter A-Z.
// Play: https://go.dev/play/p/CmWeBEk27-z
func ContainUpper(str string) bool {

View File

@@ -428,3 +428,24 @@ func ExampleIsASCII() {
// false
// false
}
func ExampleIsPrintable() {
result1 := IsPrintable("ABC")
result2 := IsPrintable("{id: 123}")
result3 := IsPrintable("")
result4 := IsPrintable("😄")
result5 := IsPrintable("\u0000")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// true
// false
}

View File

@@ -411,3 +411,13 @@ func TestIsASCII(t *testing.T) {
assert.Equal(false, IsASCII("😄"))
assert.Equal(false, IsASCII("你好"))
}
func TestIsPrintable(t *testing.T) {
assert := internal.NewAssert(t, "TestIsPrintable")
assert.Equal(true, IsPrintable("ABC"))
assert.Equal(true, IsPrintable("{id: 123}"))
assert.Equal(true, IsPrintable(""))
assert.Equal(true, IsPrintable("😄"))
assert.Equal(false, IsPrintable("\u0000"))
}