lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2016-04-26 5:30 GMT+02:00 Daurnimator <quae@daurnimator.com>:

> I had a look into how lua does this locale independently and found
> this function:
> http://www.lua.org/source/5.3/llex.c.html#trydecpoint
> ==> if parsing in the current locale fails, it tries replacing "."
> with whatever the current locale's decimal separator is.
> This means was suprising to me and has a number of consequences:
>   - Using a seperator such as "," is impossible in the first place as:
>       -  A number has 'ended' if it doesn't match: `else if
> (ls->current == '.')` in
> http://www.lua.org/source/5.3/llex.c.html#read_numeral
>       - Numbers without a leading zero using a non-"." separator (e.g.
> ,2) would never work due to the `case '.'` in the lexer.
>   - If running in a locale where '.' is not the decimal separator,
> parsing lua could result in *many* calls to `localeconv`.
>   - localeconv is not threadsafe, which means that if another thread
> changes the locale while lua is parsing, "interesting" results could
> occur.
>
> I'd suggest that future versions of lua use 'strtod_l' and friends
> (the locale independent variants) where available, and only fall back
> to the `localeconv` hack if compiling in C89 mode.
>   - strtod_l is available on linux (at least both glibc and musl) if
> _GNU_SOURCE is defined
>   - _strtod_l is available in MSVC since VS2005
>   - strtod_l is available on OSX since Darwin 8.
>   - strtod_l is available on FreeBSD since 9.1
>
> Fixing lua_str2number to be locale independent will:
>   - allow the `trydecpoint` hack to be removed from llex.c
>   - fix tonumber() to *not* be locale dependant.

+1.