summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qbytearray.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/text/qbytearray.h')
-rw-r--r--src/corelib/text/qbytearray.h555
1 files changed, 325 insertions, 230 deletions
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 9f646aaa62..3c8a3bba45 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2022 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QBYTEARRAY_H
#define QBYTEARRAY_H
@@ -45,13 +9,13 @@
#include <QtCore/qnamespace.h>
#include <QtCore/qarraydata.h>
#include <QtCore/qarraydatapointer.h>
+#include <QtCore/qcompare.h>
#include <QtCore/qcontainerfwd.h>
#include <QtCore/qbytearrayalgorithms.h>
#include <QtCore/qbytearrayview.h>
#include <stdlib.h>
#include <string.h>
-#include <stdarg.h>
#include <string>
#include <iterator>
@@ -70,55 +34,15 @@ Q_FORWARD_DECLARE_CF_TYPE(CFData);
Q_FORWARD_DECLARE_OBJC_CLASS(NSData);
#endif
-QT_BEGIN_NAMESPACE
-
-
-/*****************************************************************************
- Safe and portable C string functions; extensions to standard string.h
- *****************************************************************************/
-
-Q_CORE_EXPORT char *qstrdup(const char *);
-
-inline size_t qstrlen(const char *str)
-{ return str ? strlen(str) : 0; }
-
-inline size_t qstrnlen(const char *str, size_t maxlen)
-{
- size_t length = 0;
- if (str) {
- while (length < maxlen && *str++)
- length++;
- }
- return length;
+#if defined(Q_OS_WASM) || defined(Q_QDOC)
+namespace emscripten {
+ class val;
}
+#endif
-Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
-Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
-
-Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
+class tst_QByteArray;
-inline int qstrncmp(const char *str1, const char *str2, size_t len)
-{
- return (str1 && str2) ? strncmp(str1, str2, len)
- : (str1 ? 1 : (str2 ? -1 : 0));
-}
-Q_CORE_EXPORT int qstricmp(const char *, const char *);
-Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
-Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);
-
-// implemented in qvsnprintf.cpp
-Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
-Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
-
-// qChecksum: Internet checksum
-Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
-
-#if QT_DEPRECATED_SINCE(6, 0)
-QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
-inline quint16 qChecksum(const char *s, qsizetype len,
- Qt::ChecksumType standard = Qt::ChecksumIso3309)
-{ return qChecksum(QByteArrayView(s, len), standard); }
-#endif
+QT_BEGIN_NAMESPACE
class QString;
class QDataStream;
@@ -138,6 +62,11 @@ private:
DataPointer d;
static const char _empty;
+
+ friend class ::tst_QByteArray;
+
+ template <typename InputIterator>
+ using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
public:
enum Base64Option {
@@ -163,19 +92,22 @@ public:
QByteArray(const char *, qsizetype size = -1);
QByteArray(qsizetype size, char c);
QByteArray(qsizetype size, Qt::Initialization);
+ explicit QByteArray(QByteArrayView v) : QByteArray(v.data(), v.size()) {}
inline QByteArray(const QByteArray &) noexcept;
inline ~QByteArray();
QByteArray &operator=(const QByteArray &) noexcept;
QByteArray &operator=(const char *str);
inline QByteArray(QByteArray && other) noexcept
- { qSwap(d, other.d); }
+ = default;
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QByteArray)
inline void swap(QByteArray &other) noexcept
- { qSwap(d, other.d); }
+ { d.swap(other.d); }
- inline bool isEmpty() const;
+ bool isEmpty() const noexcept { return size() == 0; }
void resize(qsizetype size);
+ void resize(qsizetype size, char c);
+ void resizeForOverwrite(qsizetype size);
QByteArray &fill(char c, qsizetype size = -1);
@@ -188,11 +120,11 @@ public:
inline operator const void *() const;
#endif
inline char *data();
- inline const char *data() const;
- inline const char *constData() const;
+ inline const char *data() const noexcept;
+ const char *constData() const noexcept { return data(); }
inline void detach();
inline bool isDetached() const;
- inline bool isSharedWith(const QByteArray &other) const
+ inline bool isSharedWith(const QByteArray &other) const noexcept
{ return data() == other.data() && size() == other.size(); }
void clear();
@@ -204,12 +136,16 @@ public:
[[nodiscard]] char back() const { return at(size() - 1); }
[[nodiscard]] inline char &back();
+ QT_CORE_INLINE_SINCE(6, 7)
qsizetype indexOf(char c, qsizetype from = 0) const;
qsizetype indexOf(QByteArrayView bv, qsizetype from = 0) const
{ return QtPrivate::findByteArray(qToByteArrayViewIgnoringNull(*this), from, bv); }
+ QT_CORE_INLINE_SINCE(6, 7)
qsizetype lastIndexOf(char c, qsizetype from = -1) const;
- qsizetype lastIndexOf(QByteArrayView bv, qsizetype from = -1) const
+ qsizetype lastIndexOf(QByteArrayView bv) const
+ { return lastIndexOf(bv, size()); }
+ qsizetype lastIndexOf(QByteArrayView bv, qsizetype from) const
{ return QtPrivate::lastIndexOf(qToByteArrayViewIgnoringNull(*this), from, bv); }
inline bool contains(char c) const;
@@ -220,20 +156,69 @@ public:
inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
- [[nodiscard]] QByteArray left(qsizetype len) const;
- [[nodiscard]] QByteArray right(qsizetype len) const;
- [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const;
-
- [[nodiscard]] QByteArray first(qsizetype n) const
- { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArray(data(), n); }
- [[nodiscard]] QByteArray last(qsizetype n) const
- { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArray(data() + size() - n, n); }
- [[nodiscard]] QByteArray sliced(qsizetype pos) const
- { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QByteArray(data() + pos, size() - pos); }
- [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const
- { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QByteArray(data() + pos, n); }
- [[nodiscard]] QByteArray chopped(qsizetype len) const
- { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return first(size() - len); }
+#if QT_CORE_REMOVED_SINCE(6, 7)
+ QByteArray left(qsizetype len) const;
+ QByteArray right(qsizetype len) const;
+ QByteArray mid(qsizetype index, qsizetype len = -1) const;
+ QByteArray first(qsizetype n) const;
+ QByteArray last(qsizetype n) const;
+ QByteArray sliced(qsizetype pos) const;
+ QByteArray sliced(qsizetype pos, qsizetype n) const;
+ QByteArray chopped(qsizetype len) const;
+#else
+ [[nodiscard]] QByteArray left(qsizetype n) const &
+ {
+ if (n >= size())
+ return *this;
+ return first(qMax(n, 0));
+ }
+ [[nodiscard]] QByteArray left(qsizetype n) &&
+ {
+ if (n >= size())
+ return std::move(*this);
+ return std::move(*this).first(qMax(n, 0));
+ }
+ [[nodiscard]] QByteArray right(qsizetype n) const &
+ {
+ if (n >= size())
+ return *this;
+ return last(qMax(n, 0));
+ }
+ [[nodiscard]] QByteArray right(qsizetype n) &&
+ {
+ if (n >= size())
+ return std::move(*this);
+ return std::move(*this).last(qMax(n, 0));
+ }
+ [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const &;
+ [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) &&;
+
+ [[nodiscard]] QByteArray first(qsizetype n) const &
+ { verify(0, n); return sliced(0, n); }
+ [[nodiscard]] QByteArray last(qsizetype n) const &
+ { verify(0, n); return sliced(size() - n, n); }
+ [[nodiscard]] QByteArray sliced(qsizetype pos) const &
+ { verify(pos, 0); return sliced(pos, size() - pos); }
+ [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const &
+ { verify(pos, n); return QByteArray(d.data() + pos, n); }
+ [[nodiscard]] QByteArray chopped(qsizetype len) const &
+ { verify(0, len); return sliced(0, size() - len); }
+
+ [[nodiscard]] QByteArray first(qsizetype n) &&
+ {
+ verify(0, n);
+ resize(n); // may detach and allocate memory
+ return std::move(*this);
+ }
+ [[nodiscard]] QByteArray last(qsizetype n) &&
+ { verify(0, n); return sliced_helper(*this, size() - n, n); }
+ [[nodiscard]] QByteArray sliced(qsizetype pos) &&
+ { verify(pos, 0); return sliced_helper(*this, pos, size() - pos); }
+ [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) &&
+ { verify(pos, n); return sliced_helper(*this, pos, n); }
+ [[nodiscard]] QByteArray chopped(qsizetype len) &&
+ { verify(0, len); return std::move(*this).first(size() - len); }
+#endif
bool startsWith(QByteArrayView bv) const
{ return QtPrivate::startsWith(qToByteArrayViewIgnoringNull(*this), bv); }
@@ -246,10 +231,26 @@ public:
bool isUpper() const;
bool isLower() const;
+ [[nodiscard]] bool isValidUtf8() const noexcept
+ {
+ return QtPrivate::isValidUtf8(qToByteArrayViewIgnoringNull(*this));
+ }
+
void truncate(qsizetype pos);
void chop(qsizetype n);
-#if !defined(Q_CLANG_QDOC)
+ QByteArray &slice(qsizetype pos)
+ { verify(pos, 0); return remove(0, pos); }
+ QByteArray &slice(qsizetype pos, qsizetype n)
+ {
+ verify(pos, n);
+ if (isNull())
+ return *this;
+ resize(pos + n);
+ return remove(0, pos);
+ }
+
+#if !defined(Q_QDOC)
[[nodiscard]] QByteArray toLower() const &
{ return toLower_helper(*this); }
[[nodiscard]] QByteArray toLower() &&
@@ -290,13 +291,27 @@ public:
QByteArray &append(char c);
inline QByteArray &append(qsizetype count, char c);
QByteArray &append(const char *s)
- { return append(QByteArrayView(s, qsizetype(qstrlen(s)))); }
+ { return append(s, -1); }
QByteArray &append(const char *s, qsizetype len)
- { return append(QByteArrayView(s, len)); }
+ { return append(QByteArrayView(s, len < 0 ? qsizetype(qstrlen(s)) : len)); }
QByteArray &append(const QByteArray &a);
QByteArray &append(QByteArrayView a)
{ return insert(size(), a); }
+ QByteArray &assign(QByteArrayView v);
+ QByteArray &assign(qsizetype n, char c)
+ {
+ Q_ASSERT(n >= 0);
+ return fill(c, n);
+ }
+ template <typename InputIterator, if_input_iterator<InputIterator> = true>
+ QByteArray &assign(InputIterator first, InputIterator last)
+ {
+ d.assign(first, last);
+ d.data()[d.size] = '\0';
+ return *this;
+ }
+
QByteArray &insert(qsizetype i, QByteArrayView data);
inline QByteArray &insert(qsizetype i, const char *s)
{ return insert(i, QByteArrayView(s)); }
@@ -309,6 +324,17 @@ public:
{ return insert(i, QByteArrayView(s, len)); }
QByteArray &remove(qsizetype index, qsizetype len);
+ QByteArray &removeAt(qsizetype pos)
+ { return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; }
+ QByteArray &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; }
+ QByteArray &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; }
+
+ template <typename Predicate>
+ QByteArray &removeIf(Predicate pred)
+ {
+ removeIf_helper(pred);
+ return *this;
+ }
QByteArray &replace(qsizetype index, qsizetype len, const char *s, qsizetype alen)
{ return replace(index, len, QByteArrayView(s, alen)); }
@@ -334,64 +360,15 @@ public:
[[nodiscard]] QByteArray repeated(qsizetype times) const;
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
+#if QT_CORE_REMOVED_SINCE(6, 8)
QT_ASCII_CAST_WARN inline bool operator==(const QString &s2) const;
QT_ASCII_CAST_WARN inline bool operator!=(const QString &s2) const;
QT_ASCII_CAST_WARN inline bool operator<(const QString &s2) const;
QT_ASCII_CAST_WARN inline bool operator>(const QString &s2) const;
QT_ASCII_CAST_WARN inline bool operator<=(const QString &s2) const;
QT_ASCII_CAST_WARN inline bool operator>=(const QString &s2) const;
-#endif
- friend inline bool operator==(const QByteArray &a1, const QByteArray &a2) noexcept
- { return QByteArrayView(a1) == QByteArrayView(a2); }
- friend inline bool operator==(const QByteArray &a1, const char *a2) noexcept
- { return a2 ? QtPrivate::compareMemory(a1, a2) == 0 : a1.isEmpty(); }
- friend inline bool operator==(const char *a1, const QByteArray &a2) noexcept
- { return a1 ? QtPrivate::compareMemory(a1, a2) == 0 : a2.isEmpty(); }
- friend inline bool operator!=(const QByteArray &a1, const QByteArray &a2) noexcept
- { return !(a1==a2); }
- friend inline bool operator!=(const QByteArray &a1, const char *a2) noexcept
- { return a2 ? QtPrivate::compareMemory(a1, a2) != 0 : !a1.isEmpty(); }
- friend inline bool operator!=(const char *a1, const QByteArray &a2) noexcept
- { return a1 ? QtPrivate::compareMemory(a1, a2) != 0 : !a2.isEmpty(); }
- friend inline bool operator<(const QByteArray &a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) < 0; }
- friend inline bool operator<(const QByteArray &a1, const char *a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) < 0; }
- friend inline bool operator<(const char *a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) < 0; }
- friend inline bool operator<=(const QByteArray &a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) <= 0; }
- friend inline bool operator<=(const QByteArray &a1, const char *a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) <= 0; }
- friend inline bool operator<=(const char *a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) <= 0; }
- friend inline bool operator>(const QByteArray &a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) > 0; }
- friend inline bool operator>(const QByteArray &a1, const char *a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) > 0; }
- friend inline bool operator>(const char *a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) > 0; }
- friend inline bool operator>=(const QByteArray &a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) >= 0; }
- friend inline bool operator>=(const QByteArray &a1, const char *a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) >= 0; }
- friend inline bool operator>=(const char *a1, const QByteArray &a2) noexcept
- { return QtPrivate::compareMemory(a1, a2) >= 0; }
-
- // Check isEmpty() instead of isNull() for backwards compatibility.
- friend inline bool operator==(const QByteArray &a1, std::nullptr_t) noexcept { return a1.isEmpty(); }
- friend inline bool operator!=(const QByteArray &a1, std::nullptr_t) noexcept { return !a1.isEmpty(); }
- friend inline bool operator< (const QByteArray & , std::nullptr_t) noexcept { return false; }
- friend inline bool operator> (const QByteArray &a1, std::nullptr_t) noexcept { return !a1.isEmpty(); }
- friend inline bool operator<=(const QByteArray &a1, std::nullptr_t) noexcept { return a1.isEmpty(); }
- friend inline bool operator>=(const QByteArray & , std::nullptr_t) noexcept { return true; }
-
- friend inline bool operator==(std::nullptr_t, const QByteArray &a2) noexcept { return a2 == nullptr; }
- friend inline bool operator!=(std::nullptr_t, const QByteArray &a2) noexcept { return a2 != nullptr; }
- friend inline bool operator< (std::nullptr_t, const QByteArray &a2) noexcept { return a2 > nullptr; }
- friend inline bool operator> (std::nullptr_t, const QByteArray &a2) noexcept { return a2 < nullptr; }
- friend inline bool operator<=(std::nullptr_t, const QByteArray &a2) noexcept { return a2 >= nullptr; }
- friend inline bool operator>=(std::nullptr_t, const QByteArray &a2) noexcept { return a2 <= nullptr; }
+#endif // QT_CORE_REMOVED_SINCE(6, 8)
+#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
short toShort(bool *ok = nullptr, int base = 10) const;
ushort toUShort(bool *ok = nullptr, int base = 10) const;
@@ -408,6 +385,7 @@ public:
QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
const QByteArray &include = QByteArray(),
char percent = '%') const;
+ [[nodiscard]] QByteArray percentDecoded(char percent = '%') const;
inline QByteArray &setNum(short, int base = 10);
inline QByteArray &setNum(ushort, int base = 10);
@@ -417,8 +395,8 @@ public:
inline QByteArray &setNum(ulong, int base = 10);
QByteArray &setNum(qlonglong, int base = 10);
QByteArray &setNum(qulonglong, int base = 10);
- inline QByteArray &setNum(float, char f = 'g', int prec = 6);
- QByteArray &setNum(double, char f = 'g', int prec = 6);
+ inline QByteArray &setNum(float, char format = 'g', int precision = 6);
+ QByteArray &setNum(double, char format = 'g', int precision = 6);
QByteArray &setRawData(const char *a, qsizetype n);
[[nodiscard]] static QByteArray number(int, int base = 10);
@@ -427,7 +405,7 @@ public:
[[nodiscard]] static QByteArray number(ulong, int base = 10);
[[nodiscard]] static QByteArray number(qlonglong, int base = 10);
[[nodiscard]] static QByteArray number(qulonglong, int base = 10);
- [[nodiscard]] static QByteArray number(double, char f = 'g', int prec = 6);
+ [[nodiscard]] static QByteArray number(double, char format = 'g', int precision = 6);
[[nodiscard]] static QByteArray fromRawData(const char *data, qsizetype size)
{
return QByteArray(DataPointer(nullptr, const_cast<char *>(data), size));
@@ -451,26 +429,31 @@ public:
NSData *toRawNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
#endif
+#if defined(Q_OS_WASM) || defined(Q_QDOC)
+ static QByteArray fromEcmaUint8Array(emscripten::val uint8array);
+ emscripten::val toEcmaUint8Array();
+#endif
+
typedef char *iterator;
typedef const char *const_iterator;
typedef iterator Iterator;
typedef const_iterator ConstIterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
- inline iterator begin();
- inline const_iterator begin() const;
- inline const_iterator cbegin() const;
- inline const_iterator constBegin() const;
- inline iterator end();
- inline const_iterator end() const;
- inline const_iterator cend() const;
- inline const_iterator constEnd() const;
+ iterator begin() { return data(); }
+ const_iterator begin() const noexcept { return d.data(); }
+ const_iterator cbegin() const noexcept { return begin(); }
+ const_iterator constBegin() const noexcept { return begin(); }
+ iterator end() { return begin() + size(); }
+ const_iterator end() const noexcept { return begin() + size(); }
+ const_iterator cend() const noexcept { return end(); }
+ const_iterator constEnd() const noexcept { return end(); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
- const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
- const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
- const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
- const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
+ const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
+ const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
+ const_reverse_iterator crbegin() const noexcept { return rbegin(); }
+ const_reverse_iterator crend() const noexcept { return rend(); }
// stl compatibility
typedef qsizetype size_type;
@@ -497,27 +480,94 @@ public:
void push_front(QByteArrayView a)
{ prepend(a); }
void shrink_to_fit() { squeeze(); }
+ iterator erase(const_iterator first, const_iterator last);
+ inline iterator erase(const_iterator it) { return erase(it, it + 1); }
+ static constexpr qsizetype max_size() noexcept
+ {
+ // -1 to deal with the NUL terminator
+ return Data::max_size() - 1;
+ }
- static inline QByteArray fromStdString(const std::string &s);
- inline std::string toStdString() const;
+ static QByteArray fromStdString(const std::string &s);
+ std::string toStdString() const;
- inline qsizetype size() const { return d->size; }
- inline qsizetype count() const { return size(); }
- inline qsizetype length() const { return size(); }
- bool isNull() const;
+ inline qsizetype size() const noexcept { return d->size; }
+#if QT_DEPRECATED_SINCE(6, 4)
+ QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.")
+ inline qsizetype count() const noexcept { return size(); }
+#endif
+ inline qsizetype length() const noexcept { return size(); }
+ QT_CORE_INLINE_SINCE(6, 4)
+ bool isNull() const noexcept;
+ inline const DataPointer &data_ptr() const { return d; }
inline DataPointer &data_ptr() { return d; }
- explicit inline QByteArray(const DataPointer &dd)
- : d(dd)
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
+ explicit inline QByteArray(const DataPointer &dd) : d(dd) {}
+#endif
+ explicit inline QByteArray(DataPointer &&dd) : d(std::move(dd)) {}
+
+private:
+ friend bool comparesEqual(const QByteArray &lhs, const QByteArrayView &rhs) noexcept
+ { return QByteArrayView(lhs) == rhs; }
+ friend Qt::strong_ordering
+ compareThreeWay(const QByteArray &lhs, const QByteArrayView &rhs) noexcept
{
+ const int res = QtPrivate::compareMemory(QByteArrayView(lhs), rhs);
+ return Qt::compareThreeWay(res, 0);
}
+ Q_DECLARE_STRONGLY_ORDERED(QByteArray)
+ Q_DECLARE_STRONGLY_ORDERED(QByteArray, QByteArrayView)
+ Q_DECLARE_STRONGLY_ORDERED(QByteArray, const char *)
+#if defined(__GLIBCXX__) && defined(__cpp_lib_three_way_comparison)
+ // libstdc++ has a bug [0] when `operator const void *()` is preferred over
+ // `operator<=>()` when calling std::less<> and other similar methods.
+ // Fix it by explicitly providing relational operators in such case.
+ // [0]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153
+ friend bool operator<(const QByteArray &lhs, const QByteArray &rhs) noexcept
+ { return is_lt(compareThreeWay(lhs, rhs)); }
+ friend bool operator<=(const QByteArray &lhs, const QByteArray &rhs) noexcept
+ { return is_lteq(compareThreeWay(lhs, rhs)); }
+ friend bool operator>(const QByteArray &lhs, const QByteArray &rhs) noexcept
+ { return is_gt(compareThreeWay(lhs, rhs)); }
+ friend bool operator>=(const QByteArray &lhs, const QByteArray &rhs) noexcept
+ { return is_gteq(compareThreeWay(lhs, rhs)); }
+#endif // defined(__GLIBCXX__) && defined(__cpp_lib_three_way_comparison)
+
+ // Check isEmpty() instead of isNull() for backwards compatibility.
+ friend bool comparesEqual(const QByteArray &lhs, std::nullptr_t) noexcept
+ { return lhs.isEmpty(); }
+ friend Qt::strong_ordering compareThreeWay(const QByteArray &lhs, std::nullptr_t) noexcept
+ { return lhs.isEmpty() ? Qt::strong_ordering::equivalent : Qt::strong_ordering::greater; }
+ Q_DECLARE_STRONGLY_ORDERED(QByteArray, std::nullptr_t)
+
+ // defined in qstring.cpp
+ friend Q_CORE_EXPORT bool comparesEqual(const QByteArray &lhs, const QChar &rhs) noexcept;
+ friend Q_CORE_EXPORT Qt::strong_ordering
+ compareThreeWay(const QByteArray &lhs, const QChar &rhs) noexcept;
+ friend Q_CORE_EXPORT bool comparesEqual(const QByteArray &lhs, char16_t rhs) noexcept;
+ friend Q_CORE_EXPORT Qt::strong_ordering
+ compareThreeWay(const QByteArray &lhs, char16_t rhs) noexcept;
+#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
+ Q_DECLARE_STRONGLY_ORDERED(QByteArray, QChar, QT_ASCII_CAST_WARN)
+ Q_DECLARE_STRONGLY_ORDERED(QByteArray, char16_t, QT_ASCII_CAST_WARN)
+#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
+
-private:
void reallocData(qsizetype alloc, QArrayData::AllocationOption option);
void reallocGrowData(qsizetype n);
void expand(qsizetype i);
- QByteArray nulTerminated() const;
+ Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
+ [[maybe_unused]] qsizetype n = 1) const
+ {
+ Q_ASSERT(pos >= 0);
+ Q_ASSERT(pos <= d.size);
+ Q_ASSERT(n >= 0);
+ Q_ASSERT(n <= d.size - pos);
+ }
+
+ static QByteArray sliced_helper(QByteArray &a, qsizetype pos, qsizetype n);
static QByteArray toLower_helper(const QByteArray &a);
static QByteArray toLower_helper(QByteArray &a);
static QByteArray toUpper_helper(const QByteArray &a);
@@ -526,9 +576,20 @@ private:
static QByteArray trimmed_helper(QByteArray &a);
static QByteArray simplified_helper(const QByteArray &a);
static QByteArray simplified_helper(QByteArray &a);
+ template <typename Predicate>
+ qsizetype removeIf_helper(Predicate pred)
+ {
+ const qsizetype result = d->eraseIf(pred);
+ if (result > 0)
+ d.data()[d.size] = '\0';
+ return result;
+ }
friend class QString;
friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, qsizetype nbytes);
+
+ template <typename T> friend qsizetype erase(QByteArray &ba, const T &t);
+ template <typename Predicate> friend qsizetype erase_if(QByteArray &ba, Predicate pred);
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
@@ -537,12 +598,10 @@ inline constexpr QByteArray::QByteArray() noexcept {}
inline QByteArray::~QByteArray() {}
inline char QByteArray::at(qsizetype i) const
-{ Q_ASSERT(size_t(i) < size_t(size())); return d.data()[i]; }
+{ verify(i, 1); return d.data()[i]; }
inline char QByteArray::operator[](qsizetype i) const
-{ Q_ASSERT(size_t(i) < size_t(size())); return d.data()[i]; }
+{ verify(i, 1); return d.data()[i]; }
-inline bool QByteArray::isEmpty() const
-{ return size() == 0; }
#ifndef QT_NO_CAST_FROM_BYTEARRAY
inline QByteArray::operator const char *() const
{ return data(); }
@@ -555,7 +614,7 @@ inline char *QByteArray::data()
Q_ASSERT(d.data());
return d.data();
}
-inline const char *QByteArray::data() const
+inline const char *QByteArray::data() const noexcept
{
#if QT5_NULL_STRINGS == 1
return d.data() ? d.data() : &_empty;
@@ -563,8 +622,6 @@ inline const char *QByteArray::data() const
return d.data();
#endif
}
-inline const char *QByteArray::constData() const
-{ return data(); }
inline void QByteArray::detach()
{ if (d->needsDetach()) reallocData(size(), QArrayData::KeepSize); }
inline bool QByteArray::isDetached() const
@@ -593,25 +650,9 @@ inline void QByteArray::squeeze()
}
inline char &QByteArray::operator[](qsizetype i)
-{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; }
+{ verify(i, 1); return data()[i]; }
inline char &QByteArray::front() { return operator[](0); }
inline char &QByteArray::back() { return operator[](size() - 1); }
-inline QByteArray::iterator QByteArray::begin()
-{ return data(); }
-inline QByteArray::const_iterator QByteArray::begin() const
-{ return data(); }
-inline QByteArray::const_iterator QByteArray::cbegin() const
-{ return data(); }
-inline QByteArray::const_iterator QByteArray::constBegin() const
-{ return data(); }
-inline QByteArray::iterator QByteArray::end()
-{ return data() + size(); }
-inline QByteArray::const_iterator QByteArray::end() const
-{ return data() + size(); }
-inline QByteArray::const_iterator QByteArray::cend() const
-{ return data() + size(); }
-inline QByteArray::const_iterator QByteArray::constEnd() const
-{ return data() + size(); }
inline QByteArray &QByteArray::append(qsizetype n, char ch)
{ return insert(size(), n, ch); }
inline QByteArray &QByteArray::prepend(qsizetype n, char ch)
@@ -626,40 +667,57 @@ inline int QByteArray::compare(QByteArrayView a, Qt::CaseSensitivity cs) const n
qstrnicmp(data(), size(), a.data(), a.size());
}
#if !defined(QT_USE_QSTRINGBUILDER)
-inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
+inline QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
{ return QByteArray(a1) += a2; }
-inline const QByteArray operator+(const QByteArray &a1, const char *a2)
+inline QByteArray operator+(QByteArray &&lhs, const QByteArray &rhs)
+{ return std::move(lhs += rhs); }
+inline QByteArray operator+(const QByteArray &a1, const char *a2)
{ return QByteArray(a1) += a2; }
-inline const QByteArray operator+(const QByteArray &a1, char a2)
+inline QByteArray operator+(QByteArray &&lhs, const char *rhs)
+{ return std::move(lhs += rhs); }
+inline QByteArray operator+(const QByteArray &a1, char a2)
{ return QByteArray(a1) += a2; }
-inline const QByteArray operator+(const char *a1, const QByteArray &a2)
+inline QByteArray operator+(QByteArray &&lhs, char rhs)
+{ return std::move(lhs += rhs); }
+inline QByteArray operator+(const char *a1, const QByteArray &a2)
{ return QByteArray(a1) += a2; }
-inline const QByteArray operator+(char a1, const QByteArray &a2)
+inline QByteArray operator+(char a1, const QByteArray &a2)
{ return QByteArray(&a1, 1) += a2; }
#endif // QT_USE_QSTRINGBUILDER
inline QByteArray &QByteArray::setNum(short n, int base)
-{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); }
+{ return setNum(qlonglong(n), base); }
inline QByteArray &QByteArray::setNum(ushort n, int base)
{ return setNum(qulonglong(n), base); }
inline QByteArray &QByteArray::setNum(int n, int base)
-{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); }
+{ return setNum(qlonglong(n), base); }
inline QByteArray &QByteArray::setNum(uint n, int base)
{ return setNum(qulonglong(n), base); }
inline QByteArray &QByteArray::setNum(long n, int base)
-{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ulong(n)), base); }
+{ return setNum(qlonglong(n), base); }
inline QByteArray &QByteArray::setNum(ulong n, int base)
{ return setNum(qulonglong(n), 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::setNum(float n, char format, int precision)
+{ return setNum(double(n), format, precision); }
-inline QByteArray QByteArray::fromStdString(const std::string &s)
-{ return QByteArray(s.data(), qsizetype(s.size())); }
+#if QT_CORE_INLINE_IMPL_SINCE(6, 4)
+bool QByteArray::isNull() const noexcept
+{
+ return d->isNull();
+}
+#endif
+#if QT_CORE_INLINE_IMPL_SINCE(6, 7)
+qsizetype QByteArray::indexOf(char ch, qsizetype from) const
+{
+ return qToByteArrayViewIgnoringNull(*this).indexOf(ch, from);
+}
+qsizetype QByteArray::lastIndexOf(char ch, qsizetype from) const
+{
+ return qToByteArrayViewIgnoringNull(*this).lastIndexOf(ch, from);
+}
+#endif
-#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
+#if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
#endif
@@ -683,8 +741,8 @@ public:
void swap(QByteArray::FromBase64Result &other) noexcept
{
- qSwap(decoded, other.decoded);
- qSwap(decodingStatus, other.decodingStatus);
+ decoded.swap(other.decoded);
+ std::swap(decodingStatus, other.decodingStatus);
}
explicit operator bool() const noexcept { return decodingStatus == QByteArray::Base64DecodingStatus::Ok; }
@@ -720,14 +778,51 @@ Q_DECLARE_SHARED(QByteArray::FromBase64Result)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray::FromBase64Result &key, size_t seed = 0) noexcept;
+template <typename T>
+qsizetype erase(QByteArray &ba, const T &t)
+{
+ return ba.removeIf_helper([&t](const auto &e) { return t == e; });
+}
+
+template <typename Predicate>
+qsizetype erase_if(QByteArray &ba, Predicate pred)
+{
+ return ba.removeIf_helper(pred);
+}
+
//
// QByteArrayView members that require QByteArray:
//
QByteArray QByteArrayView::toByteArray() const
{
- return QByteArray(data(), size());
+ return QByteArray(*this);
+}
+
+namespace Qt {
+inline namespace Literals {
+inline namespace StringLiterals {
+
+inline QByteArray operator""_ba(const char *str, size_t size) noexcept
+{
+ return QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), qsizetype(size)));
}
+} // StringLiterals
+} // Literals
+} // Qt
+
+inline namespace QtLiterals {
+#if QT_DEPRECATED_SINCE(6, 8)
+
+QT_DEPRECATED_VERSION_X_6_8("Use _ba from Qt::StringLiterals namespace instead.")
+inline QByteArray operator""_qba(const char *str, size_t size) noexcept
+{
+ return Qt::StringLiterals::operator""_ba(str, size);
+}
+
+#endif // QT_DEPRECATED_SINCE(6, 8)
+} // QtLiterals
+
QT_END_NAMESPACE
#endif // QBYTEARRAY_H