Skip to content

Commit 6c5e7e1

Browse files
authored
Merge pull request swiftlang#23013 from compnerd/my-locale-is-c
Correct Floating Point locale handling on Windows
2 parents 1efe185 + 6da949d commit 6c5e7e1

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

stdlib/public/stubs/Stubs.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,20 @@ static inline locale_t getCLocale() {
155155
// as C locale.
156156
return nullptr;
157157
}
158-
#elif defined(__CYGWIN__) || defined(_WIN32) || defined(__HAIKU__)
158+
#elif defined(__CYGWIN__) || defined(__HAIKU__)
159159
// In Cygwin, getCLocale() is not used.
160+
#elif defined(_WIN32)
161+
static _locale_t makeCLocale() {
162+
_locale_t CLocale = _create_locale(LC_ALL, "C");
163+
if (!CLocale) {
164+
swift::crash("makeCLocale: _create_locale() returned a null pointer");
165+
}
166+
return CLocale;
167+
}
168+
169+
static _locale_t getCLocale() {
170+
return SWIFT_LAZY_CONSTANT(makeCLocale());
171+
}
160172
#else
161173
static locale_t makeCLocale() {
162174
locale_t CLocale = newlocale(LC_ALL_MASK, "C", nullptr);
@@ -373,41 +385,41 @@ static const char *_swift_stdlib_strtoX_clocale_impl(
373385

374386
#if defined(_WIN32)
375387
template <>
376-
static const char *
388+
const char *
377389
_swift_stdlib_strtoX_clocale_impl<float>(const char *str, float *result) {
378390
if (swift_stringIsSignalingNaN(str)) {
379391
*result = std::numeric_limits<float>::signaling_NaN();
380392
return str + std::strlen(str);
381393
}
382394

383395
char *end;
384-
*result = std::strtof(str, &end);
396+
*result = _strtof_l(str, &end, getCLocale());
385397
return end;
386398
}
387399

388400
template <>
389-
static const char *
401+
const char *
390402
_swift_stdlib_strtoX_clocale_impl<double>(const char *str, double *result) {
391403
if (swift_stringIsSignalingNaN(str)) {
392404
*result = std::numeric_limits<double>::signaling_NaN();
393405
return str + std::strlen(str);
394406
}
395407

396408
char *end;
397-
*result = std::strtod(str, &end);
409+
*result = _strtod_l(str, &end, getCLocale());
398410
return end;
399411
}
400412

401413
template <>
402-
static const char *
414+
const char *
403415
_swift_stdlib_strtoX_clocale_impl<long double>(const char *str, long double *result) {
404416
if (swift_stringIsSignalingNaN(str)) {
405417
*result = std::numeric_limits<long double>::signaling_NaN();
406418
return str + std::strlen(str);
407419
}
408420

409421
char *end;
410-
*result = std::strtold(str, &end);
422+
*result = _strtod_l(str, &end, getCLocale());
411423
return end;
412424
}
413425
#endif

0 commit comments

Comments
 (0)