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/corelib/tools/qstring.h | |
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/corelib/tools/qstring.h')
-rw-r--r-- | src/corelib/tools/qstring.h | 26 |
1 files changed, 26 insertions, 0 deletions
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 &); |