Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:critical reason:data-parser
4
5#ifndef QBYTEARRAYALGORITHMS_H
6#define QBYTEARRAYALGORITHMS_H
7
8#include <QtCore/qnamespace.h>
9
10#include <string.h>
11#include <stdarg.h>
12
13#if 0
14#pragma qt_class(QByteArrayAlgorithms)
15#endif
16
17QT_BEGIN_NAMESPACE
18
19class QByteArrayView;
20
21namespace QtPrivate {
22
23[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
24bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
25
26[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
27bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
28
29[[nodiscard]] inline // defined in qbytearrayview.h
30qsizetype findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept;
31
32[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
34
35[[nodiscard]] inline // defined in qbytearrayview.h
36qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept;
37
38[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
40
41[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
43
44[[nodiscard]] Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs);
45
46[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept;
47
48[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept;
49
50template <typename T>
52{
53 T m_value;
54 quint32 m_error : 1;
55 quint32 m_reserved : 31;
56 void *m_reserved2 = nullptr;
57public:
58 constexpr ParsedNumber() noexcept : m_value(), m_error(true), m_reserved(0) {}
59 constexpr explicit ParsedNumber(T v) : m_value(v), m_error(false), m_reserved(0) {}
60
61 // minimal optional-like API:
62 explicit operator bool() const noexcept { return !m_error; }
63 T &operator*() { Q_ASSERT(*this); return m_value; }
64 const T &operator*() const { Q_ASSERT(*this); return m_value; }
65 T *operator->() noexcept { return *this ? &m_value : nullptr; }
66 const T *operator->() const noexcept { return *this ? &m_value : nullptr; }
67 template <typename U> // not = T, as that'd allow calls that are incompatible with std::optional
68 T value_or(U &&u) const { return *this ? m_value : T(std::forward<U>(u)); }
69};
70
71[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<double> toDouble(QByteArrayView a) noexcept;
72[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<float> toFloat(QByteArrayView a) noexcept;
73[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qlonglong> toSignedInteger(QByteArrayView data, int base);
74[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qulonglong> toUnsignedInteger(QByteArrayView data, int base);
75
76// QByteArrayView has incomplete type here, and we can't include qbytearrayview.h,
77// since it includes qbytearrayalgorithms.h. Use the ByteArrayView template type as
78// a workaround.
79template <typename T, typename ByteArrayView,
81static inline T toIntegral(ByteArrayView data, bool *ok, int base)
82{
83 const auto val = [&] {
84 if constexpr (std::is_unsigned_v<T>)
86 else
87 return toSignedInteger(data, base);
88 }();
89 const bool failed = !val || T(*val) != *val;
90 if (ok)
91 *ok = !failed;
92 if (failed)
93 return 0;
94 return T(*val);
95}
96
97[[nodiscard]] constexpr inline bool isAsciiUpper(char32_t c) noexcept
98{
99 return c >= 'A' && c <= 'Z';
100}
101
102[[nodiscard]] constexpr inline char toAsciiLower(char ch) noexcept
103{
104 return isAsciiUpper(ch) ? ch - 'A' + 'a' : ch;
105}
106
107[[nodiscard]] constexpr inline int caseCompareAscii(char lhs, char rhs) noexcept
108{
109 const char lhsLower = toAsciiLower(lhs);
110 const char rhsLower = toAsciiLower(rhs);
111 return int(uchar(lhsLower)) - int(uchar(rhsLower));
112}
113
114constexpr
115int qstrnicmp_impl(const char *s1, const char *s2, size_t len)
116{
117 if (!s1 || !s2)
118 return s1 ? 1 : (s2 ? -1 : 0);
119 for (; len--; ++s1, ++s2) {
120 const char c = *s1;
121 if (int res = QtPrivate::caseCompareAscii(c, *s2))
122 return res;
123 if (!c) // strings are equal
124 break;
125 }
126 return 0;
127}
128
129constexpr
130int qstrnicmp_impl(const char *s1, qsizetype len1, const char *s2, qsizetype len2)
131{
132 Q_PRE(len1 >= 0);
133 Q_PRE(len2 >= -1);
134 Q_PRE(s1 || len1 == 0);
135 Q_PRE(s2 || len2 == 0 || len2 == -1);
136 if (!s1 || !len1) {
137 if (len2 == 0)
138 return 0;
139 if (len2 == -1)
140 return (!s2 || !*s2) ? 0 : -1;
141 return -1;
142 }
143 if (!s2)
144 return len1 == 0 ? 0 : 1;
145
146 if (len2 == -1) {
147 // null-terminated s2
148 qsizetype i = 0;
149 for (i = 0; i < len1; ++i) {
150 const char c = s2[i];
151 if (!c)
152 return 1;
153
154 if (int res = QtPrivate::caseCompareAscii(s1[i], c))
155 return res;
156 }
157 return s2[i] ? -1 : 0;
158 } else {
159 // not null-terminated
160 const qsizetype len = qMin(len1, len2);
161 for (qsizetype i = 0; i < len; ++i) {
162 if (int res = QtPrivate::caseCompareAscii(s1[i], s2[i]))
163 return res;
164 }
165 if (len1 == len2)
166 return 0;
167 return len1 < len2 ? -1 : 1;
168 }
169}
170} // namespace QtPrivate
171
172/*****************************************************************************
173 Safe and portable C string functions; extensions to standard string.h
174 *****************************************************************************/
175
176[[nodiscard]] Q_DECL_PURE_FUNCTION Q_CORE_EXPORT
177const void *qmemrchr(const void *s, int needle, size_t n) noexcept;
178Q_CORE_EXPORT char *qstrdup(const char *);
179
180inline size_t qstrlen(const char *str)
181{
182 QT_WARNING_PUSH
183#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 900 && Q_CC_GNU < 1000
184 // spurious compiler warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91490#c6)
185 // when Q_DECLARE_METATYPE_TEMPLATE_1ARG is used
186 QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
187#endif
188 return str ? strlen(str) : 0;
189 QT_WARNING_POP
190}
191
192inline size_t qstrnlen(const char *str, size_t maxlen)
193{
194 if (!str)
195 return 0;
196 auto end = static_cast<const char *>(memchr(str, '\0', maxlen));
197 return end ? end - str : maxlen;
198}
199
200// implemented in qbytearray.cpp
201Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
202Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
203
204Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
205
206inline int qstrncmp(const char *str1, const char *str2, size_t len)
207{
208 return (str1 && str2) ? strncmp(str1, str2, len)
209 : (str1 ? 1 : (str2 ? -1 : 0));
210}
211Q_CORE_EXPORT int qstricmp(const char *, const char *);
212
213
214#if !QT_CORE_INLINE_IMPL_SINCE(6, 12)
215QT6_ONLY(Q_CORE_EXPORT)
216#endif
217// These are unfortunately NOT constexpr in static builds (e.g. iOS)
218QT_CORE_CONSTEXPR_INLINE_SINCE(6, 12)
219int qstrnicmp(const char *s1, const char *s2, size_t len);
220
221#if !QT_CORE_INLINE_IMPL_SINCE(6, 12)
222QT6_ONLY(Q_CORE_EXPORT)
223#endif
224QT_CORE_CONSTEXPR_INLINE_SINCE(6, 12)
225int qstrnicmp(const char *s1, qsizetype len1, const char *s2, qsizetype len2 = -1);
226
227#if QT_CORE_INLINE_IMPL_SINCE(6, 12)
228QT_CORE_CONSTEXPR_INLINE_SINCE(6, 12)
229int qstrnicmp(const char *s1, const char *s2, size_t len)
230{
231 // ### Qt7: inline the function here
232 return QtPrivate::qstrnicmp_impl(s1, s2, len);
233}
234
235QT_CORE_CONSTEXPR_INLINE_SINCE(6, 12)
236int qstrnicmp(const char *s1, qsizetype len1, const char *s2, qsizetype len2)
237{
238 // ### Qt7: inline the function here
239 return QtPrivate::qstrnicmp_impl(s1, len1, s2, len2);
240}
241#endif // INLINE_SINCE(6, 12)
242
243#ifndef QT_NO_QSNPRINTF // use std::(v)snprintf() from <cstdio> instead
244#if QT_DEPRECATED_SINCE(6, 9)
245#define QSNPF_DEPR(vsn)
246 QT_DEPRECATED_VERSION_X_6_9("Use C++11 std::" #vsn "printf() instead, taking care to "
247 "ensure that you didn't rely on QString::asprintf() "
248 "idiosyncrasies that q" #vsn "printf might, but "
249 "std::" #vsn "printf() does not, support.")
250// implemented in qvsnprintf.cpp
251QSNPF_DEPR(vsn)
252Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
253 Q_ATTRIBUTE_FORMAT_PRINTF(3, 0);
254QSNPF_DEPR(sn)
255Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...)
256 Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
257#undef QSNPF_DEPR
258#endif // QT_DEPRECATED_SINCE(6, 9)
259#endif // QT_NO_QSNPRINTF
260
261// qChecksum: Internet checksum
262Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
263
264QT_END_NAMESPACE
265
266#endif // QBYTEARRAYALGORITHMS_H
constexpr ParsedNumber() noexcept
const T * operator->() const 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)
constexpr char toAsciiLower(char ch) noexcept
qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept
qsizetype findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept
constexpr int qstrnicmp_impl(const char *s1, const char *s2, size_t len)
constexpr int qstrnicmp_impl(const char *s1, qsizetype len1, const char *s2, qsizetype len2)
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
constexpr int caseCompareAscii(char lhs, char rhs) noexcept
constexpr bool isAsciiUpper(char32_t c) 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
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)
int qstrnicmp(const char *s1, qsizetype len1, const char *s2, qsizetype len2=-1)
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 char * qstrcpy(char *dst, const char *src)
int qstrnicmp(const char *s1, const char *s2, size_t len)
Q_DECL_PURE_FUNCTION Q_CORE_EXPORT const void * qmemrchr(const void *s, int needle, size_t n) noexcept
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)