MCPcopy
hub / github.com/canopy-network/canopy / keyBytesFromStr

Function keyBytesFromStr

store/smt_test.go:2088–2108  ·  view source on GitHub ↗

keyBytesFromStr converts a string of binary bits to a byte slice CONTRACT: str must be < than 8 bits

(str string)

Source from the content-addressed store, hash-verified

2086// keyBytesFromStr converts a string of binary bits to a byte slice
2087// CONTRACT: str must be < than 8 bits
2088func keyBytesFromStr(str string) []byte {
2089 // create a new key from the byts
2090 var byts []byte
2091 if str != "" {
2092 leftPadding := 0
2093 for _, ch := range str {
2094 if ch != '0' {
2095 break
2096 }
2097 leftPadding++
2098 }
2099 if leftPadding == len(str) {
2100 leftPadding--
2101 }
2102 val, _ := strconv.ParseUint(str, 2, 8)
2103 // convert the bits to bytes now producing the key
2104 byts = []byte{byte(val), byte(leftPadding)}
2105 }
2106 // return the key bytes
2107 return byts
2108}

Callers 4

TestSetFunction · 0.85
TestDeleteFunction · 0.85
TestTraverseFunction · 0.85
newTestNodeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected