summaryrefslogtreecommitdiff
path: root/core/src/xmake/string
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-17 11:04:23 +0800
committerOpportunityLiu <[email protected]>2019-07-17 11:04:23 +0800
commit7f51f9e0f1ec49cb56b3205608046505903d312d (patch)
treef352aaad01c7cd2d73244e21863e1bec4a183575 /core/src/xmake/string
parent7138d5770975053ad4fe60f1d8bdd5e4c774fe5b (diff)
custom trim
Diffstat (limited to 'core/src/xmake/string')
-rw-r--r--core/src/xmake/string/trim.c99
1 files changed, 67 insertions, 32 deletions
diff --git a/core/src/xmake/string/trim.c b/core/src/xmake/string/trim.c
index c7cce0168..1a0738206 100644
--- a/core/src/xmake/string/trim.c
+++ b/core/src/xmake/string/trim.c
@@ -11,10 +11,10 @@
* 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
+ * @author OpportunityLiu
* @file trim.c
*
*/
@@ -22,8 +22,8 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
-#define TB_TRACE_MODULE_NAME "trim"
-#define TB_TRACE_MODULE_DEBUG (0)
+#define TB_TRACE_MODULE_NAME "trim"
+#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
@@ -31,48 +31,83 @@
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
+ * privates
+ */
+
+static tb_char_t const* xm_string_ltrim(tb_char_t const* strstart, tb_char_t const* strend, tb_char_t const* trimchars,
+ size_t ntrimchars)
+{
+ // check
+ tb_assert_and_check_return_val(strstart && strend && trimchars, tb_null);
+
+ // done
+ tb_char_t const* p = strstart;
+ while (p < strend && tb_strnchr(trimchars, ntrimchars, *p))
+ p++;
+
+ return p;
+}
+
+static tb_char_t const* xm_string_rtrim(tb_char_t const* strstart, tb_char_t const* strend, tb_char_t const* trimchars,
+ size_t ntrimchars)
+{
+ // check
+ tb_assert_and_check_return_val(strstart && strend && trimchars, tb_null);
+
+ // done
+ tb_char_t const* p = strend - 1;
+ while (p >= strstart && tb_strnchr(trimchars, ntrimchars, *p))
+ p--;
+
+ return p + 1;
+}
+
+/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
-/* trim string
+/* trim string
+ *
+ * @param str the string
+ * @param trimchars the chars to trim
+ * @param trimtype 0 to trim left and right, -1 to trim left, 1 to trim right
*
- * @param cstr the c-string
- * @param mode the trim mode, all: 0, left: -1, right: 1
+ * @code
+ * local result = string.trim(str, "\r\n \v\t", 0)
+ * local result = string.trim(str, "(", -1)
+ * @endcode
*/
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);
+ size_t lstr, ltrim;
+ tb_char_t const* sstr = luaL_checklstring(lua, 1, &lstr);
+ tb_char_t const* estr = sstr + lstr;
+ tb_char_t const* trimchars = luaL_optlstring(lua, 2, "\r\n\t \f\v", &ltrim);
+ tb_int64_t const trimtype = (tb_int64_t)luaL_optinteger(lua, 3, 0);
+
+ tb_char_t const* const rsstr = sstr;
+ tb_char_t const* const restr = estr;
- // 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++;
+ tb_assert_and_check_goto(sstr && trimchars, failed);
+ // empty string or empty tmim chars
+ tb_check_goto(ltrim != 0 && lstr != 0, failed);
- // trim right
- if (mode >= 0)
- {
- e--;
- while (e >= cstr && tb_isspace(*e)) e--;
- e++;
- }
+ // trim chars
+ if (trimtype <= 0) sstr = xm_string_ltrim(sstr, estr, trimchars, ltrim);
+ if (trimtype >= 0) estr = xm_string_rtrim(sstr, estr, trimchars, ltrim);
- // save trimed string
- if (e > p) lua_pushlstring(lua, p, e - p);
- else lua_pushstring(lua, "");
- }
+ // no trimed chars
+ tb_check_goto(sstr != rsstr || estr != restr, failed);
// ok
+ lua_pushlstring(lua, sstr, estr - sstr);
+ return 1;
+
+failed:
+ // return orignal value
+ lua_settop(lua, 1);
return 1;
}