diff options
| author | Shulhan <ms@kilabit.info> | 2025-01-22 21:33:10 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-01-22 21:34:16 +0700 |
| commit | 360cd7bdc721c2aa968e2f6888db1c7e36538ae2 (patch) | |
| tree | 08c6610214a4da1b54707231c235e81127deeae6 /lib/bytes | |
| parent | cdf43c8a97f6fbe7548aa45d3ce2680bdeb70e36 (diff) | |
| download | pakakeh.go-360cd7bdc721c2aa968e2f6888db1c7e36538ae2.tar.xz | |
lib/bytes: replace Copy and Concat with standard library
Since Go 1.20, the standard bytes package have the Copy function.
Since Go 1.22, the standard slices package have the Concat function.
Diffstat (limited to 'lib/bytes')
| -rw-r--r-- | lib/bytes/bytes.go | 25 | ||||
| -rw-r--r-- | lib/bytes/bytes_example_test.go | 36 | ||||
| -rw-r--r-- | lib/bytes/parser.go | 18 |
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 |
