summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-17 22:38:21 +0800
committerruki <[email protected]>2019-07-17 09:50:12 +0800
commit7138d5770975053ad4fe60f1d8bdd5e4c774fe5b (patch)
treea6ca7509a4522337687e22f586f786c8c8395208 /core/src
parent54386754982c5580e9d351484eabd45ebfd20c48 (diff)
optimize string.trim using c implemention
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/machine.c4
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/string/trim.c78
3 files changed, 82 insertions, 1 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 77661a445..ff0758882 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -129,6 +129,7 @@ tb_int_t xm_winos_registry_query(lua_State* lua);
#endif
// the string functions
+tb_int_t xm_string_trim(lua_State* lua);
tb_int_t xm_string_convert(lua_State* lua);
tb_int_t xm_string_endswith(lua_State* lua);
tb_int_t xm_string_startswith(lua_State* lua);
@@ -263,7 +264,8 @@ static luaL_Reg const g_hash_functions[] =
// the string functions
static luaL_Reg const g_string_functions[] =
{
- { "convert", xm_string_convert }
+ { "trim", xm_string_trim }
+, { "convert", xm_string_convert }
, { "endswith", xm_string_endswith }
, { "startswith", xm_string_startswith }
, { tb_null, tb_null }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index f35becda6..7232e0fd9 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -61,6 +61,7 @@ xmake_C_FILES += \
path/is_absolute \
hash/uuid \
hash/sha256 \
+ string/trim \
string/convert \
string/endswith \
string/startswith \
diff --git a/core/src/xmake/string/trim.c b/core/src/xmake/string/trim.c
new file mode 100644
index 000000000..c7cce0168
--- /dev/null
+++ b/core/src/xmake/string/trim.c
@@ -0,0 +1,78 @@
+/*!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 - 2019, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file trim.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "trim"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* trim string
+ *
+ * @param cstr the c-string
+ * @param mode the trim mode, all: 0, left: -1, right: 1
+ */
+tb_int_t xm_string_trim(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the string and trim mode
+ size_t size = 0;
+ tb_char_t const* cstr = luaL_checklstring(lua, 1, &size);
+ ptrdiff_t mode = luaL_optinteger(lua, 2, 0);
+ tb_check_return_val(cstr, 0);
+
+ // empty string?
+ if (!size) lua_pushstring(lua, "");
+ else
+ {
+ tb_char_t const* p = cstr;
+ tb_char_t const* e = cstr + size;
+
+ // trim left?
+ if (mode <= 0) while (p < e && tb_isspace(*p)) p++;
+
+ // trim right
+ if (mode >= 0)
+ {
+ e--;
+ while (e >= cstr && tb_isspace(*e)) e--;
+ e++;
+ }
+
+ // save trimed string
+ if (e > p) lua_pushlstring(lua, p, e - p);
+ else lua_pushstring(lua, "");
+ }
+
+ // ok
+ return 1;
+}