diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-01 16:41:25 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-01 16:41:25 -0200 |
commit | c64f36ab2bec5b156329edc5d16a27226b598852 (patch) | |
tree | f1c449425f46120bc72d2b03027ed9ef75b62164 | |
parent | e4830ddce3d477f51ecf1da447dd8e361d74a1a0 (diff) | |
download | lua-c64f36ab2bec5b156329edc5d16a27226b598852.tar.gz lua-c64f36ab2bec5b156329edc5d16a27226b598852.tar.bz2 lua-c64f36ab2bec5b156329edc5d16a27226b598852.zip |
better behavior for "strsub" when indices are out-of-range
-rw-r--r-- | lstrlib.c | 18 |
1 files changed, 8 insertions, 10 deletions
@@ -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 | ||
35 | static void closeandpush (void) | 35 | static void closeandpush (void) { |
36 | { | ||
37 | lua_pushlstring(luaL_buffer(), luaL_getsize()); | 36 | lua_pushlstring(luaL_buffer(), luaL_getsize()); |
38 | } | 37 | } |
39 | 38 | ||
40 | 39 | ||
41 | static long posrelat (long pos, long len) | 40 | static 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 | ||
48 | static void str_sub (void) | 46 | static 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 | ||
60 | static void str_lower (void) | 59 | static 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); |