FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CovenantSQL/utils/integer.go at develop · sourcecreative/CovenantSQL · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
sourcecreative
/
CovenantSQL
Public
forked from
CovenantSQL/CovenantSQL
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
CovenantSQL
/
utils
/
integer.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (73 loc) · 2.48 KB
Breadcrumbs
CovenantSQL
/
utils
/
integer.go
Copy path
File metadata and controls
84 lines (73 loc) · 2.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
utils
import
(
"fmt"
"math"
"strconv"
)
// 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
) {
if
s
==
""
{
return
0
,
true
}
if
len
(
s
)
>=
2
&&
(
s
[:
2
]
==
"0x"
||
s
[:
2
]
==
"0X"
) {
v
,
err
:=
strconv
.
ParseUint
(
s
[
2
:],
16
,
64
)
return
v
,
err
==
nil
}
v
,
err
:=
strconv
.
ParseUint
(
s
,
10
,
64
)
return
v
,
err
==
nil
}
// MustParseUint64 parses s as an integer and panics if the string is invalid.
func
MustParseUint64
(
s
string
)
uint64
{
v
,
ok
:=
ParseUint64
(
s
)
if
!
ok
{
panic
(
"invalid unsigned 64 bit integer: "
+
s
)
}
return
v
}
// NOTE: The following methods need to be optimised using either bit checking or asm
// SafeSub returns subtraction result and whether overflow occurred.
func
SafeSub
(
x
,
y
uint64
) (
uint64
,
bool
) {
return
x
-
y
,
x
<
y
}
// SafeAdd returns the result and whether overflow occurred.
func
SafeAdd
(
x
,
y
uint64
) (
uint64
,
bool
) {
return
x
+
y
,
y
>
math
.
MaxUint64
-
x
}
// SafeMul returns multiplication result and whether overflow occurred.
func
SafeMul
(
x
,
y
uint64
) (
uint64
,
bool
) {
if
x
==
0
||
y
==
0
{
return
0
,
false
}
return
x
*
y
,
y
>
math
.
MaxUint64
/
x
}
Back
|
FazBrowse Home
|
New Git URL