summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2022-03-21 14:32:58 +0100
committerSona Kurazyan <sona.kurazyan@qt.io>2022-04-07 09:22:28 +0200
commit8aa3cf21da696f241ab06504362fe243c3ca32b0 (patch)
treee9c1b988d3543af02eedbf637fd718b23639d047 /src/corelib/text
parenta0470ec2617357293d13fa5ec2c87efbd3965b4c (diff)
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 <ivan.solovev@qt.io> Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qbytearray.cpp25
-rw-r--r--src/corelib/text/qbytearray.h15
-rw-r--r--src/corelib/text/qstring.cpp25
-rw-r--r--src/corelib/text/qstring.h19
4 files changed, 76 insertions, 8 deletions
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
@@ -4779,6 +4779,31 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA
*/
/*!
+ \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
\ingroup tools
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<char *>(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<char *>(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
@@ -10984,6 +10984,31 @@ QString QString::toHtmlEscaped() const
*/
/*!
+ \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)
\relates QLatin1StringView
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<char16_t *>(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<char16_t *>(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)