xfy 31faf77fcc style: add doc comments for exported hash and utils functions
Fix revive lint warnings for FNV64a, FNV64aBytes, BytesContainsFold.
2026-06-04 11:17:08 +08:00

49 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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
}