From e9bebd11ddbf508468f05abb9c504ed0433fc8b6 Mon Sep 17 00:00:00 2001 From: oozan <38402864+oozan@users.noreply.github.com> Date: Thu, 23 Jul 2026 12:31:05 +0300 Subject: [PATCH] chore: replace go-hex with encoding/hex --- go.mod | 1 - go.sum | 2 -- internal/hex.go | 8 +++---- internal/hex_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 internal/hex_test.go diff --git a/go.mod b/go.mod index a3b976e77..8dc97cd5a 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/puzpuzpuz/xsync/v3 v3.5.1 github.com/rs/zerolog v1.34.0 github.com/stretchr/testify v1.8.1 - github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc github.com/vmihailenco/msgpack/v5 v5.4.1 ) diff --git a/go.sum b/go.sum index b7d7cbe13..135588a6b 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= -github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= diff --git a/internal/hex.go b/internal/hex.go index 6fae2bb78..12fc14879 100644 --- a/internal/hex.go +++ b/internal/hex.go @@ -1,8 +1,6 @@ package internal -import ( - fasthex "github.com/tmthrgd/go-hex" -) +import "encoding/hex" type HexEncoder struct { b []byte @@ -27,8 +25,8 @@ func (enc *HexEncoder) Write(b []byte) (int, error) { } i := len(enc.b) - enc.b = append(enc.b, make([]byte, fasthex.EncodedLen(len(b)))...) - fasthex.Encode(enc.b[i:], b) + enc.b = append(enc.b, make([]byte, hex.EncodedLen(len(b)))...) + hex.Encode(enc.b[i:], b) return len(b), nil } diff --git a/internal/hex_test.go b/internal/hex_test.go new file mode 100644 index 000000000..8a547c3f3 --- /dev/null +++ b/internal/hex_test.go @@ -0,0 +1,55 @@ +package internal + +import "testing" + +func TestHexEncoder(t *testing.T) { + tests := []struct { + name string + writes [][]byte + want string + }{ + { + name: "no writes", + want: "NULL", + }, + { + name: "single write", + writes: [][]byte{{0x00, 0xab, 0xff}}, + want: "'\\x00abff'", + }, + { + name: "multiple writes", + writes: [][]byte{{0x01}, {0x23, 0x45}}, + want: "'\\x012345'", + }, + { + name: "empty write", + writes: [][]byte{{}}, + want: "'\\x'", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + enc := NewHexEncoder(nil) + + for _, part := range tt.writes { + n, err := enc.Write(part) + if err != nil { + t.Fatalf("Write returned an error: %v", err) + } + if n != len(part) { + t.Fatalf("Write returned %d, want %d", n, len(part)) + } + } + + if err := enc.Close(); err != nil { + t.Fatalf("Close returned an error: %v", err) + } + + if got := string(enc.Bytes()); got != tt.want { + t.Fatalf("encoded value = %q, want %q", got, tt.want) + } + }) + } +}