Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 3 additions & 5 deletions internal/hex.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package internal

import (
fasthex "github.com/tmthrgd/go-hex"
)
import "encoding/hex"

type HexEncoder struct {
b []byte
Expand All @@ -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
}
Expand Down
55 changes: 55 additions & 0 deletions internal/hex_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading