Skip to content

Commit

Permalink
improve tk_atob
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 7, 2024
1 parent 8046e91 commit d845261
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 修复locale_infos_unref接口释放info对象无法清除assets_manager上的野指针导致崩溃的问题(感谢雨欣提供补丁)
* 完善object_hash(感谢兆坤提供补丁)
* 完善object_array(感谢兆坤提供补丁)
* 完善tk_atob(感谢兆坤提供补丁)
* 统一是否可从名字中获取index的判断逻辑(感谢兆坤提供补丁)

2024/11/06
Expand Down
10 changes: 1 addition & 9 deletions src/tkc/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
38 changes: 37 additions & 1 deletion tests/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

0 comments on commit d845261

Please sign in to comment.