[tz] [PROPOSED] Refactor by using max and min macros
Paul Eggert
eggert at cs.ucla.edu
Thu Jun 30 23:24:58 UTC 2022
* localtime.c (SMALLEST, BIGGEST): Remove.
All uses replaced by min and max.
* private.h (max, min): New macros.
* zdump.c (main):
* zic.c (growalloc, timerange_option):
Prefer max and min to doing it by hand.
---
localtime.c | 17 ++++++-----------
private.h | 5 +++++
zdump.c | 2 +-
zic.c | 6 +++---
4 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/localtime.c b/localtime.c
index 8644aa6..3db7821 100644
--- a/localtime.c
+++ b/localtime.c
@@ -102,9 +102,6 @@ struct lsinfo { /* leap second information */
int_fast32_t ls_corr; /* correction to apply */
};
-#define SMALLEST(a, b) (((a) < (b)) ? (a) : (b))
-#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
-
/* This abbreviation means local time is unspecified. */
static char const UNSPEC[] = "-00";
@@ -112,7 +109,7 @@ static char const UNSPEC[] = "-00";
This needs to be at least 1 for null termination in case the input
data isn't properly terminated, and it also needs to be big enough
for ttunspecified to work without crashing. */
-enum { CHARS_EXTRA = BIGGEST(sizeof UNSPEC, 2) - 1 };
+enum { CHARS_EXTRA = max(sizeof UNSPEC, 2) - 1 };
#ifdef TZNAME_MAX
#define MY_TZNAME_MAX TZNAME_MAX
@@ -131,9 +128,8 @@ struct state {
time_t ats[TZ_MAX_TIMES];
unsigned char types[TZ_MAX_TIMES];
struct ttinfo ttis[TZ_MAX_TYPES];
- char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + CHARS_EXTRA,
- sizeof gmt),
- (2 * (MY_TZNAME_MAX + 1)))];
+ char chars[max(max(TZ_MAX_CHARS + CHARS_EXTRA, sizeof gmt),
+ 2 * (MY_TZNAME_MAX + 1))];
struct lsinfo lsis[TZ_MAX_LEAPS];
/* The time type to use for early times or if no transitions.
@@ -394,8 +390,7 @@ union local_storage {
} u;
/* The file name to be opened. */
- char fullname[BIGGEST(sizeof(struct file_analysis),
- sizeof tzdirslash + 1024)];
+ char fullname[max(sizeof(struct file_analysis), sizeof tzdirslash + 1024)];
};
/* Load tz data from the file named NAME into *SP. Read extended
@@ -2086,10 +2081,10 @@ time2sub(struct tm *const tmp,
&& (yourtm.TM_GMTOFF < 0
? (-SECSPERDAY <= yourtm.TM_GMTOFF
&& (mytm.TM_GMTOFF <=
- (SMALLEST(INT_FAST32_MAX, LONG_MAX)
+ (min(INT_FAST32_MAX, LONG_MAX)
+ yourtm.TM_GMTOFF)))
: (yourtm.TM_GMTOFF <= SECSPERDAY
- && ((BIGGEST(INT_FAST32_MIN, LONG_MIN)
+ && ((max(INT_FAST32_MIN, LONG_MIN)
+ yourtm.TM_GMTOFF)
<= mytm.TM_GMTOFF)))) {
/* MYTM matches YOURTM except with the wrong UT offset.
diff --git a/private.h b/private.h
index e8c0942..b083c1b 100644
--- a/private.h
+++ b/private.h
@@ -632,6 +632,11 @@ time_t time2posix_z(timezone_t, time_t) ATTRIBUTE_PURE;
#define TYPE_SIGNED(type) (((type) -1) < 0)
#define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
+/* Minimum and maximum of two values. Use lower case to avoid
+ naming clashes with standard include files. */
+#define max(a, b) ((a) > (b) ? (a) : (b))
+#define min(a, b) ((a) < (b) ? (a) : (b))
+
/* Max and min values of the integer type T, of which only the bottom
B bits are used, and where the highest-order used bit is considered
to be a sign bit if T is signed. */
diff --git a/zdump.c b/zdump.c
index 8b6788a..b10f574 100644
--- a/zdump.c
+++ b/zdump.c
@@ -548,7 +548,7 @@ main(int argc, char *argv[])
for (i = optind; i < argc; i++) {
size_t arglen = strlen(argv[i]);
if (longest < arglen)
- longest = arglen < INT_MAX ? arglen : INT_MAX;
+ longest = min(arglen, INT_MAX);
}
for (i = optind; i < argc; ++i) {
diff --git a/zic.c b/zic.c
index 436677f..c0ff561 100644
--- a/zic.c
+++ b/zic.c
@@ -488,7 +488,7 @@ growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
return ptr;
else {
ptrdiff_t nitems_max = PTRDIFF_MAX - WORK_AROUND_QTBUG_53071;
- ptrdiff_t amax = nitems_max < SIZE_MAX ? nitems_max : SIZE_MAX;
+ ptrdiff_t amax = min(nitems_max, SIZE_MAX);
if ((amax - 1) / 3 * 2 < *nitems_alloc)
memory_exhausted(_("integer overflow"));
*nitems_alloc += (*nitems_alloc >> 1) + 1;
@@ -707,8 +707,8 @@ timerange_option(char *timerange)
}
if (*hi_end || hi < lo || max_time < lo || hi < min_time)
return false;
- lo_time = lo < min_time ? min_time : lo;
- hi_time = max_time < hi ? max_time : hi;
+ lo_time = max(lo, min_time);
+ hi_time = min(hi, max_time);
return true;
}
--
2.36.1
More information about the tz
mailing list