diff options
| author | Shulhan <m.shulhan@gmail.com> | 2019-09-24 23:08:05 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2019-09-25 00:43:59 +0700 |
| commit | 1338d2d896b78dc9ca0a29c0b2c75f74678483e9 (patch) | |
| tree | eb28e1ba9d6d9e7175dddb7b95e95d97591b4f0c /lib/bytes/bytes.go | |
| parent | 2011f07cc430a89879c326686ad0bdf9c7a314a7 (diff) | |
| download | pakakeh.go-1338d2d896b78dc9ca0a29c0b2c75f74678483e9.tar.xz | |
bytes: add function to take snippets from string by indexes
Diffstat (limited to 'lib/bytes/bytes.go')
| -rw-r--r-- | lib/bytes/bytes.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index ae7aae12..4db067a0 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -445,6 +445,29 @@ func SkipAfterToken(line, token []byte, startAt int, checkEsc bool) (int, bool) } // +// SnippetByIndexes take snippet in between of each index with minimum +// snippet length. The sniplen is the length before and after index, not the +// length of all snippet. +// +func SnippetByIndexes(s []byte, indexes []int, sniplen int) (snippets [][]byte) { + var start, end int + for _, idx := range indexes { + start = idx - sniplen + if start < 0 { + start = 0 + } + end = idx + sniplen + if end > len(s) { + end = len(s) + } + + snippets = append(snippets, s[start:end]) + } + + return snippets +} + +// // TokenFind return the first index of matched token in line, start at custom // index. // If "startat" parameter is less than 0, then it will be set to 0. |
