aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-01-22 21:33:10 +0700
committerShulhan <ms@kilabit.info>2025-01-22 21:34:16 +0700
commit360cd7bdc721c2aa968e2f6888db1c7e36538ae2 (patch)
tree08c6610214a4da1b54707231c235e81127deeae6
parentcdf43c8a97f6fbe7548aa45d3ce2680bdeb70e36 (diff)
downloadpakakeh.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.
-rw-r--r--lib/bytes/bytes.go25
-rw-r--r--lib/bytes/bytes_example_test.go36
-rw-r--r--lib/bytes/parser.go18
-rw-r--r--lib/dns/rdata_hinfo.go13
-rw-r--r--lib/dns/server.go11
-rw-r--r--lib/dns/zone_parser.go8
-rw-r--r--lib/email/message.go9
-rw-r--r--lib/ini/reader.go12
-rw-r--r--lib/mlog/multi_logger.go12
-rw-r--r--lib/telemetry/dsv_formatter.go12
-rw-r--r--lib/telemetry/ilp_formatter.go12
-rw-r--r--lib/websocket/frame_test.go20
-rw-r--r--lib/websocket/frames_test.go16
-rw-r--r--lib/websocket/server.go10
14 files changed, 73 insertions, 141 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
diff --git a/lib/dns/rdata_hinfo.go b/lib/dns/rdata_hinfo.go
index 0b9358e1..c1da5162 100644
--- a/lib/dns/rdata_hinfo.go
+++ b/lib/dns/rdata_hinfo.go
@@ -1,14 +1,13 @@
-// 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 dns
import (
+ "bytes"
"fmt"
"strings"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
// RDataHINFO HINFO records are used to acquire general information about a
@@ -27,11 +26,11 @@ func (hinfo *RDataHINFO) unpack(packet []byte) error {
size = int(packet[x])
)
x++
- hinfo.CPU = libbytes.Copy(packet[x : x+size])
+ hinfo.CPU = bytes.Clone(packet[x : x+size])
x += size
size = int(packet[x])
x++
- hinfo.OS = libbytes.Copy(packet[x : x+size])
+ hinfo.OS = bytes.Clone(packet[x : x+size])
return nil
}
diff --git a/lib/dns/server.go b/lib/dns/server.go
index 8d2c7f4e..9d341dac 100644
--- a/lib/dns/server.go
+++ b/lib/dns/server.go
@@ -1,10 +1,11 @@
-// 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 dns
import (
+ "bytes"
"context"
"crypto/tls"
"encoding/base64"
@@ -17,8 +18,6 @@ import (
"strings"
"sync"
"time"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
const (
@@ -367,7 +366,7 @@ func (srv *Server) serveUDP() {
}
req = newRequest()
- req.message.packet = libbytes.Copy(packet[:n])
+ req.message.packet = bytes.Clone(packet[:n])
req.kind = connTypeUDP
req.writer = &UDPClient{
diff --git a/lib/dns/zone_parser.go b/lib/dns/zone_parser.go
index dabf392a..b5f5d260 100644
--- a/lib/dns/zone_parser.go
+++ b/lib/dns/zone_parser.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 dns
@@ -414,7 +414,7 @@ func (m *zoneParser) parseRR(prevRR *ResourceRecord, tok []byte) (rr *ResourceRe
return nil, fmt.Errorf(`%s: line %d: invalid RR statement '%s'`, logp, m.lineno, tok)
}
- orgtok = libbytes.Copy(tok)
+ orgtok = bytes.Clone(tok)
tok = ascii.ToUpper(tok)
stok = string(tok)
diff --git a/lib/email/message.go b/lib/email/message.go
index e2570947..55ade647 100644
--- a/lib/email/message.go
+++ b/lib/email/message.go
@@ -1,6 +1,6 @@
-// Copyright 2019, 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: 2019 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
package email
@@ -13,7 +13,6 @@ import (
"strings"
"time"
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
"git.sr.ht/~shulhan/pakakeh.go/lib/email/dkim"
)
@@ -255,7 +254,7 @@ func (msg *Message) DKIMVerify() (*dkim.Status, error) {
sig, err := dkim.Parse([]byte(subHeader.Fields[0].Value))
if sig != nil && len(sig.SDID) > 0 {
- msg.dkimStatus.SDID = libbytes.Copy(sig.SDID)
+ msg.dkimStatus.SDID = bytes.Clone(sig.SDID)
}
if err != nil {
msg.dkimStatus.Type = dkim.StatusPermFail
diff --git a/lib/ini/reader.go b/lib/ini/reader.go
index 9e7b1ad7..5e27a226 100644
--- a/lib/ini/reader.go
+++ b/lib/ini/reader.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 ini
@@ -11,8 +11,6 @@ import (
"io"
"strings"
"unicode"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
const (
@@ -493,7 +491,7 @@ func (reader *reader) parseVarValue() (err error) {
reader.bufFormat.WriteByte(reader.b)
- reader._var.rawValue = libbytes.Copy(reader.buf.Bytes())
+ reader._var.rawValue = bytes.Clone(reader.buf.Bytes())
reader._var.value = parseRawValue(reader._var.rawValue)
return reader.parseComment()
@@ -505,7 +503,7 @@ func (reader *reader) parseVarValue() (err error) {
return errValueInvalid
}
- reader._var.rawValue = libbytes.Copy(reader.buf.Bytes())
+ reader._var.rawValue = bytes.Clone(reader.buf.Bytes())
reader._var.value = parseRawValue(reader._var.rawValue)
reader._var.format = reader.bufFormat.String()
diff --git a/lib/mlog/multi_logger.go b/lib/mlog/multi_logger.go
index d0dcab82..bf8c3b87 100644
--- a/lib/mlog/multi_logger.go
+++ b/lib/mlog/multi_logger.go
@@ -1,6 +1,6 @@
-// Copyright 2021, 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: 2021 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
package mlog
@@ -12,8 +12,6 @@ import (
"runtime/debug"
"sync"
"time"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
var (
@@ -226,7 +224,7 @@ func (mlog *MultiLogger) UnregisterOutputWriter(name string) {
// Write write the b to all error writers.
// It will always return the length of b without an error.
func (mlog *MultiLogger) Write(b []byte) (n int, err error) {
- mlog.qerr <- libbytes.Copy(b)
+ mlog.qerr <- bytes.Clone(b)
return len(b), nil
}
@@ -326,7 +324,7 @@ func (mlog *MultiLogger) writeTo(q chan []byte, format string, v ...interface{})
format = buf.String()
buf.Reset()
fmt.Fprintf(buf, format, v...)
- b = libbytes.Copy(buf.Bytes())
+ b = bytes.Clone(buf.Bytes())
q <- b
mlog.bufPool.Put(buf)
diff --git a/lib/telemetry/dsv_formatter.go b/lib/telemetry/dsv_formatter.go
index 72478ade..43be36ea 100644
--- a/lib/telemetry/dsv_formatter.go
+++ b/lib/telemetry/dsv_formatter.go
@@ -1,14 +1,12 @@
-// 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 telemetry
import (
"bytes"
"fmt"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
// DsvFormatter format the [Metric] in single line where each value is separated
@@ -54,7 +52,7 @@ func (dsv *DsvFormatter) BulkFormat(listm []Metric, md *Metadata) []byte {
for _, m = range listm {
dsv.formatMetric(&bb, m)
}
- return libbytes.Copy(bb.Bytes())
+ return bytes.Clone(bb.Bytes())
}
// Format single Metric into single line DSV.
@@ -95,7 +93,7 @@ func (dsv *DsvFormatter) generateMetadata(md *Metadata) {
v = vals[k]
fmt.Fprintf(&bb, `%s=%s`, k, v)
}
- dsv.mdRaw = libbytes.Copy(bb.Bytes())
+ dsv.mdRaw = bytes.Clone(bb.Bytes())
dsv.mdVersion = mdVersion
}
diff --git a/lib/telemetry/ilp_formatter.go b/lib/telemetry/ilp_formatter.go
index 33d20bab..550bf57a 100644
--- a/lib/telemetry/ilp_formatter.go
+++ b/lib/telemetry/ilp_formatter.go
@@ -1,14 +1,12 @@
-// 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 telemetry
import (
"bytes"
"fmt"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
const (
@@ -71,7 +69,7 @@ func (ilp *IlpFormatter) BulkFormat(list []Metric, md *Metadata) []byte {
fmt.Fprintf(&bb, " %d\n", m.Timestamp)
- return libbytes.Copy(bb.Bytes())
+ return bytes.Clone(bb.Bytes())
}
// Format single Metric.
@@ -99,7 +97,7 @@ func (ilp *IlpFormatter) generateMetadata(md *Metadata) {
v = vals[k]
fmt.Fprintf(&bb, `,%s=%s`, k, v)
}
- ilp.mdRaw = libbytes.Copy(bb.Bytes())
+ ilp.mdRaw = bytes.Clone(bb.Bytes())
ilp.mdVersion = mdVersion
}
diff --git a/lib/websocket/frame_test.go b/lib/websocket/frame_test.go
index bab2b04c..b4b84181 100644
--- a/lib/websocket/frame_test.go
+++ b/lib/websocket/frame_test.go
@@ -1,13 +1,13 @@
-// 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 websocket
import (
+ "slices"
"testing"
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
@@ -75,7 +75,7 @@ func TestNewFrameClose(t *testing.T) {
opcode: OpcodeClose,
closeCode: StatusBadRequest,
masked: frameIsMasked,
- payload: libbytes.Concat([]byte{0x03, 0xEA},
+ payload: slices.Concat([]byte{0x03, 0xEA},
[]byte("Hello!")),
},
}, {
@@ -86,7 +86,7 @@ func TestNewFrameClose(t *testing.T) {
opcode: OpcodeClose,
closeCode: StatusBadRequest,
masked: frameIsMasked,
- payload: libbytes.Concat([]byte{0x03, 0xEA},
+ payload: slices.Concat([]byte{0x03, 0xEA},
_dummyPayload256[:123]),
},
}}
@@ -339,7 +339,7 @@ func TestFramePack(t *testing.T) {
masked: 0,
payload: _dummyPayload256,
},
- exp: libbytes.Concat([]byte{0x82, 0x7E, 0x01, 0x00},
+ exp: slices.Concat([]byte{0x82, 0x7E, 0x01, 0x00},
_dummyPayload256),
}, {
desc: `256 bytes binary message in a single masked frame`,
@@ -350,7 +350,7 @@ func TestFramePack(t *testing.T) {
payload: _dummyPayload256,
maskKey: _testMaskKey,
},
- exp: libbytes.Concat([]byte{
+ exp: slices.Concat([]byte{
0x82, 0xFE,
0x01, 0x00,
_testMaskKey[0], _testMaskKey[1], _testMaskKey[2],
@@ -364,7 +364,7 @@ func TestFramePack(t *testing.T) {
masked: 0,
payload: _dummyPayload65536,
},
- exp: libbytes.Concat([]byte{
+ exp: slices.Concat([]byte{
0x82, 0x7F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
}, _dummyPayload65536),
@@ -378,7 +378,7 @@ func TestFramePack(t *testing.T) {
maskKey: _testMaskKey,
len: 65536,
},
- exp: libbytes.Concat([]byte{
+ exp: slices.Concat([]byte{
0x82, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
_testMaskKey[0], _testMaskKey[1], _testMaskKey[2],
diff --git a/lib/websocket/frames_test.go b/lib/websocket/frames_test.go
index 5cd4a098..6753210d 100644
--- a/lib/websocket/frames_test.go
+++ b/lib/websocket/frames_test.go
@@ -1,13 +1,13 @@
-// Copyright 2019, 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: 2019 M. Shulhan <ms@kilabit.info>
+//
+// SPDX-License-Identifier: BSD-3-Clause
package websocket
import (
+ "slices"
"testing"
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
@@ -114,7 +114,7 @@ func TestFrameUnpack(t *testing.T) {
},
}, {
desc: `256 bytes binary message in a single unmasked frame`,
- in: libbytes.Concat([]byte{0x82, 0x7E, 0x01, 0x00},
+ in: slices.Concat([]byte{0x82, 0x7E, 0x01, 0x00},
_dummyPayload256),
exp: &Frame{
fin: frameIsFinished,
@@ -126,7 +126,7 @@ func TestFrameUnpack(t *testing.T) {
},
}, {
desc: `256 bytes binary message in a single masked frame`,
- in: libbytes.Concat([]byte{
+ in: slices.Concat([]byte{
0x82, 0xFE, 0x01, 0x00,
_testMaskKey[0], _testMaskKey[1], _testMaskKey[2],
_testMaskKey[3],
@@ -142,7 +142,7 @@ func TestFrameUnpack(t *testing.T) {
},
}, {
desc: `65536 binary message in a single unmasked frame`,
- in: libbytes.Concat([]byte{
+ in: slices.Concat([]byte{
0x82, 0x7F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
}, _dummyPayload65536),
@@ -156,7 +156,7 @@ func TestFrameUnpack(t *testing.T) {
},
}, {
desc: `65536 binary message in a single masked frame`,
- in: libbytes.Concat([]byte{
+ in: slices.Concat([]byte{
0x82, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
_testMaskKey[0], _testMaskKey[1], _testMaskKey[2],
diff --git a/lib/websocket/server.go b/lib/websocket/server.go
index 37d39a7d..0a0a1200 100644
--- a/lib/websocket/server.go
+++ b/lib/websocket/server.go
@@ -1,10 +1,11 @@
-// 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 websocket
import (
+ "bytes"
"context"
"encoding/json"
"errors"
@@ -19,7 +20,6 @@ import (
"golang.org/x/sys/unix"
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net"
)
@@ -211,7 +211,7 @@ func (serv *Server) handleUpgrade(hs *Handshake) (ctx context.Context, key []byt
goto out
}
- key = libbytes.Copy(hs.Key)
+ key = bytes.Clone(hs.Key)
if serv.Options.HandleAuth != nil {
ctx, err = serv.Options.HandleAuth(hs)
}