summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2015-02-12 21:07:13 +0100
committerMarc Mutz <marc.mutz@kdab.com>2015-03-26 23:21:31 +0000
commite9bd162b725607673f8f826a7d1e815217734f18 (patch)
treebe9f760758beee4810d80140f2b69d7fb2e1a3f2 /src/corelib
parent211c6f3dc7c6a55ab40ca0d5a59ddacf859de43e (diff)
QDate: further optimize toString()
Factor the TextDate and ISODate legs into separate functions and replace a chain of QString::arg() with QStringBuilder. Saves 76B in text size. More importantly, restores (N)RVO for these code paths. Change-Id: I8b99bb672f5a4b5506d53487eca189d48b4025e9 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qdatetime.cpp34
1 files changed, 23 insertions, 11 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 8a1a885246..27b21cf745 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -766,6 +766,27 @@ QString QDate::longDayName(int weekday, MonthNameType type)
#ifndef QT_NO_DATESTRING
+#ifndef QT_NO_TEXTDATE
+static QString toStringTextDate(QDate date)
+{
+ const ParsedDate pd = getDateFromJulianDay(date.toJulianDay());
+ static const QLatin1Char sp(' ');
+ return date.shortDayName(date.dayOfWeek()) + sp
+ + date.shortMonthName(pd.month) + sp
+ + QString::number(pd.day) + sp
+ + QString::number(pd.year);
+}
+#endif // QT_NO_TEXTDATE
+
+static QString toStringIsoDate(qint64 jd)
+{
+ const ParsedDate pd = getDateFromJulianDay(jd);
+ if (pd.year >= 0 && pd.year <= 9999)
+ return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day);
+ else
+ return QString();
+}
+
/*!
\fn QString QDate::toString(Qt::DateFormat format) const
@@ -817,8 +838,6 @@ QString QDate::toString(Qt::DateFormat format) const
if (!isValid())
return QString();
- ParsedDate pd;
-
switch (format) {
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
@@ -835,17 +854,10 @@ QString QDate::toString(Qt::DateFormat format) const
default:
#ifndef QT_NO_TEXTDATE
case Qt::TextDate:
- pd = getDateFromJulianDay(jd);
- return QString::fromLatin1("%1 %2 %3 %4").arg(shortDayName(dayOfWeek()))
- .arg(shortMonthName(pd.month))
- .arg(pd.day)
- .arg(pd.year);
+ return toStringTextDate(*this);
#endif
case Qt::ISODate:
- pd = getDateFromJulianDay(jd);
- if (pd.year < 0 || pd.year > 9999)
- return QString();
- return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day);
+ return toStringIsoDate(jd);
}
}