diff options
author | Allan Sandfeld Jensen <allan.jensen@digia.com> | 2014-06-19 11:01:33 +0200 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2014-06-25 16:51:53 +0200 |
commit | a1fc11ca655850a47701f3715f1408c336ddb6c7 (patch) | |
tree | 39f897d1674a11368ef98a840edea178be48ee4e /tests/auto/corelib/tools/qbytearray | |
parent | 273ed8e0fa527f4c4382d45eaced54314318173f (diff) |
Introduce std::string conversion to QByteArray
Add conversion methods similar to those in QString to QByteArray. This
is often more useful than the QString version since std::string like
QByteArray are byte arrays.
[ChangeLog][QtCore][QByteArray] Added convenience methods to convert
directly to and from std::string.
Change-Id: I92c29d4bb1d9e06a667dd9cdd936970e2d272006
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/corelib/tools/qbytearray')
-rw-r--r-- | tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index cdcbd19ae8..8fac232962 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -153,6 +153,8 @@ private slots: #endif void macTypes(); + + void stdString(); }; static const struct StaticByteArrays { @@ -2007,6 +2009,23 @@ void tst_QByteArray::macTypes() #endif } +void tst_QByteArray::stdString() +{ + std::string stdstr( "QByteArray" ); + + const QByteArray stlqt = QByteArray::fromStdString(stdstr); + QCOMPARE(stlqt.length(), int(stdstr.length())); + QCOMPARE(stlqt.data(), stdstr.c_str()); + QCOMPARE(stlqt.toStdString(), stdstr); + + std::string utf8str( "Nøt æscii" ); + const QByteArray u8 = QByteArray::fromStdString(utf8str); + const QByteArray l1 = QString::fromUtf8(u8).toLatin1(); + std::string l1str = l1.toStdString(); + QVERIFY(l1str.length() < utf8str.length()); +} + + const char globalChar = '1'; QTEST_MAIN(tst_QByteArray) |