1 Star 1 Fork 0

czy/go-ceph

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
splitbuf.go 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
package cutil
import "C"
import (
"bytes"
)
// SplitBuffer splits a byte-slice buffer, typically returned from C code,
// into a slice of strings.
// The contents of the buffer are assumed to be null-byte separated.
// If the buffer contains a sequence of null-bytes it will assume that the
// "space" between the bytes are meant to be empty strings.
func SplitBuffer(b []byte) []string {
return splitBufStrings(b, true)
}
// SplitSparseBuffer splits a byte-slice buffer, typically returned from C code,
// into a slice of strings.
// The contents of the buffer are assumed to be null-byte separated.
// This function assumes that buffer to be "sparse" such that only non-null-byte
// strings will be returned, and no "empty" strings exist if null-bytes
// are found adjacent to each other.
func SplitSparseBuffer(b []byte) []string {
return splitBufStrings(b, false)
}
// If keepEmpty is true, empty substrings will be returned, by default they are
// excluded from the results.
// This is almost certainly a suboptimal implementation, especially for
// keepEmpty=true case. Optimizing the functions is a job for another day.
func splitBufStrings(b []byte, keepEmpty bool) []string {
values := make([]string, 0)
// the final null byte should be the terminating null in C
// we never want to preserve the empty string after it
if len(b) > 0 && b[len(b)-1] == 0 {
b = b[:len(b)-1]
}
if len(b) == 0 {
return values
}
for _, s := range bytes.Split(b, []byte{0}) {
if !keepEmpty && len(s) == 0 {
continue
}
values = append(values, string(s))
}
return values
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/czy233/go-ceph.git
git@gitee.com:czy233/go-ceph.git
czy233
go-ceph
go-ceph
v0.14.2

搜索帮助