summaryrefslogtreecommitdiff
path: root/core/src/xmake/string
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-10 21:00:21 +0800
committerruki <[email protected]>2025-11-10 21:00:21 +0800
commit2c387895657ae13cd717d7f42f5baed5eb028a8e (patch)
tree8f005a8dfc3272e9f027560e5e1b5881a7a8bc70 /core/src/xmake/string
parent831447a51da0b094e08bb047247bf269a6e6cf71 (diff)
format more if-else codes in core
Diffstat (limited to 'core/src/xmake/string')
-rw-r--r--core/src/xmake/string/convert.c12
-rw-r--r--core/src/xmake/string/lastof.c15
-rw-r--r--core/src/xmake/string/split.c11
-rw-r--r--core/src/xmake/string/trim.c25
4 files changed, 39 insertions, 24 deletions
diff --git a/core/src/xmake/string/convert.c b/core/src/xmake/string/convert.c
index 88dc5ceb1..b3adaf8b8 100644
--- a/core/src/xmake/string/convert.c
+++ b/core/src/xmake/string/convert.c
@@ -76,10 +76,11 @@ static xm_charset_entry_ref_t xm_string_charset_find_by_name(tb_char_t const *na
// find it by the binary search
tb_size_t itor = tb_binary_find_all_if(iterator, xm_string_charset_comp_by_name, name);
- if (itor != tb_iterator_tail(iterator))
+ if (itor != tb_iterator_tail(iterator)) {
return (xm_charset_entry_ref_t)tb_iterator_item(iterator, itor);
- else
+ } else {
return tb_null;
+ }
}
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -115,9 +116,9 @@ tb_int_t xm_string_convert(lua_State *lua) {
tb_check_return_val(fcharset && tcharset, 0);
// empty string?
- if (!src_size)
+ if (!src_size) {
lua_pushstring(lua, "");
- else {
+ } else {
// convert string
tb_long_t dst_size = 0;
tb_size_t dst_maxn = (tb_size_t)src_size << 2;
@@ -131,8 +132,9 @@ tb_int_t xm_string_convert(lua_State *lua) {
dst_maxn)) >= 0 &&
dst_size < dst_maxn) {
lua_pushlstring(lua, (tb_char_t const *)dst_data, dst_size);
- } else
+ } else {
lua_pushnil(lua);
+ }
tb_free(dst_data);
}
diff --git a/core/src/xmake/string/lastof.c b/core/src/xmake/string/lastof.c
index 0d52e01ac..8785e3987 100644
--- a/core/src/xmake/string/lastof.c
+++ b/core/src/xmake/string/lastof.c
@@ -48,17 +48,19 @@ static tb_void_t xm_string_lastof_str(
} while (!next);
// found?
- if (curr)
+ if (curr) {
lua_pushinteger(lua, curr - cstr + 1);
- else
+ } else {
lua_pushnil(lua);
+ }
}
static tb_void_t xm_string_lastof_chr(lua_State *lua, tb_char_t const *cstr, tb_size_t nstr, tb_char_t ch) {
tb_char_t const *pos = tb_strrchr(cstr, ch); // faster than tb_strnrchr()
- if (pos)
+ if (pos) {
lua_pushinteger(lua, pos - cstr + 1);
- else
+ } else {
lua_pushnil(lua);
+ }
}
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -83,9 +85,10 @@ tb_int_t xm_string_lastof(lua_State *lua) {
// lastof it
lua_newtable(lua);
- if (nsubstr == 1)
+ if (nsubstr == 1) {
xm_string_lastof_chr(lua, cstr, (tb_size_t)nstr, csubstr[0]);
- else
+ } else {
xm_string_lastof_str(lua, cstr, (tb_size_t)nstr, csubstr, nsubstr);
+ }
return 1;
}
diff --git a/core/src/xmake/string/split.c b/core/src/xmake/string/split.c
index 56e8a636d..5ca0c92bb 100644
--- a/core/src/xmake/string/split.c
+++ b/core/src/xmake/string/split.c
@@ -45,8 +45,9 @@ static tb_void_t xm_string_split_str(lua_State *lua,
tb_char_t const *pos = tb_strstr(cstr, cdls); // faster than tb_strnstr()
while (pos && pos < end) {
if (pos > cstr || strict) {
- if (limit > 0 && num + 1 >= limit)
+ if (limit > 0 && num + 1 >= limit) {
break;
+ }
lua_pushlstring(lua, cstr, pos - cstr);
lua_rawseti(lua, -2, ++num);
@@ -70,8 +71,9 @@ static tb_void_t xm_string_split_chr(
tb_char_t const *pos = tb_strchr(cstr, ch); // faster than tb_strnchr()
while (pos && pos < end) {
if (pos > cstr || strict) {
- if (limit > 0 && num + 1 >= limit)
+ if (limit > 0 && num + 1 >= limit) {
break;
+ }
lua_pushlstring(lua, cstr, pos - cstr);
lua_rawseti(lua, -2, ++num);
@@ -119,9 +121,10 @@ tb_int_t xm_string_split(lua_State *lua) {
// split it
lua_newtable(lua);
- if (ndls == 1)
+ if (ndls == 1) {
xm_string_split_chr(lua, cstr, (tb_size_t)nstr, cdls[0], strict, limit);
- else
+ } else {
xm_string_split_str(lua, cstr, (tb_size_t)nstr, cdls, ndls, strict, limit);
+ }
return 1;
}
diff --git a/core/src/xmake/string/trim.c b/core/src/xmake/string/trim.c
index c1dcdad8d..54e6e4b5a 100644
--- a/core/src/xmake/string/trim.c
+++ b/core/src/xmake/string/trim.c
@@ -40,15 +40,18 @@ static tb_void_t xm_string_trim_space(tb_char_t const **psstr, tb_char_t const *
tb_char_t const *e = *pestr;
// trim left?
- if (mode <= 0)
- while (p < e && tb_isspace(*p))
+ if (mode <= 0) {
+ while (p < e && tb_isspace(*p)) {
p++;
+ }
+ }
// trim right
if (mode >= 0) {
e--;
- while (e >= p && tb_isspace(*e))
+ while (e >= p && tb_isspace(*e)) {
e--;
+ }
e++;
}
@@ -64,8 +67,9 @@ static tb_char_t const *xm_string_ltrim(tb_char_t const *sstr,
tb_assert(sstr && estr && ctrim);
tb_char_t const *p = sstr;
- while (p < estr && tb_strnchr(ctrim, ntrim, *p))
+ while (p < estr && tb_strnchr(ctrim, ntrim, *p)) {
p++;
+ }
return p;
}
@@ -76,8 +80,9 @@ static tb_char_t const *xm_string_rtrim(tb_char_t const *sstr,
tb_assert(sstr && estr && ctrim);
tb_char_t const *p = estr - 1;
- while (p >= sstr && tb_strnchr(ctrim, ntrim, *p))
+ while (p >= sstr && tb_strnchr(ctrim, ntrim, *p)) {
p--;
+ }
return p + 1;
}
@@ -110,14 +115,16 @@ tb_int_t xm_string_trim(lua_State *lua) {
tb_char_t const *const rsstr = sstr;
tb_char_t const *const restr = estr;
- if (ltrim == 0)
+ if (ltrim == 0) {
xm_string_trim_space(&sstr, &estr, trimtype);
- else {
+ } else {
// trim chars
- if (trimtype <= 0)
+ if (trimtype <= 0) {
sstr = xm_string_ltrim(sstr, estr, trimchars, ltrim);
- if (trimtype >= 0)
+ }
+ if (trimtype >= 0) {
estr = xm_string_rtrim(sstr, estr, trimchars, ltrim);
+ }
}
// no trimed chars