strtod_nan_main.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Convert string for NaN payload to corresponding NaN.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ieee754.h>
  16. #include <locale.h>
  17. #include <math.h>
  18. #include <stdlib.h>
  19. #include <wchar.h>
  20. /* If STR starts with an optional n-char-sequence as defined by ISO C
  21. (a sequence of ASCII letters, digits and underscores), followed by
  22. ENDC, return a NaN whose payload is set based on STR. Otherwise,
  23. return a default NAN. If ENDPTR is not NULL, set *ENDPTR to point
  24. to the character after the initial n-char-sequence. */
  25. FLOAT
  26. STRTOD_NAN (const STRING_TYPE *str, STRING_TYPE **endptr, STRING_TYPE endc)
  27. {
  28. const STRING_TYPE *cp = str;
  29. while ((*cp >= L_('0') && *cp <= L_('9'))
  30. || (*cp >= L_('A') && *cp <= L_('Z'))
  31. || (*cp >= L_('a') && *cp <= L_('z'))
  32. || *cp == L_('_'))
  33. ++cp;
  34. FLOAT retval = NAN;
  35. if (*cp != endc)
  36. goto out;
  37. /* This is a system-dependent way to specify the bitmask used for
  38. the NaN. We expect it to be a number which is put in the
  39. mantissa of the number. */
  40. STRING_TYPE *endp;
  41. unsigned long long int mant;
  42. mant = STRTOULL (str, &endp, 0);
  43. if (endp == cp)
  44. SET_NAN_PAYLOAD (retval, mant);
  45. out:
  46. if (endptr != NULL)
  47. *endptr = (STRING_TYPE *) cp;
  48. return retval;
  49. }
  50. libc_hidden_def (STRTOD_NAN)