Convert a string into a double. Returns 1 if the string could be parsed * into a (non-overflowing) double, 0 otherwise. The value will be set to * the parsed value when appropriate. * * Note that this function demands that the string strictly represents * a double: no spaces or other characters before or after the string * representing the number are accepted. */
| 492 | * a double: no spaces or other characters before or after the string |
| 493 | * representing the number are accepted. */ |
| 494 | int string2ld(const char *s, size_t slen, long double *dp) { |
| 495 | char buf[MAX_LONG_DOUBLE_CHARS]; |
| 496 | long double value; |
| 497 | char *eptr; |
| 498 | |
| 499 | if (slen == 0 || slen >= sizeof(buf)) return 0; |
| 500 | memcpy(buf,s,slen); |
| 501 | buf[slen] = '\0'; |
| 502 | |
| 503 | errno = 0; |
| 504 | value = strtold(buf, &eptr); |
| 505 | if (isspace(buf[0]) || eptr[0] != '\0' || |
| 506 | (size_t)(eptr-buf) != slen || |
| 507 | (errno == ERANGE && |
| 508 | (value == HUGE_VAL || value == -HUGE_VAL || value == 0)) || |
| 509 | errno == EINVAL || |
| 510 | isnan(value)) |
| 511 | return 0; |
| 512 | |
| 513 | if (dp) *dp = value; |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | /* Convert a string into a double. Returns 1 if the string could be parsed |
| 518 | * into a (non-overflowing) double, 0 otherwise. The value will be set to |
no test coverage detected