Repository navigation
lib: parse and format floats without switching the thread locale - #314
Conversation
|
I was meaning to look into the locale handling. I think there is a better solution for formatting and parsing floating point values that doesn't require switching the current locale at all. Most platforms have strtod_l() (_strtod_l() on Windows, which can be aliased to strtod_l() in wincompat.h) for converting string to double, for a given locale. MacOS and Windows have snprintf_l() / _snprintf_l() for going the other way, double to string. Linux doesn't have that, but we could instead use normal snprintf() and then just post-process the string to change the radix symbol from localeconv()->decimal_point to '.' |
config_read*() and config_write() switched the calling thread to the "C" numeric locale for the duration of the call, and the restore didn't put back what the thread had: on Windows it always disabled per-thread locales, so a thread that had set its own numeric locale (say de-DE) came back on the process-wide one, and on POSIX it always moved the thread to LC_GLOBAL_LOCALE, dropping any locale it had installed with uselocale(). Stop touching the thread's locale at all. Floats are parsed with strtod_l() (_strtod_l() on Windows) against a "C" numeric locale object created for each read or write, and formatted with snprintf_l() on macOS and _snprintf_l() on Windows. Where there's no snprintf_l(), they are formatted with snprintf() and the locale's radix character from localeconv() is replaced with '.'. The new ThreadLocaleUnchanged test reads and writes a float while the thread has its own locale (de_DE where installed) and checks both the value and the text, and that the thread's locale is unchanged afterwards.
353c32d to
fbc0e82
Compare
|
agreed, that's cleaner. reworked it that way and force-pushed (also rebased on master, it was conflicting after #315). the thread's locale isn't touched at all now. parsing uses strtod_l() (aliased to _strtod_l() in wincompat.h) against a "C" LC_NUMERIC locale that's created per config_read/config_write call, so there's still no global state. formatting uses snprintf_l() on macOS, _snprintf_l() on Windows, and snprintf() with localeconv()->decimal_point swapped for '.' everywhere else. I added a strtod_l check to configure.ac and CMake, and without it parsing falls back to plain strtod() like before. the test now reads and writes a float with a de_DE thread locale and checks the value, the written text and that the locale is unchanged. it passes on macOS and glibc (cmake and autotools). I haven't been able to run the msvc branch on windows yet, so that part is worth a second look. |
| size_t len = strlen(radix); | ||
|
|
||
| *p = '.'; | ||
| memmove(p + 1, p + len, strlen(p + len) + 1); |
There was a problem hiding this comment.
done, the memmove is only there for a multibyte radix now. I also got to build it with msvc on windows in the meantime, and the full suite including ThreadLocaleUnchanged passes there.
|
looks good. thanks for doing this! |
config_read*()/config_write()switched the calling thread to the "C" numeric locale and the restore didn't put back what the thread had (on Windows it always disabled per-thread locales, on POSIX it always moved the thread toLC_GLOBAL_LOCALE), so a thread with its own locale came back with its number formatting changed under it. This drops the locale switching entirely: floats are parsed withstrtod_l()(_strtod_l()on Windows) against a "C" numeric locale object created per read/write call, and formatted withsnprintf_l()on macOS,_snprintf_l()on Windows, and elsewhere withsnprintf()plus replacinglocaleconv()->decimal_pointwith.. There's a newstrtod_lcheck in configure.ac and CMake; without it parsing falls back to plainstrtod()as before. The newThreadLocaleUnchangedtest reads and writes a float with a de_DE thread locale and checks the value, the written text, and that the thread's locale is unchanged; it fails on current master.