aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-09-25 23:57:01 +0700
committerShulhan <ms@kilabit.info>2024-09-30 23:04:56 +0700
commit04ab5cfb262c8dbdadf2090bde99506ff2eac5a9 (patch)
treef4ae4c1357266f81c9c51d9c7c3d10d8abc260d4 /lib/bytes
parentaccefcac3be38899a6f05d8eefec8ac8f11a8ddd (diff)
downloadpakakeh.go-04ab5cfb262c8dbdadf2090bde99506ff2eac5a9.tar.xz
lib/bytes: add function AppendInt64 and AppendUint64
The AppendInt64 append an int64 value into slice of byte. The AppendUint64 append an uint64 value into slice of byte.
Diffstat (limited to 'lib/bytes')
-rw-r--r--lib/bytes/bytes.go32
-rw-r--r--lib/bytes/bytes_example_test.go60
2 files changed, 86 insertions, 6 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 6e44f0a6..b47b0a85 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -1,6 +1,6 @@
-// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
+// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
// Package bytes provide a library for working with byte or slice of bytes.
package bytes
@@ -32,6 +32,19 @@ func AppendInt32(data []byte, v int32) []byte {
return data
}
+// AppendInt64 append an int64 value into slice of byte.
+func AppendInt64(data []byte, v int64) []byte {
+ data = append(data, byte(v>>56))
+ data = append(data, byte(v>>48))
+ data = append(data, byte(v>>40))
+ data = append(data, byte(v>>32))
+ data = append(data, byte(v>>24))
+ data = append(data, byte(v>>16))
+ data = append(data, byte(v>>8))
+ data = append(data, byte(v))
+ return data
+}
+
// AppendUint16 append an uint16 value into slice of byte.
func AppendUint16(data []byte, v uint16) []byte {
data = append(data, byte(v>>8))
@@ -48,6 +61,19 @@ func AppendUint32(data []byte, v uint32) []byte {
return data
}
+// AppendUint64 append an uint64 value into slice of byte.
+func AppendUint64(data []byte, v uint64) []byte {
+ data = append(data, byte(v>>56))
+ data = append(data, byte(v>>48))
+ data = append(data, byte(v>>40))
+ data = append(data, byte(v>>32))
+ data = append(data, byte(v>>24))
+ data = append(data, byte(v>>16))
+ data = append(data, byte(v>>8))
+ data = append(data, byte(v))
+ return data
+}
+
// Concat merge one or more []byte or string in args into slice of
// byte.
// Any type that is not []byte or string in args will be ignored.
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index d1efbafe..0c2fbf49 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -1,6 +1,6 @@
-// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
+// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
package bytes_test
@@ -38,6 +38,33 @@ func ExampleAppendInt32() {
// 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
}
+func ExampleAppendInt64() {
+ var listInt64 = []int64{
+ math.MaxInt64,
+ 1000,
+ math.MinInt64,
+ }
+
+ var (
+ v int64
+ out []byte
+ )
+ for _, v = range listInt64 {
+ out = libbytes.AppendInt64(nil, v)
+ fmt.Printf("%d\n=> %#x\n=> %#v\n", v, v, out)
+ }
+ // Output:
+ // 9223372036854775807
+ // => 0x7fffffffffffffff
+ // => []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
+ // 1000
+ // => 0x3e8
+ // => []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xe8}
+ // -9223372036854775808
+ // => -0x8000000000000000
+ // => []byte{0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
+}
+
func ExampleAppendUint16() {
inputs := []uint16{0, 0xab, 0xabc, math.MaxInt16, math.MaxUint16}
for _, v := range inputs {
@@ -78,6 +105,33 @@ func ExampleAppendUint32() {
// 1 => 0x1 => []byte{0x0, 0x0, 0x0, 0x1}
}
+func ExampleAppendUint64() {
+ var listUint64 = []uint64{
+ math.MaxUint64,
+ math.MaxInt64,
+ 1000,
+ }
+
+ var (
+ v uint64
+ out []byte
+ )
+ for _, v = range listUint64 {
+ out = libbytes.AppendUint64(nil, v)
+ fmt.Printf("%d\n=> %#x\n=> %#v\n", v, v, out)
+ }
+ // Output:
+ // 18446744073709551615
+ // => 0xffffffffffffffff
+ // => []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
+ // 9223372036854775807
+ // => 0x7fffffffffffffff
+ // => []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
+ // 1000
+ // => 0x3e8
+ // => []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xe8}
+}
+
func ExampleConcat() {
fmt.Printf("%s\n", libbytes.Concat())
fmt.Printf("%s\n", libbytes.Concat([]byte{}))