diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2015-01-14 15:39:36 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2015-01-24 11:50:13 +0100 |
commit | dd07b1e38927d47802087c75c365f13d13a04248 (patch) | |
tree | dbb2116dfde06fbf22eea7e4f55c42b0f77e3b2d /src | |
parent | 05cda3d46514822ab6748514ee8fb658e60fdc0b (diff) |
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) <ogoffart@woboq.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 48 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 26 |
2 files changed, 70 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 &); |