36
36
#endif
37
37
#include < climits>
38
38
#include < clocale>
39
+ #include < cmath>
39
40
#include < cstdarg>
40
41
#include < cstdint>
41
42
#include < cstdio>
42
43
#include < cstdlib>
43
44
#include < cstring>
44
- #if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__linux__) || defined(__wasi__)
45
- #include < locale.h>
46
- #elif defined(__CYGWIN__) || defined(__HAIKU__) || defined(_WIN32)
47
45
#include < sstream>
48
- #include < cmath>
46
+ #if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__linux__) || defined(__wasi__) || defined(_WIN32)
47
+ #include < locale.h>
48
+ #if defined(_WIN32)
49
+ #define locale_t _locale_t
50
+ #endif
49
51
#else
50
52
#include < xlocale.h>
51
53
#endif
@@ -351,52 +353,61 @@ static bool swift_stringIsSignalingNaN(const char *nptr) {
351
353
return strcasecmp (nptr, " snan" ) == 0 ;
352
354
}
353
355
354
- #if defined(__OpenBSD__)
355
- static double swift_strtod_l (const char *nptr, char **endptr, locale_t loc) {
356
- return strtod (nptr, endptr);
357
- }
358
- #elif defined(__ANDROID_API__) && __ANDROID_API__ < 26
359
- static double swift_strtod_l (const char *nptr, char **endptr, locale_t loc) {
360
- return strtod (nptr, endptr);
356
+ // This implementation should only be used on platforms without the
357
+ // relevant strto* functions, such as Cygwin or Haiku.
358
+ // Note that using this currently causes test failures.
359
+ template <typename T>
360
+ T _swift_strto (const char *nptr, char **endptr) {
361
+ std::istringstream ValueStream (nptr);
362
+ ValueStream.imbue (std::locale::classic ());
363
+ T ParsedValue;
364
+ ValueStream >> ParsedValue;
365
+
366
+ std::streamoff pos = ValueStream.tellg ();
367
+ if (ValueStream.eof ())
368
+ pos = static_cast <std::streamoff>(strlen (nptr));
369
+ if (pos <= 0 ) {
370
+ errno = ERANGE;
371
+ return 0.0 ;
372
+ }
373
+
374
+ return ParsedValue;
361
375
}
362
- # elif defined(_WIN32)
376
+
363
377
static double swift_strtod_l (const char *nptr, char **endptr, locale_t loc) {
378
+ #if defined(_WIN32)
364
379
return _strtod_l (nptr, endptr, getCLocale ());
365
- }
380
+ #elif defined(__CYGWIN__) || defined(__HAIKU__)
381
+ return _swift_strto<double >(nptr, endptr);
382
+ #else
383
+ return strtod (nptr, endptr);
366
384
#endif
367
-
368
- #if defined(__OpenBSD__)
369
- static float swift_strtof_l (const char *nptr, char **endptr, locale_t loc) {
370
- return strtof (nptr, endptr);
371
- }
372
- #elif defined(__ANDROID_API__) && __ANDROID_API__ < 26
373
- static float swift_strtof_l (const char *nptr, char **endptr, locale_t loc) {
374
- return strtof (nptr, endptr);
375
385
}
376
- # elif defined(_WIN32)
386
+
377
387
static float swift_strtof_l (const char *nptr, char **endptr, locale_t loc) {
388
+ #if defined(_WIN32)
378
389
return _strtof_l (nptr, endptr, getCLocale ());
379
- }
390
+ #elif defined(__CYGWIN__) || defined(__HAIKU__)
391
+ return _swift_strto<float >(nptr, endptr);
392
+ #else
393
+ return strtof (nptr, endptr);
380
394
#endif
381
-
382
- #if defined(__OpenBSD__)
383
- static long double swift_strtold_l (const char *nptr, char **endptr,
384
- locale_t loc) {
385
- return strtold (nptr, endptr);
386
- }
387
- #elif defined(__ANDROID_API__) && __ANDROID_API__ < 21
388
- static long double swift_strtold_l (const char *nptr, char **endptr,
389
- locale_t loc) {
390
- return strtod (nptr, endptr);
391
395
}
392
- # elif defined(_WIN32)
396
+
393
397
static long double swift_strtold_l (const char *nptr, char **endptr,
394
398
locale_t loc) {
399
+ #if defined(_WIN32)
395
400
return _strtod_l (nptr, endptr, getCLocale ());
396
- }
401
+ #elif defined(__ANDROID__)
402
+ return strtod (nptr, endptr);
403
+ #elif defined(__CYGWIN__) || defined(__HAIKU__)
404
+ return _swift_strto<long double >(nptr, endptr);
405
+ #else
406
+ return strtold (nptr, endptr);
397
407
#endif
408
+ }
398
409
399
- #if defined(__OpenBSD__) || defined(_WIN32)
410
+ #if defined(__OpenBSD__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__HAIKU__)
400
411
#define strtod_l swift_strtod_l
401
412
#define strtof_l swift_strtof_l
402
413
#define strtold_l swift_strtold_l
@@ -409,55 +420,8 @@ static long double swift_strtold_l(const char *nptr, char **endptr,
409
420
#define strtod_l swift_strtod_l
410
421
#define strtof_l swift_strtof_l
411
422
#endif
412
- #elif defined(_WIN32)
413
- #define strtod_l swift_strtod_l
414
- #define strtof_l swift_strtof_l
415
- #define strtold_l swift_strtold_l
416
423
#endif
417
424
418
- #if defined(__CYGWIN__) || defined(__HAIKU__)
419
- // Cygwin does not support uselocale(), but we can use the locale feature
420
- // in stringstream object.
421
- template <typename T>
422
- static const char *_swift_stdlib_strtoX_clocale_impl (
423
- const char *nptr, T *outResult) {
424
- if (swift_stringIsSignalingNaN (nptr)) {
425
- *outResult = std::numeric_limits<T>::signaling_NaN ();
426
- return nptr + std::strlen (nptr);
427
- }
428
-
429
- std::istringstream ValueStream (nptr);
430
- ValueStream.imbue (std::locale::classic ());
431
- T ParsedValue;
432
- ValueStream >> ParsedValue;
433
- *outResult = ParsedValue;
434
-
435
- std::streamoff pos = ValueStream.tellg ();
436
- if (ValueStream.eof ())
437
- pos = static_cast <std::streamoff>(strlen (nptr));
438
- if (pos <= 0 )
439
- return nullptr ;
440
-
441
- return nptr + pos;
442
- }
443
-
444
- const char *swift::_swift_stdlib_strtold_clocale (
445
- const char *nptr, void *outResult) {
446
- return _swift_stdlib_strtoX_clocale_impl (
447
- nptr, static_cast <long double *>(outResult));
448
- }
449
-
450
- const char *swift::_swift_stdlib_strtod_clocale (
451
- const char * nptr, double *outResult) {
452
- return _swift_stdlib_strtoX_clocale_impl (nptr, outResult);
453
- }
454
-
455
- const char *swift::_swift_stdlib_strtof_clocale (
456
- const char * nptr, float *outResult) {
457
- return _swift_stdlib_strtoX_clocale_impl (nptr, outResult);
458
- }
459
- #else
460
-
461
425
static inline void _swift_set_errno (int to) {
462
426
#if defined(_WIN32)
463
427
_set_errno (0 );
@@ -508,7 +472,6 @@ const char *swift::_swift_stdlib_strtof_clocale(
508
472
return _swift_stdlib_strtoX_clocale_impl (
509
473
nptr, outResult, HUGE_VALF, strtof_l);
510
474
}
511
- #endif
512
475
513
476
const char *swift::_swift_stdlib_strtof16_clocale (
514
477
const char * nptr, __fp16 *outResult) {
0 commit comments