Sfoglia il codice sorgente

new function `string.reverse'

Roberto Ierusalimschy 22 anni fa
parent
commit
0ddedaee92
1 ha cambiato i file con 13 aggiunte e 1 eliminazioni
  1. 13 1
      lstrlib.c

+ 13 - 1
lstrlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstrlib.c,v 1.97 2003/03/19 21:16:12 roberto Exp roberto $
+** $Id: lstrlib.c,v 1.98 2003/04/03 13:35:34 roberto Exp roberto $
 ** Standard library for string operations and pattern-matching
 ** See Copyright Notice in lua.h
 */
@@ -56,6 +56,17 @@ static int str_sub (lua_State *L) {
 }
 
 
+static int str_reverse (lua_State *L) {
+  size_t l;
+  luaL_Buffer b;
+  const char *s = luaL_checklstring(L, 1, &l);
+  luaL_buffinit(L, &b);
+  while (l--) luaL_putchar(&b, s[l]);
+  luaL_pushresult(&b);
+  return 1;
+}
+
+
 static int str_lower (lua_State *L) {
   size_t l;
   size_t i;
@@ -746,6 +757,7 @@ static int str_format (lua_State *L) {
 static const luaL_reg strlib[] = {
   {"len", str_len},
   {"sub", str_sub},
+  {"reverse", str_reverse},
   {"lower", str_lower},
   {"upper", str_upper},
   {"char", str_char},