1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

Compare commits

...

8 Commits

Author SHA1 Message Date
dudaodong
8bdd46bda4 doc: add play ground demo 2023-04-03 10:33:57 +08:00
dudaodong
e29b56c3c3 release v2.1.18 2023-04-03 10:12:50 +08:00
dudaodong
c357fc68c8 doc: update readme file for new feature 2023-04-03 10:12:13 +08:00
dudaodong
bc25e7a037 feat: add IsPrintable 2023-03-31 12:00:32 +08:00
dudaodong
217350042b feat: add RemoveNonPrintable 2023-03-30 14:52:32 +08:00
dudaodong
e56a8a1ef5 feat: add IsASCII 2023-03-30 14:41:32 +08:00
dudaodong
6453f755a6 doc: add doc for IsPrime 2023-03-30 14:09:05 +08:00
dudaodong
027abd6ad5 feat: add IsPrime 2023-03-30 11:54:20 +08:00
17 changed files with 551 additions and 37 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.17-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.18-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -709,7 +709,18 @@ import "github.com/duke-git/lancet/v2/mathutil"
- **<big>RangeWithStep</big>** : Creates a slice of numbers from start to end with specified step. - **<big>RangeWithStep</big>** : Creates a slice of numbers from start to end with specified step.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
[[play](https://go.dev/play/p/akLWz0EqOSM)] [[play](https://go.dev/play/p/akLWz0EqOSM)]
- **<big>AngleToRadian</big>** : converts angle value to radian value.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#AngleToRadian)]
[[play](https://go.dev/play/p/CIvlICqrHql)]
- **<big>RadianToAngle</big>** : converts radian value to angle value.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RadianToAngle)]
[[play](https://go.dev/play/p/dQtmOTUOMgi)]
- **<big>PointDistance</big>** : get two points distance.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#PointDistance)]
[[play](https://go.dev/play/p/RrG4JIaziM8)]
- **<big>IsPrime</big>** : checks if number is prime number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#IsPrime)]
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
### 13. Netutil package contains functions to get net information and send http request. ### 13. Netutil package contains functions to get net information and send http request.
@@ -1032,7 +1043,6 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)]
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
### 17. Structs package provides several high level functions to manipulate struct, tag, and field. ### 17. Structs package provides several high level functions to manipulate struct, tag, and field.
```go ```go
@@ -1142,6 +1152,10 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>WordCount</big>** : return the number of meaningful word of a string, word only contains alphabetic characters. - **<big>WordCount</big>** : return the number of meaningful word of a string, word only contains alphabetic characters.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#WordCount)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#WordCount)]
[[play](https://go.dev/play/p/bj7_odx3vRf)] [[play](https://go.dev/play/p/bj7_odx3vRf)]
- **<big>RemoveNonPrintable</big>** : remove non-printable characters from a string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#RemoveNonPrintable)]
[[play](https://go.dev/play/p/og47F5x_jTZ)]
### 19. System package contain some functions about os, runtime, shell command. ### 19. System package contain some functions about os, runtime, shell command.
@@ -1271,6 +1285,12 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsGBK</big>** : check if data encoding is gbk. - **<big>IsGBK</big>** : check if data encoding is gbk.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsGBK)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsGBK)]
[[play](https://go.dev/play/p/E2nt3unlmzP)] [[play](https://go.dev/play/p/E2nt3unlmzP)]
- **<big>IsASCII</big>** : checks if string is all ASCII char.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsASCII)]
[[play](https://go.dev/play/p/hfQNPLX0jNa)]
- **<big>IsPrintable</big>** : checks if string is all printable chars.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsPrintable)]
[[play](https://go.dev/play/p/Pe1FE2gdtTP)]
### 21. xerror package implements helpers for errors. ### 21. xerror package implements helpers for errors.

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.17-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.18-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -708,7 +708,18 @@ import "github.com/duke-git/lancet/v2/mathutil"
- **<big>RangeWithStep</big>** : 根据指定的起始值,结束值,步长,创建一个数字切片。 - **<big>RangeWithStep</big>** : 根据指定的起始值,结束值,步长,创建一个数字切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RangeWithStep)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RangeWithStep)]
[[play](https://go.dev/play/p/akLWz0EqOSM)] [[play](https://go.dev/play/p/akLWz0EqOSM)]
- **<big>AngleToRadian</big>** : 将角度值转为弧度值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#AngleToRadian)]
[[play](https://go.dev/play/p/CIvlICqrHql)]
- **<big>RadianToAngle</big>** : 将弧度值转为角度值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RadianToAngle)]
[[play](https://go.dev/play/p/dQtmOTUOMgi)]
- **<big>PointDistance</big>** : 计算两个坐标点的距离。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#PointDistance)]
[[play](https://go.dev/play/p/RrG4JIaziM8)]
- **<big>IsPrime</big>** : 判断质数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#IsPrime)]
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。 ### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。
@@ -1143,7 +1154,9 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>WordCount</big>** : 返回有意义单词的数量,只支持字母字符单词。 - **<big>WordCount</big>** : 返回有意义单词的数量,只支持字母字符单词。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#WordCount)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#WordCount)]
[[play](https://go.dev/play/p/bj7_odx3vRf)] [[play](https://go.dev/play/p/bj7_odx3vRf)]
- **<big>RemoveNonPrintable</big>** : 删除字符串中不可打印的字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#RemoveNonPrintable)]
[[play](https://go.dev/play/p/og47F5x_jTZ)]
### 19. system 包含 os, runtime, shell command 的相关函数。 ### 19. system 包含 os, runtime, shell command 的相关函数。
@@ -1273,6 +1286,12 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsGBK</big>** : 检查数据编码是否为 gbk汉字内部代码扩展规范 - **<big>IsGBK</big>** : 检查数据编码是否为 gbk汉字内部代码扩展规范
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsGBK)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsGBK)]
[[play](https://go.dev/play/p/E2nt3unlmzP)] [[play](https://go.dev/play/p/E2nt3unlmzP)]
- **<big>IsASCII</big>** : 验证字符串全部为 ASCII 字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsASCII)]
[[play](https://go.dev/play/p/hfQNPLX0jNa)]
- **<big>IsPrintable</big>** : 检查字符串是否全部为可打印字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsPrintable)]
[[play](https://go.dev/play/p/Pe1FE2gdtTP)]
### 21. xerror 包实现一些错误处理函数 ### 21. xerror 包实现一些错误处理函数

View File

@@ -39,6 +39,7 @@ import (
- [AngleToRadian](#AngleToRadian) - [AngleToRadian](#AngleToRadian)
- [RadianToAngle](#RadianToAngle) - [RadianToAngle](#RadianToAngle)
- [PointDistance](#PointDistance) - [PointDistance](#PointDistance)
- [IsPrime](#IsPrime)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -662,3 +663,43 @@ func main() {
// 5 // 5
} }
``` ```
### <span id="IsPrime">IsPrime</span>
<p>Checks if number is prime number.</p>
<b>Signature:</b>
```go
func IsPrime(n int) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.IsPrime(-1)
result2 := mathutil.IsPrime(0)
result3 := mathutil.IsPrime(1)
result4 := mathutil.IsPrime(2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// false
// true
}
```

View File

@@ -39,6 +39,7 @@ import (
- [AngleToRadian](#AngleToRadian) - [AngleToRadian](#AngleToRadian)
- [RadianToAngle](#RadianToAngle) - [RadianToAngle](#RadianToAngle)
- [PointDistance](#PointDistance) - [PointDistance](#PointDistance)
- [IsPrime](#IsPrime)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -661,3 +662,43 @@ func main() {
// 5 // 5
} }
``` ```
### <span id="IsPrime">IsPrime</span>
<p>判断质数。</p>
<b>函数签名:</b>
```go
func IsPrime(n int) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.IsPrime(-1)
result2 := mathutil.IsPrime(0)
result3 := mathutil.IsPrime(1)
result4 := mathutil.IsPrime(2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// false
// true
}

View File

@@ -45,6 +45,7 @@ import (
- [Unwrap](#Unwrap) - [Unwrap](#Unwrap)
- [SplitWords](#SplitWords) - [SplitWords](#SplitWords)
- [WordCount](#WordCount) - [WordCount](#WordCount)
- [RemoveNonPrintable](#RemoveNonPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -938,3 +939,34 @@ func main() {
// 0 // 0
} }
``` ```
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
<p>Remove non-printable characters from a string.</p>
<b>Signature:</b>
```go
func RemoveNonPrintable(str string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
result2 := strutil.RemoveNonPrintable("你好😄")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// hello world
// 你好😄
}
```

View File

@@ -45,6 +45,7 @@ import (
- [Unwrap](#Unwrap) - [Unwrap](#Unwrap)
- [SplitWords](#SplitWords) - [SplitWords](#SplitWords)
- [WordCount](#WordCount) - [WordCount](#WordCount)
- [RemoveNonPrintable](#RemoveNonPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -937,3 +938,34 @@ func main() {
// 0 // 0
} }
``` ```
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
<p>删除字符串中不可打印的字符。</p>
<b>函数签名:</b>
```go
func RemoveNonPrintable(str string) string
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
result2 := strutil.RemoveNonPrintable("你好😄")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// hello world
// 你好😄
}
```

View File

@@ -29,6 +29,7 @@ import (
- [IsAlpha](#IsAlpha) - [IsAlpha](#IsAlpha)
- [IsAllUpper](#IsAllUpper) - [IsAllUpper](#IsAllUpper)
- [IsAllLower](#IsAllLower) - [IsAllLower](#IsAllLower)
- [IsASCII](#IsASCII)
- [IsBase64](#IsBase64) - [IsBase64](#IsBase64)
- [IsChineseMobile](#IsChineseMobile) - [IsChineseMobile](#IsChineseMobile)
- [IsChineseIdNum](#IsChineseIdNum) - [IsChineseIdNum](#IsChineseIdNum)
@@ -50,6 +51,7 @@ import (
- [IsWeakPassword](#IsWeakPassword) - [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue) - [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK) - [IsGBK](#IsGBK)
- [IsPrintable](#IsPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -293,6 +295,46 @@ func main() {
} }
``` ```
### <span id="IsASCII">IsASCII</span>
<p>Checks if string is all ASCII char.</p>
<b>Signature:</b>
```go
func IsASCII(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsASCII("ABC")
result2 := validator.IsASCII("123")
result3 := validator.IsASCII("")
result4 := validator.IsASCII("😄")
result5 := validator.IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
```
### <span id="IsBase64">IsBase64</span> ### <span id="IsBase64">IsBase64</span>
<p>Check if the string is base64 string.</p> <p>Check if the string is base64 string.</p>
@@ -990,3 +1032,44 @@ func main() {
// true // true
} }
``` ```
### <span id="IsPrintable">IsPrintable</span>
<p>Checks if string is all printable chars.</p>
<b>Signature:</b>
```go
func IsPrintable(str string) bool
```
<b>Example:</b>
```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
}
```

View File

@@ -29,6 +29,7 @@ import (
- [IsAlpha](#IsAlpha) - [IsAlpha](#IsAlpha)
- [IsAllUpper](#IsAllUpper) - [IsAllUpper](#IsAllUpper)
- [IsAllLower](#IsAllLower) - [IsAllLower](#IsAllLower)
- [IsASCII](#IsASCII)
- [IsBase64](#IsBase64) - [IsBase64](#IsBase64)
- [IsChineseMobile](#IsChineseMobile) - [IsChineseMobile](#IsChineseMobile)
- [IsChineseIdNum](#IsChineseIdNum) - [IsChineseIdNum](#IsChineseIdNum)
@@ -50,6 +51,7 @@ import (
- [IsWeakPassword](#IsWeakPassword) - [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue) - [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK) - [IsGBK](#IsGBK)
- [IsPrintable](#IsPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -293,6 +295,46 @@ func main() {
} }
``` ```
### <span id="IsASCII">IsASCII</span>
<p>验证字符串全部为ASCII字符。</p>
<b>函数签名:</b>
```go
func IsASCII(str string) bool
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsASCII("ABC")
result2 := validator.IsASCII("123")
result3 := validator.IsASCII("")
result4 := validator.IsASCII("😄")
result5 := validator.IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
```
### <span id="IsBase64">IsBase64</span> ### <span id="IsBase64">IsBase64</span>
<p>验证字符串是否是base64编码</p> <p>验证字符串是否是base64编码</p>
@@ -990,3 +1032,44 @@ func main() {
// true // true
} }
``` ```
### <span id="IsPrintable">IsPrintable</span>
<p>检查字符串是否全部为可打印字符。</p>
<b>函数签名:</b>
```go
func IsPrintable(str string) bool
```
<b>示例:</b>
```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
}
```

View File

@@ -218,21 +218,21 @@ func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T
} }
// AngleToRadian converts angle value to radian value. // AngleToRadian converts angle value to radian value.
// Play: todo // Play: https://go.dev/play/p/CIvlICqrHql
func AngleToRadian(angle float64) float64 { func AngleToRadian(angle float64) float64 {
radian := angle * (math.Pi / 180) radian := angle * (math.Pi / 180)
return radian return radian
} }
// RadianToAngle converts radian value to angle value. // RadianToAngle converts radian value to angle value.
// Play: todo // Play: https://go.dev/play/p/dQtmOTUOMgi
func RadianToAngle(radian float64) float64 { func RadianToAngle(radian float64) float64 {
angle := radian * (180 / math.Pi) angle := radian * (180 / math.Pi)
return angle return angle
} }
// PointDistance get two points distance. // PointDistance get two points distance.
// Play: todo // Play: https://go.dev/play/p/RrG4JIaziM8
func PointDistance(x1, y1, x2, y2 float64) float64 { func PointDistance(x1, y1, x2, y2 float64) float64 {
a := x1 - x2 a := x1 - x2
b := y1 - y2 b := y1 - y2
@@ -240,3 +240,19 @@ func PointDistance(x1, y1, x2, y2 float64) float64 {
return math.Sqrt(c) return math.Sqrt(c)
} }
// IsPrimes checks if number is prime number.
// Play: https://go.dev/play/p/Rdd8UTHZJ7u
func IsPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
if n%i == 0 {
return false
}
}
return true
}

View File

@@ -263,3 +263,21 @@ func ExamplePointDistance() {
// Output: // Output:
// 5 // 5
} }
func ExampleIsPrime() {
result1 := IsPrime(-1)
result2 := IsPrime(0)
result3 := IsPrime(1)
result4 := IsPrime(2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// false
// true
}

View File

@@ -200,3 +200,14 @@ func TestPointDistance(t *testing.T) {
assert.Equal(float64(5), result1) assert.Equal(float64(5), result1)
} }
func TestIsPrime(t *testing.T) {
assert := internal.NewAssert(t, "TestIsPrime")
assert.Equal(false, IsPrime(-1))
assert.Equal(false, IsPrime(0))
assert.Equal(false, IsPrime(1))
assert.Equal(true, IsPrime(2))
assert.Equal(true, IsPrime(3))
assert.Equal(false, IsPrime(4))
}

View File

@@ -360,3 +360,16 @@ func WordCount(s string) int {
return count return count
} }
// RemoveNonPrintable remove non-printable characters from a string.
// Play: https://go.dev/play/p/og47F5x_jTZ
func RemoveNonPrintable(str string) string {
result := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, str)
return result
}

View File

@@ -438,3 +438,14 @@ func ExampleWordCount() {
// 0 // 0
// 0 // 0
} }
func ExampleRemoveNonPrintable() {
result1 := RemoveNonPrintable("hello\u00a0 \u200bworld\n")
result2 := RemoveNonPrintable("你好😄")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// hello world
// 你好😄
}

View File

@@ -342,3 +342,10 @@ func TestWordCount(t *testing.T) {
assert.Equal(v, WordCount(k)) assert.Equal(v, WordCount(k))
} }
} }
func TestRemoveNonPrintable(t *testing.T) {
assert := internal.NewAssert(t, "TestRemoveNonPrintable")
assert.Equal("hello world", RemoveNonPrintable("hello\u00a0 \u200bworld\n"))
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
}

View File

@@ -58,6 +58,31 @@ func IsAllLower(str string) bool {
return str != "" return str != ""
} }
// IsASCII checks if string is all ASCII char.
// Play: https://go.dev/play/p/hfQNPLX0jNa
func IsASCII(str string) bool {
for i := 0; i < len(str); i++ {
if str[i] > unicode.MaxASCII {
return false
}
}
return true
}
// IsPrintable checks if string is all printable chars.
// Play: https://go.dev/play/p/Pe1FE2gdtTP
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. // ContainUpper check if the string contain at least one upper case letter A-Z.
// Play: https://go.dev/play/p/CmWeBEk27-z // Play: https://go.dev/play/p/CmWeBEk27-z
func ContainUpper(str string) bool { func ContainUpper(str string) bool {

View File

@@ -407,3 +407,45 @@ func ExampleIsGBK() {
// Output: // Output:
// true // true
} }
func ExampleIsASCII() {
result1 := IsASCII("ABC")
result2 := IsASCII("123")
result3 := IsASCII("")
result4 := IsASCII("😄")
result5 := IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// 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

@@ -401,3 +401,23 @@ func TestIsGBK(t *testing.T) {
assert.Equal(true, IsGBK(gbkData)) assert.Equal(true, IsGBK(gbkData))
assert.Equal(false, utf8.Valid(gbkData)) assert.Equal(false, utf8.Valid(gbkData))
} }
func TestIsASCII(t *testing.T) {
assert := internal.NewAssert(t, "TestIsASCII")
assert.Equal(true, IsASCII("ABC"))
assert.Equal(true, IsASCII("123"))
assert.Equal(true, IsASCII(""))
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"))
}