From db295b6d6e951617fe4e47849e76d1e029d0b993 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Fri, 15 Mar 2024 15:17:09 +0300 Subject: [PATCH] QTimeZonePrivate: extract method for available zone ids according to DRY. Also don't use erase after unique call, just use new past-the-end iterator. Change-Id: I5c033b6433105842e72eca9a7a2d5ea9ec0032ec Reviewed-by: Thiago Macieira --- src/corelib/time/qtimezoneprivate.cpp | 36 +++++++++++---------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/corelib/time/qtimezoneprivate.cpp b/src/corelib/time/qtimezoneprivate.cpp index 78671543a61..861ebefbdf8 100644 --- a/src/corelib/time/qtimezoneprivate.cpp +++ b/src/corelib/time/qtimezoneprivate.cpp @@ -532,6 +532,18 @@ QList QTimeZonePrivate::availableTimeZoneIds() const return QList(); } +static QList selectAvailable(QList&& desired, const QList& all) +{ + std::sort(desired.begin(), desired.end()); + const auto newEnd = std::unique(desired.begin(), desired.end()); + const auto newSize = std::distance(desired.begin(), newEnd); + QList result; + result.reserve(qMin(all.size(), newSize)); + std::set_intersection(all.begin(), all.end(), desired.cbegin(), + std::next(desired.cbegin(), newSize), std::back_inserter(result)); + return result; +} + QList QTimeZonePrivate::availableTimeZoneIds(QLocale::Territory territory) const { // Default fall-back mode, use the zoneTable to find Region of know Zones @@ -544,17 +556,7 @@ QList QTimeZonePrivate::availableTimeZoneIds(QLocale::Territory terr regions << QByteArray(l1.data(), l1.size()); } } - - std::sort(regions.begin(), regions.end()); - regions.erase(std::unique(regions.begin(), regions.end()), regions.end()); - - // Then select just those that are available - const QList all = availableTimeZoneIds(); - QList result; - result.reserve(qMin(all.size(), regions.size())); - std::set_intersection(all.begin(), all.end(), regions.cbegin(), regions.cend(), - std::back_inserter(result)); - return result; + return selectAvailable(std::move(regions), availableTimeZoneIds()); } QList QTimeZonePrivate::availableTimeZoneIds(int offsetFromUtc) const @@ -572,17 +574,7 @@ QList QTimeZonePrivate::availableTimeZoneIds(int offsetFromUtc) cons } } } - - std::sort(offsets.begin(), offsets.end()); - offsets.erase(std::unique(offsets.begin(), offsets.end()), offsets.end()); - - // Then select just those that are available - const QList all = availableTimeZoneIds(); - QList result; - result.reserve(qMin(all.size(), offsets.size())); - std::set_intersection(all.begin(), all.end(), offsets.cbegin(), offsets.cend(), - std::back_inserter(result)); - return result; + return selectAvailable(std::move(offsets), availableTimeZoneIds()); } #ifndef QT_NO_DATASTREAM