diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-02-23 10:39:15 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-03-04 01:58:10 +0100 |
commit | e440fec7fc729342dce7c8421618a95dab11a36b (patch) | |
tree | 64e2bc568b0da05adb69d84850bb0761105738b1 /src/corelib | |
parent | d45d62f09dd276faf3d960d18aba191e4c3c78ba (diff) |
Add literal operators for QLatin1String and QLatin1Char
The operators are declared in the Qt::Literals::StringLiterals
namespace, to avoid collisions in the global namespace.
[ChangeLog][QtCore][QLatin1String] Added literal operator""_L1 that
converts string literals and chars to QLatin1String and QLatin1Char.
Fixes: QTBUG-98434
Change-Id: Ia945a6acf4b8d4fbbb5f803264e4d79d7b17a8da
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/text/qchar.cpp | 17 | ||||
-rw-r--r-- | src/corelib/text/qchar.h | 13 | ||||
-rw-r--r-- | src/corelib/text/qstring.cpp | 19 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 13 | ||||
-rw-r--r-- | src/corelib/text/qtliterals.qdoc | 33 |
5 files changed, 95 insertions, 0 deletions
diff --git a/src/corelib/text/qchar.cpp b/src/corelib/text/qchar.cpp index d4a30c1dc6..f9ada412c0 100644 --- a/src/corelib/text/qchar.cpp +++ b/src/corelib/text/qchar.cpp @@ -1821,6 +1821,23 @@ QDataStream &operator>>(QDataStream &in, QChar &chr) that of \a c2; otherwise returns \c false. */ +/*! + \fn Qt::Literals::StringLiterals::operator""_L1(char ch) + + \relates QLatin1Char + \since 6.4 + + Literal operator that creates a QLatin1Char out of \a ch. + + The following code creates a QLatin1Char: + \code + using namespace Qt::Literals::StringLiterals; + + auto ch = 'a'_L1; + \endcode + + \sa Qt::Literals::StringLiterals +*/ // --------------------------------------------------------------------------- diff --git a/src/corelib/text/qchar.h b/src/corelib/text/qchar.h index f8d63c54f1..c391dd13ec 100644 --- a/src/corelib/text/qchar.h +++ b/src/corelib/text/qchar.h @@ -654,6 +654,19 @@ Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QChar); Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QChar &); #endif +namespace Qt { +inline namespace Literals { +inline namespace StringLiterals { + +constexpr inline QLatin1Char operator"" _L1(char ch) noexcept +{ + return QLatin1Char(ch); +} + +} // StringLiterals +} // Literals +} // Qt + QT_END_NAMESPACE namespace std { diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index ae5f57ce41..9b6fe88cf1 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -10850,6 +10850,25 @@ QString QString::toHtmlEscaped() const */ /*! + \fn Qt::Literals::StringLiterals::operator""_L1(const char *str, size_t size) + + \relates QLatin1String + \since 6.4 + + Literal operator that creates a QLatin1String out of the first \a size + characters in the char string literal \a str. + + The following code creates a QLatin1String: + \code + using namespace Qt::Literals::StringLiterals; + + auto str = "hello"_L1; + \endcode + + \sa Qt::Literals::StringLiterals +*/ + +/*! \internal */ void QAbstractConcatenable::appendLatin1To(QLatin1String in, QChar *out) noexcept diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 350659ed46..c8c25da3cc 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1589,6 +1589,19 @@ inline QString operator"" _qs(const char16_t *str, size_t size) noexcept } } // QtLiterals +namespace Qt { +inline namespace Literals { +inline namespace StringLiterals { + +constexpr inline QLatin1String operator"" _L1(const char *str, size_t size) noexcept +{ + return QLatin1String(str, qsizetype(size)); +} + +} // StringLiterals +} // Literals +} // Qt + QT_END_NAMESPACE #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) diff --git a/src/corelib/text/qtliterals.qdoc b/src/corelib/text/qtliterals.qdoc index d5875c3e08..ce248ab5ed 100644 --- a/src/corelib/text/qtliterals.qdoc +++ b/src/corelib/text/qtliterals.qdoc @@ -31,3 +31,36 @@ \brief The QtLiterals namespace declares literal operators for Qt types. */ + +/*! + \namespace Qt::Literals + \inmodule QtCore + + \brief The Literals inline namespace declares literal operators for Qt types. +*/ + +/*! + \namespace Qt::Literals::StringLiterals + \inmodule QtCore + + \brief The StringLiterals namespace declares string literal operators + for Qt types. + + The inline Qt::Literals::StringLiterals namespace declares string literal + operators for Qt types. Because both \c Literals and \c StringLiterals + namespaces are declared as inline, the symbols from this namespace can be + accessed by adding one of the following to your code: + + \code + // Makes visible only the literal operators declared in StringLiterals + using namespace Qt::Literals::StringLiterals; + + // Makes visible literal operators declared in all inline namespaces + // inside Literals + using namespace Qt::Literals; + + // Makes visible all symbols (including all literal operators) declared + // in the Qt namespace + using namespace Qt; + \endcode +*/ |