File indexing completed on 2025-09-17 09:09:09
0001
0002
0003
0004 #ifndef QBYTEARRAYALGORITHMS_H
0005 #define QBYTEARRAYALGORITHMS_H
0006
0007 #include <QtCore/qnamespace.h>
0008
0009 #include <string.h>
0010 #include <stdarg.h>
0011
0012 #if 0
0013 #pragma qt_class(QByteArrayAlgorithms)
0014 #endif
0015
0016 QT_BEGIN_NAMESPACE
0017
0018 class QByteArrayView;
0019
0020 namespace QtPrivate {
0021
0022 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0023 bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
0024
0025 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0026 bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
0027
0028 [[nodiscard]] inline
0029 qsizetype findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept;
0030
0031 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0032 qsizetype findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
0033
0034 [[nodiscard]] inline
0035 qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept;
0036
0037 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0038 qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
0039
0040 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0041 qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept;
0042
0043 [[nodiscard]] Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs);
0044
0045 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept;
0046
0047 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept;
0048
0049 template <typename T>
0050 class ParsedNumber
0051 {
0052 T m_value;
0053 quint32 m_error : 1;
0054 quint32 m_reserved : 31;
0055 void *m_reserved2 = nullptr;
0056 public:
0057 constexpr ParsedNumber() noexcept : m_value(), m_error(true), m_reserved(0) {}
0058 constexpr explicit ParsedNumber(T v) : m_value(v), m_error(false), m_reserved(0) {}
0059
0060
0061 explicit operator bool() const noexcept { return !m_error; }
0062 T &operator*() { Q_ASSERT(*this); return m_value; }
0063 const T &operator*() const { Q_ASSERT(*this); return m_value; }
0064 T *operator->() noexcept { return *this ? &m_value : nullptr; }
0065 const T *operator->() const noexcept { return *this ? &m_value : nullptr; }
0066 template <typename U>
0067 T value_or(U &&u) const { return *this ? m_value : T(std::forward<U>(u)); }
0068 };
0069
0070 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<double> toDouble(QByteArrayView a) noexcept;
0071 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<float> toFloat(QByteArrayView a) noexcept;
0072 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qlonglong> toSignedInteger(QByteArrayView data, int base);
0073 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qulonglong> toUnsignedInteger(QByteArrayView data, int base);
0074
0075
0076
0077
0078 template <typename T, typename ByteArrayView,
0079 typename = std::enable_if_t<std::is_same_v<ByteArrayView, QByteArrayView>>>
0080 static inline T toIntegral(ByteArrayView data, bool *ok, int base)
0081 {
0082 const auto val = [&] {
0083 if constexpr (std::is_unsigned_v<T>)
0084 return toUnsignedInteger(data, base);
0085 else
0086 return toSignedInteger(data, base);
0087 }();
0088 const bool failed = !val || T(*val) != *val;
0089 if (ok)
0090 *ok = !failed;
0091 if (failed)
0092 return 0;
0093 return T(*val);
0094 }
0095
0096 }
0097
0098
0099
0100
0101
0102 [[nodiscard]] Q_DECL_PURE_FUNCTION Q_CORE_EXPORT
0103 const void *qmemrchr(const void *s, int needle, size_t n) noexcept;
0104 Q_CORE_EXPORT char *qstrdup(const char *);
0105
0106 inline size_t qstrlen(const char *str)
0107 {
0108 QT_WARNING_PUSH
0109 #if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 900 && Q_CC_GNU < 1000
0110
0111
0112 QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
0113 #endif
0114 return str ? strlen(str) : 0;
0115 QT_WARNING_POP
0116 }
0117
0118 inline size_t qstrnlen(const char *str, size_t maxlen)
0119 {
0120 if (!str)
0121 return 0;
0122 auto end = static_cast<const char *>(memchr(str, '\0', maxlen));
0123 return end ? end - str : maxlen;
0124 }
0125
0126
0127 Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
0128 Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
0129
0130 Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
0131
0132 inline int qstrncmp(const char *str1, const char *str2, size_t len)
0133 {
0134 return (str1 && str2) ? strncmp(str1, str2, len)
0135 : (str1 ? 1 : (str2 ? -1 : 0));
0136 }
0137 Q_CORE_EXPORT int qstricmp(const char *, const char *);
0138 Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
0139 Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);
0140
0141 #ifndef QT_NO_QSNPRINTF
0142 #if QT_DEPRECATED_SINCE(6, 9)
0143 #define QSNPF_DEPR(vsn) \
0144 QT_DEPRECATED_VERSION_X_6_9("Use C++11 std::" #vsn "printf() instead, taking care to " \
0145 "ensure that you didn't rely on QString::asprintf() " \
0146 "idiosyncrasies that q" #vsn "printf might, but " \
0147 "std::" #vsn "printf() does not, support.")
0148
0149 QSNPF_DEPR(vsn)
0150 Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
0151 Q_ATTRIBUTE_FORMAT_PRINTF(3, 0);
0152 QSNPF_DEPR(sn)
0153 Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...)
0154 Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0155 #undef QSNPF_DEPR
0156 #endif
0157 #endif
0158
0159
0160 Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
0161
0162 QT_END_NAMESPACE
0163
0164 #endif