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

Compare commits

...

4 Commits

Author SHA1 Message Date
dudaodong
2725575d2f doc: add doc for IsGBK' 2022-12-09 19:28:45 +08:00
dudaodong
037d2729ce doc: update doc for ExecCommand 2022-12-09 19:20:15 +08:00
dudaodong
09d98745b0 fix: fix ExecCommand bug in windows 2022-12-09 19:10:55 +08:00
dudaodong
af5cfe6da1 feat: add IsGBK validator 2022-12-09 17:36:59 +08:00
10 changed files with 213 additions and 31 deletions

View File

@@ -211,7 +211,7 @@ func main() {
### <span id="ExecCommand">CompareOsEnv</span> ### <span id="ExecCommand">CompareOsEnv</span>
<p>Use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p> <p>Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -227,10 +227,24 @@ import (
) )
func main() { func main() {
out, errout, err := system.ExecCommand("ls") // linux or mac
fmt.Println(out) stdout, stderr, err := system.ExecCommand("ls")
fmt.Println(errout) fmt.Println("std out: ", stdout)
fmt.Println(err) fmt.Println("std err: ", stderr)
assert.Equal("", stderr)
// windows
stdout, stderr, err = system.ExecCommand("dir")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
// error command
stdout, stderr, err = system.ExecCommand("abc")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
if err != nil {
fmt.Println(err.Error())
}
} }
``` ```

View File

@@ -212,7 +212,7 @@ func main() {
### <span id="ExecCommand">ExecCommand</span> ### <span id="ExecCommand">ExecCommand</span>
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p> <p>执行shell命令返回命令的stdout和stderr字符串如果出现错误则返回错误。参数`command`是一个完整的命令字符串如ls-alinuxdirwindowsping 127.0.0.1。在linux中使用/bin/bash-c执行命令在windows中使用powershell.exe执行命令</p>
<b>Signature:</b> <b>Signature:</b>
@@ -228,10 +228,24 @@ import (
) )
func main() { func main() {
out, errout, err := system.ExecCommand("ls") // linux or mac
fmt.Println(out) stdout, stderr, err := system.ExecCommand("ls")
fmt.Println(errout) fmt.Println("std out: ", stdout)
fmt.Println(err) fmt.Println("std err: ", stderr)
assert.Equal("", stderr)
// windows
stdout, stderr, err = system.ExecCommand("dir")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
// error command
stdout, stderr, err = system.ExecCommand("abc")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
if err != nil {
fmt.Println(err.Error())
}
} }
``` ```

View File

@@ -47,6 +47,7 @@ import (
- [IsUrl](#IsUrl) - [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword) - [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue) - [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -821,7 +822,34 @@ func main() {
``` ```
### <span id="IsGBK">IsGBK</span>
<p>Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.</p>
<b>Signature:</b>
```go
func IsGBK(data []byte) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
data := []byte("你好")
// check utf8 first
if utf8.Valid(data) {
fmt.Println("data encoding is utf-8")
}else if(validator.IsGBK(data)) {
fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")
}
```

View File

@@ -47,6 +47,7 @@ import (
- [IsUrl](#IsUrl) - [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword) - [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue) - [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -820,3 +821,31 @@ func main() {
} }
``` ```
### <span id="IsGBK">IsGBK</span>
<p>检查数据编码是否为gbk汉字内部代码扩展规范。该函数的实现取决于双字节是否在gbk的编码范围内而utf-8编码格式的每个字节都在gbk编码范围内。因此应该首先调用utf8.valid检查它是否是utf-8编码然后调用IsGBK检查gbk编码。如示例所示。</p>
<b>函数签名:</b>
```go
func IsGBK(data []byte) bool
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
data := []byte("你好")
// 先检查utf8编码
if utf8.Valid(data) {
fmt.Println("data encoding is utf-8")
}else if(validator.IsGBK(data)) {
fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")
}
```

2
go.mod
View File

@@ -1,3 +1,5 @@
module github.com/duke-git/lancet/v2 module github.com/duke-git/lancet/v2
go 1.18 go 1.18
require golang.org/x/text v0.5.0

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=

View File

@@ -9,6 +9,10 @@ import (
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"unicode/utf8"
"github.com/duke-git/lancet/v2/validator"
"golang.org/x/text/encoding/simplifiedchinese"
) )
// IsWindows check if current os is windows // IsWindows check if current os is windows
@@ -50,27 +54,61 @@ func CompareOsEnv(key, comparedEnv string) bool {
return env == comparedEnv return env == comparedEnv
} }
// ExecCommand use shell /bin/bash -c to execute command // ExecCommand execute command, return the stdout and stderr string of command, and error if error occur
// param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1
// in linux, use /bin/bash -c to execute command
// in windows, use powershell.exe to execute command
func ExecCommand(command string) (stdout, stderr string, err error) { func ExecCommand(command string) (stdout, stderr string, err error) {
var out bytes.Buffer var out bytes.Buffer
var errout bytes.Buffer var errOut bytes.Buffer
cmd := exec.Command("/bin/bash", "-c", command) cmd := exec.Command("/bin/bash", "-c", command)
if IsWindows() { if IsWindows() {
cmd = exec.Command("cmd") cmd = exec.Command("powershell.exe", command)
} }
cmd.Stdout = &out cmd.Stdout = &out
cmd.Stderr = &errout cmd.Stderr = &errOut
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
stderr = string(errout.Bytes()) if utf8.Valid(errOut.Bytes()) {
stderr = byteToString(errOut.Bytes(), "UTF8")
} else if validator.IsGBK(errOut.Bytes()) {
stderr = byteToString(errOut.Bytes(), "GBK")
}
return
}
data := out.Bytes()
if utf8.Valid(data) {
stdout = byteToString(data, "UTF8")
} else if validator.IsGBK(data) {
stdout = byteToString(data, "GBK")
} }
stdout = string(out.Bytes())
return return
} }
func byteToString(data []byte, charset string) string {
var result string
switch charset {
case "GBK":
decodeBytes, _ := simplifiedchinese.GBK.NewDecoder().Bytes(data)
result = string(decodeBytes)
case "GB18030":
decodeBytes, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(data)
result = string(decodeBytes)
case "UTF8":
fallthrough
default:
result = string(data)
}
return result
}
// GetOsBits get this system bits 32bit or 64bit // GetOsBits get this system bits 32bit or 64bit
// return bit int (32/64) // return bit int (32/64)
func GetOsBits() int { func GetOsBits() int {

View File

@@ -11,10 +11,10 @@ func TestOsDetection(t *testing.T) {
assert := internal.NewAssert(t, "TestOsJudgment") assert := internal.NewAssert(t, "TestOsJudgment")
osType, _, _ := ExecCommand("echo $OSTYPE") osType, _, _ := ExecCommand("echo $OSTYPE")
if strings.Index(osType, "linux") != -1 { if strings.Contains(osType, "linux") {
assert.Equal(true, IsLinux()) assert.Equal(true, IsLinux())
} }
if strings.Index(osType, "darwin") != -1 { if strings.Contains(osType, "darwin") {
assert.Equal(true, IsMac()) assert.Equal(true, IsMac())
} }
} }
@@ -44,21 +44,27 @@ func TestOsEnvOperation(t *testing.T) {
func TestExecCommand(t *testing.T) { func TestExecCommand(t *testing.T) {
assert := internal.NewAssert(t, "TestExecCommand") assert := internal.NewAssert(t, "TestExecCommand")
out, errout, err := ExecCommand("ls") // linux or mac
t.Log("std out: ", out) stdout, stderr, err := ExecCommand("ls")
t.Log("std err: ", errout) t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
assert.Equal("", stderr)
assert.IsNil(err) assert.IsNil(err)
out, errout, err = ExecCommand("abc") // windows
t.Log("std out: ", out) stdout, stderr, err = ExecCommand("dir")
t.Log("std err: ", errout) t.Log("std out: ", stdout)
if err != nil { t.Log("std err: ", stderr)
t.Logf("error: %v\n", err) assert.IsNil(err)
}
if !IsWindows() { // error command
assert.IsNotNil(err) stdout, stderr, err = ExecCommand("abc")
} t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
// if err != nil {
// t.Log(err.Error())
// }
assert.IsNotNil(err)
} }
func TestGetOsBits(t *testing.T) { func TestGetOsBits(t *testing.T) {

View File

@@ -274,3 +274,40 @@ func IsZeroValue(value any) bool {
return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface()) return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
} }
// IsGBK check if data encoding is gbk
// Note: this function is implemented by whether double bytes fall within the encoding range of gbk,
// while each byte of utf-8 encoding format falls within the encoding range of gbk.
// Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding,
// and then call IsGBK() to check gbk encoding. like below
/**
data := []byte("你好")
if utf8.Valid(data) {
fmt.Println("data encoding is utf-8")
}else if(IsGBK(data)) {
fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")
**/
func IsGBK(data []byte) bool {
i := 0
for i < len(data) {
if data[i] <= 0xff {
i++
continue
} else {
if data[i] >= 0x81 &&
data[i] <= 0xfe &&
data[i+1] >= 0x40 &&
data[i+1] <= 0xfe &&
data[i+1] != 0xf7 {
i += 2
continue
} else {
return false
}
}
}
return true
}

View File

@@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"testing" "testing"
"time" "time"
"unicode/utf8"
"github.com/duke-git/lancet/v2/internal" "github.com/duke-git/lancet/v2/internal"
"golang.org/x/text/encoding/simplifiedchinese"
) )
func TestIsAllUpper(t *testing.T) { func TestIsAllUpper(t *testing.T) {
@@ -388,3 +390,13 @@ func TestIsZeroValue(t *testing.T) {
assert.Equal(false, IsZeroValue(value)) assert.Equal(false, IsZeroValue(value))
} }
} }
func TestIsGBK(t *testing.T) {
assert := internal.NewAssert(t, "TestIsGBK")
str := "你好"
gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))
assert.Equal(true, IsGBK(gbkData))
assert.Equal(false, utf8.Valid(gbkData))
}