src/corelib/tools/qbytearray.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   152     \a src or \a dst is 0, returns 0 immediately.
   152     \a src or \a dst is 0, returns 0 immediately.
   153 
   153 
   154     This function assumes that \a dst is at least \a len characters
   154     This function assumes that \a dst is at least \a len characters
   155     long.
   155     long.
   156 
   156 
       
   157     \note When compiling with Visual C++ compiler version 14.00
       
   158     (Visual C++ 2005) or later, internally the function strncpy_s
       
   159     will be used.
       
   160 
   157     \sa qstrcpy()
   161     \sa qstrcpy()
   158 */
   162 */
   159 
   163 
   160 char *qstrncpy(char *dst, const char *src, uint len)
   164 char *qstrncpy(char *dst, const char *src, uint len)
   161 {
   165 {
  1055 
  1059 
  1056     \internal
  1060     \internal
  1057 */
  1061 */
  1058 
  1062 
  1059 /*! \fn bool QByteArray::isDetached() const
  1063 /*! \fn bool QByteArray::isDetached() const
       
  1064 
       
  1065     \internal
       
  1066 */
       
  1067 
       
  1068 /*! \fn bool QByteArray::isSharedWith(const QByteArray &other) const
  1060 
  1069 
  1061     \internal
  1070     \internal
  1062 */
  1071 */
  1063 
  1072 
  1064 /*! \fn char QByteArray::at(int i) const
  1073 /*! \fn char QByteArray::at(int i) const
  1797 
  1806 
  1798     \overload
  1807     \overload
  1799 */
  1808 */
  1800 QByteArray &QByteArray::replace(int pos, int len, const char *after)
  1809 QByteArray &QByteArray::replace(int pos, int len, const char *after)
  1801 {
  1810 {
  1802     int alen = qstrlen(after);
  1811     return replace(pos,len,after,qstrlen(after));
       
  1812 }
       
  1813 
       
  1814 /*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen)
       
  1815 
       
  1816     \overload
       
  1817 
       
  1818     Replaces \a len bytes from index position \a pos with \a alen bytes
       
  1819     from the string \a after. \a after is allowed to have '\0' characters.
       
  1820 
       
  1821     \since 4.7
       
  1822 */
       
  1823 QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen)
       
  1824 {
  1803     if (len == alen && (pos + len <= d->size)) {
  1825     if (len == alen && (pos + len <= d->size)) {
  1804         detach();
  1826         detach();
  1805         memcpy(d->data + pos, after, len*sizeof(char));
  1827         memcpy(d->data + pos, after, len*sizeof(char));
  1806         return *this;
  1828         return *this;
  1807     } else {
  1829     } else {
  2657 /*! \relates QByteArray
  2679 /*! \relates QByteArray
  2658 
  2680 
  2659     Writes byte array \a ba to the stream \a out and returns a reference
  2681     Writes byte array \a ba to the stream \a out and returns a reference
  2660     to the stream.
  2682     to the stream.
  2661 
  2683 
  2662     \sa {Format of the QDataStream operators}
  2684     \sa {Serializing Qt Data Types}
  2663 */
  2685 */
  2664 
  2686 
  2665 QDataStream &operator<<(QDataStream &out, const QByteArray &ba)
  2687 QDataStream &operator<<(QDataStream &out, const QByteArray &ba)
  2666 {
  2688 {
  2667     if (ba.isNull() && out.version() >= 6) {
  2689     if (ba.isNull() && out.version() >= 6) {
  2674 /*! \relates QByteArray
  2696 /*! \relates QByteArray
  2675 
  2697 
  2676     Reads a byte array into \a ba from the stream \a in and returns a
  2698     Reads a byte array into \a ba from the stream \a in and returns a
  2677     reference to the stream.
  2699     reference to the stream.
  2678 
  2700 
  2679     \sa {Format of the QDataStream operators}
  2701     \sa {Serializing Qt Data Types}
  2680 */
  2702 */
  2681 
  2703 
  2682 QDataStream &operator>>(QDataStream &in, QByteArray &ba)
  2704 QDataStream &operator>>(QDataStream &in, QByteArray &ba)
  2683 {
  2705 {
  2684     ba.clear();
  2706     ba.clear();
  3790     position \a size. While that does not matter for QDataStream or
  3812     position \a size. While that does not matter for QDataStream or
  3791     functions like indexOf(), passing the byte array to a function
  3813     functions like indexOf(), passing the byte array to a function
  3792     accepting a \c{const char *} expected to be '\\0'-terminated will
  3814     accepting a \c{const char *} expected to be '\\0'-terminated will
  3793     fail.
  3815     fail.
  3794 
  3816 
  3795     \sa data(), constData()
  3817     \sa setRawData(), data(), constData()
  3796 */
  3818 */
  3797 
  3819 
  3798 QByteArray QByteArray::fromRawData(const char *data, int size)
  3820 QByteArray QByteArray::fromRawData(const char *data, int size)
  3799 {
  3821 {
  3800     Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
  3822     Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
  3807     }
  3829     }
  3808     x->ref = 1;
  3830     x->ref = 1;
  3809     x->alloc = x->size = size;
  3831     x->alloc = x->size = size;
  3810     *x->array = '\0';
  3832     *x->array = '\0';
  3811     return QByteArray(x, 0, 0);
  3833     return QByteArray(x, 0, 0);
       
  3834 }
       
  3835 
       
  3836 /*!
       
  3837     \since 4.7
       
  3838 
       
  3839     Resets the QByteArray to use the first \a size bytes of the
       
  3840     \a data array. The bytes are \e not copied. The QByteArray will
       
  3841     contain the \a data pointer. The caller guarantees that \a data
       
  3842     will not be deleted or modified as long as this QByteArray and any
       
  3843     copies of it exist that have not been modified.
       
  3844 
       
  3845     This function can be used instead of fromRawData() to re-use
       
  3846     existings QByteArray objects to save memory re-allocations.
       
  3847 
       
  3848     \sa fromRawData(), data(), constData()
       
  3849 */
       
  3850 QByteArray &QByteArray::setRawData(const char *data, uint size)
       
  3851 {
       
  3852     if (d->ref != 1 || d->alloc) {
       
  3853         *this = fromRawData(data, size);
       
  3854     } else {
       
  3855         if (data) {
       
  3856             d->data = const_cast<char *>(data);
       
  3857         } else {
       
  3858             d->data = d->array;
       
  3859             size = 0;
       
  3860         }
       
  3861         d->alloc = d->size = size;
       
  3862         *d->array = '\0';
       
  3863     }
       
  3864     return *this;
  3812 }
  3865 }
  3813 
  3866 
  3814 /*!
  3867 /*!
  3815     Returns a decoded copy of the Base64 array \a base64. Input is not checked
  3868     Returns a decoded copy of the Base64 array \a base64. Input is not checked
  3816     for validity; invalid characters in the input are skipped, enabling the
  3869     for validity; invalid characters in the input are skipped, enabling the
  4204     \note QByteArray uses implicit sharing so if you modify a copy, only the
  4257     \note QByteArray uses implicit sharing so if you modify a copy, only the
  4205     copy is changed.
  4258     copy is changed.
  4206 */
  4259 */
  4207 
  4260 
  4208 /*!
  4261 /*!
  4209     \fn QByteArray& QByteArray::setRawData(const char *a, uint n)
       
  4210 
       
  4211     Use fromRawData() instead.
       
  4212 */
       
  4213 
       
  4214 /*!
       
  4215     \fn void QByteArray::resetRawData(const char *data, uint n)
  4262     \fn void QByteArray::resetRawData(const char *data, uint n)
  4216 
  4263 
  4217     Use clear() instead.
  4264     Use clear() instead.
  4218 */
  4265 */
  4219 
  4266