From 9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Aug 2000 14:57:04 -0300 Subject: first version for new API --- lstrlib.c | 104 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 57 insertions(+), 47 deletions(-) (limited to 'lstrlib.c') diff --git a/lstrlib.c b/lstrlib.c index 16066499..c380359b 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.45 2000/06/12 14:37:18 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.46 2000/08/09 19:16:57 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -25,10 +25,11 @@ static void addnchar (lua_State *L, const char *s, size_t n) { } -static void str_len (lua_State *L) { +static int str_len (lua_State *L) { size_t l; luaL_check_lstr(L, 1, &l); lua_pushnumber(L, l); + return 1; } @@ -43,7 +44,7 @@ static long posrelat (long pos, size_t len) { } -static void str_sub (lua_State *L) { +static int str_sub (lua_State *L) { size_t l; const char *s = luaL_check_lstr(L, 1, &l); long start = posrelat(luaL_check_long(L, 2), l); @@ -53,10 +54,11 @@ static void str_sub (lua_State *L) { if (start <= end) lua_pushlstring(L, s+start-1, end-start+1); else lua_pushstring(L, ""); + return 1; } -static void str_lower (lua_State *L) { +static int str_lower (lua_State *L) { size_t l; size_t i; const char *s = luaL_check_lstr(L, 1, &l); @@ -64,10 +66,11 @@ static void str_lower (lua_State *L) { for (i=0; i 0) addnchar(L, s, l); closeandpush(L); + return 1; } -static void str_byte (lua_State *L) { +static int str_byte (lua_State *L) { size_t l; const char *s = luaL_check_lstr(L, 1, &l); long pos = posrelat(luaL_opt_long(L, 2, 1), l); luaL_arg_check(L, 0level; i++) { - int l = cap->capture[i].len; - if (l == -1) lua_error(L, "unfinished capture"); - lua_pushlstring(L, cap->capture[i].init, l); - } -} - - static int check_capture (lua_State *L, int l, struct Capture *cap) { l -= '1'; if (!(0 <= l && l < cap->level && cap->capture[l].len != -1)) @@ -400,20 +398,31 @@ static const char *lmemfind (const char *s1, size_t l1, } -static void str_find (lua_State *L) { +static int push_captures (lua_State *L, struct Capture *cap) { + int i; + for (i=0; ilevel; i++) { + int l = cap->capture[i].len; + if (l == -1) lua_error(L, "unfinished capture"); + lua_pushlstring(L, cap->capture[i].init, l); + } + return cap->level; /* number of strings pushed */ +} + + +static int str_find (lua_State *L) { size_t l1, l2; const char *s = luaL_check_lstr(L, 1, &l1); const char *p = luaL_check_lstr(L, 2, &l2); long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; struct Capture cap; luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range"); - if (lua_getparam(L, 4) != LUA_NOOBJECT || - strpbrk(p, SPECIALS) == NULL) { /* no special characters? */ + if (lua_gettop(L) > 3 || /* extra argument? */ + strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ const char *s2 = lmemfind(s+init, l1-init, p, l2); if (s2) { lua_pushnumber(L, s2-s+1); lua_pushnumber(L, s2-s+l2); - return; + return 2; } } else { @@ -426,19 +435,19 @@ static void str_find (lua_State *L) { if ((res=match(L, s1, p, &cap)) != NULL) { lua_pushnumber(L, s1-s+1); /* start */ lua_pushnumber(L, res-s); /* end */ - push_captures(L, &cap); - return; + return push_captures(L, &cap) + 2; } } while (s1++= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)), + 3, "string or function expected"); luaL_resetbuffer(L); cap.src_end = src+srcl; while (n < max_s) { @@ -496,7 +504,7 @@ static void str_gsub (lua_State *L) { e = match(L, src, p, &cap); if (e) { n++; - add_s(L, newp, &cap); + add_s(L, &cap); } if (e && e>src) /* non empty match? */ src = e; /* skip it */ @@ -508,6 +516,7 @@ static void str_gsub (lua_State *L) { addnchar(L, src, cap.src_end-src); closeandpush(L); lua_pushnumber(L, n); /* number of substitutions */ + return 2; } /* }====================================================== */ @@ -534,7 +543,7 @@ static void luaI_addquoted (lua_State *L, int arg) { /* maximum size of each format specification (such as '%-099.99d') */ #define MAX_FORMAT 20 /* arbitrary limit */ -static void str_format (lua_State *L) { +static int str_format (lua_State *L) { int arg = 1; const char *strfrmt = luaL_check_string(L, arg); luaL_resetbuffer(L); @@ -597,6 +606,7 @@ static void str_format (lua_State *L) { } } closeandpush(L); /* push the result */ + return 1; } -- cgit v1.2.3-55-g6feb