Allow preallocated encode, match 18ecc51ae61e777d1f855aa4351541b664e21734 upstream
This commit is contained in:
parent
3e013d432d
commit
2b2aa76694
29
base58.go
29
base58.go
|
@ -59,7 +59,7 @@ func encodeChunkTail(raw []byte, buf []byte) []byte {
|
|||
return buf
|
||||
}
|
||||
|
||||
func decodeChunk(buf []byte, encoded string) []byte {
|
||||
func decodeChunk(buf []byte, encoded []byte) []byte {
|
||||
var intResult uint64
|
||||
currentMultiplier := uint64(1)
|
||||
for i := len(encoded) - 1; i >= 0; i-- {
|
||||
|
@ -94,32 +94,41 @@ func decodeChunk(buf []byte, encoded string) []byte {
|
|||
return nil
|
||||
}
|
||||
|
||||
func Encode(data ...[]byte) string {
|
||||
func EncodeMoneroBase58(data ...[]byte) []byte {
|
||||
return EncodeMoneroBase58PreAllocated(make([]byte, 0, func() (result int) {
|
||||
for _, v := range data {
|
||||
result += len(v)
|
||||
}
|
||||
return
|
||||
}()), data...)
|
||||
}
|
||||
|
||||
func EncodeMoneroBase58PreAllocated(buf []byte, data ...[]byte) []byte {
|
||||
//preallocate common case
|
||||
combined := make([]byte, 0, 96)
|
||||
for _, item := range data {
|
||||
combined = append(combined, item...)
|
||||
}
|
||||
|
||||
result := make([]byte, 0, len(combined)*2)
|
||||
buf := make([]byte, 0, len(combined)*2)
|
||||
result := buf
|
||||
tmpBuf := make([]byte, 0, len(combined)*2)
|
||||
length := len(combined)
|
||||
rounds := length / 8
|
||||
for i := 0; i < rounds; i++ {
|
||||
result = append(result, encodeChunk(combined[i*8:(i+1)*8], buf[:0])...)
|
||||
result = append(result, encodeChunk(combined[i*8:(i+1)*8], tmpBuf[:0])...)
|
||||
}
|
||||
if length%8 > 0 {
|
||||
result = append(result, encodeChunkTail(combined[rounds*8:], buf[:0])...)
|
||||
result = append(result, encodeChunkTail(combined[rounds*8:], tmpBuf[:0])...)
|
||||
}
|
||||
return string(result)
|
||||
return result
|
||||
}
|
||||
|
||||
func Decode(data string) (result []byte) {
|
||||
func DecodeMoneroBase58(data []byte) (result []byte) {
|
||||
//common case
|
||||
return DecodePreAllocated(make([]byte, 0, 69), data)
|
||||
return DecodeMoneroBase58PreAllocated(make([]byte, 0, 69), data)
|
||||
}
|
||||
|
||||
func DecodePreAllocated(buf []byte, data string) (result []byte) {
|
||||
func DecodeMoneroBase58PreAllocated(buf []byte, data []byte) (result []byte) {
|
||||
result = buf
|
||||
length := len(data)
|
||||
rounds := length / 11
|
||||
|
|
Loading…
Reference in a new issue