Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Encode method for memory sensitive cases #56

Merged
merged 1 commit into from
Jul 19, 2020
Merged
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
6 changes: 6 additions & 0 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func (id ID) String() string {
return *(*string)(unsafe.Pointer(&text))
}

// Encode encodes the id using base32 encoding, writing 20 bytes to dst and return it.
func (id ID) Encode(dst []byte) []byte {
encode(dst, id[:])
return dst
}

// MarshalText implements encoding/text TextMarshaler interface
func (id ID) MarshalText() ([]byte, error) {
text := make([]byte, encodedLen)
Expand Down
8 changes: 8 additions & 0 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func TestIDString(t *testing.T) {
}
}

func TestIDEncode(t *testing.T) {
id := ID{0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9}
text := make([]byte, encodedLen)
if got, want := string(id.Encode(text)), "9m4e2mr0ui3e8a215n4g"; got != want {
t.Errorf("Encode() = %v, want %v", got, want)
}
}

func TestFromString(t *testing.T) {
got, err := FromString("9m4e2mr0ui3e8a215n4g")
if err != nil {
Expand Down