strutil: handle empty fractional in parse rational

Accept numbers like "123." where the period (dot) is present yet the
fractional part is empty. Adding a period but no additional digits is a
popular method of turning an otherwise integer literal into a float.
Compilers and strtod() routines accept this notation, too, so we have to
expect seeing such input.
This commit is contained in:
Gerhard Sittig 2018-07-19 21:05:54 +02:00 committed by Uwe Hermann
parent dd3202febf
commit 42408643f9
1 changed files with 7 additions and 0 deletions

View File

@ -631,8 +631,15 @@ SR_API int sr_parse_rational(const char *str, struct sr_rational *ret)
errno = 0; errno = 0;
if (*endptr == '.') { if (*endptr == '.') {
bool is_exp, is_eos;
const char *start = endptr + 1; const char *start = endptr + 1;
fractional = g_ascii_strtoll(start, &endptr, 10); fractional = g_ascii_strtoll(start, &endptr, 10);
is_exp = *endptr == 'E' || *endptr == 'e';
is_eos = *endptr == '\0';
if (endptr == start && (is_exp || is_eos)) {
fractional = 0;
errno = 0;
}
if (errno) if (errno)
return SR_ERR; return SR_ERR;
fractional_len = endptr - start; fractional_len = endptr - start;