aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-03-04 15:02:57 +0700
committerShulhan <ms@kilabit.info>2019-03-04 17:00:44 +0700
commitd035639e9d3aef7b63b91b958ae79bdac025bb7e (patch)
treecf382872579c9551915b6ce1832567d02e1f74fc /lib/bytes/bytes.go
parentf5245822d6b5b2335c122bb1fc2b2a676d13a24c (diff)
downloadpakakeh.go-d035639e9d3aef7b63b91b958ae79bdac025bb7e.tar.xz
bytes: add function to concat slice of byte or string into []byte
Diffstat (limited to 'lib/bytes/bytes.go')
-rw-r--r--lib/bytes/bytes.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index b59ac3ac..5c9da0be 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -7,6 +7,7 @@ package bytes
import (
"fmt"
+ "reflect"
)
const (
@@ -69,6 +70,35 @@ func AppendUint32(data *[]byte, v uint32) {
}
//
+// Concat merge one or more slice of byte or string in arguments into slice of
+// byte.
+// Any type that is not []byte or string in arguments will be ignored.
+//
+func Concat(sb []byte, args ...interface{}) (out []byte) {
+ if len(sb) > 0 {
+ out = append(out, sb...)
+ }
+ for _, arg := range args {
+ v := reflect.ValueOf(arg)
+ if v.Kind() == reflect.String {
+ out = append(out, arg.(string)...)
+ }
+ if v.Kind() != reflect.Slice {
+ continue
+ }
+ if v.Len() == 0 {
+ continue
+ }
+ if v.Index(0).Kind() != reflect.Uint8 {
+ continue
+ }
+ b := v.Bytes()
+ out = append(out, b...)
+ }
+ return
+}
+
+//
// Copy slice of bytes from parameter.
//
func Copy(src []byte) (dst *[]byte) {