3 ms·
The worst part is that there is no way to turn off the locale sensitivity. This is bad for not only performance, but correctness. If you are writing a JSON se
by haberman 5y ago
The worst part is that there is no way to turn off the locale sensitivity.
This is bad for not only performance, but correctness. If you are writing a JSON serializer and you use sprintf() to format float/double values, someone can make you produce incorrect output by setting the locale to something that uses "," as a decimal separator.
And there is no way to turn this off!
This is one moment where the difference between an application language and a system language seems clear. An application language could maybe assume that string formatting is for showing to a user. A systems language should assume that you are implementing protocols, and that mere user preference should not change your output.
- codemac 5y agoMy joke would be there's a difference between a programmer's laptop, and an application user's laptop. Install linux on your laptop, seeing the source code is worth it.
- tedunangst 5y agoThe old way to turn it off would be setlocale() in your library before and after sprintf, but this is a disaster for threads, and so now there's the _l functions.
- deleted 5y ago[deleted]
- nwellnhof 5y agoglibc only supports a few _l functions. It doesn't support snprintf_l, for example. The solution here is uselocale.
- dhosek 5y agoKind of like a bug in PHP where identifiers with i in them didn't work properly if the locale was Turkish since PHP managed its case insensitivity by uppercasing the identifiers, but used a locale-dependent format in which, if the locale specified Turkish, would uppercase i to İ rather than I.
- smcameron 5y agoWell, you can replace setlocale() with your own setlocale that uses dlsym() to look up the "real" set locale, and your setlocale can just call the real setlocale with params (LC_ALL, "C"). That turns it off pretty well. Even if other libs call setlocale (looking at you, gtk), they'll get your version. like so: https://github.com/smcameron/space-nerds-in-space/blob/master/c-is-the-locale.c#L46 https://github.com/smcameron/space-nerds-in-space/blob/maste...
- Nitramp 5y agoChrome had a bug in how css animation values would get serialized, where you'd end up with "5,43" instead of "5.43" in many European locales. It took surprisingly long to track down, partially due to this all being dependent on various ambient Information (env vars, browser config, arguments passed, what exact function is called, etc).