Skip to content

Commit 311482d

Browse files
committed
[NFC] Pull out the win32 implementation.
Here the template specializations can be adapted to a strto* wrapper, for use with the general function-pointer implementation of _swift_stdlib_strtoX_clocale_impl.
1 parent bf0a183 commit 311482d

File tree

1 file changed

+26
-58
lines changed

1 file changed

+26
-58
lines changed

stdlib/public/stubs/Stubs.cpp

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,25 @@ static float swift_strtof_l(const char *nptr, char **endptr, locale_t loc) {
387387
#define strtod_l swift_strtod_l
388388
#define strtof_l swift_strtof_l
389389
#endif
390+
#elif defined(_WIN32)
391+
static double swift_strtod_l(const char *nptr, char **endptr, locale_t loc) {
392+
return _strtod_l(str, &end, getCLocale());
393+
}
394+
395+
static float swift_strtof_l(const char *nptr, char **endptr, locale_t loc) {
396+
return _strtof_l(str, &end, getCLocale());
397+
}
398+
399+
static long double swift_strtold_l(const char *nptr, char **endptr,
400+
locale_t loc) {
401+
return _strtod_l(str, &end, getCLocale());
402+
}
403+
#define strtod_l swift_strtod_l
404+
#define strtof_l swift_strtof_l
405+
#define strtold_l swift_strtold_l
390406
#endif
391407

392-
#if defined(__CYGWIN__) || defined(_WIN32) || defined(__HAIKU__)
408+
#if defined(__CYGWIN__) || defined(__HAIKU__)
393409
// Cygwin does not support uselocale(), but we can use the locale feature
394410
// in stringstream object.
395411
template <typename T>
@@ -415,62 +431,6 @@ static const char *_swift_stdlib_strtoX_clocale_impl(
415431
return nptr + pos;
416432
}
417433

418-
#if defined(_WIN32)
419-
template <>
420-
const char *
421-
_swift_stdlib_strtoX_clocale_impl<float>(const char *str, float *result) {
422-
if (swift_stringIsSignalingNaN(str)) {
423-
*result = std::numeric_limits<float>::signaling_NaN();
424-
return str + std::strlen(str);
425-
}
426-
427-
char *end;
428-
_set_errno(0);
429-
*result = _strtof_l(str, &end, getCLocale());
430-
if (*result == HUGE_VALF || *result == -HUGE_VALF || *result == 0.0 || *result == -0.0) {
431-
if (errno == ERANGE)
432-
end = nullptr;
433-
}
434-
return end;
435-
}
436-
437-
template <>
438-
const char *
439-
_swift_stdlib_strtoX_clocale_impl<double>(const char *str, double *result) {
440-
if (swift_stringIsSignalingNaN(str)) {
441-
*result = std::numeric_limits<double>::signaling_NaN();
442-
return str + std::strlen(str);
443-
}
444-
445-
char *end;
446-
_set_errno(0);
447-
*result = _strtod_l(str, &end, getCLocale());
448-
if (*result == HUGE_VAL || *result == -HUGE_VAL || *result == 0.0 || *result == -0.0) {
449-
if (errno == ERANGE)
450-
end = nullptr;
451-
}
452-
return end;
453-
}
454-
455-
template <>
456-
const char *
457-
_swift_stdlib_strtoX_clocale_impl<long double>(const char *str, long double *result) {
458-
if (swift_stringIsSignalingNaN(str)) {
459-
*result = std::numeric_limits<long double>::signaling_NaN();
460-
return str + std::strlen(str);
461-
}
462-
463-
char *end;
464-
_set_errno(0);
465-
*result = _strtod_l(str, &end, getCLocale());
466-
if (*result == HUGE_VALL || *result == -HUGE_VALL || *result == 0.0 || *result == -0.0) {
467-
if (errno == ERANGE)
468-
end = nullptr;
469-
}
470-
return end;
471-
}
472-
#endif
473-
474434
const char *swift::_swift_stdlib_strtold_clocale(
475435
const char *nptr, void *outResult) {
476436
return _swift_stdlib_strtoX_clocale_impl(
@@ -488,6 +448,14 @@ const char *swift::_swift_stdlib_strtof_clocale(
488448
}
489449
#else
490450

451+
static inline void _swift_set_errno(int to) {
452+
#if defined(_WIN32)
453+
_set_errno(0);
454+
#else
455+
errno = 0;
456+
#endif
457+
}
458+
491459
// We can't return Float80, but we can receive a pointer to one, so
492460
// switch the return type and the out parameter on strtold.
493461
template <typename T>
@@ -503,7 +471,7 @@ static const char *_swift_stdlib_strtoX_clocale_impl(
503471
}
504472

505473
char *EndPtr;
506-
errno = 0;
474+
_swift_set_errno(0);
507475
const auto result = posixImpl(nptr, &EndPtr, getCLocale());
508476
*outResult = result;
509477
if (result == huge || result == -huge || result == 0.0 || result == -0.0) {

0 commit comments

Comments
 (0)