1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-15 18:22:27 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
dudaodong
01a3b139c0 doc: add document for new functions in slice and strutil package 2023-04-18 15:49:09 +08:00
dudaodong
fcb3b97b45 doc: add docment and playground demo for v2.1.19 2023-04-18 15:21:03 +08:00
15 changed files with 622 additions and 54 deletions

View File

@@ -543,6 +543,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[play](https://go.dev/play/p/g0w34kS7B8m)]
- **<big>CurrentPath</big>** : return current absolute path.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#CurrentPath)]
[[play](https://go.dev/play/p/s74a9iBGcSw)]
### 9. Formatter contains some functions for data formatting.
@@ -557,16 +558,22 @@ import "github.com/duke-git/lancet/v2/formatter"
[[play](https://go.dev/play/p/eRD5k2vzUVX)]
- **<big>Pretty</big>** : pretty print data to JSON string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Pretty)]
[[play](https://go.dev/play/p/YsciGj3FH2x)]
- **<big>PrettyToWriter</big>** : pretty encode data to writer.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#PrettyToWriter)]
[[play](https://go.dev/play/p/LPLZ3lDi5ma)]
- **<big>DecimalBytes</big>** : returns a human readable byte size under decimal standard (base 1000).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#DecimalBytes)]
[[play](https://go.dev/play/p/FPXs1suwRcs)]
- **<big>BinaryBytes</big>** : returns a human-readable byte size under binary standard (base 1024).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#BinaryBytes)]
[[play](https://go.dev/play/p/G9oHHMCAZxP)]
- **<big>ParseDecimalBytes</big>** : return the human readable bytes size string into the amount it represents(base 1000).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#ParseDecimalBytes)]
[[play](https://go.dev/play/p/Am98ybWjvjj)]
- **<big>ParseBinaryBytes</big>** : return the human readable bytes size string into the amount it represents(base 1024).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#ParseBinaryBytes)]
[[play](https://go.dev/play/p/69v1tTT62x8)]
### 10. Function package can control the flow of function execution and support part of functional programming
@@ -614,6 +621,7 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<big>MapTo</big>** : quick map any value to struct or any base type.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#MapTo)]
[[play](https://go.dev/play/p/4K7KBEPgS5M)]
- **<big>ForEach</big>** : executes iteratee funcation for every key and value pair in map.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)]
[[play](https://go.dev/play/p/OaThj6iNVXK)]
@@ -988,11 +996,13 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/8uI8f1lwNrQ)]
- **<big>Reduce<sup>deprecated</sup></big>** : creates an slice of values by running each element of slice thru iteratee function.(Deprecated: use ReduceBy)
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce)]
[[play](https://go.dev/play/p/_RfXJJWIsIm)]
- **<big>ReduceBy</big>** : produces a value from slice by accumulating the result of each element as passed through the reducer function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReduceBy)]
[[play](https://go.dev/play/p/YKDpLi7gtee)]
- **<big>ReduceRight</big>** : ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReduceRight)]
[[play](https://go.dev/play/p/_RfXJJWIsIm)]
[[play](https://go.dev/play/p/qT9dZC03A1K)]
- **<big>Replace</big>** : returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Replace)]
[[play](https://go.dev/play/p/P5mZp7IhOFo)]
@@ -1351,11 +1361,13 @@ import "github.com/duke-git/lancet/v2/validator"
[[play](https://go.dev/play/p/dpzgUjFnBCX)]
- **<big>IsFloat</big>** : check if the value is float(float32, float34) or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsFloat)]
[[play](https://go.dev/play/p/vsyG-sxr99_Z)]
- **<big>IsFloatStr</big>** : check if the string can convert to a float.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsFloatStr)]
[[play](https://go.dev/play/p/LOYwS_Oyl7U)]
- **<big>IsNumber</big>** : check if the value is number(integer, float) or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsNumber)]
[[play](https://go.dev/play/p/mdJHOAvtsvF)]
- **<big>IsNumberStr</big>** : check if the string can convert to a number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsNumberStr)]
[[play](https://go.dev/play/p/LzaKocSV79u)]
@@ -1367,6 +1379,7 @@ import "github.com/duke-git/lancet/v2/validator"
[[play](https://go.dev/play/p/z_XeZo_litG)]
- **<big>IsInt</big>** : check if the string can convert to a number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsInt)]
[[play](https://go.dev/play/p/eFoIHbgzl-z)]
- **<big>IsIntStr</big>** : check if the string can convert to a integer.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIntStr)]
[[play](https://go.dev/play/p/jQRtFv-a0Rk)]

View File

@@ -542,6 +542,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[play](https://go.dev/play/p/g0w34kS7B8m)]
- **<big>CurrentPath</big>** : 返回当前位置的绝对路径。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CurrentPath)]
[[play](https://go.dev/play/p/s74a9iBGcSw)]
### 9. formatter 格式化器包含一些数据格式化处理方法。
@@ -556,16 +557,22 @@ import "github.com/duke-git/lancet/v2/formatter"
[[play](https://go.dev/play/p/eRD5k2vzUVX)]
- **<big>Pretty</big>** : 返回 pretty JSON 字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Pretty)]
[[play](https://go.dev/play/p/YsciGj3FH2x)]
- **<big>PrettyToWriter</big>** : Pretty encode 数据到 writer。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#PrettyToWriter)]
[[play](https://go.dev/play/p/LPLZ3lDi5ma)]
- **<big>DecimalBytes</big>** : 返回十进制标准(以 1000 为基数下的可读字节单位字符串。precision 参数指定小数点后的位数,默认为 4。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#DecimalBytes)]
[[play](https://go.dev/play/p/FPXs1suwRcs)]
- **<big>BinaryBytes</big>** : 返回 binary 标准(以 1024 为基数下的可读字节单位字符串。precision 参数指定小数点后的位数,默认为 4。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#BinaryBytes)]
[[play](https://go.dev/play/p/G9oHHMCAZxP)]
- **<big>ParseDecimalBytes</big>** : 将字节单位字符串转换成其所表示的字节数(以 1000 为基数)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#ParseDecimalBytes)]
[[play](https://go.dev/play/p/Am98ybWjvjj)]
- **<big>ParseBinaryBytes</big>** : 将字节单位字符串转换成其所表示的字节数(以 1024 为基数)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#ParseBinaryBytes)]
[[play](https://go.dev/play/p/69v1tTT62x8)]
### 10. function 函数包控制函数执行流程,包含部分函数式编程。
@@ -613,6 +620,7 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<big>MapTo</big>** : 快速将 map 或者其他类型映射到结构体或者指定类型。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#MapTo)]
[[play](https://go.dev/play/p/4K7KBEPgS5M)]
- **<big>ForEach</big>** : 对 map 中的每对 key 和 value 执行 iteratee 函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ForEach)]
[[play](https://go.dev/play/p/OaThj6iNVXK)]
@@ -990,8 +998,10 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/_RfXJJWIsIm)]
- **<big>ReduceBy</big>** : 对切片元素执行 reduce 操作。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReduceBy)]
[[play](https://go.dev/play/p/YKDpLi7gtee)]
- **<big>ReduceRight</big>** : 类似 ReduceBy 操作,迭代切片元素顺序从右至左。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReduceRight)]
[[play](https://go.dev/play/p/qT9dZC03A1K)]
- **<big>Replace</big>** : 返回切片的副本,其中前 n 个不重叠的 old 替换为 new。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Replace)]
[[play](https://go.dev/play/p/P5mZp7IhOFo)]
@@ -1353,11 +1363,13 @@ import "github.com/duke-git/lancet/v2/validator"
[[play](https://go.dev/play/p/dpzgUjFnBCX)]
- **<big>IsFloat</big>** : 验证参数是否是浮点数((float32float34)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsFloat)]
[[play](https://go.dev/play/p/vsyG-sxr99_Z)]
- **<big>IsFloatStr</big>** : 验证字符串是否是可以转换为浮点数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsFloatStr)]
[[play](https://go.dev/play/p/LOYwS_Oyl7U)]
- **<big>IsNumber</big>** : 验证参数是否是数字(integerfloat)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsNumber)]
[[play](https://go.dev/play/p/mdJHOAvtsvF)]
- **<big>IsNumberStr</big>** : 验证字符串是否是可以转换为数字。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsNumberStr)]
[[play](https://go.dev/play/p/LzaKocSV79u)]
@@ -1369,6 +1381,7 @@ import "github.com/duke-git/lancet/v2/validator"
[[play](https://go.dev/play/p/z_XeZo_litG)]
- **<big>IsInt</big>** : 验证参数是否是整数(int, unit)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsInt)]
[[play](https://go.dev/play/p/eFoIHbgzl-z)]
- **<big>IsIntStr</big>** : 验证字符串是否是可以转换为整数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIntStr)]
[[play](https://go.dev/play/p/jQRtFv-a0Rk)]

View File

@@ -43,8 +43,10 @@ import (
- [EqualWith](#EqualWith)
- [Every](#Every)
- [Filter](#Filter)
- [Find](#Find)
- [FindLast](#FindLast)
- [Find<sup>deprecated</sup>](#Find)
- [FindBy](#FindBy)
- [FindLast<sup>deprecated</sup>](#FindLast)
- [FindLastBy](#FindLastBy)
- [Flatten](#Flatten)
- [FlattenDeep](#FlattenDeep)
- [ForEach](#ForEach)
@@ -837,7 +839,7 @@ func main() {
}
```
### <span id="Find">Find</span>
### <span id="Find">Find(deprecated: use FindBy)</span>
<p>Iterates over elements of slice, returning the first one that passes a truth test on function.</p>
@@ -873,7 +875,43 @@ func main() {
}
```
### <span id="FindLast">FindLast</span>
### <span id="FindBy">FindBy</span>
<p>Iterates over elements of slice, returning the first one that passes a truth test on predicate function.If return T is nil or zero value then no items matched the predicate func. In contrast to Find or FindLast, its return value no longer requires dereferencing</p>
<b>Signature:</b>
```go
func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool)
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result, ok := slice.FindBy(nums, isEven)
fmt.Println(result)
fmt.Println(ok)
// Output:
// 2
// true
}
```
### <span id="FindLast">FindLast(deprecated: use FindLastBy)</span>
<p>iterates over elements of slice from end to begin, returning the last one that passes a truth test on function.</p>
@@ -909,6 +947,42 @@ func main() {
}
```
### <span id="FindLastBy">FindLastBy</span>
<p>FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function. If return T is nil or zero value then no items matched the predicate func. In contrast to Find or FindLast, its return value no longer requires dereferencing</p>
<b>Signature:</b>
```go
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool)
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result, ok := slice.FindLastBy(nums, isEven)
fmt.Println(result)
fmt.Println(ok)
// Output:
// 4
// true
}
```
### <span id="Flatten">Flatten</span>
<p>Flatten slice with one level.</p>

View File

@@ -43,8 +43,10 @@ import (
- [Equal](#Equal)
- [EqualWith](#EqualWith)
- [Filter](#Filter)
- [Find](#Find)
- [FindLast](#FindLast)
- [Find<sup>deprecated</sup>](#Find)
- [FindBy](#FindBy)
- [FindLast<sup>deprecated</sup>](#FindLast)
- [FindLastBy](#FindLastBy)
- [Flatten](#Flatten)
- [FlattenDeep](#FlattenDeep)
- [ForEach](#ForEach)
@@ -838,9 +840,9 @@ func main() {
}
```
### <span id="Find">Find</span>
### <span id="Find">Find (废弃:使用 FindBy)</span>
<p>遍历切片的元素返回第一个通过predicate函数真值测试的元素</p>
<p>遍历slice的元素返回第一个通过predicate函数真值测试的元素</p>
<b>函数签名:</b>
@@ -874,9 +876,45 @@ func main() {
}
```
### <span id="FindLast">FindLast</span>
### <span id="FindBy">FindBy</span>
<p>从头到尾遍历slice的元素返回最后一个通过predicate函数真值测试的元素</p>
<p>遍历slice的元素返回一个通过predicate函数真值测试的元素</p>
<b>函数签名:</b>
```go
func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool)
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result, ok := slice.FindBy(nums, isEven)
fmt.Println(result)
fmt.Println(ok)
// Output:
// 2
// true
}
```
### <span id="FindLast">FindLast(废弃:使用 FindLastBy)</span>
<p>遍历slice的元素返回最后一个通过predicate函数真值测试的元素。</p>
<b>函数签名:</b>
@@ -910,6 +948,42 @@ func main() {
}
```
### <span id="FindLastBy">FindLastBy</span>
<p>从遍历slice的元素返回最后一个通过predicate函数真值测试的元素。</p>
<b>函数签名:</b>
```go
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool)
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result, ok := slice.FindLastBy(nums, isEven)
fmt.Println(result)
fmt.Println(ok)
// Output:
// 4
// true
}
```
### <span id="Flatten">Flatten</span>
<p>将切片压平一层</p>

View File

@@ -46,6 +46,12 @@ import (
- [SplitWords](#SplitWords)
- [WordCount](#WordCount)
- [RemoveNonPrintable](#RemoveNonPrintable)
- [StringToBytes](#StringToBytes)
- [BytesToString](#BytesToString)
- [IsBlank](#IsBlank)
- [HasPrefixAny](#HasPrefixAny)
- [HasSuffixAny](#HasSuffixAny)
- [IndexOffset](#IndexOffset)
<div STYLE="page-break-after: always;"></div>
@@ -852,7 +858,6 @@ func main() {
}
```
### <span id="SplitWords">SplitWords</span>
<p>Splits a string into words, word only contains alphabetic characters.</p>
@@ -896,7 +901,6 @@ func main() {
}
```
### <span id="WordCount">WordCount</span>
<p>Return the number of meaningful word, word only contains alphabetic characters.</p>
@@ -940,7 +944,6 @@ func main() {
}
```
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
<p>Remove non-printable characters from a string.</p>
@@ -969,4 +972,196 @@ func main() {
// hello world
// 你好😄
}
```
```
### <span id="StringToBytes">StringToBytes</span>
<p>Converts a string to byte slice without a memory allocation.</p>
<b>Signature:</b>
```go
func StringToBytes(str string) (b []byte)
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.StringToBytes("abc")
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// [97 98 99]
// true
}
```
### <span id="BytesToString">BytesToString</span>
<p>Converts a byte slice to string without a memory allocation.</p>
<b>Signature:</b>
```go
func BytesToString(bytes []byte) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
bytes := []byte{'a', 'b', 'c'}
result := strutil.BytesToString(bytes)
fmt.Println(result)
// Output:
// abc
}
```
### <span id="IsBlank">IsBlank</span>
<p>Checks if a string is whitespace or empty.</p>
<b>Signature:</b>
```go
func IsBlank(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.IsBlank("")
result2 := strutil.IsBlank("\t\v\f\n")
result3 := strutil.IsBlank(" 中文")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
```
### <span id="HasPrefixAny">HasPrefixAny</span>
<p>Checks if a string starts with any of an array of specified strings.</p>
<b>Signature:</b>
```go
func HasPrefixAny(str string, prefixes []string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```
### <span id="HasSuffixAny">HasSuffixAny</span>
<p>Checks if a string ends with any of an array of specified strings.</p>
<b>Signature:</b>
```go
func HasSuffixAny(str string, suffixes []string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```
### <span id="IndexOffset">IndexOffset</span>
<p>Returns the index of the first instance of substr in string after offsetting the string by `idxFrom`, or -1 if substr is not present in string.</p>
<b>Signature:</b>
```go
func IndexOffset(str string, substr string, idxFrom int) int
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
str := "foo bar hello world"
result1 := strutil.IndexOffset(str, "o", 5)
result2 := strutil.IndexOffset(str, "o", 0)
result3 := strutil.IndexOffset(str, "d", len(str)-1)
result4 := strutil.IndexOffset(str, "d", len(str))
result5 := strutil.IndexOffset(str, "f", -1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// 12
// 1
// 18
// -1
// -1
}
```

View File

@@ -46,6 +46,12 @@ import (
- [SplitWords](#SplitWords)
- [WordCount](#WordCount)
- [RemoveNonPrintable](#RemoveNonPrintable)
- [StringToBytes](#StringToBytes)
- [BytesToString](#BytesToString)
- [IsBlank](#IsBlank)
- [HasPrefixAny](#HasPrefixAny)
- [HasSuffixAny](#HasSuffixAny)
- [IndexOffset](#IndexOffset)
<div STYLE="page-break-after: always;"></div>
@@ -895,7 +901,6 @@ func main() {
}
```
### <span id="WordCount">WordCount</span>
<p>返回有意义单词的数量,只支持字母字符单词。</p>
@@ -939,7 +944,6 @@ func main() {
}
```
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
<p>删除字符串中不可打印的字符。</p>
@@ -968,4 +972,196 @@ func main() {
// hello world
// 你好😄
}
```
```
### <span id="StringToBytes">StringToBytes</span>
<p>在不分配内存的情况下将字符串转换为字节片。</p>
<b>函数签名:</b>
```go
func StringToBytes(str string) (b []byte)
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.StringToBytes("abc")
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// [97 98 99]
// true
}
```
### <span id="BytesToString">BytesToString</span>
<p>在不分配内存的情况下将字节切片转换为字符串。</p>
<b>函数签名:</b>
```go
func BytesToString(bytes []byte) string
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
bytes := []byte{'a', 'b', 'c'}
result := strutil.BytesToString(bytes)
fmt.Println(result)
// Output:
// abc
}
```
### <span id="IsBlank">IsBlank</span>
<p>检查字符串是否为空格或空。</p>
<b>函数签名:</b>
```go
func IsBlank(str string) bool
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.IsBlank("")
result2 := strutil.IsBlank("\t\v\f\n")
result3 := strutil.IsBlank(" 中文")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
```
### <span id="HasPrefixAny">HasPrefixAny</span>
<p>检查字符串是否以指定字符串数组中的任何一个开头。</p>
<b>函数签名:</b>
```go
func HasPrefixAny(str string, prefixes []string) bool
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```
### <span id="HasSuffixAny">HasSuffixAny</span>
<p>检查字符串是否以指定字符串数组中的任何一个结尾。</p>
<b>函数签名:</b>
```go
func HasSuffixAny(str string, suffixes []string) bool
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```
### <span id="IndexOffset">IndexOffset</span>
<p>将字符串偏移idxFrom后返回字符串中第一个 substr 实例的索引,如果字符串中不存在 substr则返回 -1。</p>
<b>函数签名:</b>
```go
func IndexOffset(str string, substr string, idxFrom int) int
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
str := "foo bar hello world"
result1 := strutil.IndexOffset(str, "o", 5)
result2 := strutil.IndexOffset(str, "o", 0)
result3 := strutil.IndexOffset(str, "d", len(str)-1)
result4 := strutil.IndexOffset(str, "d", len(str))
result5 := strutil.IndexOffset(str, "f", -1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// 12
// 1
// 18
// -1
// -1
}
```

View File

@@ -239,14 +239,17 @@ func main() {
}
```
### <span id="ExecCommand">CompareOsEnv</span>
### <span id="ExecCommand">ExecCommand</span>
<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>
```go
func ExecCommand(command string) (stdout, stderr string, err error)
type (
Option func(*exec.Cmd)
)
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
```
<b>Example:</b>

View File

@@ -246,7 +246,10 @@ func main() {
<b>Signature:</b>
```go
func ExecCommand(command string) (stdout, stderr string, err error)
type (
Option func(*exec.Cmd)
)
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
```
<b>Example:</b>

View File

@@ -348,7 +348,7 @@ func MiMeType(file any) string {
}
// CurrentPath return current absolute path.
// Play: todo
// Play: https://go.dev/play/p/s74a9iBGcSw
func CurrentPath() string {
var absPath string
_, filename, _, ok := runtime.Caller(1)

View File

@@ -82,7 +82,7 @@ var (
// DecimalBytes returns a human readable byte size under decimal standard (base 1000)
// The precision parameter specifies the number of digits after the decimal point, which defaults to 4.
// Play: todo
// Play: https://go.dev/play/p/FPXs1suwRcs
func DecimalBytes(size float64, precision ...int) string {
p := 5
if len(precision) > 0 {
@@ -95,7 +95,7 @@ func DecimalBytes(size float64, precision ...int) string {
// BinaryBytes returns a human-readable byte size under binary standard (base 1024)
// The precision parameter specifies the number of digits after the decimal point, which defaults to 4.
// Play: todo
// Play: https://go.dev/play/p/G9oHHMCAZxP
func BinaryBytes(size float64, precision ...int) string {
p := 5
if len(precision) > 0 {
@@ -118,14 +118,14 @@ func calculateByteSize(size float64, base float64, byteUnits []string) (float64,
// ParseDecimalBytes return the human readable bytes size string into the amount it represents(base 1000).
// ParseDecimalBytes("42 MB") -> 42000000, nil
// Play: todo
// Play: https://go.dev/play/p/Am98ybWjvjj
func ParseDecimalBytes(size string) (uint64, error) {
return parseBytes(size, "decimal")
}
// ParseBinaryBytes return the human readable bytes size string into the amount it represents(base 1024).
// ParseBinaryBytes("42 mib") -> 44040192, nil
// Play: todo
// Play: https://go.dev/play/p/69v1tTT62x8
func ParseBinaryBytes(size string) (uint64, error) {
return parseBytes(size, "binary")
}

View File

@@ -48,14 +48,14 @@ func Comma[T constraints.Float | constraints.Integer | string](value T, symbol s
}
// Pretty data to JSON string.
// Play: todo
// Play: https://go.dev/play/p/YsciGj3FH2x
func Pretty(v any) (string, error) {
out, err := json.MarshalIndent(v, "", " ")
return string(out), err
}
// PrettyToWriter pretty encode data to writer.
// Play: todo
// Play: https://go.dev/play/p/LPLZ3lDi5ma
func PrettyToWriter(v any, out io.Writer) error {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")

View File

@@ -60,6 +60,7 @@ var _ = func() struct{} {
log.Println(dist)
*/
// Play: https://go.dev/play/p/4K7KBEPgS5M
func MapTo(src any, dst any) error {
dstRef := reflect.ValueOf(dst)

View File

@@ -331,25 +331,14 @@ func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
// Play: https://go.dev/play/p/FFDPV_j7URd
// Deprecated
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
index := -1
for i := len(slice) - 1; i >= 0; i-- {
if predicate(i, slice[i]) {
index = i
break
}
}
if index == -1 {
return nil, false
}
return &slice[index], true
v, ok := FindLastBy(slice, predicate)
return &v, ok
}
// FindBy iterates over elements of slice, returning the first one that passes a truth test on predicate function.
// If return T is nil or zero value then no items matched the predicate func.
// In contrast to Find or FindLast, its return value no longer requires dereferencing
// Play: todo
func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
index := -1
@@ -370,6 +359,7 @@ func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok b
// FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function.
// If return T is nil or zero value then no items matched the predicate func.
// In contrast to Find or FindLast, its return value no longer requires dereferencing
// Play: todo
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
index := -1
@@ -523,7 +513,7 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
}
// ReduceBy produces a value from slice by accumulating the result of each element as passed through the reducer function.
// Play: todo
// Play: https://go.dev/play/p/YKDpLi7gtee
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
accumulator := initial
@@ -535,7 +525,7 @@ func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T
}
// ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.
// Play: todo
// Play: https://go.dev/play/p/qT9dZC03A1K
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
accumulator := initial

View File

@@ -376,7 +376,8 @@ func RemoveNonPrintable(str string) string {
return result
}
// StringToBytes converts a string to byte slice without a memory allocation
// StringToBytes converts a string to byte slice without a memory allocation.
// Play: todo
func StringToBytes(str string) (b []byte) {
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
@@ -384,12 +385,14 @@ func StringToBytes(str string) (b []byte) {
return b
}
// BytesToString converts a byte slice to string without a memory allocation
// BytesToString converts a byte slice to string without a memory allocation.
// Play: todo
func BytesToString(bytes []byte) string {
return *(*string)(unsafe.Pointer(&bytes))
}
// IsBlank checks if a string is whitespace, empty
// IsBlank checks if a string is whitespace, empty.
// Play: todo
func IsBlank(str string) bool {
if len(str) == 0 {
return true
@@ -404,7 +407,8 @@ func IsBlank(str string) bool {
return true
}
// HasPrefixAny check if a string starts with any of an array of specified strings
// HasPrefixAny check if a string starts with any of an array of specified strings.
// Play: todo
func HasPrefixAny(str string, prefixes []string) bool {
if len(str) == 0 || len(prefixes) == 0 {
return false
@@ -417,7 +421,8 @@ func HasPrefixAny(str string, prefixes []string) bool {
return false
}
// HasSuffixAny check if a string ends with any of an array of specified strings
// HasSuffixAny check if a string ends with any of an array of specified strings.
// Play: todo
func HasSuffixAny(str string, suffixes []string) bool {
if len(str) == 0 || len(suffixes) == 0 {
return false
@@ -430,8 +435,9 @@ func HasSuffixAny(str string, suffixes []string) bool {
return false
}
// IndexOffset returns the index of the first instance of substr in s after offsetting the string by `idxFrom`,
// or -1 if substr is not present in s.
// IndexOffset returns the index of the first instance of substr in string after offsetting the string by `idxFrom`,
// or -1 if substr is not present in string.
// Play: todo
func IndexOffset(str string, substr string, idxFrom int) int {
if idxFrom > len(str)-1 || idxFrom < 0 {
return -1

View File

@@ -371,13 +371,13 @@ func IsGBK(data []byte) bool {
}
// IsNumberStr check if the value is number(integer, float) or not.
// Play: todo
// Play: https://go.dev/play/p/mdJHOAvtsvF
func IsNumber(v any) bool {
return IsInt(v) || IsFloat(v)
}
// IsFloat check if the value is float(float32, float34) or not.
// Play: todo
// Play: https://go.dev/play/p/vsyG-sxr99_Z
func IsFloat(v any) bool {
switch v.(type) {
case float32, float64:
@@ -387,7 +387,7 @@ func IsFloat(v any) bool {
}
// IsInt check if the value is integer(int, unit) or not.
// Play: todo
// Play: https://go.dev/play/p/eFoIHbgzl-z
func IsInt(v any) bool {
switch v.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr: