Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qoffsetstringarray_p.h
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// Copyright (C) 2021 Intel Corporation.
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
// Qt-Security score:significant reason:default
5
6
#
ifndef
QOFFSETSTRINGARRAY_P_H
7
#
define
QOFFSETSTRINGARRAY_P_H
8
9
//
10
// W A R N I N G
11
// -------------
12
//
13
// This file is not part of the Qt API. It exists purely as an
14
// implementation detail. This header file may change from version to
15
// version without notice, or even be removed.
16
//
17
// We mean it.
18
//
19
20
#
include
"private/qglobal_p.h"
21
22
#
include
<
QByteArrayView
>
23
24
#
include
<
QtCore
/
q20algorithm
.
h
>
25
#
include
<
array
>
26
#
include
<
limits
>
27
#
include
<
string_view
>
28
29
class
tst_QOffsetStringArray;
30
31
QT_BEGIN_NAMESPACE
32
33
QT_WARNING_PUSH
34
#
if
defined
(
Q_CC_GNU_ONLY
)
&&
Q_CC_GNU
>=
1100
35
// we usually don't overread, but GCC has a false positive
36
QT_WARNING_DISABLE_GCC(
"-Wstringop-overread"
)
37
#
endif
38
39
template
<
typename
StaticString,
typename
OffsetList,
int
LongestElement>
40
class
QOffsetStringArray
41
{
42
static
auto
viewType_helper()
43
{
44
// returning std::type_identity here to avoid having to #include
45
if
constexpr
(
sizeof
(Char) == 2) {
46
return
q20::type_identity<QStringView>();
47
#
ifdef
__cpp_char8_t
48
}
else
if
constexpr
(std::is_same_v<Char, char8_t>) {
49
return
q20::type_identity<QUtf8StringView>();
50
#
endif
51
}
else
{
52
return
q20::type_identity<QByteArrayView>();
53
}
54
}
55
56
public
:
57
using
Char =
typename
StaticString::value_type;
58
using
View =
typename
decltype
(viewType_helper())::type;
59
static
constexpr
typename
OffsetList::value_type MaxElementLength = LongestElement;
60
61
constexpr
QOffsetStringArray(
const
StaticString &string,
const
OffsetList &offsets)
62
: m_string(string), m_offsets(offsets)
63
{}
64
65
constexpr
const
Char *operator[](
const
int
index)
const
noexcept
66
{
67
return
m_string.data() + m_offsets[qBound(
int
(0), index, count())];
68
}
69
70
constexpr
const
Char *at(
const
int
index)
const
noexcept
71
{
72
return
m_string.data() + m_offsets[index];
73
}
74
75
constexpr
View viewAt(qsizetype index)
const
noexcept
76
{
77
return
{ m_string.data() + m_offsets[index],
78
qsizetype(m_offsets[index + 1]) - qsizetype(m_offsets[index]) - 1 };
79
}
80
81
constexpr
int
count()
const
{
return
int
(m_offsets.size()) - 1; }
82
83
bool
contains(View needle, Qt::CaseSensitivity cs = Qt::CaseSensitive)
const
noexcept
84
{
85
for
(qsizetype i = 0; i < count(); ++i) {
86
if
(viewAt(i).compare(needle, cs) == 0)
87
return
true
;
88
}
89
return
false
;
90
}
91
92
private
:
93
StaticString m_string;
94
OffsetList m_offsets;
95
friend
tst_QOffsetStringArray;
96
};
97
98
namespace
QtPrivate
{
99
template
<size_t Highest>
constexpr
auto
minifyValue
()
100
{
101
constexpr
size_t max8 = (std::numeric_limits<quint8>::max)();
102
constexpr
size_t max16 = (std::numeric_limits<quint16>::max)();
103
if
constexpr
(Highest <= max8) {
104
return
quint8(Highest);
105
}
else
if
constexpr
(Highest <= max16) {
106
return
quint16(Highest);
107
}
else
{
108
// int is probably enough for everyone
109
return
int
(Highest);
110
}
111
}
112
113
template
<
typename
Char,
int
... Nx>
114
constexpr
auto
makeOffsetStringArray
(
const
Char (&...entries)[Nx])
115
{
116
constexpr
int
LongestElement =
std
::max({ Nx... }) - 1;
117
constexpr
size_t StringLength = (Nx + ...);
118
using
OffsetType =
decltype
(QtPrivate::minifyValue<StringLength>());
119
120
// prepend the first offset (zero) pointing to the *start* of the first element
121
size_t offset = 0;
122
std
::array offsetList = {
123
OffsetType(0),
124
OffsetType(offset += Nx)...
125
};
126
127
// append an extra null terminator
128
std
::array<Char, StringLength + 1> staticString = {};
129
const
Char *strings[] = { entries... };
130
for
(size_t i = 0; i <
std
::size(strings); ++i) {
131
size_t length = offsetList[i + 1] - offsetList[i];
132
q20::copy_n(strings[i], length, staticString.begin() + offsetList[i]);
133
}
134
135
using
StaticString =
decltype
(staticString);
136
using
OffsetList =
decltype
(offsetList);
137
using
R = QOffsetStringArray<StaticString, OffsetList, LongestElement>;
138
return
R(staticString, offsetList);
139
}
140
}
// namespace QtPrivate
141
142
template
<
typename
Char,
int
... Nx>
143
#
ifdef
__cpp_concepts
144
requires
std
::
is_same_v
<
Char
,
char
> ||
std
::
is_same_v
<
Char
,
char16_t
>
145
#
ifdef
__cpp_char8_t
146
||
std
::
is_same_v
<
Char
,
char8_t
>
147
#
endif
148
#
endif
149
constexpr
auto
qOffsetStringArray
(
const
Char (&...strings)[Nx])
noexcept
150
{
151
return
QtPrivate
::makeOffsetStringArray<Char>(strings...);
152
}
153
154
QT_WARNING_POP
155
QT_END_NAMESPACE
156
157
#
endif
// QOFFSETSTRINGARRAY_P_H
QSqlRelationalDelegate
\inmodule QtSql
QtPrivate
Definition
qalloc.h:29
QtPrivate::minifyValue
constexpr auto minifyValue()
Definition
qoffsetstringarray_p.h:99
QtPrivate::makeOffsetStringArray
constexpr auto makeOffsetStringArray(const Char(&...entries)[Nx])
Definition
qoffsetstringarray_p.h:114
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qOffsetStringArray
constexpr auto qOffsetStringArray(const Char(&...strings)[Nx]) noexcept
Definition
qoffsetstringarray_p.h:149
qtbase
src
corelib
tools
qoffsetstringarray_p.h
Generated on
for Qt by
1.18.0