diff --git a/package/PikaStdLib/PikaStdData.pyi b/package/PikaStdLib/PikaStdData.pyi index a4360a32d..f5b553146 100644 --- a/package/PikaStdLib/PikaStdData.pyi +++ b/package/PikaStdLib/PikaStdData.pyi @@ -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: ... diff --git a/package/PikaStdLib/PikaStdData_Dict.c b/package/PikaStdLib/PikaStdData_Dict.c index 4d17ef125..cfe96a3c7 100644 --- a/package/PikaStdLib/PikaStdData_Dict.c +++ b/package/PikaStdLib/PikaStdData_Dict.c @@ -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); +} diff --git a/package/PikaStdLib/PikaStdData_List.c b/package/PikaStdLib/PikaStdData_List.c index 1cd169049..877de58ff 100644 --- a/package/PikaStdLib/PikaStdData_List.c +++ b/package/PikaStdLib/PikaStdData_List.c @@ -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; -} diff --git a/package/PikaStdLib/PikaStdData_Tuple.c b/package/PikaStdLib/PikaStdData_Tuple.c index 1684ef069..d2ac01b8b 100644 --- a/package/PikaStdLib/PikaStdData_Tuple.c +++ b/package/PikaStdLib/PikaStdData_Tuple.c @@ -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; +} diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 95d49ec18..14e46a4f9 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -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}", diff --git a/port/linux/.vscode/settings.json b/port/linux/.vscode/settings.json index 98631d636..f6febba16 100644 --- a/port/linux/.vscode/settings.json +++ b/port/linux/.vscode/settings.json @@ -18,6 +18,7 @@ "inet.h": "c", "re.h": "c", "cre.h": "c", - "pcre.h": "c" + "pcre.h": "c", + "pikastddata_dict.h": "c" } } \ No newline at end of file diff --git a/port/linux/package/pikascript/PikaStdData.pyi b/port/linux/package/pikascript/PikaStdData.pyi index a4360a32d..f5b553146 100644 --- a/port/linux/package/pikascript/PikaStdData.pyi +++ b/port/linux/package/pikascript/PikaStdData.pyi @@ -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: ... diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c index 4d17ef125..cfe96a3c7 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c @@ -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); +} diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c index 1cd169049..877de58ff 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c @@ -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; -} diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c index 1684ef069..d2ac01b8b 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c @@ -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; +} diff --git a/port/linux/test/stddata-test.cpp b/port/linux/test/stddata-test.cpp index 03bbd3125..db6ac7464 100644 --- a/port/linux/test/stddata-test.cpp +++ b/port/linux/test/stddata-test.cpp @@ -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 diff --git a/src/dataArg.c b/src/dataArg.c index 674139083..074608878 100644 --- a/src/dataArg.c +++ b/src/dataArg.c @@ -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; }