Skip to content

Commit

Permalink
add tk_strs_bsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 15, 2024
1 parent 06c86b7 commit b19e205
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
2024/11/15
* 修复拼写错误(感谢兆坤提供补丁)
* 修复 demoui dialog(感谢兆坤提供补丁)
* 增加tk_strs_bsearch

2024/11/14
* 修复 edit键盘长按Backspace键,弹出提示窗口后就不能连续删除字符(感谢兆坤提供补丁)
Expand Down
2 changes: 2 additions & 0 deletions src/tkc/types_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/tkc/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions src/tkc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/tkc/wasm_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <wchar.h>
#else
Expand Down
19 changes: 19 additions & 0 deletions tests/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
}

0 comments on commit b19e205

Please sign in to comment.