aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-03-22 13:54:25 +0700
committerShulhan <ms@kilabit.info>2023-03-22 13:54:25 +0700
commit3bd5fad4c0a972dd279ecfbf77de31838764f609 (patch)
treedf472c89f6b423c72f0dd421b49858b317220a73 /lib/bytes/bytes.go
parente2cb490e54e7e71109b3a681fd26519a06f29c2c (diff)
downloadpakakeh.go-3bd5fad4c0a972dd279ecfbf77de31838764f609.tar.xz
lib/bytes: add function SplitEach
The SplitEach funciton split the slice of byte into n number of bytes. If n is less or equal than zero, it will return the data as chunks.
Diffstat (limited to 'lib/bytes/bytes.go')
-rw-r--r--lib/bytes/bytes.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 1aa7585b..a0c23c1c 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -491,6 +491,29 @@ func SnippetByIndexes(s []byte, indexes []int, sniplen int) (snippets [][]byte)
return snippets
}
+// SplitEach split the data into n number of bytes.
+// If n is less or equal than zero, it will return the data as chunks.
+func SplitEach(data []byte, n int) (chunks [][]byte) {
+ if n <= 0 {
+ chunks = append(chunks, data)
+ return chunks
+ }
+
+ var (
+ size = len(data)
+ rows = (size / n)
+ total int
+ )
+ for x := 0; x < rows; x++ {
+ chunks = append(chunks, data[total:total+n])
+ total += n
+ }
+ if total < size {
+ chunks = append(chunks, data[total:])
+ }
+ return chunks
+}
+
// TokenFind return the first index of matched token in text, start at custom
// index.
// If "startat" parameter is less than 0, then it will be set to 0.