mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 10:12:29 +08:00
doc: add document for new functions in slice and strutil package
This commit is contained in:
@@ -43,8 +43,10 @@ import (
|
|||||||
- [EqualWith](#EqualWith)
|
- [EqualWith](#EqualWith)
|
||||||
- [Every](#Every)
|
- [Every](#Every)
|
||||||
- [Filter](#Filter)
|
- [Filter](#Filter)
|
||||||
- [Find](#Find)
|
- [Find<sup>deprecated</sup>](#Find)
|
||||||
- [FindLast](#FindLast)
|
- [FindBy](#FindBy)
|
||||||
|
- [FindLast<sup>deprecated</sup>](#FindLast)
|
||||||
|
- [FindLastBy](#FindLastBy)
|
||||||
- [Flatten](#Flatten)
|
- [Flatten](#Flatten)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [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>
|
<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>
|
<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>
|
### <span id="Flatten">Flatten</span>
|
||||||
|
|
||||||
<p>Flatten slice with one level.</p>
|
<p>Flatten slice with one level.</p>
|
||||||
|
|||||||
@@ -43,8 +43,10 @@ import (
|
|||||||
- [Equal](#Equal)
|
- [Equal](#Equal)
|
||||||
- [EqualWith](#EqualWith)
|
- [EqualWith](#EqualWith)
|
||||||
- [Filter](#Filter)
|
- [Filter](#Filter)
|
||||||
- [Find](#Find)
|
- [Find<sup>deprecated</sup>](#Find)
|
||||||
- [FindLast](#FindLast)
|
- [FindBy](#FindBy)
|
||||||
|
- [FindLast<sup>deprecated</sup>](#FindLast)
|
||||||
|
- [FindLastBy](#FindLastBy)
|
||||||
- [Flatten](#Flatten)
|
- [Flatten](#Flatten)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [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>
|
<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>
|
<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>
|
### <span id="Flatten">Flatten</span>
|
||||||
|
|
||||||
<p>将切片压平一层</p>
|
<p>将切片压平一层</p>
|
||||||
|
|||||||
203
docs/strutil.md
203
docs/strutil.md
@@ -46,6 +46,12 @@ import (
|
|||||||
- [SplitWords](#SplitWords)
|
- [SplitWords](#SplitWords)
|
||||||
- [WordCount](#WordCount)
|
- [WordCount](#WordCount)
|
||||||
- [RemoveNonPrintable](#RemoveNonPrintable)
|
- [RemoveNonPrintable](#RemoveNonPrintable)
|
||||||
|
- [StringToBytes](#StringToBytes)
|
||||||
|
- [BytesToString](#BytesToString)
|
||||||
|
- [IsBlank](#IsBlank)
|
||||||
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
|
- [IndexOffset](#IndexOffset)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -852,7 +858,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitWords">SplitWords</span>
|
### <span id="SplitWords">SplitWords</span>
|
||||||
|
|
||||||
<p>Splits a string into words, word only contains alphabetic characters.</p>
|
<p>Splits a string into words, word only contains alphabetic characters.</p>
|
||||||
@@ -896,7 +901,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="WordCount">WordCount</span>
|
### <span id="WordCount">WordCount</span>
|
||||||
|
|
||||||
<p>Return the number of meaningful word, word only contains alphabetic characters.</p>
|
<p>Return the number of meaningful word, word only contains alphabetic characters.</p>
|
||||||
@@ -940,7 +944,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
||||||
|
|
||||||
<p>Remove non-printable characters from a string.</p>
|
<p>Remove non-printable characters from a string.</p>
|
||||||
@@ -969,4 +972,196 @@ func main() {
|
|||||||
// hello world
|
// 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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -46,6 +46,12 @@ import (
|
|||||||
- [SplitWords](#SplitWords)
|
- [SplitWords](#SplitWords)
|
||||||
- [WordCount](#WordCount)
|
- [WordCount](#WordCount)
|
||||||
- [RemoveNonPrintable](#RemoveNonPrintable)
|
- [RemoveNonPrintable](#RemoveNonPrintable)
|
||||||
|
- [StringToBytes](#StringToBytes)
|
||||||
|
- [BytesToString](#BytesToString)
|
||||||
|
- [IsBlank](#IsBlank)
|
||||||
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
|
- [IndexOffset](#IndexOffset)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -895,7 +901,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="WordCount">WordCount</span>
|
### <span id="WordCount">WordCount</span>
|
||||||
|
|
||||||
<p>返回有意义单词的数量,只支持字母字符单词。</p>
|
<p>返回有意义单词的数量,只支持字母字符单词。</p>
|
||||||
@@ -939,7 +944,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
||||||
|
|
||||||
<p>删除字符串中不可打印的字符。</p>
|
<p>删除字符串中不可打印的字符。</p>
|
||||||
@@ -968,4 +972,196 @@ func main() {
|
|||||||
// hello world
|
// 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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -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
|
// Play: https://go.dev/play/p/FFDPV_j7URd
|
||||||
// Deprecated
|
// Deprecated
|
||||||
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
||||||
index := -1
|
v, ok := FindLastBy(slice, predicate)
|
||||||
|
return &v, ok
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindBy iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
// 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.
|
// 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
|
// 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) {
|
func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
||||||
index := -1
|
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.
|
// 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.
|
// 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
|
// 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) {
|
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
||||||
index := -1
|
index := -1
|
||||||
|
|
||||||
|
|||||||
@@ -376,7 +376,8 @@ func RemoveNonPrintable(str string) string {
|
|||||||
return result
|
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) {
|
func StringToBytes(str string) (b []byte) {
|
||||||
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str))
|
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||||
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||||
@@ -384,12 +385,14 @@ func StringToBytes(str string) (b []byte) {
|
|||||||
return b
|
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 {
|
func BytesToString(bytes []byte) string {
|
||||||
return *(*string)(unsafe.Pointer(&bytes))
|
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 {
|
func IsBlank(str string) bool {
|
||||||
if len(str) == 0 {
|
if len(str) == 0 {
|
||||||
return true
|
return true
|
||||||
@@ -404,7 +407,8 @@ func IsBlank(str string) bool {
|
|||||||
return true
|
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 {
|
func HasPrefixAny(str string, prefixes []string) bool {
|
||||||
if len(str) == 0 || len(prefixes) == 0 {
|
if len(str) == 0 || len(prefixes) == 0 {
|
||||||
return false
|
return false
|
||||||
@@ -417,7 +421,8 @@ func HasPrefixAny(str string, prefixes []string) bool {
|
|||||||
return false
|
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 {
|
func HasSuffixAny(str string, suffixes []string) bool {
|
||||||
if len(str) == 0 || len(suffixes) == 0 {
|
if len(str) == 0 || len(suffixes) == 0 {
|
||||||
return false
|
return false
|
||||||
@@ -430,8 +435,9 @@ func HasSuffixAny(str string, suffixes []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexOffset returns the index of the first instance of substr in s after offsetting the string by `idxFrom`,
|
// 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 s.
|
// or -1 if substr is not present in string.
|
||||||
|
// Play: todo
|
||||||
func IndexOffset(str string, substr string, idxFrom int) int {
|
func IndexOffset(str string, substr string, idxFrom int) int {
|
||||||
if idxFrom > len(str)-1 || idxFrom < 0 {
|
if idxFrom > len(str)-1 || idxFrom < 0 {
|
||||||
return -1
|
return -1
|
||||||
|
|||||||
Reference in New Issue
Block a user