aboutsummaryrefslogtreecommitdiffstats
path: root/common/math/integer.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-03-23 23:36:38 +0800
committerGitHub <noreply@github.com>2017-03-23 23:36:38 +0800
commit8771c3061f340451d0966adcc547338a25f2231f (patch)
treec566cab81cf95a39f85fbe2c98a932af9495eb68 /common/math/integer.go
parent11e7a712f469fb24ddb88ecebcefab6ed8880eb8 (diff)
parent37dd9086ec491900311fc39837f4a62ef5fd3a4a (diff)
downloaddexon-8771c3061f340451d0966adcc547338a25f2231f.tar
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.gz
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.bz2
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.lz
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.xz
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.zst
dexon-8771c3061f340451d0966adcc547338a25f2231f.zip
Merge pull request #3794 from fjl/core-genesis-refactor
core: refactor genesis handling
Diffstat (limited to 'common/math/integer.go')
-rw-r--r--common/math/integer.go23
1 files changed, 22 insertions, 1 deletions
diff --git a/common/math/integer.go b/common/math/integer.go
index a3eeee27e..7eff4d3b0 100644
--- a/common/math/integer.go
+++ b/common/math/integer.go
@@ -16,7 +16,10 @@
package math
-import "strconv"
+import (
+ "fmt"
+ "strconv"
+)
const (
// Integer limit values.
@@ -34,6 +37,24 @@ const (
MaxUint64 = 1<<64 - 1
)
+// HexOrDecimal64 marshals uint64 as hex or decimal.
+type HexOrDecimal64 uint64
+
+// UnmarshalText implements encoding.TextUnmarshaler.
+func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
+ int, ok := ParseUint64(string(input))
+ if !ok {
+ return fmt.Errorf("invalid hex or decimal integer %q", input)
+ }
+ *i = HexOrDecimal64(int)
+ return nil
+}
+
+// MarshalText implements encoding.TextMarshaler.
+func (i HexOrDecimal64) MarshalText() ([]byte, error) {
+ return []byte(fmt.Sprintf("%#x", uint64(i))), nil
+}
+
// ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
// Leading zeros are accepted. The empty string parses as zero.
func ParseUint64(s string) (uint64, bool) {