bytealg.IndexByteString

This commit is contained in:
xushiwei
2024-06-20 14:31:05 +08:00
parent f8b0a7105b
commit d4e7eb5888
3 changed files with 29 additions and 1 deletions

View File

@@ -18,5 +18,25 @@ package bytealg
// llgo:skip init
import (
_ "unsafe"
"unsafe"
"github.com/goplus/llgo/c"
)
func IndexByte(b []byte, ch byte) int {
ptr := unsafe.Pointer(unsafe.SliceData(b))
ret := c.Memchr(ptr, c.Int(ch), uintptr(len(b)))
if ret != nil {
return int(uintptr(ret) - uintptr(ptr))
}
return -1
}
func IndexByteString(s string, ch byte) int {
ptr := unsafe.Pointer(unsafe.StringData(s))
ret := c.Memchr(ptr, c.Int(ch), uintptr(len(s)))
if ret != nil {
return int(uintptr(ret) - uintptr(ptr))
}
return -1
}