diff options
Diffstat (limited to 'lib/bytes/bytes.go')
| -rw-r--r-- | lib/bytes/bytes.go | 24 |
1 files changed, 24 insertions, 0 deletions
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. // |
