mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-19 20:22:25 +08:00
Compare commits
9 Commits
v1.3.9
...
c8a65c33a4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8a65c33a4 | ||
|
|
69a797f8ed | ||
|
|
1e5b69e9bf | ||
|
|
275abcc8c2 | ||
|
|
065b3b84fe | ||
|
|
4bc43f3278 | ||
|
|
f3382ceac9 | ||
|
|
d20f8783b2 | ||
|
|
8432a4e1ee |
@@ -11,11 +11,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ToBool convert string to a boolean
|
// ToBool convert string to a boolean
|
||||||
@@ -374,3 +378,17 @@ func ToInterface(v reflect.Value) (value interface{}, ok bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Utf8ToGbk convert utf8 encoding data to GBK encoding data.
|
||||||
|
func Utf8ToGbk(bs []byte) ([]byte, error) {
|
||||||
|
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewEncoder())
|
||||||
|
b, err := io.ReadAll(r)
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GbkToUtf8 convert GBK encoding data to utf8 encoding data.
|
||||||
|
func GbkToUtf8(bs []byte) ([]byte, error) {
|
||||||
|
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder())
|
||||||
|
b, err := io.ReadAll(r)
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestToChar(t *testing.T) {
|
func TestToChar(t *testing.T) {
|
||||||
@@ -339,3 +341,25 @@ func TestToInterface(t *testing.T) {
|
|||||||
assert.EqualValues(nil, nilVal)
|
assert.EqualValues(nil, nilVal)
|
||||||
assert.Equal(false, ok)
|
assert.Equal(false, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUtf8ToGbk(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUtf8ToGbk")
|
||||||
|
|
||||||
|
utf8Data := []byte("hello")
|
||||||
|
gbkData, err := Utf8ToGbk(utf8Data)
|
||||||
|
|
||||||
|
assert.Equal(true, utf8.Valid(utf8Data))
|
||||||
|
assert.Equal(true, validator.IsGBK(gbkData))
|
||||||
|
assert.IsNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGbkToUtf8(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestGbkToUtf8")
|
||||||
|
|
||||||
|
gbkData, err := Utf8ToGbk([]byte("hello"))
|
||||||
|
utf8Data, err := GbkToUtf8(gbkData)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(true, utf8.Valid(utf8Data))
|
||||||
|
assert.Equal("hello", string(utf8Data))
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ import (
|
|||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
- [CopyProperties](#CopyProperties)
|
- [CopyProperties](#CopyProperties)
|
||||||
- [ToInterface](#ToInterface)
|
- [ToInterface](#ToInterface)
|
||||||
|
- [Utf8ToGbk](#Utf8ToGbk)
|
||||||
|
- [GbkToUtf8](#GbkToUtf8)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -606,15 +608,82 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
val := reflect.ValueOf("abc")
|
val := reflect.ValueOf("abc")
|
||||||
iVal, ok := convertor.ToInterface(val)
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
fmt.Printf("%T\n", iVal)
|
fmt.Printf("%T\n", iVal)
|
||||||
fmt.Printf("%v\n", iVal)
|
fmt.Printf("%v\n", iVal)
|
||||||
fmt.Println(ok)
|
fmt.Println(ok)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// string
|
// string
|
||||||
// abc
|
// abc
|
||||||
// true
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Utf8ToGbk">Utf8ToGbk</span>
|
||||||
|
|
||||||
|
<p>Converts utf8 encoding data to GBK encoding data.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Utf8ToGbk(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
utf8Data := []byte("hello")
|
||||||
|
gbkData, _ := convertor.Utf8ToGbk(utf8Data)
|
||||||
|
|
||||||
|
fmt.Println(utf8.Valid(utf8Data))
|
||||||
|
fmt.Println(validator.IsGBK(gbkData))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GbkToUtf8">GbkToUtf8</span>
|
||||||
|
|
||||||
|
<p>Converts GBK encoding data to utf8 encoding data.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GbkToUtf8(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
gbkData, _ := convertor.Utf8ToGbk([]byte("hello"))
|
||||||
|
utf8Data, _ := convertor.GbkToUtf8(gbkData)
|
||||||
|
|
||||||
|
fmt.Println(utf8.Valid(utf8Data))
|
||||||
|
fmt.Println(string(utf8Data))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -38,6 +38,8 @@ import (
|
|||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
- [CopyProperties](#CopyProperties)
|
- [CopyProperties](#CopyProperties)
|
||||||
- [ToInterface](#ToInterface)
|
- [ToInterface](#ToInterface)
|
||||||
|
- [Utf8ToGbk](#Utf8ToGbk)
|
||||||
|
- [GbkToUtf8](#GbkToUtf8)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -606,15 +608,82 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
val := reflect.ValueOf("abc")
|
val := reflect.ValueOf("abc")
|
||||||
iVal, ok := convertor.ToInterface(val)
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
fmt.Printf("%T\n", iVal)
|
fmt.Printf("%T\n", iVal)
|
||||||
fmt.Printf("%v\n", iVal)
|
fmt.Printf("%v\n", iVal)
|
||||||
fmt.Println(ok)
|
fmt.Println(ok)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// string
|
// string
|
||||||
// abc
|
// abc
|
||||||
// true
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Utf8ToGbk">Utf8ToGbk</span>
|
||||||
|
|
||||||
|
<p>utf8编码转GBK编码。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Utf8ToGbk(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
utf8Data := []byte("hello")
|
||||||
|
gbkData, _ := convertor.Utf8ToGbk(utf8Data)
|
||||||
|
|
||||||
|
fmt.Println(utf8.Valid(utf8Data))
|
||||||
|
fmt.Println(validator.IsGBK(gbkData))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GbkToUtf8">GbkToUtf8</span>
|
||||||
|
|
||||||
|
<p>GBK编码转utf8编码。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GbkToUtf8(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
gbkData, _ := convertor.Utf8ToGbk([]byte("hello"))
|
||||||
|
utf8Data, _ := convertor.GbkToUtf8(gbkData)
|
||||||
|
|
||||||
|
fmt.Println(utf8.Valid(utf8Data))
|
||||||
|
fmt.Println(string(utf8Data))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -199,18 +199,18 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
after1Year := datetime.AddYear(now, 1)
|
after1Year := datetime.AddYear(now, 1)
|
||||||
diff1 := after1Year.Sub(now)
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
before1Year := datetime.AddYear(now, -1)
|
before1Year := datetime.AddYear(now, -1)
|
||||||
diff2 := before1Year.Sub(now)
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
fmt.Println(diff1)
|
fmt.Println(diff1)
|
||||||
fmt.Println(diff2)
|
fmt.Println(diff2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 8760h0m0s
|
// 8760h0m0s
|
||||||
// -8760h0m0s
|
// -8760h0m0s
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -200,18 +200,18 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
after1Year := datetime.AddYear(now, 1)
|
after1Year := datetime.AddYear(now, 1)
|
||||||
diff1 := after1Year.Sub(now)
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
before1Year := datetime.AddYear(now, -1)
|
before1Year := datetime.AddYear(now, -1)
|
||||||
diff2 := before1Year.Sub(now)
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
fmt.Println(diff1)
|
fmt.Println(diff1)
|
||||||
fmt.Println(diff2)
|
fmt.Println(diff2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 8760h0m0s
|
// 8760h0m0s
|
||||||
// -8760h0m0s
|
// -8760h0m0s
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
109
docs/fileutil.md
109
docs/fileutil.md
@@ -43,6 +43,7 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteCsvFile](#WriteCsvFile)
|
||||||
- [WriteStringToFile](#WriteStringToFile)
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
- [WriteBytesToFile](#WriteBytesToFile)
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
@@ -503,7 +504,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsZipFile">IsZipFile</span>
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
<p>Checks if file is zip file or not.</p>
|
<p>Checks if file is zip file or not.</p>
|
||||||
@@ -664,6 +664,42 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteCsvFile">WriteCsvFile</span>
|
||||||
|
|
||||||
|
<p>Write content to target csv file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteCsvFile(filepath string, records [][]string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := [][]string{
|
||||||
|
{"Lili", "22", "female"},
|
||||||
|
{"Jim", "21", "male"},
|
||||||
|
}
|
||||||
|
err := WriteCsvFile("./testdata/test2.csv", data, false)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
content, _ := ReadCsvFile("./testdata/test2.csv")
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// [[Lili 22 female] [Jim 21 male]]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
@@ -688,33 +724,32 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./bytes.txt"
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="WriteStringToFile">WriteStringToFile</span>
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
<p>Writes string to target file.</p>
|
<p>Writes string to target file.</p>
|
||||||
@@ -738,28 +773,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./test.txt"
|
filepath := "./test.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteCsvFile](#WriteCsvFile)
|
||||||
- [WriteStringToFile](#WriteStringToFile)
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
- [WriteBytesToFile](#WriteBytesToFile)
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
@@ -503,7 +504,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsZipFile">IsZipFile</span>
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
<p>判断文件是否是zip压缩文件。</p>
|
<p>判断文件是否是zip压缩文件。</p>
|
||||||
@@ -664,6 +664,43 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteCsvFile">WriteCsvFile</span>
|
||||||
|
|
||||||
|
<p>向csv文件写入内容。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteCsvFile(filepath string, records [][]string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := [][]string{
|
||||||
|
{"Lili", "22", "female"},
|
||||||
|
{"Jim", "21", "male"},
|
||||||
|
}
|
||||||
|
err := WriteCsvFile("./testdata/test2.csv", data, false)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
content, _ := ReadCsvFile("./testdata/test2.csv")
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// [[Lili 22 female] [Jim 21 male]]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
<p>将bytes写入文件。</p>
|
<p>将bytes写入文件。</p>
|
||||||
@@ -687,33 +724,32 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./bytes.txt"
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="WriteStringToFile">WriteStringToFile</span>
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
<p>将字符串写入文件。</p>
|
<p>将字符串写入文件。</p>
|
||||||
@@ -737,28 +773,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./test.txt"
|
filepath := "./test.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -323,16 +323,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addOne := func(x interface{}) interface{} {
|
addOne := func(x interface{}) interface{} {
|
||||||
return x.(int) + 1
|
return x.(int) + 1
|
||||||
}
|
}
|
||||||
double := func(x interface{}) interface{} {
|
double := func(x interface{}) interface{} {
|
||||||
return 2 * x.(int)
|
return 2 * x.(int)
|
||||||
}
|
}
|
||||||
square := func(x interface{}) interface{} {
|
square := func(x interface{}) interface{} {
|
||||||
return x.(int) * x.(int)
|
return x.(int) * x.(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
f := function.Pipeline(addOne, double, square)
|
f := function.Pipeline(addOne, double, square)
|
||||||
|
|
||||||
result := fn(2)
|
result := fn(2)
|
||||||
|
|
||||||
|
|||||||
@@ -322,16 +322,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addOne := func(x interface{}) interface{} {
|
addOne := func(x interface{}) interface{} {
|
||||||
return x.(int) + 1
|
return x.(int) + 1
|
||||||
}
|
}
|
||||||
double := func(x interface{}) interface{} {
|
double := func(x interface{}) interface{} {
|
||||||
return 2 * x.(int)
|
return 2 * x.(int)
|
||||||
}
|
}
|
||||||
square := func(x interface{}) interface{} {
|
square := func(x interface{}) interface{} {
|
||||||
return x.(int) * x.(int)
|
return x.(int) * x.(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
f := function.Pipeline(addOne, double, square)
|
f := function.Pipeline(addOne, double, square)
|
||||||
|
|
||||||
result := fn(2)
|
result := fn(2)
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import (
|
|||||||
- [LCM](#LCM)
|
- [LCM](#LCM)
|
||||||
- [Cos](#Cos)
|
- [Cos](#Cos)
|
||||||
- [Sin](#Sin)
|
- [Sin](#Sin)
|
||||||
|
- [Log](#Log)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -363,20 +364,20 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.IsPrime(-1)
|
result1 := mathutil.IsPrime(-1)
|
||||||
result2 := mathutil.IsPrime(0)
|
result2 := mathutil.IsPrime(0)
|
||||||
result3 := mathutil.IsPrime(1)
|
result3 := mathutil.IsPrime(1)
|
||||||
result4 := mathutil.IsPrime(2)
|
result4 := mathutil.IsPrime(2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -458,7 +459,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Cos">Cos</span>
|
### <span id="Cos">Cos</span>
|
||||||
|
|
||||||
<p>Returns the cosine of the radian argument.</p>
|
<p>Returns the cosine of the radian argument.</p>
|
||||||
@@ -501,7 +501,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sin">Sin</span>
|
### <span id="Sin">Sin</span>
|
||||||
|
|
||||||
<p>Returns the sine of the radian argument.</p>
|
<p>Returns the sine of the radian argument.</p>
|
||||||
@@ -542,4 +541,40 @@ func main() {
|
|||||||
// 0
|
// 0
|
||||||
// 1
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Log">Log</span>
|
||||||
|
|
||||||
|
<p>Returns the logarithm of base n.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Log(n, base float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Log(8, 2)
|
||||||
|
result2 := mathutil.TruncRound(mathutil.Log(5, 2), 2)
|
||||||
|
result3 := mathutil.TruncRound(mathutil.Log(27, 3), 0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
|
// 2.32
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import (
|
|||||||
- [LCM](#LCM)
|
- [LCM](#LCM)
|
||||||
- [Cos](#Cos)
|
- [Cos](#Cos)
|
||||||
- [Sin](#Sin)
|
- [Sin](#Sin)
|
||||||
|
- [Log](#Log)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -363,20 +364,20 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.IsPrime(-1)
|
result1 := mathutil.IsPrime(-1)
|
||||||
result2 := mathutil.IsPrime(0)
|
result2 := mathutil.IsPrime(0)
|
||||||
result3 := mathutil.IsPrime(1)
|
result3 := mathutil.IsPrime(1)
|
||||||
result4 := mathutil.IsPrime(2)
|
result4 := mathutil.IsPrime(2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -543,4 +544,40 @@ func main() {
|
|||||||
// 0
|
// 0
|
||||||
// 1
|
// 1
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Log">Log</span>
|
||||||
|
|
||||||
|
<p>计算以base为底n的对数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Log(n, base float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Log(8, 2)
|
||||||
|
result2 := mathutil.TruncRound(mathutil.Log(5, 2), 2)
|
||||||
|
result3 := mathutil.TruncRound(mathutil.Log(27, 3), 0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
|
// 2.32
|
||||||
|
// 3
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -658,24 +658,24 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
// "Content-Type": "multipart/form-data",
|
// "Content-Type": "multipart/form-data",
|
||||||
}
|
}
|
||||||
|
|
||||||
postData := url.Values{}
|
postData := url.Values{}
|
||||||
postData.Add("userId", "1")
|
postData.Add("userId", "1")
|
||||||
postData.Add("title", "TestToDo")
|
postData.Add("title", "TestToDo")
|
||||||
|
|
||||||
// postData := make(map[string]string)
|
// postData := make(map[string]string)
|
||||||
// postData["userId"] = "1"
|
// postData["userId"] = "1"
|
||||||
// postData["title"] = "title"
|
// postData["title"] = "title"
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -657,23 +657,23 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
// "Content-Type": "multipart/form-data",
|
// "Content-Type": "multipart/form-data",
|
||||||
}
|
}
|
||||||
|
|
||||||
postData := url.Values{}
|
postData := url.Values{}
|
||||||
postData.Add("userId", "1")
|
postData.Add("userId", "1")
|
||||||
postData.Add("title", "TestToDo")
|
postData.Add("title", "TestToDo")
|
||||||
|
|
||||||
// postData := make(map[string]string)
|
// postData := make(map[string]string)
|
||||||
// postData["userId"] = "1"
|
// postData["userId"] = "1"
|
||||||
// postData["title"] = "title"
|
// postData["title"] = "title"
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
fmt.Println(body)
|
fmt.Println(body)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
- [RandNumeral](#RandNumeral)
|
- [RandNumeral](#RandNumeral)
|
||||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
- [UUIdV4](#UUIdV4)
|
- [UUIdV4](#UUIdV4)
|
||||||
|
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -245,3 +246,29 @@ func main() {
|
|||||||
fmt.Println(uuid)
|
fmt.Println(uuid)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandUniqueIntSlice">RandUniqueIntSlice</span>
|
||||||
|
|
||||||
|
<p>Generate a slice of random int of length n that do not repeat.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandUniqueIntSlice(n, min, max int) []int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := RandUniqueIntSlice(5, 0, 10)
|
||||||
|
fmt.Println(result) //[0 4 7 1 5] (random)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
- [RandNumeral](#RandNumeral)
|
- [RandNumeral](#RandNumeral)
|
||||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
- [UUIdV4](#UUIdV4)
|
- [UUIdV4](#UUIdV4)
|
||||||
|
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -245,3 +246,29 @@ func main() {
|
|||||||
fmt.Println(uuid)
|
fmt.Println(uuid)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandUniqueIntSlice">RandUniqueIntSlice</span>
|
||||||
|
|
||||||
|
<p>生成一个不重复的长度为n的随机int切片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandUniqueIntSlice(n, min, max int) []int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := RandUniqueIntSlice(5, 0, 10)
|
||||||
|
fmt.Println(result) //[0 4 7 1 5] (random)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
204
docs/strutil.md
204
docs/strutil.md
@@ -57,6 +57,7 @@ import (
|
|||||||
- [Trim](#Trim)
|
- [Trim](#Trim)
|
||||||
- [SplitAndTrim](#SplitAndTrim)
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
- [HideString](#HideString)
|
- [HideString](#HideString)
|
||||||
|
- [RemoveWhiteSpace](#RemoveWhiteSpace)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -857,13 +858,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.StringToBytes("abc")
|
result1 := strutil.StringToBytes("abc")
|
||||||
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// [97 98 99]
|
// [97 98 99]
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -887,11 +888,11 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bytes := []byte{'a', 'b', 'c'}
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
result := strutil.BytesToString(bytes)
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
// Output:
|
||||||
// abc
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -915,16 +916,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.IsBlank("")
|
result1 := strutil.IsBlank("")
|
||||||
result2 := strutil.IsBlank("\t\v\f\n")
|
result2 := strutil.IsBlank("\t\v\f\n")
|
||||||
result3 := strutil.IsBlank(" 中文")
|
result3 := strutil.IsBlank(" 中文")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -948,13 +949,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -978,13 +979,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
||||||
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1009,23 +1010,23 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := "foo bar hello world"
|
str := "foo bar hello world"
|
||||||
|
|
||||||
result1 := strutil.IndexOffset(str, "o", 5)
|
result1 := strutil.IndexOffset(str, "o", 5)
|
||||||
result2 := strutil.IndexOffset(str, "o", 0)
|
result2 := strutil.IndexOffset(str, "o", 0)
|
||||||
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
||||||
result4 := strutil.IndexOffset(str, "d", len(str))
|
result4 := strutil.IndexOffset(str, "d", len(str))
|
||||||
result5 := strutil.IndexOffset(str, "f", -1)
|
result5 := strutil.IndexOffset(str, "f", -1)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
// Output:
|
// Output:
|
||||||
// 12
|
// 12
|
||||||
// 1
|
// 1
|
||||||
// 18
|
// 18
|
||||||
// -1
|
// -1
|
||||||
// -1
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1049,16 +1050,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := "ac ab ab ac"
|
str := "ac ab ab ac"
|
||||||
replaces := map[string]string{
|
replaces := map[string]string{
|
||||||
"a": "1",
|
"a": "1",
|
||||||
"b": "2",
|
"b": "2",
|
||||||
}
|
}
|
||||||
|
|
||||||
result := strutil.ReplaceWithMap(str, replaces)
|
result := strutil.ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
// Output:
|
||||||
// 1c 12 12 1c
|
// 1c 12 12 1c
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1083,19 +1084,19 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.Trim("\nabcd")
|
result1 := strutil.Trim("\nabcd")
|
||||||
|
|
||||||
str := "$ ab cd $ "
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
result2 := strutil.Trim(str)
|
result2 := strutil.Trim(str)
|
||||||
result3 := strutil.Trim(str, "$")
|
result3 := strutil.Trim(str, "$")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// abcd
|
// abcd
|
||||||
// $ ab cd $
|
// $ ab cd $
|
||||||
// ab cd
|
// ab cd
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1120,15 +1121,15 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := " a,b, c,d,$1 "
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
result1 := strutil.SplitAndTrim(str, ",")
|
result1 := strutil.SplitAndTrim(str, ",")
|
||||||
result2 := strutil.SplitAndTrim(str, ",", "$")
|
result2 := strutil.SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [a b c d $1]
|
// [a b c d $1]
|
||||||
// [a b c d 1]
|
// [a b c d 1]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1153,21 +1154,21 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := "13242658976"
|
str := "13242658976"
|
||||||
|
|
||||||
result1 := strutil.HideString(str, 3, 3, "*")
|
result1 := strutil.HideString(str, 3, 3, "*")
|
||||||
result2 := strutil.HideString(str, 3, 4, "*")
|
result2 := strutil.HideString(str, 3, 4, "*")
|
||||||
result3 := strutil.HideString(str, 3, 7, "*")
|
result3 := strutil.HideString(str, 3, 7, "*")
|
||||||
result4 := strutil.HideString(str, 7, 11, "*")
|
result4 := strutil.HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 13242658976
|
// 13242658976
|
||||||
// 132*2658976
|
// 132*2658976
|
||||||
// 132****8976
|
// 132****8976
|
||||||
// 1324265****
|
// 1324265****
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1240,3 +1241,36 @@ func main() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RemoveWhiteSpace">RemoveWhiteSpace</span>
|
||||||
|
|
||||||
|
<p>Remove whitespace characters from a string. when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveWhiteSpace(str string, repalceAll bool) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := " hello \r\n \t world"
|
||||||
|
|
||||||
|
result1 := strutil.RemoveWhiteSpace(str, true)
|
||||||
|
result2 := strutil.RemoveWhiteSpace(str, false)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// helloworld
|
||||||
|
// hello world
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ import (
|
|||||||
- [Trim](#Trim)
|
- [Trim](#Trim)
|
||||||
- [SplitAndTrim](#SplitAndTrim)
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
- [HideString](#HideString)
|
- [HideString](#HideString)
|
||||||
|
- [RemoveWhiteSpace](#RemoveWhiteSpace)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -888,13 +890,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.StringToBytes("abc")
|
result1 := strutil.StringToBytes("abc")
|
||||||
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// [97 98 99]
|
// [97 98 99]
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -918,11 +920,11 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bytes := []byte{'a', 'b', 'c'}
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
result := strutil.BytesToString(bytes)
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
// Output:
|
||||||
// abc
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -946,16 +948,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.IsBlank("")
|
result1 := strutil.IsBlank("")
|
||||||
result2 := strutil.IsBlank("\t\v\f\n")
|
result2 := strutil.IsBlank("\t\v\f\n")
|
||||||
result3 := strutil.IsBlank(" 中文")
|
result3 := strutil.IsBlank(" 中文")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -979,13 +981,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1009,13 +1011,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
||||||
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1040,23 +1042,23 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := "foo bar hello world"
|
str := "foo bar hello world"
|
||||||
|
|
||||||
result1 := strutil.IndexOffset(str, "o", 5)
|
result1 := strutil.IndexOffset(str, "o", 5)
|
||||||
result2 := strutil.IndexOffset(str, "o", 0)
|
result2 := strutil.IndexOffset(str, "o", 0)
|
||||||
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
||||||
result4 := strutil.IndexOffset(str, "d", len(str))
|
result4 := strutil.IndexOffset(str, "d", len(str))
|
||||||
result5 := strutil.IndexOffset(str, "f", -1)
|
result5 := strutil.IndexOffset(str, "f", -1)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
// Output:
|
// Output:
|
||||||
// 12
|
// 12
|
||||||
// 1
|
// 1
|
||||||
// 18
|
// 18
|
||||||
// -1
|
// -1
|
||||||
// -1
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1080,16 +1082,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := "ac ab ab ac"
|
str := "ac ab ab ac"
|
||||||
replaces := map[string]string{
|
replaces := map[string]string{
|
||||||
"a": "1",
|
"a": "1",
|
||||||
"b": "2",
|
"b": "2",
|
||||||
}
|
}
|
||||||
|
|
||||||
result := strutil.ReplaceWithMap(str, replaces)
|
result := strutil.ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
// Output:
|
||||||
// 1c 12 12 1c
|
// 1c 12 12 1c
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1114,19 +1116,19 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.Trim("\nabcd")
|
result1 := strutil.Trim("\nabcd")
|
||||||
|
|
||||||
str := "$ ab cd $ "
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
result2 := strutil.Trim(str)
|
result2 := strutil.Trim(str)
|
||||||
result3 := strutil.Trim(str, "$")
|
result3 := strutil.Trim(str, "$")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// abcd
|
// abcd
|
||||||
// $ ab cd $
|
// $ ab cd $
|
||||||
// ab cd
|
// ab cd
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1152,15 +1154,15 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := " a,b, c,d,$1 "
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
result1 := strutil.SplitAndTrim(str, ",")
|
result1 := strutil.SplitAndTrim(str, ",")
|
||||||
result2 := strutil.SplitAndTrim(str, ",", "$")
|
result2 := strutil.SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [a b c d $1]
|
// [a b c d $1]
|
||||||
// [a b c d 1]
|
// [a b c d 1]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1186,21 +1188,21 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := "13242658976"
|
str := "13242658976"
|
||||||
|
|
||||||
result1 := strutil.HideString(str, 3, 3, "*")
|
result1 := strutil.HideString(str, 3, 3, "*")
|
||||||
result2 := strutil.HideString(str, 3, 4, "*")
|
result2 := strutil.HideString(str, 3, 4, "*")
|
||||||
result3 := strutil.HideString(str, 3, 7, "*")
|
result3 := strutil.HideString(str, 3, 7, "*")
|
||||||
result4 := strutil.HideString(str, 7, 11, "*")
|
result4 := strutil.HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 13242658976
|
// 13242658976
|
||||||
// 132*2658976
|
// 132*2658976
|
||||||
// 132****8976
|
// 132****8976
|
||||||
// 1324265****
|
// 1324265****
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1272,3 +1274,36 @@ func main() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RemoveWhiteSpace">RemoveWhiteSpace</span>
|
||||||
|
|
||||||
|
<p>删除字符串中的空格,当设置repalceAll为true时,删除全部空格,为false时,替换多个空格为1个空格。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveWhiteSpace(str string, repalceAll bool) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := " hello \r\n \t world"
|
||||||
|
|
||||||
|
result1 := strutil.RemoveWhiteSpace(str, true)
|
||||||
|
result2 := strutil.RemoveWhiteSpace(str, false)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// helloworld
|
||||||
|
// hello world
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -370,7 +370,7 @@ func MTime(filepath string) (int64, error) {
|
|||||||
return f.ModTime().Unix(), nil
|
return f.ModTime().Unix(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MTime returns file sha value, param `shaType` should be 1, 256 or 512.
|
// Sha returns file sha value, param `shaType` should be 1, 256 or 512.
|
||||||
func Sha(filepath string, shaType ...int) (string, error) {
|
func Sha(filepath string, shaType ...int) (string, error) {
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -420,6 +420,27 @@ func ReadCsvFile(filepath string) ([][]string, error) {
|
|||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteCsvFile write content to target csv file.
|
||||||
|
func WriteCsvFile(filepath string, records [][]string, append bool) error {
|
||||||
|
flag := os.O_RDWR | os.O_CREATE
|
||||||
|
|
||||||
|
if append {
|
||||||
|
flag = flag | os.O_APPEND
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(filepath, flag, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
writer := csv.NewWriter(f)
|
||||||
|
writer.Comma = ','
|
||||||
|
|
||||||
|
return writer.WriteAll(records)
|
||||||
|
}
|
||||||
|
|
||||||
// WriteStringToFile write string to target file.
|
// WriteStringToFile write string to target file.
|
||||||
func WriteStringToFile(filepath string, content string, append bool) error {
|
func WriteStringToFile(filepath string, content string, append bool) error {
|
||||||
flag := os.O_RDWR | os.O_CREATE
|
flag := os.O_RDWR | os.O_CREATE
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ func TestSha(t *testing.T) {
|
|||||||
func TestReadCsvFile(t *testing.T) {
|
func TestReadCsvFile(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestReadCsvFile")
|
assert := internal.NewAssert(t, "TestReadCsvFile")
|
||||||
|
|
||||||
content, err := ReadCsvFile("./testdata/test.csv")
|
content, err := ReadCsvFile("./testdata/demo.csv")
|
||||||
t.Log(content)
|
t.Log(content)
|
||||||
|
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
@@ -278,6 +278,27 @@ func TestReadCsvFile(t *testing.T) {
|
|||||||
assert.Equal("Bob", content[0][0])
|
assert.Equal("Bob", content[0][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteCsvFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteCsvFile")
|
||||||
|
|
||||||
|
csvFilePath := "./testdata/test1.csv"
|
||||||
|
content := [][]string{
|
||||||
|
{"Lili", "22", "female"},
|
||||||
|
{"Jim", "21", "male"},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := WriteCsvFile(csvFilePath, content, false)
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
readContent, err := ReadCsvFile(csvFilePath)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
assert.Equal(2, len(readContent))
|
||||||
|
assert.Equal(3, len(readContent[0]))
|
||||||
|
assert.Equal("Lili", content[0][0])
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteStringToFile(t *testing.T) {
|
func TestWriteStringToFile(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
||||||
|
|
||||||
|
|||||||
2
fileutil/testdata/test1.csv
vendored
Normal file
2
fileutil/testdata/test1.csv
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Lili,22,female
|
||||||
|
Jim,21,male
|
||||||
|
@@ -189,3 +189,8 @@ func Cos(radian float64, precision ...int) float64 {
|
|||||||
func Sin(radian float64, precision ...int) float64 {
|
func Sin(radian float64, precision ...int) float64 {
|
||||||
return Cos((math.Pi / 2) - radian)
|
return Cos((math.Pi / 2) - radian)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log returns the logarithm of base n.
|
||||||
|
func Log(n, base float64) float64 {
|
||||||
|
return math.Log(n) / math.Log(base)
|
||||||
|
}
|
||||||
|
|||||||
@@ -172,3 +172,11 @@ func TestSin(t *testing.T) {
|
|||||||
assert.EqualValues(0, Sin(math.Pi))
|
assert.EqualValues(0, Sin(math.Pi))
|
||||||
assert.EqualValues(1, Sin(math.Pi/2))
|
assert.EqualValues(1, Sin(math.Pi/2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLog(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLog")
|
||||||
|
|
||||||
|
assert.EqualValues(3, Log(8, 2))
|
||||||
|
assert.EqualValues(3, TruncRound(Log(27, 3), 0))
|
||||||
|
assert.EqualValues(2.32, TruncRound(Log(5, 2), 2))
|
||||||
|
}
|
||||||
|
|||||||
@@ -72,10 +72,3 @@ func TestTelnetConnected(t *testing.T) {
|
|||||||
result2 := IsTelnetConnected("www.baidu.com", "123")
|
result2 := IsTelnetConnected("www.baidu.com", "123")
|
||||||
assert.Equal(false, result2)
|
assert.Equal(false, result2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestDownloadFile(t *testing.T) {
|
|
||||||
// assert := internal.NewAssert(t, "TestDownloadFile")
|
|
||||||
|
|
||||||
// err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
|
||||||
// assert.IsNil(err)
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -101,3 +101,27 @@ func UUIdV4() (string, error) {
|
|||||||
|
|
||||||
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
|
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandUniqueIntSlice generate a slice of random int of length n that do not repeat.
|
||||||
|
func RandUniqueIntSlice(n, min, max int) []int {
|
||||||
|
if min > max {
|
||||||
|
return []int{}
|
||||||
|
}
|
||||||
|
if n > max-min {
|
||||||
|
n = max - min
|
||||||
|
}
|
||||||
|
|
||||||
|
nums := make([]int, n)
|
||||||
|
used := make(map[int]struct{}, n)
|
||||||
|
for i := 0; i < n; {
|
||||||
|
r := RandInt(min, max)
|
||||||
|
if _, use := used[r]; use {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
used[r] = struct{}{}
|
||||||
|
nums[i] = r
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return nums
|
||||||
|
}
|
||||||
|
|||||||
@@ -105,3 +105,36 @@ func TestUUIdV4(t *testing.T) {
|
|||||||
isUUiDV4 := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`)
|
isUUiDV4 := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`)
|
||||||
assert.Equal(true, isUUiDV4.MatchString(uuid))
|
assert.Equal(true, isUUiDV4.MatchString(uuid))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRandUniqueIntSlice(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRandUniqueIntSlice")
|
||||||
|
|
||||||
|
r1 := RandUniqueIntSlice(5, 0, 9)
|
||||||
|
assert.Equal(len(r1), 5)
|
||||||
|
if hasDuplicate(r1) {
|
||||||
|
t.Error("hasDuplicate int")
|
||||||
|
}
|
||||||
|
|
||||||
|
r2 := RandUniqueIntSlice(20, 0, 10)
|
||||||
|
assert.Equal(len(r2), 10)
|
||||||
|
if hasDuplicate(r2) {
|
||||||
|
t.Error("hasDuplicate int")
|
||||||
|
}
|
||||||
|
|
||||||
|
r3 := RandUniqueIntSlice(10, 20, 10)
|
||||||
|
assert.Equal(len(r3), 0)
|
||||||
|
|
||||||
|
r4 := RandUniqueIntSlice(0, 20, 10)
|
||||||
|
assert.Equal(len(r4), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasDuplicate(arr []int) bool {
|
||||||
|
elements := make(map[int]bool)
|
||||||
|
for _, v := range arr {
|
||||||
|
if elements[v] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
elements[v] = true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package strutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -503,3 +504,21 @@ func ContainsAny(str string, substrs []string) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
whitespaceRegexMatcher *regexp.Regexp = regexp.MustCompile(`\s`)
|
||||||
|
mutiWhitespaceRegexMatcher *regexp.Regexp = regexp.MustCompile(`[[:space:]]{2,}|[\s\p{Zs}]{2,}`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// RemoveWhiteSpace remove whitespace characters from a string.
|
||||||
|
// when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space.
|
||||||
|
func RemoveWhiteSpace(str string, repalceAll bool) string {
|
||||||
|
if repalceAll && str != "" {
|
||||||
|
return strings.Join(strings.Fields(str), "")
|
||||||
|
} else if str != "" {
|
||||||
|
str = mutiWhitespaceRegexMatcher.ReplaceAllString(str, " ")
|
||||||
|
str = whitespaceRegexMatcher.ReplaceAllString(str, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(str)
|
||||||
|
}
|
||||||
|
|||||||
@@ -455,3 +455,13 @@ func TestContainsAny(t *testing.T) {
|
|||||||
assert.Equal(true, ContainsAny("hello world", []string{"hello", "abc"}))
|
assert.Equal(true, ContainsAny("hello world", []string{"hello", "abc"}))
|
||||||
assert.Equal(false, ContainsAny("hello world", []string{"123", "abc"}))
|
assert.Equal(false, ContainsAny("hello world", []string{"123", "abc"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveWhiteSpace(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRemoveWhiteSpace")
|
||||||
|
|
||||||
|
str := " hello \r\n \t world"
|
||||||
|
|
||||||
|
assert.Equal("", RemoveWhiteSpace("", true))
|
||||||
|
assert.Equal("helloworld", RemoveWhiteSpace(str, true))
|
||||||
|
assert.Equal("hello world", RemoveWhiteSpace(str, false))
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func TestExecCommand(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestExecCommand")
|
assert := internal.NewAssert(t, "TestExecCommand")
|
||||||
|
|
||||||
// linux or mac
|
// linux or mac
|
||||||
stdout, stderr, err := ExecCommand("ls", WithForeground())
|
stdout, stderr, err := ExecCommand("ls")
|
||||||
t.Log("std out: ", stdout)
|
t.Log("std out: ", stdout)
|
||||||
t.Log("std err: ", stderr)
|
t.Log("std err: ", stderr)
|
||||||
assert.Equal("", stderr)
|
assert.Equal("", stderr)
|
||||||
|
|||||||
Reference in New Issue
Block a user