support str() from bytes to string

This commit is contained in:
pikastech 2022-06-10 11:34:46 +08:00
parent f0b5be9c92
commit 2afaea6e7c
4 changed files with 68 additions and 2 deletions

View File

@ -116,6 +116,10 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
res = strsFormat(&buffs, 11, "%f", val);
goto exit;
}
if (ARG_TYPE_BYTES == type) {
res = (char*)arg_getBytes(arg);
goto exit;
}
if (argType_isObject(type)) {
res = obj_toStr(arg_getPtr(arg));
if (NULL != res) {

View File

@ -116,6 +116,10 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
res = strsFormat(&buffs, 11, "%f", val);
goto exit;
}
if (ARG_TYPE_BYTES == type) {
res = (char*)arg_getBytes(arg);
goto exit;
}
if (argType_isObject(type)) {
res = obj_toStr(arg_getPtr(arg));
if (NULL != res) {

View File

@ -0,0 +1,57 @@
#include "gtest/gtest.h"
#include "test_common.h"
extern "C" {
#include "PikaMain.h"
#include "PikaParser.h"
#include "PikaStdLib_MemChecker.h"
#include "PikaVM.h"
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataStrs.h"
#include "pikaScript.h"
#include "pika_config_gtest.h"
}
extern PikaMemInfo pikaMemInfo;
/* the log_buff of printf */
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
TEST(ctypes, test1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
obj_run(pikaMain,
"read_data = ctypes.c_buffer(b'', 16)\n"
"print(\"----read size----\")\n"
"datalen = len(read_data.raw)\n"
"print(datalen)\n"
"print(read_data.raw)\n"
"print(\"----read data----\")\n"
"for i in range(0,datalen):\n"
" print(read_data.raw[i])\n"
"\n");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[67], "16\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(ctypes, test2) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
obj_run(pikaMain,
"len(b'test\\x0')\n"
"len(str(b'test\\x0'))\n");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "4\r\n");
EXPECT_STREQ(log_buff[1], "5\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -129,12 +129,13 @@ Arg* arg_setType(Arg* self, ArgType type) {
}
Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) {
self = arg_newContent(self, size + sizeof(size_t));
self = arg_newContent(self, size + sizeof(size_t) + 1);
self = arg_setName(self, name);
self = arg_setType(self, ARG_TYPE_BYTES);
void* dir = arg_getContent(self);
/* set content all to 0 */
__platform_memset(dir, 0, size + sizeof(size_t));
__platform_memset(dir, 0, size + sizeof(size_t) + 1);
/* setsize */
__platform_memcpy(dir, &size, sizeof(size_t));
/* set init value */