From b19e20509579c3b214d84bac23d9bb7460ae2cd7 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Fri, 15 Nov 2024 18:03:56 +0800 Subject: [PATCH] add tk_strs_bsearch --- docs/changes.md | 1 + src/tkc/types_def.h | 2 ++ src/tkc/utils.c | 32 ++++++++++++++++++++++++++++++++ src/tkc/utils.h | 11 +++++++++++ src/tkc/wasm_helper.h | 4 ++++ tests/utils_test.cc | 19 +++++++++++++++++++ 6 files changed, 69 insertions(+) diff --git a/docs/changes.md b/docs/changes.md index 8006182f44..0c8c552955 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -3,6 +3,7 @@ 2024/11/15 * 修复拼写错误(感谢兆坤提供补丁) * 修复 demoui dialog(感谢兆坤提供补丁) + * 增加tk_strs_bsearch 2024/11/14 * 修复 edit键盘长按Backspace键,弹出提示窗口后就不能连续删除字符(感谢兆坤提供补丁) diff --git a/src/tkc/types_def.h b/src/tkc/types_def.h index ec25c7e466..bdb9c94c98 100644 --- a/src/tkc/types_def.h +++ b/src/tkc/types_def.h @@ -573,6 +573,8 @@ typedef struct _event_source_manager_t event_source_manager_t; #define TK_VALUE_UNDEFINED "undefined" +#define TK_ADR_EQ(p1, p2) (((uint8_t*)(p1)) - ((uint8_t*)(p2)) == 0) + typedef struct _key_type_value_t { char* name; uint32_t type; diff --git a/src/tkc/utils.c b/src/tkc/utils.c index 2038db827a..cf2906288d 100644 --- a/src/tkc/utils.c +++ b/src/tkc/utils.c @@ -2608,6 +2608,38 @@ ret_t tk_mergesort(void* base, size_t nmemb, size_t size, tk_compare_t cmp) { return RET_OK; } +const char* tk_strs_bsearch(const char** strs, uint32_t nr, const char* str, bool_t case_sensitive) { + int32_t low = 0; + int32_t mid = 0; + int32_t high = 0; + int32_t result = 0; + const char* iter = NULL; + tk_compare_t cmp = case_sensitive ? (tk_compare_t)strcmp : (tk_compare_t)strcasecmp; + return_value_if_fail(strs != NULL && str != NULL, -1); + + if (nr == 0) { + return NULL; + } + + high = nr - 1; + while (low <= high) { + mid = low + ((high - low) >> 1); + iter = strs[mid]; + + result = cmp(iter, str); + + if (result == 0) { + return iter; + } else if (result < 0) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return NULL; +} + ret_t object_to_json(tk_object_t* obj, str_t* str) { return tk_object_to_json(obj, str, 2, 0, FALSE); } diff --git a/src/tkc/utils.h b/src/tkc/utils.h index 799ed4d864..c5427660fa 100644 --- a/src/tkc/utils.h +++ b/src/tkc/utils.h @@ -1333,6 +1333,17 @@ const char* tk_skip_to_chars(const char* str, const char* chars); */ ret_t tk_mergesort(void* base, size_t nmemb, size_t size, tk_compare_t cmp); +/** + * @method tk_strs_bsearch + * 在字符串数组中查找字符串。 + * @param {const char**} strs 字符串数组(已排序)。 + * @param {uint32_t} nr 字符串个数。 + * @param {const char*} str 字符串。 + * @param {bool_t} case_sensitive 是否区分大小写。 + * @return {const char*} 返回找到的字符串。 + */ +const char* tk_strs_bsearch(const char** strs, uint32_t nr, const char* str, bool_t case_sensitive); + #define TK_STRDUP(str) ((str) != NULL) ? tk_strdup(str) : NULL #define TK_STRNDUP(str, len) ((str) != NULL) ? tk_strndup(str, len) : NULL diff --git a/src/tkc/wasm_helper.h b/src/tkc/wasm_helper.h index 5b95c9dc69..b002b3e20b 100644 --- a/src/tkc/wasm_helper.h +++ b/src/tkc/wasm_helper.h @@ -18,6 +18,10 @@ BEGIN_C_DECLS #define PRId64 __PRI64_PREFIX "d" #endif /*PRIu64*/ +#ifndef PRIu32 +#define PRIu32 "u" +#endif/*PRIu32*/ + #ifdef WITH_EASTL #include #else diff --git a/tests/utils_test.cc b/tests/utils_test.cc index a4fa4b7e06..7def9b4a0f 100644 --- a/tests/utils_test.cc +++ b/tests/utils_test.cc @@ -1973,3 +1973,22 @@ TEST(Utils, atob) { ASSERT_EQ(tk_atob("abcd"), TRUE); } + +TEST(Utils, tk_strs_bsearch) { + const char* strs[] = {"abc1", "abc2", "abc3", "abc4", "abc5", "b","c", "d"}; + ASSERT_EQ(tk_strs_bsearch(strs, 0, "Abc1", TRUE), (char*)NULL); + ASSERT_EQ(tk_strs_bsearch(strs, 0, "Abc1", FALSE), (char*)NULL); + + ASSERT_EQ(tk_strs_bsearch(strs, 1, "Abc1", TRUE), (char*)NULL); + ASSERT_STREQ(tk_strs_bsearch(strs, 1, "Abc1", FALSE), "abc1"); + + for(uint32_t i = 1; i < ARRAY_SIZE(strs); i++) { + for(uint32_t j = 0; j < ARRAY_SIZE(strs); j++) { + if (j >= i) { + ASSERT_EQ(tk_strs_bsearch(strs, i, strs[j], TRUE), (char*)NULL); + } else { + ASSERT_STREQ(tk_strs_bsearch(strs, i, strs[j], TRUE), strs[j]); + } + } + } +}