aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2019-09-24 22:22:02 +0700
committerShulhan <m.shulhan@gmail.com>2019-09-24 22:38:39 +0700
commit7eacd3ade3ecc83ee59ca8ce14cb60c2082096a3 (patch)
tree431c01263c1b70a5205d7952d2da0e6ea1b45a37
parent7474d7ac41d49af9e15a5c21de99a3eaea5f68b4 (diff)
downloadpakakeh.go-7eacd3ade3ecc83ee59ca8ce14cb60c2082096a3.tar.xz
bytes: add function get all indexes of token in string
-rw-r--r--CHANGELOG.adoc2
-rw-r--r--CHANGELOG.html3
-rw-r--r--lib/bytes/bytes.go24
-rw-r--r--lib/bytes/bytes_example_test.go10
-rw-r--r--lib/bytes/bytes_test.go30
5 files changed, 68 insertions, 1 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index b1f284f7..a7711c3b 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 token in string
+
* memfs: add method to Search content of files
* sanitize: new package to sanitize markup document into plain text
diff --git a/CHANGELOG.html b/CHANGELOG.html
index a1d7b8d2..102d9824 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 token in string</p>
+</li>
+<li>
<p>memfs: add method to Search content of files</p>
</li>
<li>
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 62e7d307..ae7aae12 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -6,6 +6,7 @@
package bytes
import (
+ "bytes"
"fmt"
"reflect"
)
@@ -363,6 +364,29 @@ func InReplace(in, allowed []byte, c byte) (out []byte) {
}
//
+// Indexes returns the index of the all instance of token in s, or nil if
+// token is not present in s.
+//
+func Indexes(s []byte, token []byte) (idxs []int) {
+ if len(s) == 0 || len(token) == 0 {
+ return nil
+ }
+
+ offset := 0
+ for {
+ idx := bytes.Index(s, token)
+ if idx == -1 {
+ break
+ }
+ idxs = append(idxs, offset+idx)
+ skip := idx + len(token)
+ offset += skip
+ s = s[skip:]
+ }
+ return idxs
+}
+
+//
// SkipAfterToken skip all bytes until matched token is found and return the
// index after the token and boolean true.
//
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index 141d7f17..bd6b41ea 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -54,6 +54,16 @@ func ExampleEncloseToken() {
// '// Copyright 2016-2018 \"_Shulhan <ms@kilabit.info>\"_. All rights reserved.' true
}
+func ExampleIndexes() {
+ s := []byte("moo moomoo")
+ token := []byte("moo")
+
+ idxs := Indexes(s, token)
+ fmt.Println(idxs)
+ // Output:
+ // [0 4 7]
+}
+
func ExampleIsTokenAt() {
line := []byte("Hello, world")
token := []byte("world")
diff --git a/lib/bytes/bytes_test.go b/lib/bytes/bytes_test.go
index 938f4ca1..66b04e6a 100644
--- a/lib/bytes/bytes_test.go
+++ b/lib/bytes/bytes_test.go
@@ -171,7 +171,7 @@ func TestIsTokenAt(t *testing.T) {
p int
exp bool
}{{
- // empty
+ // empty
}, {
token: []byte("world"),
p: -1,
@@ -332,3 +332,31 @@ func TestInReplace(t *testing.T) {
test.Assert(t, "InReplace", c.exp, string(got), true)
}
}
+
+func TestIndexes(t *testing.T) {
+ cases := []struct {
+ desc string
+ s []byte
+ token []byte
+ exp []int
+ }{{
+ desc: "With empty string",
+ token: []byte("moo"),
+ }, {
+ desc: "With empty token",
+ s: []byte("moo moo"),
+ }, {
+ desc: "With non empty string and token",
+ s: []byte("moo moomoo"),
+ token: []byte("moo"),
+ exp: []int{0, 4, 7},
+ }}
+
+ for _, c := range cases {
+ t.Log(c.desc)
+
+ got := Indexes(c.s, c.token)
+
+ test.Assert(t, "Indexes", c.exp, got, true)
+ }
+}