Blame stdlib/strtod_nan_main.c

Packit 6c4009
/* Convert string for NaN payload to corresponding NaN.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* If STR starts with an optional n-char-sequence as defined by ISO C
Packit 6c4009
   (a sequence of ASCII letters, digits and underscores), followed by
Packit 6c4009
   ENDC, return a NaN whose payload is set based on STR.  Otherwise,
Packit 6c4009
   return a default NAN.  If ENDPTR is not NULL, set *ENDPTR to point
Packit 6c4009
   to the character after the initial n-char-sequence.  */
Packit 6c4009
Packit 6c4009
FLOAT
Packit 6c4009
STRTOD_NAN (const STRING_TYPE *str, STRING_TYPE **endptr, STRING_TYPE endc)
Packit 6c4009
{
Packit 6c4009
  const STRING_TYPE *cp = str;
Packit 6c4009
Packit 6c4009
  while ((*cp >= L_('0') && *cp <= L_('9'))
Packit 6c4009
	 || (*cp >= L_('A') && *cp <= L_('Z'))
Packit 6c4009
	 || (*cp >= L_('a') && *cp <= L_('z'))
Packit 6c4009
	 || *cp == L_('_'))
Packit 6c4009
    ++cp;
Packit 6c4009
Packit 6c4009
  FLOAT retval = NAN;
Packit 6c4009
  if (*cp != endc)
Packit 6c4009
    goto out;
Packit 6c4009
Packit 6c4009
  /* This is a system-dependent way to specify the bitmask used for
Packit 6c4009
     the NaN.  We expect it to be a number which is put in the
Packit 6c4009
     mantissa of the number.  */
Packit 6c4009
  STRING_TYPE *endp;
Packit 6c4009
  unsigned long long int mant;
Packit 6c4009
Packit 6c4009
  mant = STRTOULL (str, &endp, 0);
Packit 6c4009
  if (endp == cp)
Packit 6c4009
    SET_NAN_PAYLOAD (retval, mant);
Packit 6c4009
Packit 6c4009
 out:
Packit 6c4009
  if (endptr != NULL)
Packit 6c4009
    *endptr = (STRING_TYPE *) cp;
Packit 6c4009
  return retval;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (STRTOD_NAN)