Switch from string to []byte slices

The way I'm using the base58 required me to do a lot conversions between
[]byte slices and strings. Not anymore.

Also switching to []byte slices allowed some nice performance
improvements.

Before, with strings:

    BenchmarkEncode-4        3000000               511 ns/op
    BenchmarkDecode-4        5000000               300 ns/op

After, with []byte slices:

    BenchmarkEncode-4        5000000               256 ns/op
    BenchmarkDecode-4       20000000               107 ns/op
This commit is contained in:
2016-07-03 11:14:38 +01:00
parent 785190bf71
commit f8ea86748a
2 changed files with 28 additions and 16 deletions

View File

@@ -1,38 +1,41 @@
package base58
import (
"bytes"
"errors"
"strings"
)
// Alphabet is the default alphabet.
const Alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
const base = len(Alphabet)
var Alphabet = []byte(
"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
)
var base = len(Alphabet)
var errInvalidBase58 = errors.New("invalid base58")
// Encode converts a base10 integer to a base58 string using the default
// alphabet.
func Encode(num int) string {
str := ""
func Encode(num int) []byte {
str := []byte{}
for num >= base {
mod := num % base
str = string(Alphabet[mod]) + str
str = prepend(str, Alphabet[mod])
num = (num - mod) / base
}
return string(Alphabet[num]) + str
return prepend(str, Alphabet[num])
}
// Decode converts a base58 string to a base10 integer using the default
// alphabet.
func Decode(str string) (int, error) {
func Decode(str []byte) (int, error) {
num := 0
multi := 1
for i := len(str); i > 0; i-- {
char := string(str[i-1])
index := strings.Index(Alphabet, char)
char := str[i-1]
index := bytes.IndexByte(Alphabet, char)
if index == -1 {
return -1, errInvalidBase58
}
@@ -42,3 +45,10 @@ func Decode(str string) (int, error) {
return num, nil
}
func prepend(slice []byte, elem byte) []byte {
slice = append(slice, byte(0))
copy(slice[1:], slice)
slice[0] = elem
return slice
}