aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lstrlib.c18
1 files 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 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.19 1998/07/12 16:13:45 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.20 1998/11/10 19:38:12 roberto Exp roberto $
3** Standard library for strings and pattern-matching 3** Standard library for strings and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -32,33 +32,31 @@ static void str_len (void)
32} 32}
33 33
34 34
35static void closeandpush (void) 35static void closeandpush (void) {
36{
37 lua_pushlstring(luaL_buffer(), luaL_getsize()); 36 lua_pushlstring(luaL_buffer(), luaL_getsize());
38} 37}
39 38
40 39
41static long posrelat (long pos, long len) 40static long posrelat (long pos, long len) {
42{
43 /* relative string position: negative means back from end */ 41 /* relative string position: negative means back from end */
44 return (pos>=0) ? pos : len+pos+1; 42 return (pos>=0) ? pos : len+pos+1;
45} 43}
46 44
47 45
48static void str_sub (void) 46static void str_sub (void) {
49{
50 long l; 47 long l;
51 char *s = luaL_check_lstr(1, &l); 48 char *s = luaL_check_lstr(1, &l);
52 long start = posrelat(luaL_check_number(2), l); 49 long start = posrelat(luaL_check_number(2), l);
53 long end = posrelat(luaL_opt_number(3, -1), l); 50 long end = posrelat(luaL_opt_number(3, -1), l);
54 if (1 <= start && start <= end && end <= l) 51 if (start < 1) start = 1;
52 if (end > l) end = l;
53 if (start <= end)
55 lua_pushlstring(s+start-1, end-start+1); 54 lua_pushlstring(s+start-1, end-start+1);
56 else lua_pushstring(""); 55 else lua_pushstring("");
57} 56}
58 57
59 58
60static void str_lower (void) 59static void str_lower (void) {
61{
62 long l; 60 long l;
63 int i; 61 int i;
64 char *s = luaL_check_lstr(1, &l); 62 char *s = luaL_check_lstr(1, &l);