summaryrefslogtreecommitdiff
path: root/toke.c
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2014-08-21 17:29:10 -0600
committerKarl Williamson <khw@cpan.org>2014-08-22 12:14:59 -0600
commit305b86516461e93877909338ac3642c6ac09b651 (patch)
treed5d7b3c47a3af1537ffb549ee3eacf937bcd9571 /toke.c
parentb51533f3c738c0d34d686dc15720c781f1043802 (diff)
downloadperl-305b86516461e93877909338ac3642c6ac09b651.tar.gz
Add and use macros for case-insensitive comparison
This adds to handy.h isALPHA_FOLD_EQ(c1,c2) which efficiently tests if c1 and c2 are the same character, case-insensitively. For example isALPHA_FOLD_EQ(c, 's') returns true if and only if <c> is 's' or 'S'. isALPHA_FOLD_NE() is also added by this commit. At least one of c1 and c2 must be known to be in [A-Za-z] or this macro doesn't work properly. (There is an assert for this in the macro in DEBUGGING builds). That is why the name includes "ALPHA", so you won't forget when using it. This functionality has been in regcomp.c for a while, under a different name. I had thought that the only reason to make it more generally available was potential speed gain, but recent gcc versions optimize to the same code, so I thought there wasn't any point to doing so. But I now think that using this makes things easier to read (and certainly shorter to type in). Once you grok what this macro does, it simplifies what you have to keep in your mind when reading logical expressions with multiple operands. That something can be either upper or lower case can be a distraction to understanding the larger point of the expression.
Diffstat (limited to 'toke.c')
-rw-r--r--toke.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/toke.c b/toke.c
index d2e9eee735..dee6f42ff1 100644
--- a/toke.c
+++ b/toke.c
@@ -4788,7 +4788,7 @@ Perl_yylex(pTHX)
* line contains "Perl" rather than "perl" */
if (!d) {
for (d = ipathend-4; d >= ipath; --d) {
- if ((*d == 'p' || *d == 'P')
+ if (isALPHA_FOLD_EQ(*d, 'p')
&& !ibcmp(d, "perl", 4))
{
break;
@@ -4870,7 +4870,7 @@ Perl_yylex(pTHX)
!= PL_unicode)
baduni = TRUE;
}
- if (baduni || *d1 == 'M' || *d1 == 'm') {
+ if (baduni || isALPHA_FOLD_EQ(*d1, 'M')) {
const char * const m = d1;
while (*d1 && !isSPACE(*d1))
d1++;
@@ -9868,17 +9868,17 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
const char *base, *Base, *max;
/* check for hex */
- if (s[1] == 'x' || s[1] == 'X') {
+ if (isALPHA_FOLD_EQ(s[1], 'x')) {
shift = 4;
s += 2;
just_zero = FALSE;
- } else if (s[1] == 'b' || s[1] == 'B') {
+ } else if (isALPHA_FOLD_EQ(s[1], 'b')) {
shift = 1;
s += 2;
just_zero = FALSE;
}
/* check for a decimal in disguise */
- else if (s[1] == '.' || s[1] == 'e' || s[1] == 'E')
+ else if (s[1] == '.' || isALPHA_FOLD_EQ(s[1], 'e'))
goto decimal;
/* so it must be octal */
else {
@@ -9982,8 +9982,8 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
* to avoid matching ".." */
#define HEXFP_PEEK(s) \
(((s[0] == '.') && \
- (isXDIGIT(s[1]) || s[1] == 'p' || s[1] == 'P')) \
- || s[0] == 'p' || s[0] == 'P')
+ (isXDIGIT(s[1]) || isALPHA_FOLD_EQ(s[1], 'p'))) \
+ || isALPHA_FOLD_EQ(s[0], 'p'))
if (UNLIKELY(HEXFP_PEEK(s))) {
goto out;
}
@@ -10044,7 +10044,7 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
total_bits--;
}
- if (total_bits > 0 && (*h == 'p' || *h == 'P')) {
+ if (total_bits > 0 && (isALPHA_FOLD_EQ(*h, 'p'))) {
bool negexp = FALSE;
h++;
if (*h == '+')
@@ -10203,18 +10203,19 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
}
/* read exponent part, if present */
- if (((*s == 'e' || *s == 'E') ||
- UNLIKELY(hexfp && (*s == 'p' || *s == 'P'))) &&
- strchr("+-0123456789_", s[1])) {
+ if ((isALPHA_FOLD_EQ(*s, 'e')
+ || UNLIKELY(hexfp && isALPHA_FOLD_EQ(*s, 'p')))
+ && strchr("+-0123456789_", s[1]))
+ {
floatit = TRUE;
/* regardless of whether user said 3E5 or 3e5, use lower 'e',
ditto for p (hexfloats) */
- if ((*s == 'e' || *s == 'E')) {
+ if ((isALPHA_FOLD_EQ(*s, 'e'))) {
/* At least some Mach atof()s don't grok 'E' */
*d++ = 'e';
}
- else if (UNLIKELY(hexfp && (*s == 'p' || *s == 'P'))) {
+ else if (UNLIKELY(hexfp && (isALPHA_FOLD_EQ(*s, 'p')))) {
*d++ = 'p';
}