Browse Source

new function `concat'

Roberto Ierusalimschy 24 years ago
parent
commit
e7c2eebd87
1 changed files with 21 additions and 1 deletions
  1. 21 1
      lstrlib.c

+ 21 - 1
lstrlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstrlib.c,v 1.69 2001/07/17 18:46:49 roberto Exp $
+** $Id: lstrlib.c,v 1.71 2001/08/31 19:46:07 roberto Exp $
 ** Standard library for string operations and pattern-matching
 ** See Copyright Notice in lua.h
 */
@@ -87,6 +87,25 @@ static int str_rep (lua_State *L) {
 }
 
 
+static int str_concat (lua_State *L) {
+  luaL_Buffer b;
+  size_t lsep;
+  const char *sep = luaL_opt_lstr(L, 2, "", &lsep);
+  int n, i;
+  luaL_checktype(L, 1, LUA_TTABLE);
+  luaL_buffinit(L, &b);
+  n = lua_getn(L, 1);
+  for (i=1; i<=n; i++) {
+    lua_rawgeti(L, 1, i);
+    luaL_addvalue(&b);
+    if (i != n)
+      luaL_addlstring(&b, sep, lsep);
+  }
+  luaL_pushresult(&b);
+  return 1;
+}
+
+
 static int str_byte (lua_State *L) {
   size_t l;
   const l_char *s = luaL_check_lstr(L, 1, &l);
@@ -646,6 +665,7 @@ static const luaL_reg strlib[] = {
 {l_s("strchar"), str_char},
 {l_s("strrep"), str_rep},
 {l_s("strbyte"), str_byte},
+{l_s("concat"), str_concat},
 {l_s("format"), str_format},
 {l_s("strfind"), str_find},
 {l_s("gsub"), str_gsub}