From 8f134c0beab0cb6cc2fb2e662516e54e8177fa87 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 28 Dec 2021 19:55:09 +0100 Subject: [PATCH] Use isdigit(3) instead of a reimplementation of it C89 defined isdigit as a function that tests for any decimal-digit character, defining the decimal digits as 0 1 2 3 4 5 6 7 8 9. I don't own a copy of C89 to check, but check in C17: 7.4.1.5 5.2.1 More specifically: > In both the source and execution basic character sets, the value > of each character after 0 in the above list of decimal digits > shall be one greater than the value of the previous. And since in ascii(7), the character after '9' is ':', it's highly unlikely that any implementation will ever accept any _decimal digit_ other than 0..9. POSIX simply defers to the ISO C standard. This is exactly what we wanted from ISDIGIT(c), so just use it. Non-standard implementations might have been slower or considered other characters as digits in the past, but let's assume implementations available today conform to ISO C89. Signed-off-by: Alejandro Colomar --- libmisc/getdate.y | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/libmisc/getdate.y b/libmisc/getdate.y index 82f1369c..0c07f746 100644 --- a/libmisc/getdate.y +++ b/libmisc/getdate.y @@ -31,15 +31,6 @@ #include #include -/* ISDIGIT differs from isdigit(3), as follows: - - Its arg may be any int or unsigned int; it need not be an unsigned char. - - It's typically faster. - Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that - only '0' through '9' are digits. Prefer ISDIGIT to isdigit(3) unless - it's important to use the locale's definition of `digit' even when the - host does not conform to Posix. */ -#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9) - #include "getdate.h" #include @@ -760,18 +751,18 @@ yylex (void) while (isspace (*yyInput)) yyInput++; - if (ISDIGIT (c = *yyInput) || c == '-' || c == '+') + if (isdigit (c = *yyInput) || c == '-' || c == '+') { if (c == '-' || c == '+') { sign = c == '-' ? -1 : 1; - if (!ISDIGIT (*++yyInput)) + if (!isdigit (*++yyInput)) /* skip the '-' sign */ continue; } else sign = 0; - for (yylval.Number = 0; ISDIGIT (c = *yyInput++);) + for (yylval.Number = 0; isdigit (c = *yyInput++);) yylval.Number = 10 * yylval.Number + c - '0'; yyInput--; if (sign < 0)