diff --git a/docs/changes.md b/docs/changes.md index 632f00d2a..bf4ea716a 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -4,6 +4,7 @@ * 修复locale_infos_unref接口释放info对象无法清除assets_manager上的野指针导致崩溃的问题(感谢雨欣提供补丁) * 完善object_hash(感谢兆坤提供补丁) * 完善object_array(感谢兆坤提供补丁) + * 完善tk_atob(感谢兆坤提供补丁) * 统一是否可从名字中获取index的判断逻辑(感谢兆坤提供补丁) 2024/11/06 diff --git a/src/tkc/utils.c b/src/tkc/utils.c index ad76c1b4b..18e371e68 100644 --- a/src/tkc/utils.c +++ b/src/tkc/utils.c @@ -59,16 +59,8 @@ const char* tk_skip_to_num(const char* str) { return p; } -int tk_str2bool(const char* str) { - if (str == NULL || str[0] == '0' || tk_strcmp(str, "false") == 0 || tk_strcmp(str, "no") == 0) { - return 0; - } - - return 1; -} - bool_t tk_atob(const char* str) { - if (str == NULL || *str == 'f' || *str == 'F') { + if (TK_STR_IS_EMPTY(str) || *str == 'f' || *str == 'F' || tk_str_eq(str, "0")) { return FALSE; } diff --git a/tests/utils_test.cc b/tests/utils_test.cc index 36cd06c02..a4fa4b7e0 100644 --- a/tests/utils_test.cc +++ b/tests/utils_test.cc @@ -612,7 +612,10 @@ TEST(Utils, to_json) { str_init(&str, 1000); ASSERT_EQ(object_to_json(obj, &str), RET_OK); - ASSERT_STREQ(str.str, "{\n \"addr\": {\n \"city\": \"sz\",\n \"country\": \"zh\"\n },\n \"age\": 100,\n \"arr\": [\n 1,\n 2,\n \"abc\",\n \"hello\"\n ],\n \"name\": \"jim\"\n}"); + ASSERT_STREQ(str.str, + "{\n \"addr\": {\n \"city\": \"sz\",\n \"country\": \"zh\"\n },\n " + "\"age\": 100,\n \"arr\": [\n 1,\n 2,\n \"abc\",\n " + "\"hello\"\n ],\n \"name\": \"jim\"\n}"); str_reset(&str); TK_OBJECT_UNREF(obj); @@ -1937,3 +1940,36 @@ TEST(Utils, mergesort2) { ASSERT_EQ(arr[5].age, 43); ASSERT_EQ(arr[6].age, 82); } + +TEST(Utils, atob) { + ASSERT_EQ(tk_atob("FALSE"), FALSE); + ASSERT_EQ(tk_atob("False"), FALSE); + ASSERT_EQ(tk_atob("F"), FALSE); + ASSERT_EQ(tk_atob("f"), FALSE); + + ASSERT_EQ(tk_atob("\"FALSE\""), TRUE); + ASSERT_EQ(tk_atob("'FALSE'"), TRUE); + ASSERT_EQ(tk_atob(" FALSE "), TRUE); + + ASSERT_EQ(tk_atob("TRUE"), TRUE); + ASSERT_EQ(tk_atob("True"), TRUE); + ASSERT_EQ(tk_atob("T"), TRUE); + ASSERT_EQ(tk_atob("t"), TRUE); + + ASSERT_EQ(tk_atob("\"TRUE\""), TRUE); + ASSERT_EQ(tk_atob("'TRUE'"), TRUE); + ASSERT_EQ(tk_atob(" TRUE "), TRUE); + + ASSERT_EQ(tk_atob(NULL), FALSE); + ASSERT_EQ(tk_atob(""), FALSE); + ASSERT_EQ(tk_atob(" "), TRUE); + + ASSERT_EQ(tk_atob("0"), FALSE); + ASSERT_EQ(tk_atob("00"), TRUE); + ASSERT_EQ(tk_atob("0AWTK"), TRUE); + ASSERT_EQ(tk_atob("1"), TRUE); + ASSERT_EQ(tk_atob("-1"), TRUE); + ASSERT_EQ(tk_atob("2"), TRUE); + + ASSERT_EQ(tk_atob("abcd"), TRUE); +}