diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-08-06 14:35:38 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-08-06 14:35:38 -0300 |
commit | c0fdaf58420a4c224f7d0167465b40b7bf91e934 (patch) | |
tree | db3864db6b00a4b00bd1e626eae1e3c5bef4d64e | |
parent | 2430d3cb28908fbca01001041a34d250f9668167 (diff) | |
download | lua-c0fdaf58420a4c224f7d0167465b40b7bf91e934.tar.gz lua-c0fdaf58420a4c224f7d0167465b40b7bf91e934.tar.bz2 lua-c0fdaf58420a4c224f7d0167465b40b7bf91e934.zip |
string.byte may return multiple values
-rw-r--r-- | lstrlib.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.103 2004/06/08 14:31:15 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.104 2004/07/09 18:24:41 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -107,11 +107,16 @@ static int str_rep (lua_State *L) { | |||
107 | static int str_byte (lua_State *L) { | 107 | static int str_byte (lua_State *L) { |
108 | size_t l; | 108 | size_t l; |
109 | const char *s = luaL_checklstring(L, 1, &l); | 109 | const char *s = luaL_checklstring(L, 1, &l); |
110 | sint32 pos = posrelat(luaL_optinteger(L, 2, 1), l); | 110 | sint32 posi = posrelat(luaL_optinteger(L, 2, 1), l); |
111 | if (pos <= 0 || (size_t)(pos) > l) /* index out of range? */ | 111 | sint32 pose = posrelat(luaL_optinteger(L, 3, posi), l); |
112 | return 0; /* no answer */ | 112 | int n, i; |
113 | lua_pushinteger(L, uchar(s[pos-1])); | 113 | if (!(0 < posi && posi <= pose && (size_t)pose <= l)) |
114 | return 1; | 114 | return 0; /* index out of range; no answer */ |
115 | n = pose - posi + 1; | ||
116 | luaL_checkstack(L, n, "string slice too long"); | ||
117 | for (i=0; i<n; i++) | ||
118 | lua_pushinteger(L, uchar(s[posi+i-1])); | ||
119 | return n; | ||
115 | } | 120 | } |
116 | 121 | ||
117 | 122 | ||