[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to extract a floating point number locale-independantly
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 26 Apr 2016 15:21:39 -0300
> Doesn't it get called once per floating point number encountered while lexing?
No; it caches the 'decpoint' in the 'decpoint' field. It only calls
'trydecpoint' (and then 'lua_getlocaledecpoint') if the conversion
fails *after* replacing the dot with the cached 'decpoint'.
> I guess you could do something like:
>
> local function safe_tonumber(s, b)
> if b ~= nil then
> return tonumber(s, b)
> else
> local func = load("return " .. s)
> if not func then return nil end
> return func()
> end
> end
>
> Which is slightly less bad than the example earlier that uses setlocale.
> But still.... I consider this an issue with lua.
This seems quite inefficient.
Couldn't you do what Lua does? Something along these lines:
local function safe_tonumber(s)
local locale_dec_point = string.sub(tostring(1.1), 2, 2)
s = string.gsub(s, "%.", locale_dec_point)
return tonumber(s)
end
(You might try to avoid computing 'locale_dec_point' every time,
doing what Lua does: cache its result once and update when conversion
fails...)
-- Roberto