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
16QT_BEGIN_NAMESPACE
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]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
29qsizetype findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
30
31[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
32qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
33
34[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
35qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept;
36
37[[nodiscard]] Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs);
38
39[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept;
40
41[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept;
42
43template <typename T>
44class ParsedNumber
45{
46 T m_value;
47 quint32 m_error : 1;
48 quint32 m_reserved : 31;
49 void *m_reserved2 = nullptr;
50public:
51 constexpr ParsedNumber() noexcept : m_value(), m_error(true), m_reserved(0) {}
52 constexpr explicit ParsedNumber(T v) : m_value(v), m_error(false), m_reserved(0) {}
53
54 // minimal optional-like API:
55 explicit operator bool() const noexcept { return !m_error; }
56 T &operator*() { Q_ASSERT(*this); return m_value; }
57 const T &operator*() const { Q_ASSERT(*this); return m_value; }
58 T *operator->() noexcept { return *this ? &m_value : nullptr; }
59 const T *operator->() const noexcept { return *this ? &m_value : nullptr; }
60 template <typename U> // not = T, as that'd allow calls that are incompatible with std::optional
61 T value_or(U &&u) const { return *this ? m_value : T(std::forward<U>(u)); }
62};
63
64[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<double> toDouble(QByteArrayView a) noexcept;
65[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<float> toFloat(QByteArrayView a) noexcept;
66[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qlonglong> toSignedInteger(QByteArrayView data, int base);
67[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qulonglong> toUnsignedInteger(QByteArrayView data, int base);
68
69// QByteArrayView has incomplete type here, and we can't include qbytearrayview.h,
70// since it includes qbytearrayalgorithms.h. Use the ByteArrayView template type as
71// a workaround.
72template <typename T, typename ByteArrayView,
73 typename = std::enable_if_t<std::is_same_v<ByteArrayView, QByteArrayView>>>
74static inline T toIntegral(ByteArrayView data, bool *ok, int base)
75{
76 const auto val = [&] {
77 if constexpr (std::is_unsigned_v<T>)
78 return toUnsignedInteger(data, base);
79 else
80 return toSignedInteger(data, base);
81 }();
82 const bool failed = !val || T(*val) != *val;
83 if (ok)
84 *ok = !failed;
85 if (failed)
86 return 0;
87 return T(*val);
88}
89
90} // namespace QtPrivate
91
92/*****************************************************************************
93 Safe and portable C string functions; extensions to standard string.h
94 *****************************************************************************/
95
96Q_CORE_EXPORT char *qstrdup(const char *);
97
98inline size_t qstrlen(const char *str)
99{
100 QT_WARNING_PUSH
101#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 900 && Q_CC_GNU < 1000
102 // spurious compiler warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91490#c6)
103 // when Q_DECLARE_METATYPE_TEMPLATE_1ARG is used
104 QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
105#endif
106 return str ? strlen(s: str) : 0;
107 QT_WARNING_POP
108}
109
110inline size_t qstrnlen(const char *str, size_t maxlen)
111{
112 if (!str)
113 return 0;
114 auto end = static_cast<const char *>(memchr(s: str, c: '\0', n: maxlen));
115 return end ? end - str : maxlen;
116}
117
118// implemented in qbytearray.cpp
119Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
120Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
121
122Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
123
124inline int qstrncmp(const char *str1, const char *str2, size_t len)
125{
126 return (str1 && str2) ? strncmp(s1: str1, s2: str2, n: len)
127 : (str1 ? 1 : (str2 ? -1 : 0));
128}
129Q_CORE_EXPORT int qstricmp(const char *, const char *);
130Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
131Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);
132
133// implemented in qvsnprintf.cpp
134Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
135Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
136
137// qChecksum: Internet checksum
138Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
139
140QT_END_NAMESPACE
141
142#endif // QBYTEARRAYALGORITHMS_H
143

source code of qtbase/src/corelib/text/qbytearrayalgorithms.h