Skip to content

Commit

Permalink
Time-Piece: Use isSPACE_LC, not isspace; etc
Browse files Browse the repository at this point in the history
isDIGIT_LC() does the same thing as isdigit when everything goes well,
but is more robust when things get more complicated.  The _LC forms are
thread safe, for example.

But note that this code doesn't work properly for a UTF-8 locale, as it
assumes that a byte and character are the same thing.  A major overhaul
would be needed to handle that.
  • Loading branch information
khwilliamson committed May 12, 2023
1 parent 7e6ee9d commit 0b00964
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions cpan/Time-Piece/Piece.xs
Expand Up @@ -370,8 +370,8 @@ _strptime(pTHX_ const char *buf, const char *fmt, struct tm *tm, int *got_GMT)
c = *ptr++;

if (c != '%') {
if (isspace((unsigned char)c))
while (*buf != 0 && isspace((unsigned char)*buf))
if (isSPACE_LC((unsigned char)c))
while (*buf != 0 && isSPACE_LC((unsigned char)*buf))
buf++;
else if (c != *buf++)
return 0;
Expand All @@ -396,12 +396,12 @@ label:
break;

case 'C':
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

/* XXX This will break for 3-digit centuries. */
len = 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand Down Expand Up @@ -458,9 +458,9 @@ label:

case 'n': /* whitespace */
case 't':
if (!isspace((unsigned char)*buf))
if (!isSPACE_LC((unsigned char)*buf))
return 0;
while (isspace((unsigned char)*buf))
while (isSPACE_LC((unsigned char)*buf))
buf++;
break;

Expand All @@ -483,11 +483,11 @@ label:
break;

case 'j':
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = 3;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand All @@ -501,14 +501,14 @@ label:

case 'M':
case 'S':
if (*buf == 0 || isspace((unsigned char)*buf))
if (*buf == 0 || isSPACE_LC((unsigned char)*buf))
break;

if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand All @@ -524,8 +524,8 @@ label:
tm->tm_sec = i;
}

if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

Expand All @@ -541,11 +541,11 @@ label:
* XXX The %l specifier may gobble one too many
* digits if used incorrectly.
*/
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand All @@ -558,8 +558,8 @@ label:

tm->tm_hour = i;

if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

Expand Down Expand Up @@ -626,26 +626,26 @@ label:
* point to calculate a real value, so just check the
* range for now.
*/
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
}
if (i > 53)
return 0;

if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

case 'u':
case 'w':
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

i = *buf - '0';
Expand All @@ -657,8 +657,8 @@ label:
tm->tm_wday = i;

buf++;
if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

Expand All @@ -672,11 +672,11 @@ label:
* XXX The %e specifier may gobble one too many
* digits if used incorrectly.
*/
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand All @@ -686,8 +686,8 @@ label:

tm->tm_mday = i;

if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

Expand Down Expand Up @@ -727,11 +727,11 @@ label:
break;

case 'm':
if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand All @@ -741,8 +741,8 @@ label:

tm->tm_mon = i - 1;

if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

Expand Down Expand Up @@ -784,14 +784,14 @@ label:

case 'Y':
case 'y':
if (*buf == 0 || isspace((unsigned char)*buf))
if (*buf == 0 || isSPACE_LC((unsigned char)*buf))
break;

if (!isdigit((unsigned char)*buf))
if (!isDIGIT_LC((unsigned char)*buf))
return 0;

len = (c == 'Y') ? 4 : 2;
for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
for (i = 0; len && *buf != 0 && isDIGIT_LC((unsigned char)*buf); buf++) {
i *= 10;
i += *buf - '0';
len--;
Expand All @@ -805,8 +805,8 @@ label:

tm->tm_year = i;

if (*buf != 0 && isspace((unsigned char)*buf))
while (*ptr != 0 && !isspace((unsigned char)*ptr))
if (*buf != 0 && isSPACE_LC((unsigned char)*buf))
while (*ptr != 0 && !isSPACE_LC((unsigned char)*ptr))
ptr++;
break;

Expand All @@ -815,7 +815,7 @@ label:
const char *cp;
char *zonestr;

for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp)
for (cp = buf; *cp && isUPPER_LC((unsigned char)*cp); ++cp)
{/*empty*/}
if (cp - buf) {
zonestr = (char *)malloc((size_t) (cp - buf + 1));
Expand Down Expand Up @@ -850,7 +850,7 @@ label:
buf++;
i = 0;
for (len = 4; len > 0; len--) {
if (isdigit((int)*buf)) {
if (isDIGIT_LC((int)*buf)) {
i *= 10;
i += *buf - '0';
buf++;
Expand Down

0 comments on commit 0b00964

Please sign in to comment.