Skip to content

Commit a227273

Browse files
committed
stdlib: correct floating point parsing behaviour
These are supposed to be processed in the C locale always, irrespective of the current locale. We were not doing this and so we would parse the value incorrectly.
1 parent b36c77c commit a227273

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

stdlib/public/stubs/Stubs.cpp

Lines changed: 16 additions & 4 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);
@@ -381,7 +393,7 @@ _swift_stdlib_strtoX_clocale_impl<float>(const char *str, float *result) {
381393
}
382394

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

@@ -394,7 +406,7 @@ _swift_stdlib_strtoX_clocale_impl<double>(const char *str, double *result) {
394406
}
395407

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

@@ -407,7 +419,7 @@ _swift_stdlib_strtoX_clocale_impl<long double>(const char *str, long double *res
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)