Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
04fa6a37
Commit
04fa6a37
authored
Mar 07, 2017
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common/math: add HexOrDecimal64, HexOrDecimal256
parent
61d2150a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
12 deletions
+54
-12
big.go
common/math/big.go
+19
-0
big_test.go
common/math/big_test.go
+7
-6
integer.go
common/math/integer.go
+22
-1
integer_test.go
common/math/integer_test.go
+6
-5
No files found.
common/math/big.go
View file @
04fa6a37
...
...
@@ -18,6 +18,7 @@
package
math
import
(
"fmt"
"math/big"
)
...
...
@@ -35,6 +36,24 @@ const (
wordBytes
=
wordBits
/
8
)
// HexOrDecimal256 marshals big.Int as hex or decimal.
type
HexOrDecimal256
big
.
Int
// UnmarshalText implements encoding.TextUnmarshaler.
func
(
i
*
HexOrDecimal256
)
UnmarshalText
(
input
[]
byte
)
error
{
bigint
,
ok
:=
ParseBig256
(
string
(
input
))
if
!
ok
{
return
fmt
.
Errorf
(
"invalid hex or decimal integer %q"
,
input
)
}
*
i
=
HexOrDecimal256
(
*
bigint
)
return
nil
}
// MarshalText implements encoding.TextMarshaler.
func
(
i
*
HexOrDecimal256
)
MarshalText
()
([]
byte
,
error
)
{
return
[]
byte
(
fmt
.
Sprintf
(
"%#x"
,
(
*
big
.
Int
)(
i
))),
nil
}
// ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax.
// Leading zeros are accepted. The empty string parses as zero.
func
ParseBig256
(
s
string
)
(
*
big
.
Int
,
bool
)
{
...
...
common/math/big_test.go
View file @
04fa6a37
...
...
@@ -23,7 +23,7 @@ import (
"testing"
)
func
Test
ParseBig
256
(
t
*
testing
.
T
)
{
func
Test
HexOrDecimal
256
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
num
*
big
.
Int
...
...
@@ -47,13 +47,14 @@ func TestParseBig256(t *testing.T) {
{
"115792089237316195423570985008687907853269984665640564039457584007913129639936"
,
nil
,
false
},
}
for
_
,
test
:=
range
tests
{
num
,
ok
:=
ParseBig256
(
test
.
input
)
if
ok
!=
test
.
ok
{
t
.
Errorf
(
"ParseBig(%q) -> ok = %t, want %t"
,
test
.
input
,
ok
,
test
.
ok
)
var
num
HexOrDecimal256
err
:=
num
.
UnmarshalText
([]
byte
(
test
.
input
))
if
(
err
==
nil
)
!=
test
.
ok
{
t
.
Errorf
(
"ParseBig(%q) -> (err == nil) == %t, want %t"
,
test
.
input
,
err
==
nil
,
test
.
ok
)
continue
}
if
num
!=
nil
&&
test
.
num
!=
nil
&&
num
.
Cmp
(
test
.
num
)
!=
0
{
t
.
Errorf
(
"ParseBig(%q) -> %d, want %d"
,
test
.
input
,
num
,
test
.
num
)
if
test
.
num
!=
nil
&&
(
*
big
.
Int
)(
&
num
)
.
Cmp
(
test
.
num
)
!=
0
{
t
.
Errorf
(
"ParseBig(%q) -> %d, want %d"
,
test
.
input
,
(
*
big
.
Int
)(
&
num
)
,
test
.
num
)
}
}
}
...
...
common/math/integer.go
View file @
04fa6a37
...
...
@@ -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
)
{
...
...
common/math/integer_test.go
View file @
04fa6a37
...
...
@@ -65,7 +65,7 @@ func TestOverflow(t *testing.T) {
}
}
func
Test
ParseUint
64
(
t
*
testing
.
T
)
{
func
Test
HexOrDecimal
64
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
num
uint64
...
...
@@ -88,12 +88,13 @@ func TestParseUint64(t *testing.T) {
{
"18446744073709551617"
,
0
,
false
},
}
for
_
,
test
:=
range
tests
{
num
,
ok
:=
ParseUint64
(
test
.
input
)
if
ok
!=
test
.
ok
{
t
.
Errorf
(
"ParseUint64(%q) -> ok = %t, want %t"
,
test
.
input
,
ok
,
test
.
ok
)
var
num
HexOrDecimal64
err
:=
num
.
UnmarshalText
([]
byte
(
test
.
input
))
if
(
err
==
nil
)
!=
test
.
ok
{
t
.
Errorf
(
"ParseUint64(%q) -> (err == nil) = %t, want %t"
,
test
.
input
,
err
==
nil
,
test
.
ok
)
continue
}
if
ok
&&
num
!=
test
.
num
{
if
err
==
nil
&&
uint64
(
num
)
!=
test
.
num
{
t
.
Errorf
(
"ParseUint64(%q) -> %d, want %d"
,
test
.
input
,
num
,
test
.
num
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment