Selaa lähdekoodia

add: 添加string.trim方法

Wendal Chen 4 vuotta sitten
vanhempi
sitoutus
f9ddac3f10
2 muutettua tiedostoa jossa 89 lisäystä ja 2 poistoa
  1. 6 1
      lua/src/lstrlib.c
  2. 83 1
      lua/src/lstrlib_exts.c

+ 6 - 1
lua/src/lstrlib.c

@@ -1545,6 +1545,8 @@ int l_str_toBase64(lua_State *L);
 int l_str_fromBase64(lua_State *L);
 int l_str_startsWith(lua_State *L);
 int l_str_endsWith(lua_State *L);
+int l_str_strs(lua_State *L);
+int l_str_trim(lua_State *L);
 //-----------------------------------------------------------
 
 #include "rotable.h"
@@ -1580,7 +1582,10 @@ static const rotable_Reg strlib[] = {
 
   {"startsWith", l_str_startsWith, 0},
   {"endsWith", l_str_endsWith, 0},
-
+  {"trim",     l_str_trim, 0},
+#if defined(LUA_USE_LINUX) || defined(LUA_USE_WINDOWS) || defined(LUA_USE_MACOSX)
+  {"strs", l_str_strs, 0},
+#endif
   //{"urlDecode", str_urlDecode},
   //-----------------------------
   {NULL, NULL, 0}

+ 83 - 1
lua/src/lstrlib_exts.c

@@ -549,7 +549,7 @@ int l_str_endsWith(lua_State *L) {
   const char* str = luaL_checklstring(L, 1, &str_len);
   const char* suffix = luaL_checklstring(L, 2, &suffix_len);
 
-  LLOGD("%s %d : %s %d", str, str_len, suffix, suffix_len);
+  // LLOGD("%s %d : %s %d", str, str_len, suffix, suffix_len);
 
   if (str_len < suffix_len) {
     lua_pushboolean(L, 0);
@@ -562,3 +562,85 @@ int l_str_endsWith(lua_State *L) {
   }
   return 1;
 }
+
+#include "lstate.h"
+
+int l_str_strs(lua_State *L) {
+  for (size_t i = 0; i < STRCACHE_N; i++)
+  {
+    TString **p = G(L)->strcache[i];
+    for (size_t j = 0; j < STRCACHE_M; j++) {
+      if (p[j]->tt == LUA_TSHRSTR)
+        LLOGD(">> %s", getstr(p[j]));
+    }
+  }
+  return 0;
+}
+
+int l_str_trim_impl(lua_State *L, int ltrim, int rtrim) {
+  size_t str_len = 0;
+  const char* str = luaL_checklstring(L, 1, &str_len);
+  if (str_len == 0) {
+    lua_pushvalue(L, 1);
+    return 1;
+  }
+  int begin = 0;
+  int end = str_len;
+  if (ltrim) {
+    for (; begin <= end; begin++)
+    {
+      //LLOGD("ltrim %02X %d %d", str[begin], begin, end);
+      if(str[begin] != ' '  && 
+         str[begin] != '\t' && 
+         str[begin] != '\n' && 
+         str[begin] != '\r')
+      {
+        break;
+      }
+    }
+  }
+  if (rtrim) {
+    for (; begin < end; end--)
+    {
+      //LLOGD("rtrim %02X %d %d", str[end], begin, end);
+      if(str[end - 1] != ' '  && 
+         str[end - 1] != '\t' &&  
+         str[end - 1] != '\n' &&  
+         str[end - 1] != '\r')
+      {
+        break;
+      }
+    }
+  }
+  if (begin == end) {
+    lua_pushliteral(L, "");
+  }
+  else {
+    lua_pushlstring(L, str + begin, end - begin);
+  }
+  return 1;
+}
+
+/*
+裁剪字符串,去除头尾的空格
+@api string.trim(str, ltrim, rtrim)
+@string 需要处理的字符串
+@bool 清理前缀,默认为true
+@bool 清理后缀,默认为true
+@return string 清理后的字符串
+@usage
+local str = "\r\nabc\r\n"
+log.info("str", string.trim(str)) -- 打印 "abc"
+log.info("str", str:trim())       -- 打印 "abc"
+log.info("str", #string.trim(str, false, true)) -- 仅裁剪后缀,所以长度是5
+*/
+int l_str_trim(lua_State *L) {
+  int ltrim = 1;
+  int rtrim = 1;
+  if (lua_isboolean(L, 2))
+    ltrim = lua_toboolean(L, 2);
+  if (lua_isboolean(L, 3))
+    rtrim = lua_toboolean(L, 3);
+
+  return l_str_trim_impl(L, ltrim, rtrim);
+}