From 8aa3cf21da696f241ab06504362fe243c3ca32b0 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Mon, 21 Mar 2022 14:32:58 +0100 Subject: Add literal operators for QString/QByteArray to StringLiterals namespace [ChangeLog][QtCore] Added literal operators for _s and _ba for QString and QByteArray respectively in the Qt::Literals::StringLiterals namespace. Task-number: QTBUG-101408 Change-Id: I5cd4e7f36f614ea805cfecc27b91c5d981cd3794 Reviewed-by: Ivan Solovev Reviewed-by: Andrei Golubev Reviewed-by: Marc Mutz --- src/corelib/text/qbytearray.cpp | 25 +++++++++++++++++++++++++ src/corelib/text/qbytearray.h | 15 ++++++++++++++- src/corelib/text/qstring.cpp | 25 +++++++++++++++++++++++++ src/corelib/text/qstring.h | 19 ++++++++++++------- 4 files changed, 76 insertions(+), 8 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 7675d7f4a2..9ec5870ea1 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -4778,6 +4778,31 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA \sa QByteArrayLiteral, QtLiterals::operator""_qs(const char16_t *str, size_t size) */ +/*! + \fn Qt::Literals::StringLiterals::operator""_ba(const char *str, size_t size) + + \relates QByteArray + \since 6.4 + + Literal operator that creates a QByteArray out of the first \a size characters + in the char string literal \a str. + + The QByteArray is created at compile time, and the generated string data is stored + in the read-only segment of the compiled object file. Duplicate literals may share + the same read-only memory. This functionality is interchangeable with + QByteArrayLiteral, but saves typing when many string literals are present in the + code. + + The following code creates a QByteArray: + \code + using namespace Qt::Literals::StringLiterals; + + auto str = "hello"_ba; + \endcode + + \sa Qt::Literals::StringLiterals +*/ + /*! \class QByteArray::FromBase64Result \inmodule QtCore diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 75fb75fd35..8c7594ccba 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -690,10 +690,23 @@ QByteArray QByteArrayView::toByteArray() const return QByteArray(data(), size()); } +namespace Qt { +inline namespace Literals { +inline namespace StringLiterals { + +inline QByteArray operator"" _ba(const char *str, size_t size) noexcept +{ + return QByteArray(QByteArrayData(nullptr, const_cast(str), qsizetype(size))); +} + +} // StringLiterals +} // Literals +} // Qt + inline namespace QtLiterals { inline QByteArray operator"" _qba(const char *str, size_t size) noexcept { - return QByteArray(QByteArrayData(nullptr, const_cast(str), qsizetype(size))); + return Qt::StringLiterals::operator""_ba(str, size); } } // QtLiterals diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 644078f779..a350401b30 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -10983,6 +10983,31 @@ QString QString::toHtmlEscaped() const \sa QStringLiteral, QtLiterals::operator""_qba(const char *str, size_t size) */ +/*! + \fn Qt::Literals::StringLiterals::operator""_s(const char16_t *str, size_t size) + + \relates QString + \since 6.4 + + Literal operator that creates a QString out of the first \a size characters in + the char16_t string literal \a str. + + The QString is created at compile time, and the generated string data is stored + in the read-only segment of the compiled object file. Duplicate literals may + share the same read-only memory. This functionality is interchangeable with + QStringLiteral, but saves typing when many string literals are present in the + code. + + The following code creates a QString: + \code + using namespace Qt::Literals::StringLiterals; + + auto str = u"hello"_s; + \endcode + + \sa Qt::Literals::StringLiterals +*/ + /*! \fn Qt::Literals::StringLiterals::operator""_L1(const char *str, size_t size) diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index bc97585e8c..266627c331 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1658,13 +1658,6 @@ qsizetype erase_if(QString &s, Predicate pred) return QtPrivate::sequential_erase_if(s, pred); } -inline namespace QtLiterals { -inline QString operator"" _qs(const char16_t *str, size_t size) noexcept -{ - return QString(QStringPrivate(nullptr, const_cast(str), qsizetype(size))); -} -} // QtLiterals - namespace Qt { inline namespace Literals { inline namespace StringLiterals { @@ -1674,10 +1667,22 @@ constexpr inline QLatin1StringView operator"" _L1(const char *str, size_t size) return {str, qsizetype(size)}; } +inline QString operator"" _s(const char16_t *str, size_t size) noexcept +{ + return QString(QStringPrivate(nullptr, const_cast(str), qsizetype(size))); +} + } // StringLiterals } // Literals } // Qt +inline namespace QtLiterals { +inline QString operator"" _qs(const char16_t *str, size_t size) noexcept +{ + return Qt::StringLiterals::operator""_s(str, size); +} +} // QtLiterals + QT_END_NAMESPACE #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) -- cgit v1.2.3