Module Name: src Committed By: rillig Date: Sun Nov 8 22:22:04 UTC 2020
Modified Files: src/usr.bin/make: cond.c Log Message: make(1): clean up TryParseNumber in conditions More descriptive variable names, more appropriate literals for comparisons, one task per paragraph of code. To generate a diff of this commit: cvs rdiff -u -r1.188 -r1.189 src/usr.bin/make/cond.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/cond.c diff -u src/usr.bin/make/cond.c:1.188 src/usr.bin/make/cond.c:1.189 --- src/usr.bin/make/cond.c:1.188 Sun Nov 8 21:10:18 2020 +++ src/usr.bin/make/cond.c Sun Nov 8 22:22:03 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: cond.c,v 1.188 2020/11/08 21:10:18 rillig Exp $ */ +/* $NetBSD: cond.c,v 1.189 2020/11/08 22:22:03 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -93,7 +93,7 @@ #include "dir.h" /* "@(#)cond.c 8.2 (Berkeley) 1/2/94" */ -MAKE_RCSID("$NetBSD: cond.c,v 1.188 2020/11/08 21:10:18 rillig Exp $"); +MAKE_RCSID("$NetBSD: cond.c,v 1.189 2020/11/08 22:22:03 rillig Exp $"); /* * The parsing of conditional expressions is based on this grammar: @@ -336,40 +336,41 @@ FuncCommands(size_t argLen MAKE_ATTR_UNU return gn != NULL && GNode_IsTarget(gn) && !Lst_IsEmpty(gn->commands); } -/*- +/* * Convert the given number into a double. * We try a base 10 or 16 integer conversion first, if that fails * then we try a floating point conversion instead. * * Results: - * Sets 'value' to double value of string. * Returns TRUE if the conversion succeeded. + * Sets 'out_value' to the converted number. */ static Boolean -TryParseNumber(const char *str, double *value) +TryParseNumber(const char *str, double *out_value) { - char *eptr, ech; - unsigned long l_val; - double d_val; + char *end; + unsigned long ul_val; + double dbl_val; errno = 0; - if (!*str) { - *value = 0.0; + if (str[0] == '\0') { /* XXX: why is an empty string a number? */ + *out_value = 0.0; return TRUE; } - l_val = strtoul(str, &eptr, str[1] == 'x' ? 16 : 10); - ech = *eptr; - if (ech == '\0' && errno != ERANGE) { - d_val = str[0] == '-' ? -(double)-l_val : (double)l_val; - } else { - if (ech != '\0' && ech != '.' && ech != 'e' && ech != 'E') - return FALSE; - d_val = strtod(str, &eptr); - if (*eptr) - return FALSE; + + ul_val = strtoul(str, &end, str[1] == 'x' ? 16 : 10); + if (*end == '\0' && errno != ERANGE) { + *out_value = str[0] == '-' ? -(double)-ul_val : (double)ul_val; + return TRUE; } - *value = d_val; + if (*end != '\0' && *end != '.' && *end != 'e' && *end != 'E') + return FALSE; /* skip the expensive strtod call */ + dbl_val = strtod(str, &end); + if (*end != '\0') + return FALSE; + + *out_value = dbl_val; return TRUE; }