new function lua_stringtonumber
Closed this issue · 4 comments
http://www.lua.org/manual/5.3/manual.html#lua_stringtonumber
size_t lua_stringtonumber (lua_State *L, const char *s);
Converts the zero-terminated string s to a number, pushes that number into the stack, and returns the total size of the string, that is, its length plus one. The conversion can result in an integer or a float, according to the lexical conventions of Lua (see §3.1). The string may have leading and trailing spaces and a sign. If the string is not a valid numeral, returns 0 and pushes nothing. (Note that the result can be used as a boolean, true if the conversion succeeds.)
So pretty much strtod
.
Make sure to read Mike Pall's comments on the poor state of strtod
functions: http://repo.or.cz/w/luajit-2.0.git/blob/HEAD:/src/lj_strscan.c
First shot available now, though I haven't read mikes comments yet. Will revisit later.
I have read Mike's comment, but his implementation is too complicated for my taste. So I took the easy way out (like PUC-Rio Lua does) and let the developer decide which implementation to choose (by setting the lua_str2number
macro in luaconf.h
). The downside is that we might not get hexadecimal float literals (unless strtod
has support for them).
Okay :)