File indexing completed on 2025-01-18 10:07:17
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]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0029 qsizetype findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
0030
0031 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0032 qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
0033
0034 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0035 qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept;
0036
0037 [[nodiscard]] Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs);
0038
0039 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept;
0040
0041 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept;
0042
0043 template <typename T>
0044 class ParsedNumber
0045 {
0046 T m_value;
0047 quint32 m_error : 1;
0048 quint32 m_reserved : 31;
0049 void *m_reserved2 = nullptr;
0050 public:
0051 constexpr ParsedNumber() noexcept : m_value(), m_error(true), m_reserved(0) {}
0052 constexpr explicit ParsedNumber(T v) : m_value(v), m_error(false), m_reserved(0) {}
0053
0054
0055 explicit operator bool() const noexcept { return !m_error; }
0056 T &operator*() { Q_ASSERT(*this); return m_value; }
0057 const T &operator*() const { Q_ASSERT(*this); return m_value; }
0058 T *operator->() noexcept { return *this ? &m_value : nullptr; }
0059 const T *operator->() const noexcept { return *this ? &m_value : nullptr; }
0060 template <typename U>
0061 T value_or(U &&u) const { return *this ? m_value : T(std::forward<U>(u)); }
0062 };
0063
0064 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<double> toDouble(QByteArrayView a) noexcept;
0065 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<float> toFloat(QByteArrayView a) noexcept;
0066 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qlonglong> toSignedInteger(QByteArrayView data, int base);
0067 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qulonglong> toUnsignedInteger(QByteArrayView data, int base);
0068
0069
0070
0071
0072 template <typename T, typename ByteArrayView,
0073 typename = std::enable_if_t<std::is_same_v<ByteArrayView, QByteArrayView>>>
0074 static inline T toIntegral(ByteArrayView data, bool *ok, int base)
0075 {
0076 const auto val = [&] {
0077 if constexpr (std::is_unsigned_v<T>)
0078 return toUnsignedInteger(data, base);
0079 else
0080 return toSignedInteger(data, base);
0081 }();
0082 const bool failed = !val || T(*val) != *val;
0083 if (ok)
0084 *ok = !failed;
0085 if (failed)
0086 return 0;
0087 return T(*val);
0088 }
0089
0090 }
0091
0092
0093
0094
0095
0096 Q_CORE_EXPORT char *qstrdup(const char *);
0097
0098 inline size_t qstrlen(const char *str)
0099 {
0100 QT_WARNING_PUSH
0101 #if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 900 && Q_CC_GNU < 1000
0102
0103
0104 QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
0105 #endif
0106 return str ? strlen(str) : 0;
0107 QT_WARNING_POP
0108 }
0109
0110 inline size_t qstrnlen(const char *str, size_t maxlen)
0111 {
0112 if (!str)
0113 return 0;
0114 auto end = static_cast<const char *>(memchr(str, '\0', maxlen));
0115 return end ? end - str : maxlen;
0116 }
0117
0118
0119 Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
0120 Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
0121
0122 Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
0123
0124 inline int qstrncmp(const char *str1, const char *str2, size_t len)
0125 {
0126 return (str1 && str2) ? strncmp(str1, str2, len)
0127 : (str1 ? 1 : (str2 ? -1 : 0));
0128 }
0129 Q_CORE_EXPORT int qstricmp(const char *, const char *);
0130 Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
0131 Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);
0132
0133
0134 Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
0135 Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
0136
0137
0138 Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
0139
0140 QT_END_NAMESPACE
0141
0142 #endif