Qt 6.x
The Qt SDK
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
4#ifndef QANYSTRINGVIEWUTILS_P_H
5#define QANYSTRINGVIEWUTILS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16
17#include <QtCore/private/qjson_p.h>
18
19#include <QtCore/qanystringview.h>
20#include <QtCore/qcbormap.h>
21#include <QtCore/qcborvalue.h>
22
24
26
28{
30 if (!container)
31 return QAnyStringView();
32
34 const auto &e = container->elements.at(n);
35 const auto data = container->byteData(e);
36 if (!data)
37 return QAnyStringView();
39 return data->asStringView();
41 return data->asLatin1();
42 return data->asUtf8StringView();
43}
44
46{
47 return toStringView(map[key]);
48}
49
50inline bool endsWith(QAnyStringView whole, QAnyStringView part)
51{
52 return whole.length() >= part.length() && whole.last(part.length()) == part;
53}
54
56{
57 return whole.length() >= part.length() && whole.first(part.length()) == part;
58}
59
60inline bool doesContain(QStringView whole, QLatin1Char part) { return whole.contains(part); }
61inline bool doesContain(QLatin1StringView whole, QLatin1Char part) { return whole.contains(part); }
62inline bool doesContain(QUtf8StringView whole, QLatin1Char part)
63{
64 return QByteArrayView(whole.data(), whole.size()).contains(part.toLatin1());
65}
66inline bool contains(QAnyStringView whole, QLatin1Char part)
67{
68 return whole.visit([&](auto view) { return doesContain(view, part); });
69}
70
72{
73 return QByteArrayView(view.data(), view.length()).toInt();
74}
75inline int toInt(QLatin1StringView view) { return view.toInt(); }
76inline int toInt(QStringView view) { return view.toInt(); }
77
78inline int toInt(QAnyStringView string)
79{
80 return string.visit([](auto view) { return toInt(view); });
81}
82
83template<typename StringView>
84QAnyStringView doTrimmed(StringView string)
85{
86 if constexpr (std::is_same_v<StringView, QStringView>)
87 return string.trimmed();
88 if constexpr (std::is_same_v<StringView, QLatin1StringView>)
89 return string.trimmed();
90 if constexpr (std::is_same_v<StringView, QUtf8StringView>)
91 return QByteArrayView(string.data(), string.length()).trimmed();
92}
93
94
96{
97 return string.visit([](auto data) {
98 return doTrimmed(data);
99 });
100}
101
102template<typename StringView, typename Handler>
103auto processAsUtf8(StringView string, Handler &&handler)
104{
105 if constexpr (std::is_same_v<StringView, QStringView>)
106 return handler(QByteArrayView(string.toUtf8()));
107 if constexpr (std::is_same_v<StringView, QLatin1StringView>)
108 return handler(QByteArrayView(string.data(), string.length()));
109 if constexpr (std::is_same_v<StringView, QUtf8StringView>)
110 return handler(QByteArrayView(string.data(), string.length()));
111 if constexpr (std::is_same_v<StringView, QByteArrayView>)
112 return handler(string);
113 if constexpr (std::is_same_v<StringView, QByteArray>)
114 return handler(QByteArrayView(string));
115 if constexpr (std::is_same_v<StringView, QAnyStringView>) {
116
117 // Handler is:
118 // * a reference if an lvalue ref is passed
119 // * a value otherwise
120 // We conserve its nature for passing to the lambda below.
121 // This is necessary because we need to decide on the nature of
122 // the lambda capture as part of the syntax (prefix '&' or not).
123 // So we always pass a reference-conserving wrapper as value.
124 struct Wrapper { Handler handler; };
125
126 return string.visit([w = Wrapper { std::forward<Handler>(handler) }](auto view) mutable {
127 static_assert(!(std::is_same_v<decltype(view), QAnyStringView>));
128 return processAsUtf8(std::move(view), std::forward<Handler>(w.handler));
129 });
130 }
131 Q_UNREACHABLE();
132}
133
135{
137 if (source.isEmpty()) {
139 return list;
140 }
141
142 qsizetype start = 0;
144
145 for (qsizetype current = 0; current < end; ++current) {
146 if (source.mid(current, sep.length()) == sep) {
147 list.append(source.mid(start, current - start));
148 start = current + sep.length();
149 }
150 }
151
152 if (start < end)
153 list.append(source.mid(start, end - start));
154
155 return list;
156}
157
158}
159
161
162#endif // QANYSTRINGVIEWUTILS_P_H
\inmodule QtCore
constexpr qsizetype length() const noexcept
Same as size().
constexpr QAnyStringView last(qsizetype n) const
constexpr QAnyStringView first(qsizetype n) const
constexpr decltype(auto) visit(Visitor &&v) const
Calls v with either a QUtf8StringView, QLatin1String, or QStringView, depending on the encoding of th...
constexpr qsizetype size() const noexcept
const_pointer data() const noexcept
bool contains(QByteArrayView a) const noexcept
int toInt(bool *ok=nullptr, int base=10) const
const QtCbor::ByteData * byteData(QtCbor::Element e) const
QList< QtCbor::Element > elements
\inmodule QtCore\reentrant
Definition qcbormap.h:21
\inmodule QtCore\reentrant
Definition qcborvalue.h:50
static qint64 valueHelper(const QCborValue &v)
Definition qjson_p.h:174
static QCborContainerPrivate * container(const QCborValue &v)
Definition qjson_p.h:175
bool contains(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qlist.h:74
qsizetype length() const noexcept
Definition qlist.h:388
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
void append(parameter_type t)
Definition qlist.h:441
\inmodule QtCore
Definition qstringview.h:76
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QMap< QString, QString > map
[6]
double e
bool startsWith(QAnyStringView whole, QAnyStringView part)
QAnyStringView toStringView(const QCborValue &value)
bool contains(QAnyStringView whole, QLatin1Char part)
QList< QAnyStringView > split(QAnyStringView source, QAnyStringView sep)
bool endsWith(QAnyStringView whole, QAnyStringView part)
QAnyStringView trimmed(QAnyStringView string)
int toInt(QUtf8StringView view)
QAnyStringView doTrimmed(StringView string)
auto processAsUtf8(StringView string, Handler &&handler)
bool doesContain(QStringView whole, QLatin1Char part)
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint64 key
GLfloat GLfloat GLfloat w
[0]
GLuint GLuint end
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
GLfloat n
GLsizei GLsizei GLchar * source
static constexpr QChar sep
ptrdiff_t qsizetype
Definition qtypes.h:70
long long qint64
Definition qtypes.h:55
QList< int > list
[14]
QQuickView * view
[0]
\inmodule QtCore \reentrant
Definition qchar.h:17
constexpr char toLatin1() const noexcept
Converts a Latin-1 character to an 8-bit ASCII representation of the character.
Definition qchar.h:20