Reputation: 8204
I'm debugging someone else's code. I don't know Lua well. I would like to know if a negative return value is even possible from string.byte.
Upvotes: 1
Views: 1060
Reputation: 3219
if you have ANY questions regarding lua inner workings, its always easiest to just check the source: http://www.lua.org/source/5.1/lstrlib.c.html#str_byte (and yes, I do realize that understanding this requires a bit of work ;) )
Upvotes: 0
Reputation: 124
No. The range of string.byte( ) should be 0..255 inclusive; the documentation does not specify, but the source code is clear:
static int str_byte (lua_State *L) {
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
int n, i;
if (posi <= 0) posi = 1;
if ((size_t)pose > l) pose = l;
if (posi > pose) return 0; /* empty interval; return no values */
n = (int)(pose - posi + 1);
if (posi + n <= pose) /* overflow? */
luaL_error(L, "string slice too long");
luaL_checkstack(L, n, "string slice too long");
for (i=0; i<n; i++)
lua_pushinteger(L, uchar(s[posi+i-1]));
return n;
}
from lua-5.1.4/src/strlib.c, (C) 1994-2008 Lua.org; by BSD license
The important line is the call to lua_pushinteger, which is used to return an integer value to the calling function, and uchar which coerces the value to one in the range of 0..255.
Upvotes: 3
Reputation: 76577
It doesn't appear that it can return negative values - however I haven't worked with it much.
Here are some documentation links that might be helpful:
http://lua-users.org/wiki/StringLibraryTutorial
http://www.lua.org/manual/5.1/manual.html
Upvotes: 1