Use unchecked substring methods in date-time code

Change-Id: I38b9aaa0335c6168706c2508ed1117fd908e679c
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
This commit is contained in:
Edward Welbourne 2020-09-18 17:25:10 +02:00
parent 229c9736bb
commit 68bfab534e
2 changed files with 31 additions and 33 deletions

View File

@ -233,19 +233,19 @@ static ParsedRfcDateTime rfcDateImpl(QStringView s)
|| (when.size() == 8 ? when[5] != colon : when.size() > 5)) {
return result;
}
const int hour = when.left(2).toInt(&ok);
const int hour = when.first(2).toInt(&ok);
if (!ok)
return result;
const int minute = when.mid(3, 2).toInt(&ok);
const int minute = when.sliced(3, 2).toInt(&ok);
if (!ok)
return result;
const auto secs = when.size() == 8 ? when.right(2).toInt(&ok) : 0;
const auto secs = when.size() == 8 ? when.last(2).toInt(&ok) : 0;
if (!ok)
return result;
time = QTime(hour, minute, secs);
}
// Offset: [±hhmm]
// Offset: [±hh[mm]]
int offset = 0;
if (words.size()) {
const QStringView zone = words.takeFirst();
@ -256,10 +256,10 @@ static ParsedRfcDateTime rfcDateImpl(QStringView s)
negate = true;
else if (zone[0] != u'+')
return result;
const int hour = zone.mid(1, 2).toInt(&ok);
const int hour = zone.sliced(1, 2).toInt(&ok);
if (!ok)
return result;
const auto minute = zone.size() > 3 ? zone.mid(3, 2).toInt(&ok) : 0;
const auto minute = zone.size() == 5 ? zone.last(2).toInt(&ok) : 0;
if (!ok)
return result;
offset = (hour * 60 + minute) * 60;
@ -308,7 +308,7 @@ static int fromOffsetString(QStringView offsetString, bool *valid) noexcept
return 0;
// Split the hour and minute parts
const QStringView time = offsetString.mid(1);
const QStringView time = offsetString.sliced(1);
qsizetype hhLen = time.indexOf(u':');
qsizetype mmIndex;
if (hhLen == -1)
@ -316,13 +316,13 @@ static int fromOffsetString(QStringView offsetString, bool *valid) noexcept
else
mmIndex = hhLen + 1;
const QStringView hhRef = time.left(qMin(hhLen, time.size()));
const QStringView hhRef = time.first(qMin(hhLen, time.size()));
bool ok = false;
const int hour = hhRef.toInt(&ok);
if (!ok)
return 0;
const QStringView mmRef = time.mid(qMin(mmIndex, time.size()));
const QStringView mmRef = time.sliced(qMin(mmIndex, time.size()));
const int minute = mmRef.isEmpty() ? 0 : mmRef.toInt(&ok);
if (!ok || minute < 0 || minute > 59)
return 0;
@ -1503,9 +1503,9 @@ QDate QDate::fromString(QStringView string, Qt::DateFormat format)
// Semi-strict parsing, must be long enough and have punctuators as separators
if (string.size() >= 10 && string.at(4).isPunct() && string.at(7).isPunct()
&& (string.size() == 10 || !string.at(10).isDigit())) {
const ParsedInt year = readInt(string.mid(0, 4));
const ParsedInt month = readInt(string.mid(5, 2));
const ParsedInt day = readInt(string.mid(8, 2));
const ParsedInt year = readInt(string.first(4));
const ParsedInt month = readInt(string.sliced(5, 2));
const ParsedInt day = readInt(string.sliced(8, 2));
if (year.ok && year.value > 0 && year.value <= 9999 && month.ok && day.ok)
return QDate(year.value, month.value, day.value);
}
@ -4791,14 +4791,14 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
if (size < 10)
return QDateTime();
QDate date = QDate::fromString(string.left(10), Qt::ISODate);
QDate date = QDate::fromString(string.first(10), Qt::ISODate);
if (!date.isValid())
return QDateTime();
if (size == 10)
return date.startOfDay();
Qt::TimeSpec spec = Qt::LocalTime;
QStringView isoString = string.mid(10); // trim "yyyy-MM-dd"
QStringView isoString = string.sliced(10); // trim "yyyy-MM-dd"
// Must be left with T (or space) and at least one digit for the hour:
if (isoString.size() < 2
@ -4809,7 +4809,7 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
|| isoString.startsWith(u' '))) {
return QDateTime();
}
isoString = isoString.mid(1); // trim 'T' (or space)
isoString = isoString.sliced(1); // trim 'T' (or space)
int offset = 0;
// Check end of string for Time Zone definition, either Z for UTC or [+-]HH:mm for Offset
@ -4829,10 +4829,10 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
if (found) {
bool ok;
offset = fromOffsetString(isoString.mid(signIndex), &ok);
offset = fromOffsetString(isoString.sliced(signIndex), &ok);
if (!ok)
return QDateTime();
isoString = isoString.left(signIndex);
isoString = isoString.first(signIndex);
spec = Qt::OffsetFromUTC;
}
}
@ -4883,11 +4883,9 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
if (!ok || !month || !day) {
month = fromShortMonthName(parts.at(2));
if (month) {
QStringView dayStr = parts.at(1);
if (dayStr.endsWith(u'.')) {
dayStr = dayStr.left(dayStr.size() - 1);
day = dayStr.toInt(&ok);
}
QStringView dayPart = parts.at(1);
if (dayPart.endsWith(u'.'))
day = dayPart.chopped(1).toInt(&ok);
}
}
@ -4909,7 +4907,7 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
QStringView tz = parts.at(5);
if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
return QDateTime();
tz = tz.mid(3);
tz = tz.sliced(3);
if (!tz.isEmpty()) {
int offset = fromOffsetString(tz, &ok);
if (!ok)

View File

@ -499,7 +499,7 @@ bool QDateTimeParser::parseFormat(QStringView newFormat)
if (parserType != QMetaType::QTime) {
const SectionNode sn = { MonthSection, i - add, countRepeat(newFormat, i, 4), 0 };
newSectionNodes.append(sn);
newSeparators.append(unquote(newFormat.mid(index, i - index)));
newSeparators.append(unquote(newFormat.first(i).sliced(index)));
i += sn.count - 1;
index = i + 1;
newDisplay |= MonthSection;
@ -825,7 +825,7 @@ QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionIndex, i
int last = -1, used = -1;
Q_ASSERT(sectiontextSize <= sectionmaxsize);
QStringView digitsStr = sectionTextRef.left(sectiontextSize);
QStringView digitsStr = sectionTextRef.first(sectiontextSize);
for (int digits = sectiontextSize; digits >= 1; --digits) {
digitsStr.truncate(digits);
int tmp = int(loc.toUInt(digitsStr, &ok));
@ -838,7 +838,7 @@ QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionIndex, i
}
}
if (ok && tmp <= absMax) {
QDTPDEBUG << sectionTextRef.left(digits) << tmp << digits;
QDTPDEBUG << sectionTextRef.first(digits) << tmp << digits;
last = tmp;
used = digits;
break;
@ -1185,11 +1185,11 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, bool fixup) const
current = &zoneOffset;
if (sect.used > 0) {
// Synchronize with what findTimeZone() found:
QStringView zoneName = QStringView{m_text}.mid(pos, sect.used);
QStringView zoneName = QStringView{m_text}.sliced(pos, sect.used);
Q_ASSERT(!zoneName.isEmpty()); // sect.used > 0
const QStringView offsetStr = zoneName.startsWith(QLatin1String("UTC"))
? zoneName.mid(3) : zoneName;
? zoneName.sliced(3) : zoneName;
const bool isUtcOffset = offsetStr.startsWith(QLatin1Char('+'))
|| offsetStr.startsWith(QLatin1Char('-'));
const bool isUtc = zoneName == QLatin1String("Z")
@ -1246,8 +1246,8 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, bool fixup) const
isSet |= sn.type;
}
if (QStringView{m_text}.mid(pos) != separators.last()) {
QDTPDEBUG << "invalid because" << QStringView{m_text}.mid(pos)
if (QStringView{m_text}.sliced(pos) != separators.last()) {
QDTPDEBUG << "invalid because" << QStringView{m_text}.sliced(pos)
<< "!=" << separators.last() << pos;
return StateNode();
}
@ -1625,13 +1625,13 @@ QDateTimeParser::ParsedSection QDateTimeParser::findUtcOffset(QStringView str) c
const bool startsWithUtc = str.startsWith(QLatin1String("UTC"));
// Get rid of UTC prefix if it exists
if (startsWithUtc)
str = str.mid(3);
str = str.sliced(3);
const bool negativeSign = str.startsWith(QLatin1Char('-'));
// Must start with a sign:
if (!negativeSign && !str.startsWith(QLatin1Char('+')))
return ParsedSection();
str = str.mid(1); // drop sign
str = str.sliced(1); // drop sign
const int colonPosition = str.indexOf(QLatin1Char(':'));
// Colon that belongs to offset is at most at position 2 (hh:mm)
@ -1658,7 +1658,7 @@ QDateTimeParser::ParsedSection QDateTimeParser::findUtcOffset(QStringView str) c
str.truncate(i); // The rest of the string is not part of the UTC offset
bool isInt = false;
const int hours = str.mid(0, hoursLength).toInt(&isInt);
const int hours = str.first(hoursLength).toInt(&isInt);
if (!isInt)
return ParsedSection();
const QStringView minutesStr = str.mid(hasColon ? colonPosition + 1 : 2, 2);