Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbytearrayalgorithms.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBYTEARRAYALGORITHMS_H
5#define QBYTEARRAYALGORITHMS_H
6
7#include <QtCore/qnamespace.h>
8
9#include <string.h>
10#include <stdarg.h>
11
12#if 0
13#pragma qt_class(QByteArrayAlgorithms)
14#endif
15
17
18class QByteArrayView;
19
20namespace QtPrivate {
21
22[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
23bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
24
25[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
26bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
27
28[[nodiscard]] inline
29qsizetype findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept;
30
31[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
32qsizetype findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
33
34[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
35qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, char needle) noexcept;
36
37[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
38qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
39
40[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
41qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept;
42
43[[nodiscard]] Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs);
44
45[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept;
46
47[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept;
48
49template <typename T>
51{
52 T m_value;
53 quint32 m_error : 1;
54 quint32 m_reserved : 31;
55 void *m_reserved2 = nullptr;
56public:
57 constexpr ParsedNumber() noexcept : m_value(), m_error(true), m_reserved(0) {}
58 constexpr explicit ParsedNumber(T v) : m_value(v), m_error(false), m_reserved(0) {}
59
60 // minimal optional-like API:
61 explicit operator bool() const noexcept { return !m_error; }
62 T &operator*() { Q_ASSERT(*this); return m_value; }
63 const T &operator*() const { Q_ASSERT(*this); return m_value; }
64 T *operator->() noexcept { return *this ? &m_value : nullptr; }
65 const T *operator->() const noexcept { return *this ? &m_value : nullptr; }
66 template <typename U> // not = T, as that'd allow calls that are incompatible with std::optional
67 T value_or(U &&u) const { return *this ? m_value : T(std::forward<U>(u)); }
68};
69
70[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<double> toDouble(QByteArrayView a) noexcept;
71[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<float> toFloat(QByteArrayView a) noexcept;
72[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qlonglong> toSignedInteger(QByteArrayView data, int base);
73[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qulonglong> toUnsignedInteger(QByteArrayView data, int base);
74
75// QByteArrayView has incomplete type here, and we can't include qbytearrayview.h,
76// since it includes qbytearrayalgorithms.h. Use the ByteArrayView template type as
77// a workaround.
78template <typename T, typename ByteArrayView,
79 typename = std::enable_if_t<std::is_same_v<ByteArrayView, QByteArrayView>>>
80static inline T toIntegral(ByteArrayView data, bool *ok, int base)
81{
82 const auto val = [&] {
83 if constexpr (std::is_unsigned_v<T>)
85 else
86 return toSignedInteger(data, base);
87 }();
88 const bool failed = !val || T(*val) != *val;
89 if (ok)
90 *ok = !failed;
91 if (failed)
92 return 0;
93 return T(*val);
94}
95
96} // namespace QtPrivate
97
98/*****************************************************************************
99 Safe and portable C string functions; extensions to standard string.h
100 *****************************************************************************/
101
102Q_CORE_EXPORT char *qstrdup(const char *);
103
104inline size_t qstrlen(const char *str)
105{
107#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 900 && Q_CC_GNU < 1000
108 // spurious compiler warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91490#c6)
109 // when Q_DECLARE_METATYPE_TEMPLATE_1ARG is used
110 QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
111#endif
112 return str ? strlen(str) : 0;
114}
115
116inline size_t qstrnlen(const char *str, size_t maxlen)
117{
118 if (!str)
119 return 0;
120 auto end = static_cast<const char *>(memchr(str, '\0', maxlen));
121 return end ? end - str : maxlen;
122}
123
124// implemented in qbytearray.cpp
125Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
126Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
127
128Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
129
130inline int qstrncmp(const char *str1, const char *str2, size_t len)
131{
132 return (str1 && str2) ? strncmp(str1, str2, len)
133 : (str1 ? 1 : (str2 ? -1 : 0));
134}
135Q_CORE_EXPORT int qstricmp(const char *, const char *);
136Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
137Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);
138
139// implemented in qvsnprintf.cpp
140Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
141Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
142
143// qChecksum: Internet checksum
145
147
148#endif // QBYTEARRAYALGORITHMS_H
constexpr ParsedNumber() noexcept
const T * operator->() const noexcept
QString str
[2]
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, char needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber< qulonglong > toUnsignedInteger(QByteArrayView data, int base)
qsizetype findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber< qlonglong > toSignedInteger(QByteArrayView data, int base)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept
Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber< float > toFloat(QByteArrayView a) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber< double > toDouble(QByteArrayView a) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept
static T toIntegral(ByteArrayView data, bool *ok, int base)
ChecksumType
@ ChecksumIso3309
size_t qstrlen(const char *str)
int qstrncmp(const char *str1, const char *str2, size_t len)
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, size_t len)
Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len)
Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt,...)
size_t qstrnlen(const char *str, size_t maxlen)
Q_CORE_EXPORT int qstricmp(const char *, const char *)
Q_CORE_EXPORT char * qstrdup(const char *)
Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard=Qt::ChecksumIso3309)
Q_CORE_EXPORT char * qstrcpy(char *dst, const char *src)
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
#define Q_DECL_PURE_FUNCTION
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_GCC(text)
#define QT_WARNING_PUSH
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum src
GLenum GLenum dst
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLfloat * val
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int quint32
Definition qtypes.h:50
unsigned short quint16
Definition qtypes.h:48
ptrdiff_t qsizetype
Definition qtypes.h:165
static const uint base
Definition qurlidna.cpp:20
QVideoFrameFormat::PixelFormat fmt