mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
support len() for string, support int() for string
This commit is contained in:
parent
e5d07cf0ce
commit
757f00893a
@ -46,6 +46,9 @@ class SysObj(BaseObj):
|
||||
def __get__(self, obj: any, key: any) -> any:
|
||||
pass
|
||||
|
||||
def len(self, arg:any) -> int:
|
||||
pass
|
||||
|
||||
|
||||
class RangeObj(TinyObj):
|
||||
def __next__(self) -> any:
|
||||
|
@ -72,6 +72,9 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
|
||||
if (ARG_TYPE_FLOAT == type) {
|
||||
return (int)arg_getFloat(arg);
|
||||
}
|
||||
if (ARG_TYPE_STRING == type) {
|
||||
return (int)fast_atoi(arg_getStr(arg));
|
||||
}
|
||||
obj_setSysOut(self, "[error] convert to int type faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return -999999999;
|
||||
@ -204,3 +207,12 @@ void PikaStdLib_SysObj___set__(PikaObj* self,
|
||||
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
|
||||
}
|
||||
}
|
||||
|
||||
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
|
||||
if (ARG_TYPE_STRING == arg_getType(arg)) {
|
||||
return strGetSize(arg_getStr(arg));
|
||||
}
|
||||
obj_setErrorCode(self, 1);
|
||||
__platform_printf("[Error] len: arg type not support\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -46,6 +46,9 @@ class SysObj(BaseObj):
|
||||
def __get__(self, obj: any, key: any) -> any:
|
||||
pass
|
||||
|
||||
def len(self, arg:any) -> int:
|
||||
pass
|
||||
|
||||
|
||||
class RangeObj(TinyObj):
|
||||
def __next__(self) -> any:
|
||||
|
@ -72,6 +72,9 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
|
||||
if (ARG_TYPE_FLOAT == type) {
|
||||
return (int)arg_getFloat(arg);
|
||||
}
|
||||
if (ARG_TYPE_STRING == type) {
|
||||
return (int)fast_atoi(arg_getStr(arg));
|
||||
}
|
||||
obj_setSysOut(self, "[error] convert to int type faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return -999999999;
|
||||
@ -204,3 +207,12 @@ void PikaStdLib_SysObj___set__(PikaObj* self,
|
||||
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
|
||||
}
|
||||
}
|
||||
|
||||
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
|
||||
if (ARG_TYPE_STRING == arg_getType(arg)) {
|
||||
return strGetSize(arg_getStr(arg));
|
||||
}
|
||||
obj_setErrorCode(self, 1);
|
||||
__platform_printf("[Error] len: arg type not support\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -1476,7 +1476,39 @@ TEST(pikaMain, class_args_err) {
|
||||
/* collect */
|
||||
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[4], (char*)"TypeError: PikaStdLib.MemChecker() takes no arguments\r\n");
|
||||
EXPECT_STREQ(
|
||||
log_buff[4],
|
||||
(char*)"TypeError: PikaStdLib.MemChecker() takes no arguments\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(pikaMain, int_) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
obj_run(pikaMain, (char*)"a = int('3')\n");
|
||||
/* collect */
|
||||
int a = obj_getInt(pikaMain, (char*)"a");
|
||||
/* assert */
|
||||
EXPECT_EQ(a, 3);
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(pikaMain, len_) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
obj_run(pikaMain, (char*)"a = len('331dd')\n");
|
||||
/* collect */
|
||||
int a = obj_getInt(pikaMain, (char*)"a");
|
||||
/* assert */
|
||||
EXPECT_EQ(a, 5);
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user