summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-21 00:49:57 +0800
committerruki <[email protected]>2026-01-21 00:49:57 +0800
commitae8b2c49195e4f9e28f8ab06f8169e1bca68d1fc (patch)
treee62b203fa40d090996f3c6f4496f140237bd4f38 /core/src
parentf10baca5c10b1d21c6d9900ad3d6b382c37258c6 (diff)
add utf8.reverse
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/utf8/reverse.c57
-rw-r--r--core/src/xmake/utf8/utf8.c28
-rw-r--r--core/src/xmake/utf8/utf8.h1
4 files changed, 88 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index bca1cce65..9ceabd156 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -287,6 +287,7 @@ tb_int_t xm_utf8_codepoint(lua_State *lua);
tb_int_t xm_utf8_offset(lua_State *lua);
tb_int_t xm_utf8_codes(lua_State *lua);
tb_int_t xm_utf8_sub(lua_State *lua);
+tb_int_t xm_utf8_reverse(lua_State *lua);
tb_int_t xm_utf8_lastof(lua_State *lua);
tb_int_t xm_utf8_find(lua_State *lua);
tb_int_t xm_utf8_width(lua_State *lua);
@@ -604,6 +605,7 @@ static luaL_Reg const g_utf8_functions[] = {
{"len", xm_utf8_len},
{"offset", xm_utf8_offset},
{"sub", xm_utf8_sub},
+ {"reverse", xm_utf8_reverse},
{"lastof", xm_utf8_lastof},
{"find", xm_utf8_find},
{"width", xm_utf8_width},
diff --git a/core/src/xmake/utf8/reverse.c b/core/src/xmake/utf8/reverse.c
new file mode 100644
index 000000000..0c0b7aaf7
--- /dev/null
+++ b/core/src/xmake/utf8/reverse.c
@@ -0,0 +1,57 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file reverse.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "utf8.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* utf8.reverse(s)
+ */
+tb_int_t xm_utf8_reverse(lua_State *lua) {
+ size_t len;
+ tb_char_t const* s = luaL_checklstring(lua, 1, &len);
+ if (len == 0) {
+ lua_pushliteral(lua, "");
+ return 1;
+ }
+
+ // do reverse
+ if (len < 1024) {
+ tb_char_t buf[1024 + 1];
+ xm_utf8_reverse_impl(s, len, buf);
+ lua_pushlstring(lua, buf, len);
+ } else {
+ tb_char_t* buf = (tb_char_t*)tb_malloc_bytes(len + 1);
+ if (buf) {
+ xm_utf8_reverse_impl(s, len, buf);
+ lua_pushlstring(lua, buf, len);
+ tb_free(buf);
+ } else {
+ lua_pushnil(lua);
+ }
+ }
+ return 1;
+}
diff --git a/core/src/xmake/utf8/utf8.c b/core/src/xmake/utf8/utf8.c
index ff6c33a1a..ace3a213c 100644
--- a/core/src/xmake/utf8/utf8.c
+++ b/core/src/xmake/utf8/utf8.c
@@ -476,3 +476,31 @@ tb_char_t const* xm_utf8_sub_impl(tb_char_t const* s, tb_size_t len, tb_long_t i
return s + start_byte - 1;
}
+tb_char_t* xm_utf8_reverse_impl(tb_char_t const* s, tb_size_t len, tb_char_t* buf) {
+ tb_assert_and_check_return_val(s && len && buf, tb_null);
+
+ tb_char_t const* p = s;
+ tb_char_t const* e = s + len;
+ tb_char_t* d = buf + len;
+
+ while (p < e) {
+ xm_utf8_int_t code;
+ tb_char_t const* next = xm_utf8_decode(p, &code, tb_false);
+
+ // invalid utf8? treat as 1 byte
+ tb_size_t n = 1;
+ if (next) {
+ n = next - p;
+ }
+
+ // safety check
+ if (p + n > e) n = e - p;
+
+ d -= n;
+ tb_memcpy(d, p, n);
+ p += n;
+ }
+ buf[len] = '\0';
+ return buf;
+}
+
diff --git a/core/src/xmake/utf8/utf8.h b/core/src/xmake/utf8/utf8.h
index fff81f1e5..fe83e8e86 100644
--- a/core/src/xmake/utf8/utf8.h
+++ b/core/src/xmake/utf8/utf8.h
@@ -55,5 +55,6 @@ tb_long_t xm_utf8_find_impl(tb_char_t const* s, tb_size_t len, tb_char
tb_long_t xm_utf8_lastof_impl(tb_char_t const* s, tb_size_t len, tb_char_t const* sub, tb_size_t sublen);
tb_long_t xm_utf8_byte_impl(tb_char_t const* s, tb_size_t len, tb_long_t i, tb_long_t j, xm_utf8_codepoint_func_t func, tb_cpointer_t udata);
tb_char_t const* xm_utf8_sub_impl(tb_char_t const* s, tb_size_t len, tb_long_t i, tb_long_t j, tb_size_t* psublen);
+tb_char_t* xm_utf8_reverse_impl(tb_char_t const* s, tb_size_t len, tb_char_t* buf);
#endif