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
qanystringviewutils_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
5#ifndef QANYSTRINGVIEWUTILS_P_H
6#define QANYSTRINGVIEWUTILS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17
18#include <QtCore/private/qjson_p.h>
19
20#include <QtCore/qanystringview.h>
21
23
25
26// Note: This only works if part is US-ASCII, but there is no type to encode this information!
27inline bool endsWith(QAnyStringView whole, QLatin1StringView part)
28{
29 Q_ASSERT(QtPrivate::isAscii(part));
30 return whole.length() >= part.length() && whole.last(part.length()) == part;
31}
32
33// Note: This only works if part is US-ASCII, but there is no type to encode this information!
34inline bool startsWith(QAnyStringView whole, QLatin1StringView part)
35{
36 Q_ASSERT(QtPrivate::isAscii(part));
37 return whole.length() >= part.length() && whole.first(part.length()) == part;
38}
39
40inline bool doesContain(QStringView whole, QLatin1Char part) { return whole.contains(part); }
41inline bool doesContain(QLatin1StringView whole, QLatin1Char part) { return whole.contains(part); }
42inline bool doesContain(QUtf8StringView whole, QLatin1Char part)
43{
44 return QByteArrayView(whole.data(), whole.size()).contains(part.toLatin1());
45}
46inline bool contains(QAnyStringView whole, QLatin1Char part)
47{
48 return whole.visit([&](auto view) { return doesContain(view, part); });
49}
50
51inline qsizetype getLastIndexOf(QStringView whole, QLatin1StringView part)
52{
53 return whole.lastIndexOf(part);
54}
55inline qsizetype getLastIndexOf(QLatin1StringView whole, QLatin1StringView part)
56{
57 return whole.lastIndexOf(part);
58}
59inline qsizetype getLastIndexOf(QUtf8StringView whole, QLatin1StringView part)
60{
61 return QByteArrayView(whole.data(), whole.size()).lastIndexOf(part);
62}
63inline qsizetype lastIndexOf(QAnyStringView whole, QLatin1StringView part)
64{
65 Q_ASSERT(QtPrivate::isAscii(part));
66 return whole.visit([&](auto view) { return getLastIndexOf(view, part); });
67}
68
69inline int toInt(QUtf8StringView view)
70{
71 return QByteArrayView(view.data(), view.length()).toInt();
72}
73inline int toInt(QLatin1StringView view) { return view.toInt(); }
74inline int toInt(QStringView view) { return view.toInt(); }
75
76inline int toInt(QAnyStringView string)
77{
78 return string.visit([](auto view) { return toInt(view); });
79}
80
81template<typename StringView>
82QAnyStringView doTrimmed(StringView string)
83{
84 if constexpr (std::is_same_v<StringView, QStringView>)
85 return string.trimmed();
86 if constexpr (std::is_same_v<StringView, QLatin1StringView>)
87 return string.trimmed();
88 if constexpr (std::is_same_v<StringView, QUtf8StringView>)
89 return QByteArrayView(string.data(), string.length()).trimmed();
90}
91
92
93inline QAnyStringView trimmed(QAnyStringView string)
94{
95 return string.visit([](auto data) {
96 return doTrimmed(data);
97 });
98}
99
100template<typename StringView, typename Handler>
101auto processAsUtf8(StringView string, Handler &&handler)
102{
103 if constexpr (std::is_same_v<StringView, QStringView>)
104 return handler(QByteArrayView(string.toUtf8()));
105 if constexpr (std::is_same_v<StringView, QLatin1StringView>)
106 return handler(QByteArrayView(string.data(), string.length()));
107 if constexpr (std::is_same_v<StringView, QUtf8StringView>)
108 return handler(QByteArrayView(string.data(), string.length()));
109 if constexpr (std::is_same_v<StringView, QByteArrayView>)
110 return handler(string);
111 if constexpr (std::is_same_v<StringView, QByteArray>)
112 return handler(QByteArrayView(string));
113 if constexpr (std::is_same_v<StringView, QAnyStringView>) {
114
115 // Handler is:
116 // * a reference if an lvalue ref is passed
117 // * a value otherwise
118 // We conserve its nature for passing to the lambda below.
119 // This is necessary because we need to decide on the nature of
120 // the lambda capture as part of the syntax (prefix '&' or not).
121 // So we always pass a reference-conserving wrapper as value.
122 struct Wrapper { Handler handler; };
123
124 return string.visit([w = Wrapper { std::forward<Handler>(handler) }](auto view) mutable {
125 static_assert(!(std::is_same_v<decltype(view), QAnyStringView>));
126 return processAsUtf8(std::move(view), std::forward<Handler>(w.handler));
127 });
128 }
129 Q_UNREACHABLE();
130}
131
132// Note: This only works if sep is US-ASCII, but there is no type to encode this information!
133inline QList<QAnyStringView> split(QAnyStringView source, QLatin1StringView sep)
134{
135 Q_ASSERT(QtPrivate::isAscii(sep));
136
137 QList<QAnyStringView> list;
138 if (source.isEmpty()) {
139 list.append(source);
140 return list;
141 }
142
143 qsizetype start = 0;
144 qsizetype end = source.length();
145
146 for (qsizetype current = 0; current < end; ++current) {
147 if (source.mid(current, sep.length()) == sep) {
148 list.append(source.mid(start, current - start));
149 start = current + sep.length();
150 }
151 }
152
153 if (start < end)
154 list.append(source.mid(start, end - start));
155
156 return list;
157}
158
159}
160
161// Do not expose this outside of qmltyperegistrar specific code!
162// TODO: Remove this when QAnyStringView gets a proper qHash()
163constexpr inline size_t qHash(QAnyStringView string, size_t seed = 0)
164{
165 return string.visit([seed](auto view) {
166 if constexpr (std::is_same_v<decltype(view), QStringView>)
167 return qHash(view, seed);
168 if constexpr (std::is_same_v<decltype(view), QLatin1StringView>)
169 return qHash(view, seed);
170 if constexpr (std::is_same_v<decltype(view), QUtf8StringView>)
171 return qHash(QByteArrayView(view.data(), view.length()), seed);
172 });
173}
174
175QT_END_NAMESPACE
176
177#endif // QANYSTRINGVIEWUTILS_P_H
Access
Definition access.h:11
const Method::Container & constructors() const
const Enum::Container & enums() const
QAnyStringView qualifiedClassName() const
const Method::Container & sigs() const
const Method::Container & methods() const
const ClassInfo::Container & classInfos() const
friend bool operator!=(const MetaType &a, const MetaType &b) noexcept
const BaseType::Container & superClasses() const
MetaType()=default
QAnyStringView className() const
QString inputFile() const
const Interface::Container & ifaces() const
QAnyStringView metaObjectHash() const
const Property::Container & properties() const
MetaType(const QCborMap &cbor, const QString &inputFile, const QCborMap &hashes)
friend bool operator==(const MetaType &a, const MetaType &b) noexcept
QHash< QAnyStringView, QAnyStringView > foreignTypeMetaObjectHashses() const
bool processForeignTypes(const QString &foreignTypesFile)
static QList< QAnyStringView > namespaces(const MetaType &classDef)
QList< QString > includes() const
bool processTypes(const QStringList &files)
QList< UsingDeclaration > usingDeclarations() const
QList< MetaType > types() const
QList< QAnyStringView > referencedTypes() const
QList< MetaType > foreignTypes() const
MetaTypesJsonProcessor(bool privateIncludes)
qsizetype lastIndexOf(QAnyStringView whole, QLatin1StringView part)
bool contains(QAnyStringView whole, QLatin1Char part)
QAnyStringView trimmed(QAnyStringView string)
int toInt(QUtf8StringView view)
QAnyStringView doTrimmed(StringView string)
auto processAsUtf8(StringView string, Handler &&handler)
bool endsWith(QAnyStringView whole, QLatin1StringView part)
qsizetype getLastIndexOf(QStringView whole, QLatin1StringView part)
bool doesContain(QStringView whole, QLatin1Char part)
bool startsWith(QAnyStringView whole, QLatin1StringView part)
QList< QAnyStringView > split(QAnyStringView source, QLatin1StringView sep)
Combined button and popup list for selecting options.
size_t qHash(QByteArrayView key, size_t seed) noexcept
Definition qhash.cpp:876
static Access getAccess(const QCborMap &cbor)
static QCborValue fromJson(const QByteArray &json, QJsonParseError *error)
static QLatin1StringView typeRelationString(TypeRelation relation)
static void sortStringList(QList< String > *list)
static QTypeRevision getRevision(const QCborMap &cbor)
static bool qualifiedClassNameLessThan(const MetaType &a, const MetaType &b)
@ Public
Definition access.h:11
@ Private
Definition access.h:11
@ Protected
Definition access.h:11
QDebug warning(const MetaType &classDef)
QAnyStringView name
Argument(const QCborMap &cbor)
Argument()=default
QAnyStringView type
BaseType()=default
QAnyStringView name
BaseType(const QCborMap &cbor)
ClassInfo()=default
QAnyStringView value
QAnyStringView name
ClassInfo(const QCborMap &cbor)
QAnyStringView alias
QAnyStringView name
QList< QAnyStringView > values
Enum(const QCborMap &cbor)
Enum()=default
QAnyStringView type
Interface(const QCborValue &cbor)
QAnyStringView className
Interface()=default
MetaTypePrivate()=default
ClassInfo::Container classInfos
BaseType::Container superClasses
MetaTypePrivate(const QCborMap &cbor, const QString &inputFile, const QCborMap &hashes)
Property::Container properties
Interface::Container ifaces
QAnyStringView qualifiedClassName
Method::Container constructors
QAnyStringView name
Method()=default
QAnyStringView returnType
Argument::Container arguments
Method(const QCborMap &cbor, bool isConstructor)
QTypeRevision revision
static constexpr int InvalidIndex
QAnyStringView write
QAnyStringView notify
QAnyStringView read
QAnyStringView privateClass
Property(const QCborMap &cbor)
QAnyStringView name
QAnyStringView member
QAnyStringView type
Property()=default
QAnyStringView reset
QAnyStringView bindable
QTypeRevision revision
friend bool comparesEqual(const UsingDeclaration &a, const UsingDeclaration &b) noexcept
friend Qt::strong_ordering compareThreeWay(const UsingDeclaration &a, const UsingDeclaration &b) noexcept