73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
package eth
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"math/big"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func parseSimpleNumericProperty(data string) *big.Int {
|
|
if has0xPrefix(data) {
|
|
data = data[2:]
|
|
}
|
|
if len(data) > 64 {
|
|
data = data[:64]
|
|
}
|
|
if len(data) == 64 {
|
|
var n big.Int
|
|
_, ok := n.SetString(data, 16)
|
|
if ok {
|
|
return &n
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseSimpleStringProperty(data string) string {
|
|
if has0xPrefix(data) {
|
|
data = data[2:]
|
|
}
|
|
if len(data) > 128 {
|
|
n := parseSimpleNumericProperty(data[64:128])
|
|
if n != nil {
|
|
l := n.Uint64()
|
|
if l > 0 && 2*int(l) <= len(data)-128 {
|
|
b, err := hex.DecodeString(data[128 : 128+2*l])
|
|
if err == nil {
|
|
return string(b)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// allow string properties as UTF-8 data
|
|
b, err := hex.DecodeString(data)
|
|
if err == nil {
|
|
i := bytes.Index(b, []byte{0})
|
|
if i > 32 {
|
|
i = 32
|
|
}
|
|
if i > 0 {
|
|
b = b[:i]
|
|
}
|
|
if utf8.Valid(b) {
|
|
return string(b)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func Decamel(s string) string {
|
|
var b bytes.Buffer
|
|
splittable := false
|
|
for _, v := range s {
|
|
if splittable && unicode.IsUpper(v) {
|
|
b.WriteByte(' ')
|
|
}
|
|
b.WriteRune(v)
|
|
splittable = unicode.IsLower(v) || unicode.IsNumber(v)
|
|
}
|
|
return b.String()
|
|
}
|