diff options
author | obscuren <geffobscura@gmail.com> | 2015-03-16 20:23:06 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-03-16 20:23:06 +0800 |
commit | 20b7162a6206e61a39d799a5adf84379c9c8c818 (patch) | |
tree | 3d0a748c128f197137c677fe27b169c51847014c | |
parent | dc864ee3a5baadc0197b8491d55e94bba52a30d9 (diff) | |
download | dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.tar dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.tar.gz dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.tar.bz2 dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.tar.lz dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.tar.xz dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.tar.zst dexon-20b7162a6206e61a39d799a5adf84379c9c8c818.zip |
Added functions to Hash & Address
* Set, SetString, SetBytes
* StringTo*, BytesTo*
-rw-r--r-- | common/types.go | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/common/types.go b/common/types.go index 911be9b9b..e0963a7c5 100644 --- a/common/types.go +++ b/common/types.go @@ -5,6 +5,25 @@ type ( Address [20]byte ) +var ( + zeroHash Hash + zeroAddress Address +) + +func BytesToHash(b []byte) Hash { + var h Hash + h.SetBytes(b) + return h +} +func StringToHash(s string) Hash { return BytesToHash([]byte(s)) } + +func BytesToAddress(b []byte) Address { + var a Address + a.SetBytes(b) + return a +} +func StringToAddress(s string) Address { return BytesToAddress([]byte(s)) } + // Don't use the default 'String' method in case we want to overwrite // Get the string representation of the underlying hash @@ -24,8 +43,16 @@ func (h Hash) SetBytes(b []byte) { } } +// Set string `s` to h. If s is larger than len(h) it will panic func (h Hash) SetString(s string) { h.SetBytes([]byte(s)) } +// Sets h to other +func (h Hash) Set(other Hash) { + for i, v := range other { + h[i] = v + } +} + // Get the string representation of the underlying address func (a Address) Str() string { return string(a[:]) @@ -42,4 +69,13 @@ func (a Address) SetBytes(b []byte) { a[i] = b[i] } } -func (a Address) SetString(s string) { h.SetBytes([]byte(a)) } + +// Set string `s` to a. If s is larger than len(a) it will panic +func (a Address) SetString(s string) { a.SetBytes([]byte(s)) } + +// Sets a to other +func (a Address) Set(other Address) { + for i, v := range other { + a[i] = v + } +} |