aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--CHANGELOG.adoc2
-rw-r--r--CHANGELOG.html3
-rw-r--r--lib/bytes/bytes.go32
-rw-r--r--lib/bytes/bytes_example_test.go10
4 files changed, 47 insertions, 0 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 8e24d10c..5d0d29d8 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -9,6 +9,8 @@ This library is released each month, usually at the first week of month.
=== New Features
+* bytes: add function to get all indexes of word in string
+
* bytes: add function to take snippets from string by indexes
* bytes: add function to get all indexes of token in string
diff --git a/CHANGELOG.html b/CHANGELOG.html
index b4795591..9703b9ad 100644
--- a/CHANGELOG.html
+++ b/CHANGELOG.html
@@ -115,6 +115,9 @@
<div class="ulist">
<ul>
<li>
+<p>bytes: add function to get all indexes of word in string</p>
+</li>
+<li>
<p>bytes: add function to take snippets from string by indexes</p>
</li>
<li>
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) {
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index 06b96146..dcb0e1ec 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -128,3 +128,13 @@ func ExampleTokenFind() {
fmt.Printf("%d\n", at)
// Output: 7
}
+
+func ExampleWordIndexes() {
+ s := []byte("moo moomoo moo")
+ token := []byte("moo")
+
+ idxs := WordIndexes(s, token)
+ fmt.Println(idxs)
+ // Output:
+ // [0 11]
+}