1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: add document for new functions in slice and strutil package

This commit is contained in:
dudaodong
2023-04-18 15:49:09 +08:00
parent fcb3b97b45
commit 01a3b139c0
6 changed files with 573 additions and 38 deletions

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