Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gostnops
Project
Project
Details
Activity
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
wei.xuan
gostnops
Commits
11185138
Commit
11185138
authored
Nov 30, 2019
by
zhanghuiren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make big.Int embed
parent
7ebd8ffe
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
25 deletions
+26
-25
integer.go
math/big/integer.go
+11
-25
integer_test.go
math/big/integer_test.go
+15
-0
No files found.
math/big/integer.go
View file @
11185138
...
@@ -25,7 +25,7 @@ import (
...
@@ -25,7 +25,7 @@ import (
// Integer represents a integer value.
// Integer represents a integer value.
type
Integer
struct
{
type
Integer
struct
{
big
Int
big
.
Int
big
.
Int
// only used for hessian
// only used for hessian
// You Should not use it in go
// You Should not use it in go
...
@@ -40,16 +40,16 @@ type Integer struct {
...
@@ -40,16 +40,16 @@ type Integer struct {
}
}
func
(
Integer
)
JavaClassName
()
string
{
func
(
Integer
)
JavaClassName
()
string
{
return
"java.math.
Big
Integer"
return
"java.math.Integer"
}
}
// FromString set data from a 10-bases number
// FromString set data from a 10-bases number
func
(
i
*
Integer
)
FromString
(
s
string
)
error
{
func
(
i
*
Integer
)
FromString
(
s
string
)
(
err
error
)
{
intPtr
,
ok
:=
i
.
bigInt
.
SetString
(
s
,
10
)
_
,
ok
:=
i
.
SetString
(
s
,
10
)
if
!
ok
||
intPtr
==
nil
{
if
!
ok
{
return
fmt
.
Errorf
(
"'%s' is not a 10-based number"
,
s
)
err
=
fmt
.
Errorf
(
"'%s' is not a 10-based number"
,
s
)
}
}
return
nil
return
}
}
// FromSignAndMag set data from a array of big-endian unsigned uint32, it's used in hessian decoding
// FromSignAndMag set data from a array of big-endian unsigned uint32, it's used in hessian decoding
...
@@ -66,18 +66,18 @@ func (i *Integer) FromSignAndMag(signum int32, mag []int) {
...
@@ -66,18 +66,18 @@ func (i *Integer) FromSignAndMag(signum int32, mag []int) {
for
j
:=
0
;
j
<
len
(
i
.
Mag
);
j
++
{
for
j
:=
0
;
j
<
len
(
i
.
Mag
);
j
++
{
binary
.
BigEndian
.
PutUint32
(
bytes
[
j
*
4
:
(
j
+
1
)
*
4
],
uint32
(
i
.
Mag
[
j
]))
binary
.
BigEndian
.
PutUint32
(
bytes
[
j
*
4
:
(
j
+
1
)
*
4
],
uint32
(
i
.
Mag
[
j
]))
}
}
i
.
bigInt
=
*
i
.
bigInt
.
SetBytes
(
bytes
)
i
.
SetBytes
(
bytes
)
if
i
.
Signum
==
-
1
{
if
i
.
Signum
==
-
1
{
i
.
bigInt
.
Neg
(
&
i
.
big
Int
)
i
.
Neg
(
&
i
.
Int
)
}
}
}
}
// GetSignAndMag is used in hessian encoding
// GetSignAndMag is used in hessian encoding
func
(
i
*
Integer
)
GetSignAndMag
()
(
signum
int32
,
mag
[]
int
)
{
func
(
i
*
Integer
)
GetSignAndMag
()
(
signum
int32
,
mag
[]
int
)
{
signum
=
int32
(
i
.
bigInt
.
Sign
())
signum
=
int32
(
i
.
Sign
())
bytes
:=
i
.
bigInt
.
Bytes
()
bytes
:=
i
.
Bytes
()
outOf4
:=
len
(
bytes
)
%
4
outOf4
:=
len
(
bytes
)
%
4
if
outOf4
>
0
{
if
outOf4
>
0
{
bytes
=
append
(
make
([]
byte
,
4
-
outOf4
),
bytes
...
)
bytes
=
append
(
make
([]
byte
,
4
-
outOf4
),
bytes
...
)
...
@@ -93,17 +93,3 @@ func (i *Integer) GetSignAndMag() (signum int32, mag []int) {
...
@@ -93,17 +93,3 @@ func (i *Integer) GetSignAndMag() (signum int32, mag []int) {
return
return
}
}
// GetBigInt getter
func
(
i
*
Integer
)
GetBigInt
()
big
.
Int
{
return
i
.
bigInt
}
// SetBigInt setter
func
(
i
*
Integer
)
SetBigInt
(
bigInt
big
.
Int
)
{
i
.
bigInt
=
bigInt
}
func
(
i
*
Integer
)
String
()
string
{
return
i
.
bigInt
.
String
()
}
math/big/integer_test.go
View file @
11185138
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
package
gxbig
package
gxbig
import
(
import
(
"encoding/json"
"reflect"
"reflect"
"testing"
"testing"
)
)
...
@@ -109,3 +110,17 @@ func TestInteger_FromSignAndMag(t *testing.T) {
...
@@ -109,3 +110,17 @@ func TestInteger_FromSignAndMag(t *testing.T) {
})
})
}
}
}
}
func
TestInteger_Json
(
t
*
testing
.
T
)
{
i
:=
new
(
Integer
)
i
.
FromString
(
`1234567`
)
bytes
,
err
:=
json
.
Marshal
(
i
.
String
())
if
err
!=
nil
||
string
(
bytes
)
!=
`"1234567"`
{
t
.
Error
(
string
(
bytes
),
err
)
}
err
=
json
.
Unmarshal
([]
byte
(
`345`
),
i
)
if
err
!=
nil
||
i
.
String
()
!=
`345`
{
t
.
Error
(
i
,
err
)
}
}
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