diff --git a/docs/validator.md b/docs/validator.md index e39af92..3809aff 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -51,6 +51,7 @@ import ( - [IsWeakPassword](#IsWeakPassword) - [IsZeroValue](#IsZeroValue) - [IsGBK](#IsGBK) +- [IsPrintable](#IsPrintable)
@@ -296,7 +297,7 @@ func main() { ### IsASCII -

Checks if string is ASCII char.

+

Checks if string is all ASCII char.

Signature: @@ -1031,3 +1032,44 @@ func main() { // true } ``` + + +### IsPrintable + +

Checks if string is all printable chars.

+ +Signature: + +```go +func IsPrintable(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsPrintable("ABC") + result2 := validator.IsPrintable("{id: 123}") + result3 := validator.IsPrintable("") + result4 := validator.IsPrintable("😄") + result5 := validator.IsPrintable("\u0000") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // true + // true + // false +} +``` \ No newline at end of file diff --git a/docs/validator_zh-CN.md b/docs/validator_zh-CN.md index 41f8a01..b55a765 100644 --- a/docs/validator_zh-CN.md +++ b/docs/validator_zh-CN.md @@ -51,6 +51,7 @@ import ( - [IsWeakPassword](#IsWeakPassword) - [IsZeroValue](#IsZeroValue) - [IsGBK](#IsGBK) +- [IsPrintable](#IsPrintable)
@@ -1031,3 +1032,44 @@ func main() { // true } ``` + + +### IsPrintable + +

检查字符串是否全部为可打印字符。

+ +函数签名: + +```go +func IsPrintable(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsPrintable("ABC") + result2 := validator.IsPrintable("{id: 123}") + result3 := validator.IsPrintable("") + result4 := validator.IsPrintable("😄") + result5 := validator.IsPrintable("\u0000") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // true + // true + // false +} +``` \ No newline at end of file diff --git a/validator/validator.go b/validator/validator.go index 4af7e08..1f2b0bf 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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 { diff --git a/validator/validator_example_test.go b/validator/validator_example_test.go index 482d262..8b36812 100644 --- a/validator/validator_example_test.go +++ b/validator/validator_example_test.go @@ -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 +} diff --git a/validator/validator_test.go b/validator/validator_test.go index 82dafcb..3e5560e 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -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")) +}