aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-16 17:29:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-16 17:29:59 -0300
commit1848bcc15b452b3a242a071ae364202f58b93b99 (patch)
treee5922f9d71923e72d3913d018100d49342aef85d
parent4d2de484f653fe46ac97fcb7e02f3cc7762cd962 (diff)
downloadlua-1848bcc15b452b3a242a071ae364202f58b93b99.tar.gz
lua-1848bcc15b452b3a242a071ae364202f58b93b99.tar.bz2
lua-1848bcc15b452b3a242a071ae364202f58b93b99.zip
"strsub" accepts negative indices (count from the end of the string).
-rw-r--r--strlib.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/strlib.c b/strlib.c
index b9e70782..39b57737 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,7 +3,7 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.40 1997/04/06 14:08:08 roberto Exp roberto $"; 6char *rcs_strlib="$Id: strlib.c,v 1.41 1997/04/06 14:17:06 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -106,9 +106,12 @@ static void str_len (void)
106static void str_sub (void) 106static void str_sub (void)
107{ 107{
108 char *s = luaL_check_string(1); 108 char *s = luaL_check_string(1);
109 long l = strlen(s);
109 long start = (long)luaL_check_number(2); 110 long start = (long)luaL_check_number(2);
110 long end = (long)luaL_opt_number(3, strlen(s)); 111 long end = (long)luaL_opt_number(3, -1);
111 if (1 <= start && start <= end && end <= strlen(s)) { 112 if (start < 0) start = l+start+1;
113 if (end < 0) end = l+end+1;
114 if (1 <= start && start <= end && end <= l) {
112 luaI_emptybuff(); 115 luaI_emptybuff();
113 addnchar(s+start-1, end-start+1); 116 addnchar(s+start-1, end-start+1);
114 lua_pushstring(luaI_addchar(0)); 117 lua_pushstring(luaI_addchar(0));