Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
YottaChain
YTBP
提交
0c782c54
Y
YTBP
项目概览
YottaChain
/
YTBP
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
YTBP
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0c782c54
编写于
11月 01, 2017
作者:
A
Andrianto Lie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for the string literal
上级
55cfc7ae
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
77 addition
and
3 deletion
+77
-3
contracts/eoslib/string.hpp
contracts/eoslib/string.hpp
+48
-1
contracts/test_api/test_api.cpp
contracts/test_api/test_api.cpp
+1
-0
contracts/test_api/test_api.hpp
contracts/test_api/test_api.hpp
+1
-0
contracts/test_api/test_string.cpp
contracts/test_api/test_string.cpp
+25
-1
tests/api_tests/api_tests.cpp
tests/api_tests/api_tests.cpp
+2
-1
未找到文件。
contracts/eoslib/string.hpp
浏览文件 @
0c782c54
...
...
@@ -6,7 +6,21 @@
#include <eoslib/print.hpp>
namespace
eosio
{
/**
* @brief Count the length of null terminated string (excluding the null terminated symbol)
* Non-null terminated string need to be passed here,
* Otherwise it will not give the right length
* @param cstr - null terminated string
*/
inline
uint32_t
cstrlen
(
const
char
*
cstr
)
{
uint32_t
len
=
0
;
while
(
*
cstr
!=
'\0'
)
{
len
++
;
cstr
++
;
}
return
len
;
}
class
string
{
private:
...
...
@@ -63,6 +77,21 @@ namespace eosio {
}
}
/**
* @brief Constructor for string literal
* Non-null terminated string need to be passed here,
* Otherwise it will have extraneous data
* @param cstr - null terminated string
*/
string
(
const
char
*
cstr
)
{
size
=
cstrlen
(
cstr
)
+
1
;
data
=
(
char
*
)
malloc
(
size
*
sizeof
(
char
));
memcpy
(
data
,
cstr
,
size
*
sizeof
(
char
));
own_memory
=
true
;
refcount
=
(
uint32_t
*
)
malloc
(
sizeof
(
uint32_t
));
*
refcount
=
1
;
}
// Destructor
~
string
()
{
release_data_if_needed
();
...
...
@@ -131,6 +160,7 @@ namespace eosio {
return
*
(
data
+
index
);
}
// Assignment operator
string
&
operator
=
(
const
string
&
obj
)
{
if
(
this
!=
&
obj
)
{
release_data_if_needed
();
...
...
@@ -143,6 +173,23 @@ namespace eosio {
return
*
this
;
}
/**
* @brief Assignment operator for string literal
* Non-null terminated string need to be passed here,
* Otherwise it will have extraneous data
* @param cstr - null terminated string
*/
string
&
operator
=
(
const
char
*
cstr
)
{
release_data_if_needed
();
size
=
cstrlen
(
cstr
)
+
1
;
data
=
(
char
*
)
malloc
(
size
*
sizeof
(
char
));
memcpy
(
data
,
cstr
,
size
*
sizeof
(
char
));
own_memory
=
true
;
refcount
=
(
uint32_t
*
)
malloc
(
sizeof
(
uint32_t
));
*
refcount
=
1
;
return
*
this
;
}
string
&
operator
+=
(
const
string
&
str
){
assert
((
size
+
str
.
size
>
size
)
&&
(
size
+
str
.
size
>
str
.
size
),
"overflow"
);
...
...
contracts/test_api/test_api.cpp
浏览文件 @
0c782c54
...
...
@@ -111,6 +111,7 @@ extern "C" {
WASM_TEST_HANDLER
(
test_string
,
print_unicode
);
WASM_TEST_HANDLER
(
test_string
,
valid_utf8
);
WASM_TEST_HANDLER
(
test_string
,
invalid_utf8
);
WASM_TEST_HANDLER
(
test_string
,
string_literal
);
//unhandled test call
WASM_TEST_ERROR_CODE
=
WASM_TEST_FAIL
;
...
...
contracts/test_api/test_api.hpp
浏览文件 @
0c782c54
...
...
@@ -149,4 +149,5 @@ struct test_string {
static
unsigned
int
print_unicode
();
static
unsigned
int
valid_utf8
();
static
unsigned
int
invalid_utf8
();
static
unsigned
int
string_literal
();
};
contracts/test_api/test_string.cpp
浏览文件 @
0c782c54
...
...
@@ -291,7 +291,6 @@ unsigned int test_string::print_unicode() {
return
WASM_TEST_PASS
;
}
unsigned
int
test_string
::
valid_utf8
()
{
// Roman alphabet is 1 byte UTF-8
char
data
[]
=
"abcdefghij"
;
...
...
@@ -330,3 +329,28 @@ unsigned int test_string::invalid_utf8() {
return
WASM_TEST_PASS
;
}
unsigned
int
test_string
::
string_literal
()
{
// Construct
char
data1
[]
=
"abcdefghij"
;
char
data2
[]
=
"klmnopqrstuvwxyz"
;
eos
::
string
str
=
"abcdefghij"
;
WASM_ASSERT
(
str
.
get_size
()
==
11
,
"data1 str.get_size() == 11"
);
for
(
uint8_t
i
=
0
;
i
<
11
;
i
++
)
{
WASM_ASSERT
(
str
[
i
]
==
data1
[
i
],
"data1 str[i] == data1[i]"
);
}
WASM_ASSERT
(
str
.
is_own_memory
()
==
true
,
"data1 str.is_own_memory() == true"
);
str
=
"klmnopqrstuvwxyz"
;
WASM_ASSERT
(
str
.
get_size
()
==
17
,
"data2 str.get_size() == 17"
);
for
(
uint8_t
i
=
0
;
i
<
17
;
i
++
)
{
WASM_ASSERT
(
str
[
i
]
==
data2
[
i
],
"data2 str[i] == data2[i]"
);
}
WASM_ASSERT
(
str
.
is_own_memory
()
==
true
,
"data2 str.is_own_memory() == true"
);
return
WASM_TEST_PASS
;
}
tests/api_tests/api_tests.cpp
浏览文件 @
0c782c54
...
...
@@ -458,7 +458,8 @@ BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
BOOST_CHECK_EQUAL
(
capture
[
0
],
"你好,世界!"
);
BOOST_CHECK_MESSAGE
(
CALL_TEST_FUNCTION
(
TEST_METHOD
(
"test_string"
,
"valid_utf8"
),
{},
{}
)
==
WASM_TEST_PASS
,
"test_string::valid_utf8()"
);
BOOST_CHECK_EXCEPTION
(
CALL_TEST_FUNCTION
(
TEST_METHOD
(
"test_string"
,
"invalid_utf8"
),
{},
{}
),
fc
::
assert_exception
,
is_assert_exception
);
BOOST_CHECK_MESSAGE
(
CALL_TEST_FUNCTION
(
TEST_METHOD
(
"test_string"
,
"string_literal"
),
{},
{}
)
==
WASM_TEST_PASS
,
"test_string::string_literal()"
);
}
FC_LOG_AND_RETHROW
()
}
#define RUN_CODE_HANDLER_WITH_TRANSFER(account_name, test_wast) \
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录