mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 23:30:36 +00:00
36 lines
681 B
Go
36 lines
681 B
Go
|
package mpt
|
||
|
|
||
|
// lcp returns longest common prefix of a and b.
|
||
|
// Note: it does no allocations.
|
||
|
func lcp(a, b []byte) []byte {
|
||
|
if len(a) < len(b) {
|
||
|
return lcp(b, a)
|
||
|
}
|
||
|
|
||
|
var i int
|
||
|
for i = 0; i < len(b); i++ {
|
||
|
if a[i] != b[i] {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return a[:i]
|
||
|
}
|
||
|
|
||
|
// copySlice is a helper for copying slice if needed.
|
||
|
func copySlice(a []byte) []byte {
|
||
|
b := make([]byte, len(a))
|
||
|
copy(b, a)
|
||
|
return b
|
||
|
}
|
||
|
|
||
|
// toNibbles mangles path by splitting every byte into 2 containing low- and high- 4-byte part.
|
||
|
func toNibbles(path []byte) []byte {
|
||
|
result := make([]byte, len(path)*2)
|
||
|
for i := range path {
|
||
|
result[i*2] = path[i] >> 4
|
||
|
result[i*2+1] = path[i] & 0x0F
|
||
|
}
|
||
|
return result
|
||
|
}
|