49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
// Package utils 提供通用的工具函数和辅助类型。
|
||
//
|
||
// 包含字节操作相关的工具函数,用于处理字节切片和缓冲区。
|
||
//
|
||
// 作者:xfy
|
||
package utils
|
||
|
||
import (
|
||
"bytes"
|
||
"unsafe"
|
||
)
|
||
|
||
// B2s converts byte slice to string without allocation.
|
||
// WARNING: The returned string shares memory with the original slice.
|
||
// Do not modify the slice after calling this function.
|
||
func B2s(b []byte) string {
|
||
if len(b) == 0 {
|
||
return ""
|
||
}
|
||
return unsafe.String(&b[0], len(b))
|
||
}
|
||
|
||
// S2b converts string to byte slice without allocation.
|
||
// WARNING: The returned slice shares memory with the original string.
|
||
// Do not modify the slice contents.
|
||
func S2b(s string) []byte {
|
||
if s == "" {
|
||
return nil
|
||
}
|
||
return unsafe.Slice(unsafe.StringData(s), len(s))
|
||
}
|
||
|
||
// BytesContainsFold reports whether b contains subslice, case-insensitively.
|
||
func BytesContainsFold(b, sub []byte) bool {
|
||
if len(sub) == 0 {
|
||
return true
|
||
}
|
||
if len(sub) > len(b) {
|
||
return false
|
||
}
|
||
end := len(b) - len(sub) + 1
|
||
for i := 0; i < end; i++ {
|
||
if bytes.EqualFold(b[i:i+len(sub)], sub) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|