aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bytes')
-rw-r--r--lib/bytes/bytes.go25
-rw-r--r--lib/bytes/bytes_example_test.go36
-rw-r--r--lib/bytes/parser.go18
3 files changed, 11 insertions, 68 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 3bbf529b..76bb7874 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -71,31 +71,6 @@ func AppendUint64(data []byte, v uint64) []byte {
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.
-func Concat(args ...interface{}) (out []byte) {
- for _, arg := range args {
- switch v := arg.(type) {
- case string:
- out = append(out, []byte(v)...)
- case []byte:
- out = append(out, v...)
- }
- }
- return out
-}
-
-// Copy slice of bytes from parameter.
-func Copy(src []byte) (dst []byte) {
- if len(src) == 0 {
- return
- }
- dst = make([]byte, len(src))
- copy(dst, src)
- return
-}
-
// CutUntilToken cut text until we found token.
//
// If token found, it will return all bytes before token, position of byte
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index 6615f36d..e8c18d9b 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -130,42 +130,6 @@ func ExampleAppendUint64() {
// => []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xe8}
}
-func ExampleConcat() {
- fmt.Printf("%s\n", libbytes.Concat())
- fmt.Printf("%s\n", libbytes.Concat([]byte{}))
- fmt.Printf("%s\n", libbytes.Concat([]byte{}, []byte("B")))
- fmt.Printf("%s\n", libbytes.Concat("with []int:", []int{1, 2}))
- fmt.Printf("%s\n", libbytes.Concat([]byte("bytes"), " and ", []byte("string")))
- fmt.Printf("%s\n", libbytes.Concat([]byte("A"), 1, []int{2}, []byte{}, []byte("C")))
- // Output:
- //
- //
- // B
- // with []int:
- // bytes and string
- // AC
-}
-
-func ExampleCopy() {
- // Copying empty slice.
- org := []byte{}
- cp := libbytes.Copy(org)
- fmt.Printf("%d %q\n", len(cp), cp)
-
- org = []byte("slice of life")
- tmp := org
- cp = libbytes.Copy(org)
- fmt.Printf("%d %q\n", len(cp), cp)
- fmt.Printf("Original address == tmp address: %v\n", &org[0] == &tmp[0])
- fmt.Printf("Original address == copy address: %v\n", &org[0] == &cp[0])
-
- // Output:
- // 0 ""
- // 13 "slice of life"
- // Original address == tmp address: true
- // Original address == copy address: false
-}
-
func ExampleCutUntilToken() {
text := []byte(`\\abc \def \deg`)
diff --git a/lib/bytes/parser.go b/lib/bytes/parser.go
index d7a2055c..e5c7addb 100644
--- a/lib/bytes/parser.go
+++ b/lib/bytes/parser.go
@@ -1,10 +1,14 @@
-// Copyright 2023, 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: 2023 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
package bytes
-import "git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
+import (
+ "bytes"
+
+ "git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
+)
// Parser implement tokenize parser for stream of byte using one or more
// delimiters as separator between token.
@@ -33,7 +37,7 @@ func (bp *Parser) AddDelimiters(delims []byte) {
// Delimiters return the copy of current delimiters.
func (bp *Parser) Delimiters() []byte {
- return Copy(bp.delims)
+ return bytes.Clone(bp.delims)
}
// Read read a token until one of the delimiters found.
@@ -136,7 +140,7 @@ out:
// Remaining return the copy of un-parsed content.
func (bp *Parser) Remaining() []byte {
- return Copy(bp.content[bp.x:])
+ return bytes.Clone(bp.content[bp.x:])
}
// RemoveDelimiters remove delimiters delims from current delimiters.
@@ -256,7 +260,7 @@ func (bp *Parser) SkipSpaces() (n int, c byte) {
// Stop the parser, return the remaining unparsed content and its last
// position, and then call Reset to reset the internal state back to zero.
func (bp *Parser) Stop() (remain []byte, pos int) {
- remain = Copy(bp.content[bp.x:])
+ remain = bytes.Clone(bp.content[bp.x:])
pos = bp.x
bp.Reset(nil, nil)
return remain, pos