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>
This commit is contained in:
parent
d45d62f09d
commit
e440fec7fc
@ -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
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -10849,6 +10849,25 @@ QString QString::toHtmlEscaped() const
|
||||
\sa QStringLiteral, QtLiterals::operator""_qba(const char *str, size_t size)
|
||||
*/
|
||||
|
||||
/*!
|
||||
\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
|
||||
*/
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -46,6 +46,7 @@ class tst_QLatin1String : public QObject
|
||||
private Q_SLOTS:
|
||||
void constExpr();
|
||||
void construction();
|
||||
void userDefinedLiterals();
|
||||
void at();
|
||||
void arg() const;
|
||||
void midLeftRight();
|
||||
@ -146,6 +147,50 @@ void tst_QLatin1String::construction()
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QLatin1String::userDefinedLiterals()
|
||||
{
|
||||
{
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
auto str = "abcd"_L1;
|
||||
static_assert(std::is_same_v<decltype(str), QLatin1String>);
|
||||
QCOMPARE(str.size(), 4);
|
||||
QCOMPARE(str, QLatin1String("abcd"));
|
||||
QCOMPARE(str.latin1(), "abcd");
|
||||
QCOMPARE("abcd"_L1, str.latin1());
|
||||
QCOMPARE("M\xE5rten"_L1, QLatin1String("M\xE5rten"));
|
||||
|
||||
auto ch = 'a'_L1;
|
||||
static_assert(std::is_same_v<decltype(ch), QLatin1Char>);
|
||||
QCOMPARE(ch, QLatin1Char('a'));
|
||||
QCOMPARE(ch.toLatin1(), 'a');
|
||||
QCOMPARE('a'_L1, ch.toLatin1());
|
||||
QCOMPARE('\xE5'_L1, QLatin1Char('\xE5'));
|
||||
}
|
||||
{
|
||||
using namespace Qt::Literals;
|
||||
|
||||
auto str = "abcd"_L1;
|
||||
static_assert(std::is_same_v<decltype(str), QLatin1String>);
|
||||
QCOMPARE(str, QLatin1String("abcd"));
|
||||
|
||||
auto ch = 'a'_L1;
|
||||
static_assert(std::is_same_v<decltype(ch), QLatin1Char>);
|
||||
QCOMPARE(ch, QLatin1Char('a'));
|
||||
}
|
||||
{
|
||||
using namespace Qt;
|
||||
|
||||
auto str = "abcd"_L1;
|
||||
static_assert(std::is_same_v<decltype(str), QLatin1String>);
|
||||
QCOMPARE(str, QLatin1String("abcd"));
|
||||
|
||||
auto ch = 'a'_L1;
|
||||
static_assert(std::is_same_v<decltype(ch), QLatin1Char>);
|
||||
QCOMPARE(ch, QLatin1Char('a'));
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QLatin1String::at()
|
||||
{
|
||||
const QLatin1String l1("Hello World");
|
||||
|
Loading…
Reference in New Issue
Block a user