Skip to content

Commit be12b79

Browse files
committed
mapi_lib: replace ical_time::twcompare with ical_time::operator<=>
1 parent bbff52c commit be12b79

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

include/gromox/defs.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,6 @@ template<typename T> static inline T *re_alloc(void *x, size_t elem) {
141141
static inline const char *snul(const std::string &s) { return s.size() != 0 ? s.c_str() : nullptr; }
142142
static inline const char *znul(const char *s) { return s != nullptr ? s : ""; }
143143

144-
template<typename U, typename V> static int three_way_compare(U &&a, V &&b)
145-
{
146-
return (a < b) ? -1 : (a == b) ? 0 : 1;
147-
}
148-
149144
#if defined(COMPILE_DIAG) && !defined(__clang__)
150145
struct GX_EXPORT errno_t {
151146
constexpr errno_t(int x) : m_value(x) {

include/gromox/ical.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <compare>
23
#include <ctime>
34
#include <list>
45
#include <memory>
@@ -91,11 +92,8 @@ struct GX_EXPORT ical_time {
9192
year(y), month(m), day(d), hour(hr), minute(min), second(sec)
9293
{}
9394

94-
int twcompare(const ical_time &other) const;
95-
inline bool operator<(const ical_time &o) const { return twcompare(o) < 0; }
96-
inline bool operator<=(const ical_time &o) const { return twcompare(o) <= 0; }
97-
inline bool operator>(const ical_time &o) const { return twcompare(o) > 0; }
98-
inline bool operator>=(const ical_time &o) const { return twcompare(o) >= 0; }
95+
std::strong_ordering operator<=>(const ical_time &other) const;
96+
inline bool operator==(const ical_time &o) const { return (*this <=> o) == 0; }
9997
void add_year(int ys);
10098
void add_month(int ms);
10199
void add_day(int ds);

lib/email/ical.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cassert>
66
#include <cerrno>
77
#include <climits>
8+
#include <compare>
89
#include <cstdio>
910
#include <cstdlib>
1011
#include <cstring>
@@ -665,31 +666,31 @@ bool ical_time::assign_datetime(const char *str_datetime)
665666
return false;
666667
}
667668

668-
int ical_time::twcompare(const ical_time &o) const
669+
std::strong_ordering ical_time::operator<=>(const ical_time &o) const
669670
{
670-
auto r = three_way_compare(year, o.year);
671+
auto r = year <=> o.year;
671672
if (r != 0)
672673
return r;
673-
r = three_way_compare(month, o.month);
674+
r = month <=> o.month;
674675
if (r != 0)
675676
return r;
676-
r = three_way_compare(day, o.day);
677+
r = day <=> o.day;
677678
if (r != 0)
678679
return r;
679-
r = three_way_compare(hour, o.hour);
680+
r = hour <=> o.hour;
680681
if (r != 0)
681682
return r;
682-
r = three_way_compare(minute, o.minute);
683+
r = minute <=> o.minute;
683684
if (r != 0)
684685
return r;
685-
r = three_way_compare(second, o.second);
686+
r = second <=> o.second;
686687
if (r != 0)
687688
return r;
688689
if (leap_second > 59 && o.leap_second <= 59)
689-
return 1;
690+
return std::strong_ordering::greater;
690691
if (leap_second <= 59 && o.leap_second > 59)
691-
return -1;
692-
return 0;
692+
return std::strong_ordering::less;
693+
return std::strong_ordering::equivalent;
693694
}
694695

695696
static bool ical_is_leap_year(unsigned int year)
@@ -2176,7 +2177,7 @@ const char *ical_parse_rrule(const ical_component *ptz_component,
21762177
if (!ical_test_setpos(pirrule))
21772178
continue;
21782179
}
2179-
int cmp_result = itime.twcompare(pirrule->instance_itime);
2180+
auto cmp_result = itime <=> pirrule->instance_itime;
21802181
if (cmp_result < 0) {
21812182
continue;
21822183
} else if (cmp_result > 0) {

tests/utiltest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,11 @@ static int t_cmp_icaltime()
418418
ical_time a{}, b{};
419419
a.second = b.second = 59;
420420
a.leap_second = 60;
421-
if (a.twcompare(b) != -b.twcompare(a))
422-
return printf("ical_time::compare failed\n");
421+
assert(a == a);
422+
assert(b == b);
423+
assert(a != b);
424+
assert(a > b);
425+
assert(b < a);
423426
return 0;
424427
}
425428

0 commit comments

Comments
 (0)