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>
This commit is contained in:
Allan Sandfeld Jensen 2014-06-19 11:01:33 +02:00 committed by Thiago Macieira
parent 273ed8e0fa
commit a1fc11ca65
5 changed files with 53 additions and 3 deletions

View File

@ -4189,6 +4189,27 @@ QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent
return tmp; 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) /*! \fn QByteArray QByteArray::fromCFData(CFDataRef data)
\since 5.3 \since 5.3

View File

@ -50,6 +50,8 @@
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <string>
#ifdef truncate #ifdef truncate
#error qbytearray.h must be included before any header file that defines truncate #error qbytearray.h must be included before any header file that defines truncate
#endif #endif
@ -397,6 +399,9 @@ public:
void push_front(const char *c); void push_front(const char *c);
void push_front(const QByteArray &a); 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; } inline int count() const { return d->size; }
int length() const { return d->size; } int length() const { return d->size; }
bool isNull() const; 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) inline QByteArray &QByteArray::setNum(float n, char f, int prec)
{ return setNum(double(n),f,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)) #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &); Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);

View File

@ -1330,7 +1330,7 @@ const QString::Null QString::null = { };
This constructor is only available if Qt is configured with STL This constructor is only available if Qt is configured with STL
compatibility enabled. compatibility enabled.
\sa fromLatin1(), fromLocal8Bit(), fromUtf8() \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString()
*/ */
/*! \fn QString QString::fromStdWString(const std::wstring &str) /*! \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 If the QString contains non-Latin1 Unicode characters, using this
can lead to loss of information. can lead to loss of information.
\sa toLatin1(), toUtf8(), toLocal8Bit() \sa toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::toStdString()
*/ */
/*! /*!

View File

@ -1222,7 +1222,7 @@ inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteA
#endif // QT_USE_QSTRINGBUILDER #endif // QT_USE_QSTRINGBUILDER
inline std::string QString::toStdString() const 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) inline QString QString::fromStdString(const std::string &s)
{ return fromUtf8(s.data(), int(s.size())); } { return fromUtf8(s.data(), int(s.size())); }

View File

@ -153,6 +153,8 @@ private slots:
#endif #endif
void macTypes(); void macTypes();
void stdString();
}; };
static const struct StaticByteArrays { static const struct StaticByteArrays {
@ -2007,6 +2009,23 @@ void tst_QByteArray::macTypes()
#endif #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'; const char globalChar = '1';
QTEST_MAIN(tst_QByteArray) QTEST_MAIN(tst_QByteArray)