QStringView: add internal qToStringViewIgnoringNull()

As long as a null QString still returns non-null data(), QStringView's
QString constructor needs to call isNull(). That's a branch we often
can do without, so add an internal function that bypasses this
correctness check.

It's internal, since we might have a Q6String that returns nullptr
data() when null, which will remove the need for this function.

What the QStringView(QString) ctor does will also have to be
re-evaluated come Qt 6, but for now, keep the public QString-
QStringView conversion correct.

Change-Id: I35dc7383bc3bd018f46aeec429185135a38ddcef
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2017-04-12 09:52:24 +02:00
parent b3e66c76aa
commit ba5db13c8d
2 changed files with 21 additions and 0 deletions

View File

@ -704,4 +704,19 @@ QT_BEGIN_NAMESPACE
\sa toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec
*/
/*!
\fn qToStringViewIgnoringNull(const QStringLike &s);
\since 5.10
\internal
Convert \a s to a QStringView ignoring \c{s.isNull()}.
Returns a string-view that references \a{s}' data, but is never null.
This is a faster way to convert a QString or QStringRef to a QStringView,
if null QStrings can legitimately be treated as empty ones.
\sa QString::isNull(), QStringRef::isNull(), QStringView
*/
QT_END_NAMESPACE

View File

@ -272,6 +272,12 @@ private:
};
Q_DECLARE_TYPEINFO(QStringView, Q_MOVABLE_TYPE);
template <typename QStringLike, typename std::enable_if<
std::is_same<QStringLike, QString>::value || std::is_same<QStringLike, QStringRef>::value,
bool>::type = true>
inline QStringView qToStringViewIgnoringNull(const QStringLike &s) Q_DECL_NOTHROW
{ return QStringView(s.data(), s.size()); }
QT_END_NAMESPACE
#endif /* QSTRINGVIEW_H */