QDateTimeParser: adapt to make good use of QStringRef.

Avoid unnecessary allocations.

Create QString from QStringRef only where necessary.

Change-Id: I8f2a7dce51430162c84328e23ab3cc071227d6ae
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Anton Kudryavtsev 2016-04-29 15:40:46 +03:00
parent 2cf099e9f6
commit e4d838ff9d
2 changed files with 24 additions and 13 deletions

View File

@ -708,17 +708,18 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
} }
const int sectionmaxsize = sectionMaxSize(sectionIndex); const int sectionmaxsize = sectionMaxSize(sectionIndex);
QString sectiontext = text.mid(index, sectionmaxsize); QStringRef sectionTextRef = text.midRef(index, sectionmaxsize);
int sectiontextSize = sectiontext.size(); int sectiontextSize = sectionTextRef.size();
QDTPDEBUG << "sectionValue for" << sn.name() QDTPDEBUG << "sectionValue for" << sn.name()
<< "with text" << text << "and st" << sectiontext << "with text" << text << "and st" << sectionTextRef
<< text.midRef(index, sectionmaxsize) << text.midRef(index, sectionmaxsize)
<< index; << index;
int used = 0; int used = 0;
switch (sn.type) { switch (sn.type) {
case AmPmSection: { case AmPmSection: {
QString sectiontext = sectionTextRef.toString();
const int ampm = findAmPm(sectiontext, sectionIndex, &used); const int ampm = findAmPm(sectiontext, sectionIndex, &used);
switch (ampm) { switch (ampm) {
case AM: // sectiontext == AM case AM: // sectiontext == AM
@ -750,6 +751,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
case DayOfWeekSectionShort: case DayOfWeekSectionShort:
case DayOfWeekSectionLong: case DayOfWeekSectionLong:
if (sn.count >= 3) { if (sn.count >= 3) {
QString sectiontext = sectionTextRef.toString();
if (sn.type == MonthSection) { if (sn.type == MonthSection) {
int min = 1; int min = 1;
const QDate minDate = getMinimum().date(); const QDate minDate = getMinimum().date();
@ -788,7 +790,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
int last = -1; int last = -1;
used = -1; used = -1;
QString digitsStr(sectiontext); QStringRef digitsStr = sectionTextRef;
for (int i = 0; i < sectiontextSize; ++i) { for (int i = 0; i < sectiontextSize; ++i) {
if (digitsStr.at(i).isSpace()) { if (digitsStr.at(i).isSpace()) {
sectiontextSize = i; sectiontextSize = i;
@ -809,7 +811,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
} }
} }
if (ok && tmp <= absMax) { if (ok && tmp <= absMax) {
QDTPDEBUG << sectiontext.leftRef(digits) << tmp << digits; QDTPDEBUG << sectionTextRef.left(digits) << tmp << digits;
last = tmp; last = tmp;
used = digits; used = digits;
break; break;
@ -817,13 +819,13 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
} }
if (last == -1) { if (last == -1) {
QChar first(sectiontext.at(0)); QChar first(sectionTextRef.at(0));
if (separators.at(sectionIndex + 1).startsWith(first)) { if (separators.at(sectionIndex + 1).startsWith(first)) {
used = 0; used = 0;
state = Intermediate; state = Intermediate;
} else { } else {
state = Invalid; state = Invalid;
QDTPDEBUG << "invalid because" << sectiontext << "can't become a uint" << last << ok; QDTPDEBUG << "invalid because" << sectionTextRef << "can't become a uint" << last << ok;
} }
} else { } else {
num += last; num += last;
@ -1565,7 +1567,7 @@ QString QDateTimeParser::SectionNode::format() const
number that is within min and max. number that is within min and max.
*/ */
bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int index, bool QDateTimeParser::potentialValue(const QStringRef &str, int min, int max, int index,
const QDateTime &currentValue, int insert) const const QDateTime &currentValue, int insert) const
{ {
if (str.isEmpty()) { if (str.isEmpty()) {
@ -1592,8 +1594,7 @@ bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int i
if (potentialValue(str + QLatin1Char('0' + j), min, max, index, currentValue, insert)) { if (potentialValue(str + QLatin1Char('0' + j), min, max, index, currentValue, insert)) {
return true; return true;
} else if (insert >= 0) { } else if (insert >= 0) {
QString tmp = str; const QString tmp = str.left(insert) + QLatin1Char('0' + j) + str.mid(insert);
tmp.insert(insert, QLatin1Char('0' + j));
if (potentialValue(tmp, min, max, index, currentValue, insert)) if (potentialValue(tmp, min, max, index, currentValue, insert))
return true; return true;
} }
@ -1603,7 +1604,7 @@ bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int i
return false; return false;
} }
bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, const QString &text) const bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, const QStringRef &text) const
{ {
Q_ASSERT(current >= getMinimum() && current <= getMaximum()); Q_ASSERT(current >= getMinimum() && current <= getMaximum());

View File

@ -214,9 +214,19 @@ public:
QString *dayName = 0, int *used = 0) const; QString *dayName = 0, int *used = 0) const;
#endif #endif
AmPmFinder findAmPm(QString &str, int index, int *used = 0) const; AmPmFinder findAmPm(QString &str, int index, int *used = 0) const;
bool potentialValue(const QString &str, int min, int max, int index, bool potentialValue(const QStringRef &str, int min, int max, int index,
const QDateTime &currentValue, int insert) const; const QDateTime &currentValue, int insert) const;
bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const; bool potentialValue(const QString &str, int min, int max, int index,
const QDateTime &currentValue, int insert) const
{
return potentialValue(QStringRef(&str), min, max, index, currentValue, insert);
}
bool skipToNextSection(int section, const QDateTime &current, const QStringRef &sectionText) const;
bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const
{
return skipToNextSection(section, current, QStringRef(&sectionText));
}
QString stateName(State s) const; QString stateName(State s) const;