Skip to content

Commit

Permalink
Time-Piece: Use foldEQ_locale() if available
Browse files Browse the repository at this point in the history
This supported core function is thread-safe and knows about Perl
internals, so is preferable to the similar libc function, which is now
used only as a fallback.  This commit also bomb proofs the code by
adding an additional fallback, specified in C89, which isn't a great
substituted, but far better than nothing.
  • Loading branch information
khwilliamson committed May 9, 2021
1 parent 5200809 commit 46ef838
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions cpan/Time-Piece/Piece.xs
Expand Up @@ -302,9 +302,15 @@ my_mini_mktime(struct tm *ptm)
ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;
}

# if defined(WIN32) || (defined(__QNX__) && defined(__WATCOMC__))
# define strncasecmp(x,y,n) strnicmp(x,y,n)
# endif
# ifndef foldEQ_locale
# ifdef strncasecmp
# define foldEQ_locale(x,y,n) strncasecmp(x,y,n)
# elif defined(WIN32) || (defined(__QNX__) && defined(__WATCOMC__))
# define foldEQ_locale(x,y,n) strnicmp(x,y,n)
# else /* Better than nothing */
# define foldEQ_locale(x,y,n) strncmp(x,y,n)
# endif
# endif

/* strptime.c 0.1 (Powerdog) 94/03/27 */
/* strptime copied from freebsd with the following copyright: */
Expand Down Expand Up @@ -586,8 +592,8 @@ label:
* specifiers.
*/
len = strlen(Locale->am);
if (strncasecmp(buf, Locale->am, len) == 0 ||
strncasecmp(buf, Locale->AM, len) == 0) {
if (foldEQ_locale(buf, Locale->am, len) == 0 ||
foldEQ_locale(buf, Locale->AM, len) == 0) {
if (tm->tm_hour > 12)
return 0;
if (tm->tm_hour == 12)
Expand All @@ -597,8 +603,8 @@ label:
}

len = strlen(Locale->pm);
if (strncasecmp(buf, Locale->pm, len) == 0 ||
strncasecmp(buf, Locale->PM, len) == 0) {
if (foldEQ_locale(buf, Locale->pm, len) == 0 ||
foldEQ_locale(buf, Locale->PM, len) == 0) {
if (tm->tm_hour > 12)
return 0;
if (tm->tm_hour != 12)
Expand All @@ -614,13 +620,13 @@ label:
for (i = 0; i < (int)asizeof(Locale->weekday); i++) {
if (c == 'A') {
len = strlen(Locale->weekday[i]);
if (strncasecmp(buf,
if (foldEQ_locale(buf,
Locale->weekday[i],
len) == 0)
break;
} else {
len = strlen(Locale->wday[i]);
if (strncasecmp(buf,
if (foldEQ_locale(buf,
Locale->wday[i],
len) == 0)
break;
Expand Down Expand Up @@ -714,21 +720,21 @@ label:
if (Oalternative) {
if (c == 'B') {
len = strlen(Locale->alt_month[i]);
if (strncasecmp(buf,
if (foldEQ_locale(buf,
Locale->alt_month[i],
len) == 0)
break;
}
} else {
if (c == 'B') {
len = strlen(Locale->month[i]);
if (strncasecmp(buf,
if (foldEQ_locale(buf,
Locale->month[i],
len) == 0)
break;
} else {
len = strlen(Locale->mon[i]);
if (strncasecmp(buf,
if (foldEQ_locale(buf,
Locale->mon[i],
len) == 0)
break;
Expand Down

0 comments on commit 46ef838

Please sign in to comment.