summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qstring.cpp48
-rw-r--r--src/corelib/tools/qstring.h26
-rw-r--r--tests/auto/corelib/tools/qstring/qstring.pro1
-rw-r--r--tests/auto/corelib/tools/qstring/tst_qstring.cpp23
4 files changed, 94 insertions, 4 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index d9eb9c7b5d..0b5426ef0a 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -1356,7 +1356,7 @@ const QString::Null QString::null = { };
windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix
systems).
- \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4()
+ \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdU16String(), fromStdU32String()
*/
/*! \fn QString QString::fromWCharArray(const wchar_t *string, int size)
@@ -1381,7 +1381,7 @@ const QString::Null QString::null = { };
This method is mostly useful to pass a QString to a function
that accepts a std::wstring object.
- \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit()
+ \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit(), toStdU16String(), toStdU32String()
*/
int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
@@ -4632,7 +4632,7 @@ QString QString::fromUtf8_helper(const char *str, int size)
QString makes a deep copy of the Unicode data.
- \sa utf16(), setUtf16()
+ \sa utf16(), setUtf16(), fromStdU16String()
*/
QString QString::fromUtf16(const ushort *unicode, int size)
{
@@ -4656,7 +4656,7 @@ QString QString::fromUtf16(const ushort *unicode, int size)
If \a size is -1 (default), \a unicode must be terminated
with a 0.
- \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray()
+ \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray(), fromStdU32String()
*/
QString QString::fromUcs4(const uint *unicode, int size)
{
@@ -7887,6 +7887,46 @@ QString &QString::setRawData(const QChar *unicode, int size)
return *this;
}
+/*! \fn QString QString::fromStdU16String(const std::u16string &str)
+ \since 5.5
+
+ Returns a copy of the \a str string. The given string is assumed
+ to be encoded in UTF-16.
+
+ \sa fromUtf16(), fromStdWString(), fromStdU32String()
+*/
+
+/*!
+ \fn std::u16string QString::toStdU16String() const
+ \since 5.5
+
+ Returns a std::u16string object with the data contained in this
+ QString. The Unicode data is the same as returned by the utf16()
+ method.
+
+ \sa utf16(), toStdWString(), toStdU32String()
+*/
+
+/*! \fn QString QString::fromStdU32String(const std::u32string &str)
+ \since 5.5
+
+ Returns a copy of the \a str string. The given string is assumed
+ to be encoded in UCS-4.
+
+ \sa fromUcs4(), fromStdWString(), fromStdU16String()
+*/
+
+/*!
+ \fn std::u32string QString::toStdU32String() const
+ \since 5.5
+
+ Returns a std::u32string object with the data contained in this
+ QString. The Unicode data is the same as returned by the toUcs4()
+ method.
+
+ \sa toUcs4(), toStdWString(), toStdU16String()
+*/
+
/*! \class QLatin1String
\inmodule QtCore
\brief The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index d20d08bc0c..0deb664dc7 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -733,6 +733,13 @@ public:
static inline QString fromStdWString(const std::wstring &s);
inline std::wstring toStdWString() const;
+#if defined(Q_COMPILER_UNICODE_STRINGS) || defined(Q_QDOC)
+ static inline QString fromStdU16String(const std::u16string &s);
+ inline std::u16string toStdU16String() const;
+ static inline QString fromStdU32String(const std::u32string &s);
+ inline std::u32string toStdU32String() const;
+#endif
+
#if defined(Q_OS_MAC) || defined(Q_QDOC)
static QString fromCFString(CFStringRef string);
CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
@@ -1294,6 +1301,25 @@ inline std::wstring QString::toStdWString() const
inline QString QString::fromStdWString(const std::wstring &s)
{ return fromWCharArray(s.data(), int(s.size())); }
+#if defined(Q_COMPILER_UNICODE_STRINGS)
+inline QString QString::fromStdU16String(const std::u16string &s)
+{ return fromUtf16(s.data(), int(s.size())); }
+
+inline std::u16string QString::toStdU16String() const
+{ return std::u16string(reinterpret_cast<const char16_t*>(utf16()), length()); }
+
+inline QString QString::fromStdU32String(const std::u32string &s)
+{ return fromUcs4(s.data(), int(s.size())); }
+
+inline std::u32string QString::toStdU32String() const
+{
+ std::u32string u32str(length(), char32_t(0));
+ int len = toUcs4_helper(d->data(), length(), reinterpret_cast<uint*>(&u32str[0]));
+ u32str.resize(len);
+ return u32str;
+}
+#endif
+
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);
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 <locale.h>
#include <qhash.h>
+#include <string>
#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 <class T> 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");