diff options
| author | ruki <[email protected]> | 2026-01-20 23:28:32 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-01-20 23:28:32 +0800 |
| commit | 92423451b623e79ac464d388bfb1bfed2780b792 (patch) | |
| tree | 12e6734a2bfa8799cd2a97d763bf205583fbfca8 /core/src | |
| parent | a22dc9398d5323bf6c4c636915be836de3d1d850 (diff) | |
format code
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/utf8/char.c | 18 | ||||
| -rw-r--r-- | core/src/xmake/utf8/codepoint.c | 19 | ||||
| -rw-r--r-- | core/src/xmake/utf8/codes.c | 15 | ||||
| -rw-r--r-- | core/src/xmake/utf8/len.c | 11 | ||||
| -rw-r--r-- | core/src/xmake/utf8/offset.c | 15 | ||||
| -rw-r--r-- | core/src/xmake/utf8/utf8.c | 55 | ||||
| -rw-r--r-- | core/src/xmake/utf8/utf8.h | 10 |
7 files changed, 95 insertions, 48 deletions
diff --git a/core/src/xmake/utf8/char.c b/core/src/xmake/utf8/char.c index afb0a0e0f..760dab264 100644 --- a/core/src/xmake/utf8/char.c +++ b/core/src/xmake/utf8/char.c @@ -34,24 +34,26 @@ static void xm_utf8_char_push(lua_State *lua, tb_int_t arg) { tb_char_t buf[8]; tb_size_t n = xm_utf8_encode(buf, (xm_utf8_int_t)code); - if (n > 0) + if (n > 0) { lua_pushlstring(lua, buf, n); - else + } else { luaL_error(lua, "value out of range"); + } } /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -/* -** utfchar(n1, n2, ...) -> char(n1)..char(n2)... -*/ +/* utfchar(n1, n2, ...) -> char(n1)..char(n2)... + */ tb_int_t xm_utf8_char(lua_State *lua) { - tb_int_t n = lua_gettop(lua); /* number of arguments */ - if (n == 1) /* optimize common case of single char */ + tb_assert_and_check_return_val(lua, 0); + + tb_int_t n = lua_gettop(lua); // number of arguments + if (n == 1) { // optimize common case of single char xm_utf8_char_push(lua, 1); - else { + } else { tb_int_t i; luaL_Buffer b; luaL_buffinit(lua, &b); diff --git a/core/src/xmake/utf8/codepoint.c b/core/src/xmake/utf8/codepoint.c index 7d2f0de24..15828a7ab 100644 --- a/core/src/xmake/utf8/codepoint.c +++ b/core/src/xmake/utf8/codepoint.c @@ -29,6 +29,8 @@ */ static tb_bool_t xm_utf8_codepoint_cb(xm_utf8_int_t code, tb_cpointer_t udata) { lua_State* lua = (lua_State*)udata; + tb_assert_and_check_return_val(lua, tb_false); + lua_pushinteger(lua, code); return tb_true; } @@ -37,10 +39,9 @@ static tb_bool_t xm_utf8_codepoint_cb(xm_utf8_int_t code, tb_cpointer_t udata) { * implementation */ -/* -** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all -** characters that start in the range [i,j] -*/ +/* codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all + * characters that start in the range [i,j] + */ tb_int_t xm_utf8_codepoint(lua_State *lua) { size_t len; tb_char_t const* s = luaL_checklstring(lua, 1, &len); @@ -51,15 +52,19 @@ tb_int_t xm_utf8_codepoint(lua_State *lua) { luaL_argcheck(lua, posi >= 1, 2, "out of bounds"); luaL_argcheck(lua, pose <= (lua_Integer)len, 3, "out of bounds"); - if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ + if (posi > pose) { + return 0; // empty interval; return no values + } + if (pose - posi >= INT_MAX) { // (lua_Integer -> int) overflow? return luaL_error(lua, "string slice too long"); + } tb_int_t n = (tb_int_t)(pose - posi) + 1; luaL_checkstack(lua, n, "string slice too long"); - if (!xm_utf8_codepoint_impl(s, len, posi, pose, !lax, xm_utf8_codepoint_cb, lua)) + if (!xm_utf8_codepoint_impl(s, len, posi, pose, !lax, xm_utf8_codepoint_cb, lua)) { return luaL_error(lua, XM_UTF8_MSGInvalid); + } return n; } diff --git a/core/src/xmake/utf8/codes.c b/core/src/xmake/utf8/codes.c index 46664e891..420584835 100644 --- a/core/src/xmake/utf8/codes.c +++ b/core/src/xmake/utf8/codes.c @@ -29,19 +29,24 @@ */ static tb_int_t xm_utf8_codes_iter(lua_State *lua, tb_bool_t strict) { + tb_assert_and_check_return_val(lua, 0); + size_t len; tb_char_t const* s = luaL_checklstring(lua, 1, &len); lua_Unsigned n = (lua_Unsigned)lua_tointeger(lua, 2); if (n < len) { - while (n < len && xm_utf8_iscontp(s + n)) n++; /* go to next character */ + while (n < len && xm_utf8_iscontp(s + n)) { + n++; // go to next character + } } - if (n >= len) /* (also handles original 'n' being negative) */ - return 0; /* no more codepoints */ - else { + if (n >= len) { // (also handles original 'n' being negative) + return 0; // no more codepoints + } else { xm_utf8_int_t code; tb_char_t const* next = xm_utf8_decode(s + n, &code, strict); - if (next == NULL || xm_utf8_iscontp(next)) + if (next == NULL || xm_utf8_iscontp(next)) { return luaL_error(lua, XM_UTF8_MSGInvalid); + } lua_pushinteger(lua, n + 1); lua_pushinteger(lua, code); return 2; diff --git a/core/src/xmake/utf8/len.c b/core/src/xmake/utf8/len.c index 410c0a1ef..50fa8e37a 100644 --- a/core/src/xmake/utf8/len.c +++ b/core/src/xmake/utf8/len.c @@ -28,12 +28,13 @@ * implementation */ -/* -** utf8len(s [, i [, j [, lax]]]) --> number of characters that -** start in the range [i,j], or nil + current position if 's' is not -** well formed in that interval -*/ +/* utf8len(s [, i [, j [, lax]]]) --> number of characters that + * start in the range [i,j], or nil + current position if 's' is not + * well formed in that interval + */ tb_int_t xm_utf8_len(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + size_t len; tb_char_t const* s = luaL_checklstring(lua, 1, &len); lua_Integer posi = xm_utf8_posrelat(luaL_optinteger(lua, 2, 1), len); diff --git a/core/src/xmake/utf8/offset.c b/core/src/xmake/utf8/offset.c index 89635eac1..51152c7db 100644 --- a/core/src/xmake/utf8/offset.c +++ b/core/src/xmake/utf8/offset.c @@ -28,11 +28,12 @@ * implementation */ -/* -** offset(s, n, [i]) -> index where n-th character counting from -** position 'i' starts; 0 means character at 'i'. -*/ +/* offset(s, n, [i]) -> index where n-th character counting from + * position 'i' starts; 0 means character at 'i'. + */ tb_int_t xm_utf8_offset(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + size_t len; tb_char_t const* s = luaL_checklstring(lua, 1, &len); lua_Integer n = luaL_checkinteger(lua, 2); @@ -40,10 +41,12 @@ tb_int_t xm_utf8_offset(lua_State *lua) { posi = xm_utf8_posrelat(luaL_optinteger(lua, 3, posi), len); tb_long_t result = xm_utf8_offset_impl(s, len, n, posi); - if (result == -1) + if (result == -1) { return luaL_argerror(lua, 3, "position out of bounds"); - if (result == -2) + } + if (result == -2) { return luaL_error(lua, "initial position is a continuation byte"); + } if (result == 0) { lua_pushnil(lua); return 1; diff --git a/core/src/xmake/utf8/utf8.c b/core/src/xmake/utf8/utf8.c index 8e164e0a5..d13e7e6a3 100644 --- a/core/src/xmake/utf8/utf8.c +++ b/core/src/xmake/utf8/utf8.c @@ -32,30 +32,37 @@ tb_char_t const* xm_utf8_decode(tb_char_t const* s, xm_utf8_int_t* val, tb_bool_ static const xm_utf8_int_t limits[] = {~(xm_utf8_int_t)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; tb_uint32_t c = (tb_byte_t)s[0]; xm_utf8_int_t res = 0; - if (c < 0x80) + if (c < 0x80) { res = c; - else { + } else { tb_int_t count = 0; for (; c & 0x40; c <<= 1) { tb_uint32_t cc = (tb_byte_t)s[++count]; - if (!xm_utf8_iscont(cc)) + if (!xm_utf8_iscont(cc)) { return tb_null; + } res = (res << 6) | (cc & 0x3F); } res |= ((xm_utf8_int_t)(c & 0x7F) << (count * 5)); - if (count > 5 || res > XM_UTF8_MAXUTF || res < limits[count]) + if (count > 5 || res > XM_UTF8_MAXUTF || res < limits[count]) { return tb_null; + } s += count; } if (strict) { - if (res > XM_UTF8_MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) + if (res > XM_UTF8_MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) { return tb_null; + } + } + if (val) { + *val = res; } - if (val) *val = res; return s + 1; } tb_size_t xm_utf8_encode(tb_char_t* s, xm_utf8_int_t val) { + tb_assert_and_check_return_val(s, 0); + if (val < 0x80) { s[0] = (tb_char_t)val; return 1; @@ -99,11 +106,15 @@ tb_size_t xm_utf8_encode(tb_char_t* s, xm_utf8_int_t val) { } tb_long_t xm_utf8_len_impl(tb_char_t const* s, tb_size_t len, tb_long_t posi, tb_long_t posj, tb_bool_t strict, tb_size_t* errpos) { + tb_assert_and_check_return_val(s, -1); + tb_long_t n = 0; while (posi <= posj) { tb_char_t const* s1 = xm_utf8_decode(s + posi - 1, tb_null, strict); if (s1 == tb_null) { - if (errpos) *errpos = posi; + if (errpos) { + *errpos = posi; + } return -1; } posi = s1 - s + 1; @@ -113,16 +124,22 @@ tb_long_t xm_utf8_len_impl(tb_char_t const* s, tb_size_t len, tb_long_t posi, tb } tb_long_t xm_utf8_offset_impl(tb_char_t const* s, tb_size_t len, tb_long_t n, tb_long_t posi) { + tb_assert_and_check_return_val(s, -1); + // check - if (1 > posi || --posi > (tb_long_t)len) + if (1 > posi || --posi > (tb_long_t)len) { return -1; // error: position out of bounds + } if (n == 0) { // find beginning of current byte sequence - while (posi > 0 && xm_utf8_iscontp(s + posi)) posi--; + while (posi > 0 && xm_utf8_iscontp(s + posi)) { + posi--; + } } else { - if (xm_utf8_iscontp(s + posi)) + if (xm_utf8_iscontp(s + posi)) { return -2; // error: initial position is a continuation byte + } if (n < 0) { while (n < 0 && posi > 0) { @@ -142,19 +159,29 @@ tb_long_t xm_utf8_offset_impl(tb_char_t const* s, tb_size_t len, tb_long_t n, tb } } - if (n == 0) return posi + 1; + if (n == 0) { + return posi + 1; + } return 0; // nil } tb_bool_t xm_utf8_codepoint_impl(tb_char_t const* s, tb_size_t len, tb_long_t posi, tb_long_t posj, tb_bool_t strict, xm_utf8_codepoint_func_t func, tb_cpointer_t udata) { - if (posi > posj) return tb_true; + tb_assert_and_check_return_val(s, tb_false); + + if (posi > posj) { + return tb_true; + } tb_char_t const* se = s + posj; for (s += posi - 1; s < se;) { xm_utf8_int_t code; s = xm_utf8_decode(s, &code, strict); - if (s == tb_null) return tb_false; - if (func && !func(code, udata)) return tb_false; + if (s == tb_null) { + return tb_false; + } + if (func && !func(code, udata)) { + return tb_false; + } } return tb_true; } diff --git a/core/src/xmake/utf8/utf8.h b/core/src/xmake/utf8/utf8.h index a2ecebca4..3eb1b7428 100644 --- a/core/src/xmake/utf8/utf8.h +++ b/core/src/xmake/utf8/utf8.h @@ -26,9 +26,13 @@ typedef tb_bool_t (*xm_utf8_codepoint_func_t)(xm_utf8_int_t code, tb_cpointer_t * inline interfaces */ static __tb_inline__ tb_long_t xm_utf8_posrelat(tb_long_t pos, tb_size_t len) { - if (pos >= 0) return pos; - else if (0u - (tb_size_t)pos > len) return 0; - else return (tb_long_t)len + pos + 1; + if (pos >= 0) { + return pos; + } else if (0u - (tb_size_t)pos > len) { + return 0; + } else { + return (tb_long_t)len + pos + 1; + } } /* ////////////////////////////////////////////////////////////////////////////////////// |
