diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2021-10-11 17:11:22 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-10-13 11:49:10 +0200 |
commit | 56fa18c2a493ed5db6b53841072d12950ac634ab (patch) | |
tree | ae25d070f98a316b6329acf52283bc240103d46a /src | |
parent | ed045134c3788f8c9c3066bfc653243ad2abe341 (diff) |
Simplify management of two static regular expression objects
ChartAxisElement's methods used two regular expressions to match label
formats. Each was heap-allocated lazily when first needed and there
was a static object whose destructor took care of deleting them.
Repackage that as two little functions, each of which has a local
static instance of QRegularExpression (so it'll be instantiated on the
first call, equivalent to the former lazy construction, and the
compiler / library / runtime can even be expected to take care of any
thread synchronization needed to ensure this is only done by the first
to need it) that it returns.
Change-Id: I61464cddf830e2e077df001bca95d0f962f26571
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
Diffstat (limited to 'src')
-rw-r--r-- | src/charts/axis/chartaxiselement.cpp | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp index d152d7bb..1155257b 100644 --- a/src/charts/axis/chartaxiselement.cpp +++ b/src/charts/axis/chartaxiselement.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. @@ -42,20 +42,18 @@ QT_BEGIN_NAMESPACE -static const char *labelFormatMatchString = "%[\\-\\+#\\s\\d\\.\\'lhjztL]*([dicuoxfegXFEG])"; -static const char *labelFormatMatchLocalizedString = "^([^%]*)%\\.(\\d+)([defgiEG])(.*)$"; -static QRegularExpression *labelFormatMatcher = 0; -static QRegularExpression *labelFormatMatcherLocalized = 0; -class StaticLabelFormatMatcherDeleter -{ -public: - StaticLabelFormatMatcherDeleter() {} - ~StaticLabelFormatMatcherDeleter() { - delete labelFormatMatcher; - delete labelFormatMatcherLocalized; - } -}; -static StaticLabelFormatMatcherDeleter staticLabelFormatMatcherDeleter; +static const QRegularExpression &labelFormatMatcher() +{ + static const QRegularExpression re( + QLatin1String("%[\\-\\+#\\s\\d\\.\\'lhjztL]*([dicuoxfegXFEG])")); + return re; +} + +static const QRegularExpression &labelFormatMatcherLocalized() +{ + static const QRegularExpression re(QLatin1String("^([^%]*)%\\.(\\d+)([defgiEG])(.*)$")); + return re; +} ChartAxisElement::ChartAxisElement(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis) : ChartElement(item), @@ -516,11 +514,8 @@ QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, QString postStr; int precision = 6; // Six is the default precision in Qt API if (presenter()->localizeNumbers()) { - if (!labelFormatMatcherLocalized) - labelFormatMatcherLocalized - = new QRegularExpression(QString::fromLatin1(labelFormatMatchLocalizedString)); QRegularExpressionMatch rmatch; - if (format.indexOf(*labelFormatMatcherLocalized, 0, &rmatch) != -1) { + if (format.indexOf(labelFormatMatcherLocalized(), 0, &rmatch) != -1) { preStr = rmatch.captured(1); if (!rmatch.captured(2).isEmpty()) precision = rmatch.captured(2).toInt(); @@ -528,10 +523,8 @@ QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, postStr = rmatch.captured(4); } } else { - if (!labelFormatMatcher) - labelFormatMatcher = new QRegularExpression(QString::fromLatin1(labelFormatMatchString)); QRegularExpressionMatch rmatch; - if (format.indexOf(*labelFormatMatcher, 0, &rmatch) != -1) + if (format.indexOf(labelFormatMatcher(), 0, &rmatch) != -1) formatSpec = rmatch.captured(1); } if (tickType == QValueAxis::TicksFixed) { @@ -584,11 +577,8 @@ QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal b QString postStr; int precision = 6; // Six is the default precision in Qt API if (presenter()->localizeNumbers()) { - if (!labelFormatMatcherLocalized) - labelFormatMatcherLocalized = - new QRegularExpression(QString::fromLatin1(labelFormatMatchLocalizedString)); QRegularExpressionMatch rmatch; - if (format.indexOf(*labelFormatMatcherLocalized, 0, &rmatch) != -1) { + if (format.indexOf(labelFormatMatcherLocalized(), 0, &rmatch) != -1) { preStr = rmatch.captured(1); if (!rmatch.captured(2).isEmpty()) precision = rmatch.captured(2).toInt(); @@ -596,10 +586,8 @@ QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal b postStr = rmatch.captured(4); } } else { - if (!labelFormatMatcher) - labelFormatMatcher = new QRegularExpression(QString::fromLatin1(labelFormatMatchString)); QRegularExpressionMatch rmatch; - if (format.indexOf(*labelFormatMatcher, 0, &rmatch) != -1) + if (format.indexOf(labelFormatMatcher(), 0, &rmatch) != -1) formatSpec = rmatch.captured(1); } for (int i = firstTick; i < ticks + firstTick; i++) { |