Skip to content

Commit

Permalink
Convert some uses of QStringRef to QStringView
Browse files Browse the repository at this point in the history
There remain QStringRef uses where QString::splitRef() is used.
Requires converting some .count()s to .size()s, as QStringView lacks count();
and some .toInt()s need to be handled by QLocale::c().

Change-Id: If9a49e063d217671ea9335a82e4bf977b7b48be0
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
ediosyncratic committed Dec 9, 2019
1 parent 8310981 commit f16bd40
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/corelib/time/qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static const char qt_shortMonthNames[][4] = {
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

static int qt_monthNumberFromShortName(QStringRef shortName)
static int qt_monthNumberFromShortName(QStringView shortName)
{
for (unsigned int i = 0; i < sizeof(qt_shortMonthNames) / sizeof(qt_shortMonthNames[0]); ++i) {
if (shortName == QLatin1String(qt_shortMonthNames[i], 3))
Expand All @@ -136,9 +136,9 @@ static int qt_monthNumberFromShortName(QStringRef shortName)
return -1;
}
static int qt_monthNumberFromShortName(const QString &shortName)
{ return qt_monthNumberFromShortName(QStringRef(&shortName)); }
{ return qt_monthNumberFromShortName(QStringView(shortName)); }

static int fromShortMonthName(const QStringRef &monthName, int year)
static int fromShortMonthName(QStringView monthName, int year)
{
// Assume that English monthnames are the default
int month = qt_monthNumberFromShortName(monthName);
Expand Down Expand Up @@ -207,7 +207,7 @@ static QString toOffsetString(Qt::DateFormat format, int offset)

#if QT_CONFIG(datestring)
// Parse offset in [+-]HH[[:]mm] format
static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcept
static int fromOffsetString(QStringView offsetString, bool *valid) noexcept
{
*valid = false;

Expand All @@ -228,22 +228,23 @@ static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcep
return 0;

// Split the hour and minute parts
const QStringRef time = offsetString.mid(1);
int hhLen = time.indexOf(QLatin1Char(':'));
int mmIndex;
const QStringView time = offsetString.mid(1);
qsizetype hhLen = time.indexOf(QLatin1Char(':'));
qsizetype mmIndex;
if (hhLen == -1)
mmIndex = hhLen = 2; // [+-]HHmm or [+-]HH format
else
mmIndex = hhLen + 1;

const QStringRef hhRef = time.left(hhLen);
const QLocale C = QLocale::c();
const QStringView hhRef = time.left(qMin(hhLen, time.size()));
bool ok = false;
const int hour = hhRef.toInt(&ok);
const int hour = C.toInt(hhRef, &ok);
if (!ok)
return 0;

const QStringRef mmRef = time.mid(mmIndex);
const int minute = mmRef.isEmpty() ? 0 : mmRef.toInt(&ok);
const QStringView mmRef = time.mid(qMin(mmIndex, time.size()));
const int minute = mmRef.isEmpty() ? 0 : C.toInt(mmRef, &ok);
if (!ok || minute < 0 || minute > 59)
return 0;

Expand Down Expand Up @@ -2324,7 +2325,7 @@ int QTime::msecsTo(const QTime &t) const

#if QT_CONFIG(datestring)

static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format, bool *isMidnight24)
static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *isMidnight24)
{
if (isMidnight24)
*isMidnight24 = false;
Expand All @@ -2333,11 +2334,12 @@ static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format,
if (size < 5)
return QTime();

const QLocale C(QLocale::c());
bool ok = false;
int hour = string.mid(0, 2).toInt(&ok);
int hour = C.toInt(string.mid(0, 2), &ok);
if (!ok)
return QTime();
const int minute = string.mid(3, 2).toInt(&ok);
const int minute = C.toInt(string.mid(3, 2), &ok);
if (!ok)
return QTime();
int second = 0;
Expand All @@ -2358,11 +2360,11 @@ static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format,
// seconds is 4. E.g. 12:34,99999 will expand to 12:34:59.9994. The milliseconds
// will then be rounded up AND clamped to 999.

const QStringRef minuteFractionStr = string.mid(6, 5);
const long minuteFractionInt = minuteFractionStr.toLong(&ok);
const QStringView minuteFractionStr = string.mid(6, qMin(qsizetype(5), string.size() - 6));
const long minuteFractionInt = C.toLong(minuteFractionStr, &ok);
if (!ok)
return QTime();
const float minuteFraction = double(minuteFractionInt) / (std::pow(double(10), minuteFractionStr.count()));
const float minuteFraction = double(minuteFractionInt) / (std::pow(double(10), minuteFractionStr.size()));

const float secondWithMs = minuteFraction * 60;
const float secondNoMs = std::floor(secondWithMs);
Expand All @@ -2371,20 +2373,20 @@ static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format,
msec = qMin(qRound(secondFraction * 1000.0), 999);
} else {
// HH:mm:ss or HH:mm:ss.zzz
second = string.mid(6, 2).toInt(&ok);
second = C.toInt(string.mid(6, qMin(qsizetype(2), string.size() - 6)), &ok);
if (!ok)
return QTime();
if (size > 8 && (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.'))) {
QStringRef msecStr(string.mid(9, 4));
QStringView msecStr(string.mid(9, qMin(qsizetype(4), string.size() - 9)));
// toInt() ignores leading spaces, so catch them before calling it
if (!msecStr.isEmpty() && !msecStr.at(0).isDigit())
return QTime();
// We do, however, want to ignore *trailing* spaces.
msecStr = msecStr.trimmed();
int msecInt = msecStr.isEmpty() ? 0 : msecStr.toInt(&ok);
int msecInt = msecStr.isEmpty() ? 0 : C.toInt(msecStr, &ok);
if (!ok)
return QTime();
const double secondFraction(msecInt / (std::pow(double(10), msecStr.count())));
const double secondFraction(msecInt / (std::pow(double(10), msecStr.size())));
msec = qMin(qRound(secondFraction * 1000.0), 999);
}
}
Expand Down Expand Up @@ -2433,7 +2435,7 @@ QTime QTime::fromString(const QString &string, Qt::DateFormat format)
case Qt::ISODateWithMs:
case Qt::TextDate:
default:
return fromIsoTimeString(QStringRef(&string), format, nullptr);
return fromIsoTimeString(QStringView(string), format, nullptr);
}
}

Expand Down Expand Up @@ -5217,8 +5219,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
return date.startOfDay();

Qt::TimeSpec spec = Qt::LocalTime;
QStringRef isoString(&string);
isoString = isoString.mid(10); // trim "yyyy-MM-dd"
QStringView isoString = QStringView(string).mid(10); // trim "yyyy-MM-dd"

// Must be left with T (or space) and at least one digit for the hour:
if (isoString.size() < 2
Expand Down Expand Up @@ -5364,7 +5365,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
if (parts.count() == 5)
return QDateTime(date, time, Qt::LocalTime);

QStringRef tz = parts.at(5);
QStringView tz = parts.at(5);
if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
return QDateTime();
tz = tz.mid(3);
Expand Down

0 comments on commit f16bd40

Please sign in to comment.