aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2019-09-27 00:47:54 +0700
committerShulhan <m.shulhan@gmail.com>2019-09-27 00:49:49 +0700
commit42f468352f6dd7202978517fbd0a94743b9e8a5b (patch)
tree35c6dad4f8d8c68f37bbeaa10f7da88654449d97 /lib/bytes/bytes.go
parent41294ceedd738cc44fe01dd0011ba7087a5d37fb (diff)
downloadpakakeh.go-42f468352f6dd7202978517fbd0a94743b9e8a5b.tar.xz
bytes: add function to get indexes of word in string
Unlike Indexes, the word parameter must be separated by space or placed at beginning or end of string.
Diffstat (limited to 'lib/bytes/bytes.go')
-rw-r--r--lib/bytes/bytes.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 4db067a0..54a7856a 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -9,6 +9,7 @@ import (
"bytes"
"fmt"
"reflect"
+ "unicode"
)
//
@@ -510,6 +511,37 @@ func TokenFind(line, token []byte, startat int) (at int) {
}
//
+// 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) {
+ tmp := Indexes(s, word)
+ if len(tmp) == 0 {
+ return nil
+ }
+
+ for _, idx := range tmp {
+ x := idx - 1
+ if x >= 0 {
+ if !unicode.IsSpace(rune(s[x])) {
+ continue
+ }
+ }
+ x = idx + len(word)
+ if x >= len(s) {
+ idxs = append(idxs, idx)
+ continue
+ }
+ if !unicode.IsSpace(rune(s[x])) {
+ continue
+ }
+ idxs = append(idxs, idx)
+ }
+
+ return idxs
+}
+
+//
// WriteUint16 into slice of byte.
//
func WriteUint16(data *[]byte, x uint, v uint16) {