提交 0c782c54 编写于 作者: A Andrianto Lie

Add support for the string literal

上级 55cfc7ae
......@@ -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");
......
......@@ -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;
......
......@@ -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();
};
......@@ -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;
}
......@@ -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.
先完成此消息的编辑!
想要评论请 注册