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
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeChunk(buf []byte, encoded string) []byte {
|
func decodeChunk(buf []byte, encoded []byte) []byte {
|
||||||
var intResult uint64
|
var intResult uint64
|
||||||
currentMultiplier := uint64(1)
|
currentMultiplier := uint64(1)
|
||||||
for i := len(encoded) - 1; i >= 0; i-- {
|
for i := len(encoded) - 1; i >= 0; i-- {
|
||||||
|
@ -94,32 +94,41 @@ func decodeChunk(buf []byte, encoded string) []byte {
|
||||||
return nil
|
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
|
//preallocate common case
|
||||||
combined := make([]byte, 0, 96)
|
combined := make([]byte, 0, 96)
|
||||||
for _, item := range data {
|
for _, item := range data {
|
||||||
combined = append(combined, item...)
|
combined = append(combined, item...)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]byte, 0, len(combined)*2)
|
result := buf
|
||||||
buf := make([]byte, 0, len(combined)*2)
|
tmpBuf := make([]byte, 0, len(combined)*2)
|
||||||
length := len(combined)
|
length := len(combined)
|
||||||
rounds := length / 8
|
rounds := length / 8
|
||||||
for i := 0; i < rounds; i++ {
|
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 {
|
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
|
//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
|
result = buf
|
||||||
length := len(data)
|
length := len(data)
|
||||||
rounds := length / 11
|
rounds := length / 11
|
||||||
|
|
Loading…
Reference in a new issue