support decode() and encode()

for PikaStdData.String() and PikaStdData.ByteArray()

use example/BuiltIn/encode_decode.py

fix CI
This commit is contained in:
pikastech 2022-07-14 11:54:25 +08:00
parent ec3b86fee8
commit 8f9950d4ae
11 changed files with 218 additions and 53 deletions

View File

@ -0,0 +1,9 @@
from PikaStdData import String as string
from PikaStdData import ByteArray as bytearray
b = bytearray(b'test')
b_s = b.decode()
print(b_s)
s = string('test')
s_b = s.encode()
print(s_b)

View File

@ -64,6 +64,7 @@ class String(TinyObj):
# support str()
def __str__(self) -> str: ...
def __len__(self) -> int: ...
def encode(self) -> bytes: ...
def startwith(self, prefix: str) -> int: ...
def endwith(self, suffix: str) -> int: ...
@ -77,9 +78,18 @@ class String(TinyObj):
def strip(self) -> str: ...
class ByteArray(List):
class ByteArray(TinyObj):
# convert a string to ByteArray
def fromString(self, s: str): ...
def __init__(self, bytes: any): ...
# support for loop
def __iter__(self) -> any: ...
# support for loop
def __next__(self) -> any: ...
# support [] index
def __get__(self, __key: int) -> int: ...
def __set__(self, __key: int, __val: int): ...
def __str__(self) -> str: ...
def decode(self) -> str: ...
class Utils(TinyObj):

View File

@ -0,0 +1,71 @@
#include "PikaStdData_ByteArray.h"
void PikaStdData_ByteArray___init__(PikaObj* self, Arg* bytes) {
obj_setArg(self, "raw", bytes);
}
Arg* PikaStdData_ByteArray___iter__(PikaObj* self) {
obj_setInt(self, "__iter_i", 0);
return arg_setRef(NULL, "", self);
}
Arg* PikaStdData_ByteArray___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
Arg* res = NULL;
char char_buff[] = " ";
if (__iter_i < len) {
char_buff[0] = data[__iter_i];
res = arg_setInt(NULL, "", char_buff[0]);
} else {
return arg_setNull(NULL);
}
args_setInt(self->list, "__iter_i", __iter_i + 1);
return res;
}
int PikaStdData_ByteArray___get__(PikaObj* self, int __key) {
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
if (__key < len) {
return data[__key];
} else {
return 0;
}
}
void PikaStdData_ByteArray___set__(PikaObj* self, int __key, int __val) {
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
if (__key < len) {
data[__key] = __val;
}
}
char* PikaStdData_ByteArray___str__(PikaObj* self) {
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
Arg* str_arg = arg_setStr(NULL, "", "");
str_arg = arg_strAppend(str_arg, "bytearray(b'");
for (int i = 0; i < len; i++) {
char u8_str[] = "\\x00";
uint8_t u8 = data[i];
__platform_sprintf(u8_str, "\\x%02x", u8);
str_arg = arg_strAppend(str_arg, u8_str);
}
str_arg = arg_strAppend(str_arg, "')");
obj_removeArg(self, "_buf");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}
char* PikaStdData_ByteArray_decode(PikaObj* self) {
uint8_t* data = obj_getBytes(self, "raw");
Arg* str_arg = arg_setStr(NULL, "", (char*)data);
obj_removeArg(self, "_buf");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}

View File

@ -52,26 +52,6 @@ void PikaStdData_List___set__(PikaObj* self) {
obj_getInt(self, "__key"));
}
void PikaStdData_ByteArray_fromString(PikaObj* self, char* s) {
for (uint32_t i = 0; i < strGetSize(s); i++) {
obj_setInt(self, "__val", (int)s[i]);
PIKA_PYTHON_BEGIN
/* clang-format off */
PIKA_PYTHON(
append(__val)
)
/* clang-format on */
const uint8_t bytes[] = {
0x08, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, /* instruct array */
0x0e, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x76, 0x61, 0x6c, 0x00, 0x61,
0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, /* const pool */
};
pikaVM_runByteCode(self, (uint8_t*)bytes);
PIKA_PYTHON_END
}
}
void PikaStdData_List___del__(PikaObj* self) {
Args* list = obj_getPtr(self, "list");

View File

@ -210,3 +210,9 @@ char* PikaStdData_String_replace(PikaObj* self, char* new, char* old) {
strsDeinit(&buffs);
return obj_getStr(self, "_buf");
}
Arg* PikaStdData_String_encode(PikaObj* self) {
char* str = obj_getStr(self, "str");
Arg* arg = arg_setBytes(NULL, "", (uint8_t*)str, strGetSize(str));
return arg;
}

View File

@ -64,6 +64,7 @@ class String(TinyObj):
# support str()
def __str__(self) -> str: ...
def __len__(self) -> int: ...
def encode(self) -> bytes: ...
def startwith(self, prefix: str) -> int: ...
def endwith(self, suffix: str) -> int: ...
@ -77,9 +78,18 @@ class String(TinyObj):
def strip(self) -> str: ...
class ByteArray(List):
class ByteArray(TinyObj):
# convert a string to ByteArray
def fromString(self, s: str): ...
def __init__(self, bytes: any): ...
# support for loop
def __iter__(self) -> any: ...
# support for loop
def __next__(self) -> any: ...
# support [] index
def __get__(self, __key: int) -> int: ...
def __set__(self, __key: int, __val: int): ...
def __str__(self) -> str: ...
def decode(self) -> str: ...
class Utils(TinyObj):

View File

@ -0,0 +1,71 @@
#include "PikaStdData_ByteArray.h"
void PikaStdData_ByteArray___init__(PikaObj* self, Arg* bytes) {
obj_setArg(self, "raw", bytes);
}
Arg* PikaStdData_ByteArray___iter__(PikaObj* self) {
obj_setInt(self, "__iter_i", 0);
return arg_setRef(NULL, "", self);
}
Arg* PikaStdData_ByteArray___next__(PikaObj* self) {
int __iter_i = args_getInt(self->list, "__iter_i");
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
Arg* res = NULL;
char char_buff[] = " ";
if (__iter_i < len) {
char_buff[0] = data[__iter_i];
res = arg_setInt(NULL, "", char_buff[0]);
} else {
return arg_setNull(NULL);
}
args_setInt(self->list, "__iter_i", __iter_i + 1);
return res;
}
int PikaStdData_ByteArray___get__(PikaObj* self, int __key) {
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
if (__key < len) {
return data[__key];
} else {
return 0;
}
}
void PikaStdData_ByteArray___set__(PikaObj* self, int __key, int __val) {
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
if (__key < len) {
data[__key] = __val;
}
}
char* PikaStdData_ByteArray___str__(PikaObj* self) {
uint8_t* data = obj_getBytes(self, "raw");
uint16_t len = obj_getBytesSize(self, "raw");
Arg* str_arg = arg_setStr(NULL, "", "");
str_arg = arg_strAppend(str_arg, "bytearray(b'");
for (int i = 0; i < len; i++) {
char u8_str[] = "\\x00";
uint8_t u8 = data[i];
__platform_sprintf(u8_str, "\\x%02x", u8);
str_arg = arg_strAppend(str_arg, u8_str);
}
str_arg = arg_strAppend(str_arg, "')");
obj_removeArg(self, "_buf");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}
char* PikaStdData_ByteArray_decode(PikaObj* self) {
uint8_t* data = obj_getBytes(self, "raw");
Arg* str_arg = arg_setStr(NULL, "", (char*)data);
obj_removeArg(self, "_buf");
obj_setStr(self, "_buf", arg_getStr(str_arg));
arg_deinit(str_arg);
return obj_getStr(self, "_buf");
}

View File

@ -52,26 +52,6 @@ void PikaStdData_List___set__(PikaObj* self) {
obj_getInt(self, "__key"));
}
void PikaStdData_ByteArray_fromString(PikaObj* self, char* s) {
for (uint32_t i = 0; i < strGetSize(s); i++) {
obj_setInt(self, "__val", (int)s[i]);
PIKA_PYTHON_BEGIN
/* clang-format off */
PIKA_PYTHON(
append(__val)
)
/* clang-format on */
const uint8_t bytes[] = {
0x08, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, /* instruct array */
0x0e, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x76, 0x61, 0x6c, 0x00, 0x61,
0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, /* const pool */
};
pikaVM_runByteCode(self, (uint8_t*)bytes);
PIKA_PYTHON_END
}
}
void PikaStdData_List___del__(PikaObj* self) {
Args* list = obj_getPtr(self, "list");

View File

@ -210,3 +210,9 @@ char* PikaStdData_String_replace(PikaObj* self, char* new, char* old) {
strsDeinit(&buffs);
return obj_getStr(self, "_buf");
}
Arg* PikaStdData_String_encode(PikaObj* self) {
char* str = obj_getStr(self, "str");
Arg* arg = arg_setBytes(NULL, "", (uint8_t*)str, strGetSize(str));
return arg;
}

View File

@ -1404,17 +1404,17 @@ TEST(pikaMain, bytearray) {
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_run(pikaMain,
"bytes = PikaStdData.ByteArray()\n"
"bytes.fromString('test')\n"
"bytes = PikaStdData.ByteArray(b'test')\n"
"sum = 0\n"
"for byte in bytes:\n"
" sum = sum + byte\n"
"\n"
"print(sum)\n");
" sum += byte\n"
"print(bytes)\n"
"\n");
/* collect */
int sum = obj_getInt(pikaMain, "sum");
/* assert */
EXPECT_STREQ(log_buff[0], "448\r\n");
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
EXPECT_EQ(sum, 448);
EXPECT_STREQ(log_buff[0], "bytearray(b'\\x74\\x65\\x73\\x74')\r\n");
/* deinit */
obj_deinit(pikaMain);
/* mem check */

View File

@ -62,3 +62,25 @@ TEST(stddata, a2b_hex) {
/* deinit */
obj_deinit(pikaMain);
}
#if PIKA_SYNTAX_IMPORT_EX_ENABLE
TEST(stddata, encode_decode) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_runSingleFile(pikaMain, "../../examples/BuiltIn/encode_decode.py");
/* collect */
char* b_s = obj_getStr(pikaMain, "b_s");
uint8_t* s_b = obj_getBytes(pikaMain, "s_b");
/* assert */
EXPECT_STREQ(b_s, "test");
EXPECT_EQ(s_b[0], 't');
EXPECT_EQ(s_b[1], 'e');
EXPECT_EQ(s_b[2], 's');
EXPECT_EQ(s_b[3], 't');
/* deinit */
obj_deinit(pikaMain);
}
#endif