From a1fc11ca655850a47701f3715f1408c336ddb6c7 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 19 Jun 2014 11:01:33 +0200 Subject: [PATCH] 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 Reviewed-by: Olivier Goffart --- src/corelib/tools/qbytearray.cpp | 21 +++++++++++++++++++ src/corelib/tools/qbytearray.h | 10 +++++++++ src/corelib/tools/qstring.cpp | 4 ++-- src/corelib/tools/qstring.h | 2 +- .../tools/qbytearray/tst_qbytearray.cpp | 19 +++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 6fed3ae5c91..7ca47961c06 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -4189,6 +4189,27 @@ QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent return tmp; } +/*! \fn QByteArray QByteArray::fromStdString(const std::string &str) + \since 5.4 + + Returns a copy of the \a str string as a QByteArray. + + \sa toStdString(), QString::fromStdString() +*/ + +/*! + \fn std::string QByteArray::toStdString() const + \since 5.4 + + Returns a std::string object with the data contained in this + QByteArray. + + This operator is mostly useful to pass a QByteArray to a function + that accepts a std::string object. + + \sa fromStdString(), QString::toStdString() +*/ + /*! \fn QByteArray QByteArray::fromCFData(CFDataRef data) \since 5.3 diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 9155ddc977a..b5af3052333 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -50,6 +50,8 @@ #include #include +#include + #ifdef truncate #error qbytearray.h must be included before any header file that defines truncate #endif @@ -397,6 +399,9 @@ public: void push_front(const char *c); void push_front(const QByteArray &a); + static inline QByteArray fromStdString(const std::string &s); + inline std::string toStdString() const; + inline int count() const { return d->size; } int length() const { return d->size; } bool isNull() const; @@ -620,6 +625,11 @@ inline QByteArray &QByteArray::setNum(uint n, int base) inline QByteArray &QByteArray::setNum(float n, char f, int prec) { return setNum(double(n),f,prec); } +inline std::string QByteArray::toStdString() const +{ return std::string(constData(), length()); } + +inline QByteArray QByteArray::fromStdString(const std::string &s) +{ return QByteArray(s.data(), int(s.size())); } #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &); diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index a7d516a7267..a7f932698bd 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -1330,7 +1330,7 @@ const QString::Null QString::null = { }; This constructor is only available if Qt is configured with STL compatibility enabled. - \sa fromLatin1(), fromLocal8Bit(), fromUtf8() + \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString() */ /*! \fn QString QString::fromStdWString(const std::wstring &str) @@ -7748,7 +7748,7 @@ bool QString::isRightToLeft() const If the QString contains non-Latin1 Unicode characters, using this can lead to loss of information. - \sa toLatin1(), toUtf8(), toLocal8Bit() + \sa toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::toStdString() */ /*! diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 3985bc76fea..9f36c8bcf96 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1222,7 +1222,7 @@ inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteA #endif // QT_USE_QSTRINGBUILDER inline std::string QString::toStdString() const -{ const QByteArray asc = toUtf8(); return std::string(asc.constData(), asc.length()); } +{ return toUtf8().toStdString(); } inline QString QString::fromStdString(const std::string &s) { return fromUtf8(s.data(), int(s.size())); } diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index cdcbd19ae8a..8fac2329622 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)