Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
f7112cc1
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
f7112cc1
编写于
9月 14, 2020
作者:
F
Felix Lange
提交者:
GitHub
9月 14, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rlp: add SplitUint64 (#21563)
This can be useful when working with raw RLP data.
上级
71c37d82
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
0 deletion
+71
-0
rlp/raw.go
rlp/raw.go
+26
-0
rlp/raw_test.go
rlp/raw_test.go
+45
-0
未找到文件。
rlp/raw.go
浏览文件 @
f7112cc1
...
...
@@ -57,6 +57,32 @@ func SplitString(b []byte) (content, rest []byte, err error) {
return
content
,
rest
,
nil
}
// SplitUint64 decodes an integer at the beginning of b.
// It also returns the remaining data after the integer in 'rest'.
func
SplitUint64
(
b
[]
byte
)
(
x
uint64
,
rest
[]
byte
,
err
error
)
{
content
,
rest
,
err
:=
SplitString
(
b
)
if
err
!=
nil
{
return
0
,
b
,
err
}
switch
{
case
len
(
content
)
==
0
:
return
0
,
rest
,
nil
case
len
(
content
)
==
1
:
if
content
[
0
]
==
0
{
return
0
,
b
,
ErrCanonInt
}
return
uint64
(
content
[
0
]),
rest
,
nil
case
len
(
content
)
>
8
:
return
0
,
b
,
errUintOverflow
default
:
x
,
err
=
readSize
(
content
,
byte
(
len
(
content
)))
if
err
!=
nil
{
return
0
,
b
,
ErrCanonInt
}
return
x
,
rest
,
nil
}
}
// SplitList splits b into the content of a list and any remaining
// bytes after the list.
func
SplitList
(
b
[]
byte
)
(
content
,
rest
[]
byte
,
err
error
)
{
...
...
rlp/raw_test.go
浏览文件 @
f7112cc1
...
...
@@ -71,6 +71,49 @@ func TestSplitTypes(t *testing.T) {
}
}
func
TestSplitUint64
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
val
uint64
rest
string
err
error
}{
{
"01"
,
1
,
""
,
nil
},
{
"7FFF"
,
0x7F
,
"FF"
,
nil
},
{
"80FF"
,
0
,
"FF"
,
nil
},
{
"81FAFF"
,
0xFA
,
"FF"
,
nil
},
{
"82FAFAFF"
,
0xFAFA
,
"FF"
,
nil
},
{
"83FAFAFAFF"
,
0xFAFAFA
,
"FF"
,
nil
},
{
"84FAFAFAFAFF"
,
0xFAFAFAFA
,
"FF"
,
nil
},
{
"85FAFAFAFAFAFF"
,
0xFAFAFAFAFA
,
"FF"
,
nil
},
{
"86FAFAFAFAFAFAFF"
,
0xFAFAFAFAFAFA
,
"FF"
,
nil
},
{
"87FAFAFAFAFAFAFAFF"
,
0xFAFAFAFAFAFAFA
,
"FF"
,
nil
},
{
"88FAFAFAFAFAFAFAFAFF"
,
0xFAFAFAFAFAFAFAFA
,
"FF"
,
nil
},
// errors
{
""
,
0
,
""
,
io
.
ErrUnexpectedEOF
},
{
"00"
,
0
,
"00"
,
ErrCanonInt
},
{
"81"
,
0
,
"81"
,
ErrValueTooLarge
},
{
"8100"
,
0
,
"8100"
,
ErrCanonSize
},
{
"8200FF"
,
0
,
"8200FF"
,
ErrCanonInt
},
{
"8103FF"
,
0
,
"8103FF"
,
ErrCanonSize
},
{
"89FAFAFAFAFAFAFAFAFAFF"
,
0
,
"89FAFAFAFAFAFAFAFAFAFF"
,
errUintOverflow
},
}
for
i
,
test
:=
range
tests
{
val
,
rest
,
err
:=
SplitUint64
(
unhex
(
test
.
input
))
if
val
!=
test
.
val
{
t
.
Errorf
(
"test %d: val mismatch: got %x, want %x (input %q)"
,
i
,
val
,
test
.
val
,
test
.
input
)
}
if
!
bytes
.
Equal
(
rest
,
unhex
(
test
.
rest
))
{
t
.
Errorf
(
"test %d: rest mismatch: got %x, want %s (input %q)"
,
i
,
rest
,
test
.
rest
,
test
.
input
)
}
if
err
!=
test
.
err
{
t
.
Errorf
(
"test %d: error mismatch: got %q, want %q"
,
i
,
err
,
test
.
err
)
}
}
}
func
TestSplit
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
...
...
@@ -78,7 +121,9 @@ func TestSplit(t *testing.T) {
val
,
rest
string
err
error
}{
{
input
:
"00FFFF"
,
kind
:
Byte
,
val
:
"00"
,
rest
:
"FFFF"
},
{
input
:
"01FFFF"
,
kind
:
Byte
,
val
:
"01"
,
rest
:
"FFFF"
},
{
input
:
"7FFFFF"
,
kind
:
Byte
,
val
:
"7F"
,
rest
:
"FFFF"
},
{
input
:
"80FFFF"
,
kind
:
String
,
val
:
""
,
rest
:
"FFFF"
},
{
input
:
"C3010203"
,
kind
:
List
,
val
:
"010203"
},
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录