mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
6 Commits
2894bec80c
...
8944109c4c
| Author | SHA1 | Date | |
|---|---|---|---|
| 8944109c4c | |||
| 10e3732f32 | |||
| a415597c6b | |||
| 69b32fd043 | |||
| 75ed359084 | |||
| 2c71b6375c |
@@ -247,3 +247,9 @@ func DayOfYear(t time.Time) int {
|
|||||||
|
|
||||||
return int(nowDate.Sub(firstDay).Hours() / 24)
|
return int(nowDate.Sub(firstDay).Hours() / 24)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsWeekend checks if passed time is weekend or not.
|
||||||
|
// Play: todo
|
||||||
|
func IsWeekend(t time.Time) bool {
|
||||||
|
return time.Saturday == t.Weekday() || time.Sunday == t.Weekday()
|
||||||
|
}
|
||||||
|
|||||||
@@ -389,3 +389,22 @@ func ExampleDayOfYear() {
|
|||||||
// 1
|
// 1
|
||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleIsWeekend() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := IsWeekend(date1)
|
||||||
|
result2 := IsWeekend(date2)
|
||||||
|
result3 := IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|||||||
@@ -286,3 +286,19 @@ func TestDayOfYear(t *testing.T) {
|
|||||||
result3 := DayOfYear(date3)
|
result3 := DayOfYear(date3)
|
||||||
assert.Equal(0, result3)
|
assert.Equal(0, result3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsWeekend(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsWeekend")
|
||||||
|
date := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
result := IsWeekend(date)
|
||||||
|
assert.Equal(true, result)
|
||||||
|
|
||||||
|
date1 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
result1 := IsWeekend(date1)
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
result2 := IsWeekend(date2)
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ import (
|
|||||||
- [IsLeapYear](#IsLeapYear)
|
- [IsLeapYear](#IsLeapYear)
|
||||||
- [BetweenSeconds](#BetweenSeconds)
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
- [DayOfYear](#DayOfYear)
|
- [DayOfYear](#DayOfYear)
|
||||||
|
- [IsWeekend](#IsWeekend)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1218,3 +1219,44 @@ func main() {
|
|||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend</span>
|
||||||
|
|
||||||
|
<p>Checks if passed time is weekend or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsWeekend(t time.Time) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := datetime.IsWeekend(date1)
|
||||||
|
result2 := datetime.IsWeekend(date2)
|
||||||
|
result3 := datetime.IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ import (
|
|||||||
- [IsLeapYear](#IsLeapYear)
|
- [IsLeapYear](#IsLeapYear)
|
||||||
- [BetweenSeconds](#BetweenSeconds)
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
- [DayOfYear](#DayOfYear)
|
- [DayOfYear](#DayOfYear)
|
||||||
|
- [IsWeekend](#IsWeekend)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -1216,3 +1217,43 @@ func main() {
|
|||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend</span>
|
||||||
|
|
||||||
|
<p>判断日期是否是周末。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsWeekend(t time.Time) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := datetime.IsWeekend(date1)
|
||||||
|
result2 := datetime.IsWeekend(date2)
|
||||||
|
result3 := datetime.IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -660,3 +662,101 @@ func main() {
|
|||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
|
<p>Writes bytes to target file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
|
<p>Writes string to target file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -660,3 +662,103 @@ func main() {
|
|||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
|
<p>将bytes写入文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
|
<p>将字符串写入文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
+157
-85
@@ -56,6 +56,8 @@ import (
|
|||||||
- [Trim](#Trim)
|
- [Trim](#Trim)
|
||||||
- [SplitAndTrim](#SplitAndTrim)
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
- [HideString](#HideString)
|
- [HideString](#HideString)
|
||||||
|
- [ContainsAll](#ContainsAll)
|
||||||
|
- [ContainsAny](#ContainsAny)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -999,14 +1001,14 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1030,12 +1032,12 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1059,17 +1061,17 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1093,14 +1095,14 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1124,14 +1126,14 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1156,24 +1158,24 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1197,16 +1199,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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1231,19 +1233,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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1268,15 +1270,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]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1301,20 +1303,90 @@ 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****
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ContainsAll">ContainsAll</span>
|
||||||
|
|
||||||
|
<p>Return true if target string contains all the substrings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAll(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAny">ContainsAny</span>
|
||||||
|
|
||||||
|
<p>Return true if target string contains any one of the substrings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAny(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := strutil.ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
+154
-85
@@ -999,13 +999,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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1029,12 +1029,12 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1058,17 +1058,17 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1092,14 +1092,14 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1123,14 +1123,14 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1155,24 +1155,24 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1197,16 +1197,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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1231,19 +1231,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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1269,15 +1269,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]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1303,20 +1303,89 @@ 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****
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAll">ContainsAll</span>
|
||||||
|
|
||||||
|
<p>判断字符串是否包括全部给定的子字符串切片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAll(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAny">ContainsAny</span>
|
||||||
|
|
||||||
|
<p>判断字符串是否包括给定的子字符串切片中任意一个子字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAny(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := strutil.ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -453,3 +453,35 @@ func ReadCsvFile(filepath string) ([][]string, error) {
|
|||||||
|
|
||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteStringToFile write string to target file.
|
||||||
|
// Play: todo
|
||||||
|
func WriteStringToFile(filepath string, content 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()
|
||||||
|
|
||||||
|
_, err = f.WriteString(content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteBytesToFile write bytes to target file.
|
||||||
|
// Play: todo
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error {
|
||||||
|
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.Write(content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -285,3 +285,59 @@ func ExampleReadCsvFile() {
|
|||||||
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
|
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
|
||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleWriteStringToFile() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWriteBytesToFile() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
|||||||
@@ -296,3 +296,68 @@ func TestReadCsvFile(t *testing.T) {
|
|||||||
assert.Equal(3, len(content[0]))
|
assert.Equal(3, len(content[0]))
|
||||||
assert.Equal("Bob", content[0][0])
|
assert.Equal("Bob", content[0][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteStringToFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
||||||
|
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, "hello", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content1, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, " world", true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content2, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("hello", content1)
|
||||||
|
assert.Equal("hello world", string(content2))
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteBytesToFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteBytesToFile")
|
||||||
|
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("hello", string(content))
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user