From c64f36ab2bec5b156329edc5d16a27226b598852 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Dec 1998 16:41:25 -0200 Subject: better behavior for "strsub" when indices are out-of-range --- lstrlib.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index fdb90deb..98e90ab2 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.19 1998/07/12 16:13:45 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.20 1998/11/10 19:38:12 roberto Exp roberto $ ** Standard library for strings and pattern-matching ** See Copyright Notice in lua.h */ @@ -32,33 +32,31 @@ static void str_len (void) } -static void closeandpush (void) -{ +static void closeandpush (void) { lua_pushlstring(luaL_buffer(), luaL_getsize()); } -static long posrelat (long pos, long len) -{ +static long posrelat (long pos, long len) { /* relative string position: negative means back from end */ return (pos>=0) ? pos : len+pos+1; } -static void str_sub (void) -{ +static void str_sub (void) { long l; char *s = luaL_check_lstr(1, &l); long start = posrelat(luaL_check_number(2), l); long end = posrelat(luaL_opt_number(3, -1), l); - if (1 <= start && start <= end && end <= l) + if (start < 1) start = 1; + if (end > l) end = l; + if (start <= end) lua_pushlstring(s+start-1, end-start+1); else lua_pushstring(""); } -static void str_lower (void) -{ +static void str_lower (void) { long l; int i; char *s = luaL_check_lstr(1, &l); -- cgit v1.2.3-55-g6feb