aboutsummaryrefslogtreecommitdiffstats
path: root/common/math/integer.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2017-03-08 06:19:44 +0800
committerFelix Lange <fjl@twurst.com>2017-03-23 22:58:42 +0800
commit04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76 (patch)
tree61cdf35a8b91b8d67cc34738cdd60253acdb9326 /common/math/integer.go
parent61d2150a0750a554250c3bf090ef994be6c060f0 (diff)
downloaddexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar
dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.gz
dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.bz2
dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.lz
dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.xz
dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.zst
dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.zip
common/math: add HexOrDecimal64, HexOrDecimal256
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) {