mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
support __contains__ for dict
This commit is contained in:
parent
b298e152e5
commit
45c70ffe79
@ -20,6 +20,9 @@ class Tuple:
|
||||
def __str__(self) -> str: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in list """
|
||||
|
||||
|
||||
class List(Tuple):
|
||||
def __init__(self): ...
|
||||
@ -41,9 +44,6 @@ class List(Tuple):
|
||||
def __add__(self, others: List) -> List:
|
||||
""" support list + list"""
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in list """
|
||||
|
||||
|
||||
class Dict:
|
||||
def __init__(self): ...
|
||||
@ -64,6 +64,9 @@ class Dict:
|
||||
def keys(self) -> dict_keys: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in list """
|
||||
|
||||
|
||||
class dict_keys:
|
||||
def __iter__(self) -> any: ...
|
||||
|
@ -172,3 +172,23 @@ int PikaStdData_dict_keys___len__(PikaObj* self) {
|
||||
PikaDict* keys = obj_getPtr(dictptr, "_keys");
|
||||
return args_getSize(&keys->super);
|
||||
}
|
||||
|
||||
int dict_contains(PikaDict* dict, Arg* key) {
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item = args_getArgByidex(&dict->super, i);
|
||||
if (NULL == item) {
|
||||
break;
|
||||
}
|
||||
if (arg_isEqual(item, key)) {
|
||||
return PIKA_TRUE;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
|
||||
int PikaStdData_Dict___contains__(PikaObj *self, Arg* val){
|
||||
PikaDict* dict = obj_getPtr(self, "_keys");
|
||||
return dict_contains(dict, val);
|
||||
}
|
||||
|
@ -77,14 +77,3 @@ PikaObj* PikaStdData_List___add__(PikaObj* self, PikaObj* others) {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int PikaStdData_List___contains__(PikaObj* self, Arg* val) {
|
||||
PikaList* list = obj_getPtr(self, "list");
|
||||
for (size_t i = 0; i < list_getSize(list); i++) {
|
||||
Arg* arg = list_getArg(list, i);
|
||||
if (arg_isEqual(arg, val)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -74,3 +74,14 @@ char* PikaStdData_Tuple___str__(PikaObj* self) {
|
||||
int PikaStdData_Tuple___len__(PikaObj* self) {
|
||||
return PikaStdData_Tuple_len(self);
|
||||
}
|
||||
|
||||
int PikaStdData_Tuple___contains__(PikaObj* self, Arg* val) {
|
||||
PikaList* list = obj_getPtr(self, "list");
|
||||
for (size_t i = 0; i < list_getSize(list); i++) {
|
||||
Arg* arg = list_getArg(list, i);
|
||||
if (arg_isEqual(arg, val)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
// "--gtest_filter=re.search"
|
||||
"--gtest_filter=dict.in"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
3
port/linux/.vscode/settings.json
vendored
3
port/linux/.vscode/settings.json
vendored
@ -18,6 +18,7 @@
|
||||
"inet.h": "c",
|
||||
"re.h": "c",
|
||||
"cre.h": "c",
|
||||
"pcre.h": "c"
|
||||
"pcre.h": "c",
|
||||
"pikastddata_dict.h": "c"
|
||||
}
|
||||
}
|
@ -20,6 +20,9 @@ class Tuple:
|
||||
def __str__(self) -> str: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in list """
|
||||
|
||||
|
||||
class List(Tuple):
|
||||
def __init__(self): ...
|
||||
@ -41,9 +44,6 @@ class List(Tuple):
|
||||
def __add__(self, others: List) -> List:
|
||||
""" support list + list"""
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in list """
|
||||
|
||||
|
||||
class Dict:
|
||||
def __init__(self): ...
|
||||
@ -64,6 +64,9 @@ class Dict:
|
||||
def keys(self) -> dict_keys: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in list """
|
||||
|
||||
|
||||
class dict_keys:
|
||||
def __iter__(self) -> any: ...
|
||||
|
@ -172,3 +172,23 @@ int PikaStdData_dict_keys___len__(PikaObj* self) {
|
||||
PikaDict* keys = obj_getPtr(dictptr, "_keys");
|
||||
return args_getSize(&keys->super);
|
||||
}
|
||||
|
||||
int dict_contains(PikaDict* dict, Arg* key) {
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item = args_getArgByidex(&dict->super, i);
|
||||
if (NULL == item) {
|
||||
break;
|
||||
}
|
||||
if (arg_isEqual(item, key)) {
|
||||
return PIKA_TRUE;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
|
||||
int PikaStdData_Dict___contains__(PikaObj *self, Arg* val){
|
||||
PikaDict* dict = obj_getPtr(self, "_keys");
|
||||
return dict_contains(dict, val);
|
||||
}
|
||||
|
@ -77,14 +77,3 @@ PikaObj* PikaStdData_List___add__(PikaObj* self, PikaObj* others) {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int PikaStdData_List___contains__(PikaObj* self, Arg* val) {
|
||||
PikaList* list = obj_getPtr(self, "list");
|
||||
for (size_t i = 0; i < list_getSize(list); i++) {
|
||||
Arg* arg = list_getArg(list, i);
|
||||
if (arg_isEqual(arg, val)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -74,3 +74,14 @@ char* PikaStdData_Tuple___str__(PikaObj* self) {
|
||||
int PikaStdData_Tuple___len__(PikaObj* self) {
|
||||
return PikaStdData_Tuple_len(self);
|
||||
}
|
||||
|
||||
int PikaStdData_Tuple___contains__(PikaObj* self, Arg* val) {
|
||||
PikaList* list = obj_getPtr(self, "list");
|
||||
for (size_t i = 0; i < list_getSize(list); i++) {
|
||||
Arg* arg = list_getArg(list, i);
|
||||
if (arg_isEqual(arg, val)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -122,4 +122,24 @@ TEST(list, in){
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(dict, in){
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"if \"a\" in {\"a\":1, \"b\":2}:\n"
|
||||
" print('a in dict')\n"
|
||||
);
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[0], "a in dict\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -458,18 +458,19 @@ PIKA_BOOL arg_isEqual(Arg* self, Arg* other) {
|
||||
if (arg_getType(self) != arg_getType(other)) {
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
if (arg_getContentSize(self) != arg_getContentSize(other)) {
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
if (arg_getType(self) == ARG_TYPE_OBJECT) {
|
||||
if (arg_getPtr(self) != arg_getPtr(other)) {
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
} else {
|
||||
if (0 != __platform_memcmp(arg_getContent(self), arg_getContent(other),
|
||||
arg_getContentSize(self))) {
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
if (arg_getType(self) == ARG_TYPE_STRING){
|
||||
if (strEqu(arg_getStr(self), arg_getStr(other))){
|
||||
return PIKA_TRUE;
|
||||
}
|
||||
}
|
||||
if (0 != __platform_memcmp(arg_getContent(self), arg_getContent(other),
|
||||
arg_getContentSize(self))) {
|
||||
return PIKA_FALSE;
|
||||
}
|
||||
return PIKA_TRUE;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user