From 208c71768cca1dda9ea42f3a395889a85810005e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 Mar 2017 11:07:54 +0100 Subject: QChar: add (char16_t) and (wchar_t) ctors ... for better std C++ integration. [ChangeLog][QtCore][QChar] Added constructors from char16_t and, on Windows, wchar_t. Change-Id: I2d18ea3a37e869b8ea9f4036d7200d9d13c7d929 Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira --- src/corelib/tools/qchar.cpp | 18 ++++++++++++++++++ src/corelib/tools/qchar.h | 7 +++++++ tests/auto/corelib/tools/qchar/tst_qchar.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/corelib/tools/qchar.cpp b/src/corelib/tools/qchar.cpp index 5d5ea4c8a1..db440390c1 100644 --- a/src/corelib/tools/qchar.cpp +++ b/src/corelib/tools/qchar.cpp @@ -607,6 +607,24 @@ QT_BEGIN_NAMESPACE Constructs a QChar for the predefined character value \a ch. */ +/*! + \fn QChar::QChar(char16_t ch) + \since 5.10 + + Constructs a QChar corresponding to the UTF-16 character \a ch. + + \note This constructor is not available on MSVC 2013. +*/ + +/*! + \fn QChar::QChar(wchar_t ch) + \since 5.10 + + Constructs a QChar corresponding to the wide character \a ch. + + \note This constructor is only available on Windows. +*/ + /*! \fn QChar::QChar(char ch) diff --git a/src/corelib/tools/qchar.h b/src/corelib/tools/qchar.h index a83e5e6f98..8509e2e182 100644 --- a/src/corelib/tools/qchar.h +++ b/src/corelib/tools/qchar.h @@ -86,6 +86,13 @@ public: Q_DECL_CONSTEXPR QChar(int rc) Q_DECL_NOTHROW : ucs(ushort(rc & 0xffff)) {} Q_DECL_CONSTEXPR QChar(SpecialCharacter s) Q_DECL_NOTHROW : ucs(ushort(s)) {} // implicit Q_DECL_CONSTEXPR QChar(QLatin1Char ch) Q_DECL_NOTHROW : ucs(ch.unicode()) {} // implicit +#if !defined(Q_OS_WIN) || defined(Q_COMPILER_UNICODE_STRINGS) + Q_DECL_CONSTEXPR QChar(char16_t ch) Q_DECL_NOTHROW : ucs(ushort(ch)) {} // implicit +#endif +#if defined(Q_OS_WIN) + Q_STATIC_ASSERT(sizeof(wchar_t) == sizeof(ushort)); + Q_DECL_CONSTEXPR QChar(wchar_t ch) Q_DECL_NOTHROW : ucs(ushort(ch)) {} // implicit +#endif #ifndef QT_NO_CAST_FROM_ASCII QT_ASCII_CAST_WARN Q_DECL_CONSTEXPR explicit QChar(char c) Q_DECL_NOTHROW : ucs(uchar(c)) { } diff --git a/tests/auto/corelib/tools/qchar/tst_qchar.cpp b/tests/auto/corelib/tools/qchar/tst_qchar.cpp index 13898ace7b..dede56b4bf 100644 --- a/tests/auto/corelib/tools/qchar/tst_qchar.cpp +++ b/tests/auto/corelib/tools/qchar/tst_qchar.cpp @@ -36,6 +36,8 @@ class tst_QChar : public QObject { Q_OBJECT private slots: + void fromChar16_t(); + void fromWchar_t(); void operator_eqeq_int(); void operators_data(); void operators(); @@ -75,6 +77,30 @@ private slots: QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED +void tst_QChar::fromChar16_t() +{ +#if !defined(Q_OS_WIN) || defined(Q_COMPILER_UNICODE_STRINGS) + QChar aUmlaut = u'\u00E4'; // German small letter a-umlaut + QCOMPARE(aUmlaut, QChar(0xE4)); + QChar replacementCharacter = u'\uFFFD'; + QCOMPARE(replacementCharacter, QChar(QChar::ReplacementCharacter)); +#else + QSKIP("This test requires C++11 char16_t support enabled in the compiler."); +#endif +} + +void tst_QChar::fromWchar_t() +{ +#if defined(Q_OS_WIN) + QChar aUmlaut = L'\u00E4'; // German small letter a-umlaut + QCOMPARE(aUmlaut, QChar(0xE4)); + QChar replacementCharacter = L'\uFFFD'; + QCOMPARE(replacementCharacter, QChar(QChar::ReplacementCharacter)); +#else + QSKIP("This is a Windows-only test."); +#endif +} + void tst_QChar::operator_eqeq_int() { { -- cgit v1.2.3