Create an immortal C locale

This adds a function fish_c_locale() which returns an immortal locale_t
corresponding to the C locale, and switches builtin_printf to use wcstod_l.
This commit is contained in:
ridiculousfish
2018-07-28 17:56:42 -07:00
parent 09541e9524
commit 42c648ab35
3 changed files with 10 additions and 9 deletions

View File

@@ -271,19 +271,11 @@ template <>
long double raw_string_to_scalar_type(const wchar_t *s, wchar_t **end) {
double val = wcstod(s, end);
if (**end == L'\0') return val;
// The conversion using the user's locale failed. That may be due to the string not being a
// valid floating point value. It could also be due to the locale using different separator
// characters than the normal english convention. So try again by forcing the use of a locale
// that employs the english convention for writing floating point numbers.
//
// TODO: switch to the wcstod_l() function to avoid changing the global locale.
char *saved_locale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
val = wcstod(s, end);
setlocale(LC_NUMERIC, saved_locale);
free(saved_locale);
return val;
return wcstod_l(s, end, fish_c_locale());
}
template <typename T>