OSDN Git Service

introduce std::string conversion to QByteArray
authorIvailo Monev <xakepa10@gmail.com>
Thu, 14 Jan 2016 18:02:27 +0000 (20:02 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Thu, 14 Jan 2016 18:02:27 +0000 (20:02 +0200)
changes to qstring header are not the same, it requires switch to UTF by
default which is introduced with upstream commit
https://github.com/qtproject/qtbase/commit/b6e4c859d38674e77fb3b1372cb7751f3cfe1682

upstream commits:
https://github.com/qtproject/qtbase/commit/a1fc11ca655850a47701f3715f1408c336ddb6c7

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/core/tools/qbytearray.cpp
src/core/tools/qbytearray.h
src/core/tools/qstring.cpp
src/core/tools/qstring.h

index c8f6d21..c6a1806 100644 (file)
@@ -859,6 +859,27 @@ QByteArray::Data QByteArray::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1),
     Same as prepend(\a ch).
 */
 
+/*! \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(const QByteArray &other)
 
     Constructs a copy of \a other.
index 5a599e1..7638043 100644 (file)
 #include <string.h>
 #include <stdarg.h>
 
+#ifndef QT_NO_STL
+#  include <string>
+#endif
+
 #ifdef truncate
 #error qbytearray.h must be included before any header file that defines truncate
 #endif
@@ -332,6 +336,11 @@ public:
     void push_front(const char *c);
     void push_front(const QByteArray &a);
 
+#ifndef QT_NO_STL
+    static inline QByteArray fromStdString(const std::string &s);
+    inline std::string toStdString() const;
+#endif
+
     inline int count() const { return d->size; }
     int length() const { return d->size; }
     bool isNull() const;
@@ -547,6 +556,13 @@ inline QByteArray &QByteArray::setNum(uint n, int base)
 inline QByteArray &QByteArray::setNum(float n, char f, int prec)
 { return setNum(double(n),f,prec); }
 
+#ifndef QT_NO_STL
+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())); }
+#endif
 
 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
index 8fa2351..c73fb1c 100644 (file)
@@ -923,7 +923,7 @@ int QString::grow(int size)
     This constructor is only available if Qt is configured with STL
     compatibility enabled.
 
-    \sa  fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
+    \sa  fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString()
 */
 
 /*! \fn QString QString::fromStdWString(const std::wstring &str)
@@ -7104,7 +7104,7 @@ bool QString::isRightToLeft() const
     This operator is only available if Qt is configured with STL
     compatibility enabled.
 
-    \sa toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
+    \sa toAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::fromStdString()
 */
 
 /*!
index 6664fd0..c087ae1 100644 (file)
@@ -929,7 +929,7 @@ inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteA
 
 #ifndef QT_NO_STL
 inline std::string QString::toStdString() const
-{ const QByteArray asc = toAscii(); return std::string(asc.constData(), asc.length()); }
+{ return toAscii().toStdString(); }
 
 inline QString QString::fromStdString(const std::string &s)
 { return fromAscii(s.data(), int(s.size())); }