diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-16 15:41:43 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-16 15:41:43 -0200 |
commit | e7c2eebd87ac0241d346ae3ac01336708f0729c1 (patch) | |
tree | 66763f9933a2acc3ba2ee82f3d5f247bb0a5f134 /lstrlib.c | |
parent | 0c3ea96541525c5e347f1072ac2659f010e2ec34 (diff) | |
download | lua-e7c2eebd87ac0241d346ae3ac01336708f0729c1.tar.gz lua-e7c2eebd87ac0241d346ae3ac01336708f0729c1.tar.bz2 lua-e7c2eebd87ac0241d346ae3ac01336708f0729c1.zip |
new function `concat'
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 22 |
1 files changed, 21 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.69 2001/07/17 18:46:49 roberto Exp $ | 2 | ** $Id: lstrlib.c,v 1.71 2001/08/31 19:46:07 roberto Exp $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -87,6 +87,25 @@ static int str_rep (lua_State *L) { | |||
87 | } | 87 | } |
88 | 88 | ||
89 | 89 | ||
90 | static int str_concat (lua_State *L) { | ||
91 | luaL_Buffer b; | ||
92 | size_t lsep; | ||
93 | const char *sep = luaL_opt_lstr(L, 2, "", &lsep); | ||
94 | int n, i; | ||
95 | luaL_checktype(L, 1, LUA_TTABLE); | ||
96 | luaL_buffinit(L, &b); | ||
97 | n = lua_getn(L, 1); | ||
98 | for (i=1; i<=n; i++) { | ||
99 | lua_rawgeti(L, 1, i); | ||
100 | luaL_addvalue(&b); | ||
101 | if (i != n) | ||
102 | luaL_addlstring(&b, sep, lsep); | ||
103 | } | ||
104 | luaL_pushresult(&b); | ||
105 | return 1; | ||
106 | } | ||
107 | |||
108 | |||
90 | static int str_byte (lua_State *L) { | 109 | static int str_byte (lua_State *L) { |
91 | size_t l; | 110 | size_t l; |
92 | const l_char *s = luaL_check_lstr(L, 1, &l); | 111 | const l_char *s = luaL_check_lstr(L, 1, &l); |
@@ -646,6 +665,7 @@ static const luaL_reg strlib[] = { | |||
646 | {l_s("strchar"), str_char}, | 665 | {l_s("strchar"), str_char}, |
647 | {l_s("strrep"), str_rep}, | 666 | {l_s("strrep"), str_rep}, |
648 | {l_s("strbyte"), str_byte}, | 667 | {l_s("strbyte"), str_byte}, |
668 | {l_s("concat"), str_concat}, | ||
649 | {l_s("format"), str_format}, | 669 | {l_s("format"), str_format}, |
650 | {l_s("strfind"), str_find}, | 670 | {l_s("strfind"), str_find}, |
651 | {l_s("gsub"), str_gsub} | 671 | {l_s("gsub"), str_gsub} |