summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-07-29 18:40:54 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2021-08-10 19:43:03 +0200
commitd055abd1958879ff1775772cfb0d5039e975fe9c (patch)
tree4d26a66ca3c27341af910ccccdf92467b853ff05 /src
parent33f9591e378a2162378d8be5e75a47adf034536b (diff)
Assert that special handling of '0' padding does what it should
When formatting numbers, if the fill character used to left-pad to field widths is '0', the code delegates that padding to the QLocaleData's ZeroPadded formatting option. Since we want the zeros before any minus sign, and don't want to subsequently add more zeros before it, check that this has worked as expected when calling replaceArgEscapes(), to confirm that it doesn't need to worry about that. Add some tests that verify the expected behavior. In the process, tidy up the code doing this. Rename a local variable to match our coding style, split a long line. Pick-to: 6.2 Change-Id: I7cc430c5bceb006cf4e226bca33da16bd2bb1937 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qstring.cpp47
1 files changed, 33 insertions, 14 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 6674d7a026..ddb12effd0 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2020 Intel Corporation.
** Copyright (C) 2019 Mail.ru Group.
** Contact: https://www.qt.io/licensing/
@@ -8050,22 +8050,28 @@ QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) cons
}
unsigned flags = QLocaleData::NoFlags;
+ // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign:
if (fillChar == QLatin1Char('0'))
flags = QLocaleData::ZeroPadded;
QString arg;
- if (d.occurrences > d.locale_occurrences)
+ if (d.occurrences > d.locale_occurrences) {
arg = QLocaleData::c()->longLongToString(a, -1, base, fieldWidth, flags);
+ Q_ASSERT(fillChar != QLatin1Char('0') || !qIsFinite(a)
+ || fieldWidth <= arg.length());
+ }
- QString locale_arg;
+ QString localeArg;
if (d.locale_occurrences > 0) {
QLocale locale;
if (!(locale.numberOptions() & QLocale::OmitGroupSeparator))
flags |= QLocaleData::GroupDigits;
- locale_arg = locale.d->m_data->longLongToString(a, -1, base, fieldWidth, flags);
+ localeArg = locale.d->m_data->longLongToString(a, -1, base, fieldWidth, flags);
+ Q_ASSERT(fillChar != QLatin1Char('0') || !qIsFinite(a)
+ || fieldWidth <= localeArg.length());
}
- return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
+ return replaceArgEscapes(*this, d, fieldWidth, arg, localeArg, fillChar);
}
/*!
@@ -8092,22 +8098,28 @@ QString QString::arg(qulonglong a, int fieldWidth, int base, QChar fillChar) con
}
unsigned flags = QLocaleData::NoFlags;
+ // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign:
if (fillChar == QLatin1Char('0'))
flags = QLocaleData::ZeroPadded;
QString arg;
- if (d.occurrences > d.locale_occurrences)
+ if (d.occurrences > d.locale_occurrences) {
arg = QLocaleData::c()->unsLongLongToString(a, -1, base, fieldWidth, flags);
+ Q_ASSERT(fillChar != QLatin1Char('0') || !qIsFinite(a)
+ || fieldWidth <= arg.length());
+ }
- QString locale_arg;
+ QString localeArg;
if (d.locale_occurrences > 0) {
QLocale locale;
if (!(locale.numberOptions() & QLocale::OmitGroupSeparator))
flags |= QLocaleData::GroupDigits;
- locale_arg = locale.d->m_data->unsLongLongToString(a, -1, base, fieldWidth, flags);
+ localeArg = locale.d->m_data->unsLongLongToString(a, -1, base, fieldWidth, flags);
+ Q_ASSERT(fillChar != QLatin1Char('0') || !qIsFinite(a)
+ || fieldWidth <= localeArg.length());
}
- return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
+ return replaceArgEscapes(*this, d, fieldWidth, arg, localeArg, fillChar);
}
/*!
@@ -8186,6 +8198,7 @@ QString QString::arg(double a, int fieldWidth, char format, int precision, QChar
}
unsigned flags = QLocaleData::NoFlags;
+ // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign:
if (fillChar == QLatin1Char('0'))
flags |= QLocaleData::ZeroPadded;
@@ -8211,10 +8224,14 @@ QString QString::arg(double a, int fieldWidth, char format, int precision, QChar
}
QString arg;
- if (d.occurrences > d.locale_occurrences)
- arg = QLocaleData::c()->doubleToString(a, precision, form, fieldWidth, flags | QLocaleData::ZeroPadExponent);
+ if (d.occurrences > d.locale_occurrences) {
+ arg = QLocaleData::c()->doubleToString(a, precision, form, fieldWidth,
+ flags | QLocaleData::ZeroPadExponent);
+ Q_ASSERT(fillChar != QLatin1Char('0') || !qIsFinite(a)
+ || fieldWidth <= arg.length());
+ }
- QString locale_arg;
+ QString localeArg;
if (d.locale_occurrences > 0) {
QLocale locale;
@@ -8225,10 +8242,12 @@ QString QString::arg(double a, int fieldWidth, char format, int precision, QChar
flags |= QLocaleData::ZeroPadExponent;
if (numberOptions & QLocale::IncludeTrailingZeroesAfterDot)
flags |= QLocaleData::AddTrailingZeroes;
- locale_arg = locale.d->m_data->doubleToString(a, precision, form, fieldWidth, flags);
+ localeArg = locale.d->m_data->doubleToString(a, precision, form, fieldWidth, flags);
+ Q_ASSERT(fillChar != QLatin1Char('0') || !qIsFinite(a)
+ || fieldWidth <= localeArg.length());
}
- return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
+ return replaceArgEscapes(*this, d, fieldWidth, arg, localeArg, fillChar);
}
static inline char16_t to_unicode(const QChar c) { return c.unicode(); }