Skip to content

Commit

Permalink
QTimeZonePrivate: extract method for available zone ids
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
antkudr committed Mar 16, 2024
1 parent a2136a0 commit db295b6
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/corelib/time/qtimezoneprivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,18 @@ QList<QByteArray> QTimeZonePrivate::availableTimeZoneIds() const
return QList<QByteArray>();
}

static QList<QByteArray> selectAvailable(QList<QByteArray>&& desired, const QList<QByteArray>& 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<QByteArray> 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<QByteArray> QTimeZonePrivate::availableTimeZoneIds(QLocale::Territory territory) const
{
// Default fall-back mode, use the zoneTable to find Region of know Zones
Expand All @@ -544,17 +556,7 @@ QList<QByteArray> 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<QByteArray> all = availableTimeZoneIds();
QList<QByteArray> 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<QByteArray> QTimeZonePrivate::availableTimeZoneIds(int offsetFromUtc) const
Expand All @@ -572,17 +574,7 @@ QList<QByteArray> 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<QByteArray> all = availableTimeZoneIds();
QList<QByteArray> 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
Expand Down

0 comments on commit db295b6

Please sign in to comment.