aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-04-05 00:16:25 +0700
committerShulhan <ms@kilabit.info>2023-04-05 00:21:03 +0700
commit3c849a75ca157d79d90f23e88221c30b09d59a65 (patch)
tree52b7bedadb1198f294528bc187b1f6615a4a6a28 /lib/bytes/bytes.go
parent56157387c1771d5afceee5638e5252843b239f79 (diff)
downloadpakakeh.go-3c849a75ca157d79d90f23e88221c30b09d59a65.tar.xz
lib/bytes: add function TrimNull
The TrimNull function remove 0 value ("\0" or NULL in C) at leading and trailing of input.
Diffstat (limited to 'lib/bytes/bytes.go')
-rw-r--r--lib/bytes/bytes.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 9329f24a..8656b4a9 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -525,6 +525,27 @@ func TokenFind(text, token []byte, startat int) (at int) {
return inbytes.TokenFind(text, token, startat)
}
+// TrimNull remove 0 value ("\0" or NULL in C) at leading and trailing of
+// in.
+func TrimNull(in []byte) (out []byte) {
+ var start int
+ for ; start < len(in); start++ {
+ if in[start] != 0 {
+ break
+ }
+ }
+
+ var end = len(in) - 1
+ for ; end > start; end-- {
+ if in[end] != 0 {
+ break
+ }
+ }
+ end++
+
+ return in[start:end]
+}
+
// WordIndexes returns the index of the all instance of word in s as long as
// word is separated by space or at the beginning or end of s.
func WordIndexes(s []byte, word []byte) (idxs []int) {