From dd07b1e38927d47802087c75c365f13d13a04248 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 14 Jan 2015 15:39:36 +0100 Subject: Add conversion functions for C++11 u16string and u32string The patch adds convenience functions for working on C++11's new char width specific unicode strings u16string and u32string. [ChangeLog][QtCore][QString] Added methods for convenient conversion to and from std::u16string and std::u32string. Change-Id: I67c082e4755c592d61daaaaa70c8867ef0b23dcb Reviewed-by: Olivier Goffart (Woboq GmbH) --- tests/auto/corelib/tools/qstring/qstring.pro | 1 + tests/auto/corelib/tools/qstring/tst_qstring.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qstring/qstring.pro b/tests/auto/corelib/tools/qstring/qstring.pro index 971e2fb782..1eda27e1ff 100644 --- a/tests/auto/corelib/tools/qstring/qstring.pro +++ b/tests/auto/corelib/tools/qstring/qstring.pro @@ -4,6 +4,7 @@ QT = core testlib SOURCES = tst_qstring.cpp DEFINES += QT_NO_CAST_TO_ASCII contains(QT_CONFIG,icu):DEFINES += QT_USE_ICU +contains(QT_CONFIG,c++11): CONFIG += c++11 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 mac { diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 875bf6571a..b0f0b24a67 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -59,6 +59,7 @@ #include #include +#include #define CREATE_REF(string) \ const QString padded = QString::fromLatin1(" %1 ").arg(string); \ @@ -263,6 +264,7 @@ private slots: void assignQLatin1String(); void isRightToLeft_data(); void isRightToLeft(); + void unicodeStrings(); }; template const T &verifyZeroTermination(const T &t) { return t; } @@ -5222,6 +5224,27 @@ void tst_QString::fromUtf16_char16() #endif } +void tst_QString::unicodeStrings() +{ +#ifdef Q_COMPILER_UNICODE_STRINGS + QString s1, s2; + static const std::u16string u16str1(u"Hello Unicode World"); + static const std::u32string u32str1(U"Hello Unicode World"); + s1 = QString::fromStdU16String(u16str1); + s2 = QString::fromStdU32String(u32str1); + QCOMPARE(s1, QString("Hello Unicode World")); + QCOMPARE(s1, s2); + + QCOMPARE(s2.toStdU16String(), u16str1); + QCOMPARE(s1.toStdU32String(), u32str1); + + s1 = QString::fromStdU32String(std::u32string(U"\u221212\U000020AC\U00010000")); + QCOMPARE(s1, QString::fromUtf8("\342\210\222" "12" "\342\202\254" "\360\220\200\200")); +#else + QSKIP("Compiler does not support C++11 unicode strings"); +#endif +} + void tst_QString::latin1String() { QString s("Hello"); -- cgit v1.2.3