summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qoffsetstringarray_p.h
blob: 0f2ee2cc9968eebb79e867ed5bd26d9846bfe7ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (C) 2020 The Qt Company Ltd.
// Copyright (C) 2021 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QOFFSETSTRINGARRAY_P_H
#define QOFFSETSTRINGARRAY_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "private/qglobal_p.h"

#include <QByteArrayView>

#include <QtCore/q20algorithm.h>
#include <array>
#include <limits>
#include <string_view>
#include <tuple>

#ifdef __cpp_concepts
#  include <concepts>
#endif

class tst_QOffsetStringArray;

QT_BEGIN_NAMESPACE

QT_WARNING_PUSH
#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 1100
// we usually don't overread, but GCC has a false positive
QT_WARNING_DISABLE_GCC("-Wstringop-overread")
#endif


template <typename StaticString, typename OffsetList>
class QOffsetStringArray
{
    static auto viewType_helper()
    {
        // returning std::type_identity here to avoid having to #include
        if constexpr (sizeof(Char) == 2) {
            return q20::type_identity<QStringView>();
#ifdef __cpp_char8_t
        } else if constexpr (std::is_same_v<Char, char8_t>) {
            return q20::type_identity<QUtf8StringView>();
#endif
        } else {
            return q20::type_identity<QByteArrayView>();
        }
    }

public:
    using Char = typename StaticString::value_type;
    using View = typename decltype(viewType_helper())::type;

    constexpr QOffsetStringArray(const StaticString &string, const OffsetList &offsets)
        : m_string(string), m_offsets(offsets)
    {}

    constexpr const Char *operator[](const int index) const noexcept
    {
        return m_string.data() + m_offsets[qBound(int(0), index, count())];
    }

    constexpr const Char *at(const int index) const noexcept
    {
        return m_string.data() + m_offsets[index];
    }

    constexpr View viewAt(qsizetype index) const noexcept
    {
        return { m_string.data() + m_offsets[index],
                    qsizetype(m_offsets[index + 1]) - qsizetype(m_offsets[index]) - 1 };
    }

    constexpr int count() const { return int(m_offsets.size()) - 1; }

    bool contains(View needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
    {
        for (qsizetype i = 0; i < count(); ++i) {
            if (viewAt(i).compare(needle, cs) == 0)
                return true;
        }
        return false;
    }

private:
    StaticString m_string;
    OffsetList m_offsets;
    friend tst_QOffsetStringArray;
};

namespace QtPrivate {
template <size_t Highest> constexpr auto minifyValue()
{
    constexpr size_t max8 = (std::numeric_limits<quint8>::max)();
    constexpr size_t max16 = (std::numeric_limits<quint16>::max)();
    if constexpr (Highest <= max8) {
        return quint8(Highest);
    } else if constexpr (Highest <= max16) {
        return quint16(Highest);
    } else {
        // int is probably enough for everyone
        return int(Highest);
    }
}

template <typename Char, int... Nx>
constexpr auto makeOffsetStringArray(const Char (&...entries)[Nx])
{
    constexpr size_t StringLength = (Nx + ...);
    using OffsetType = decltype(QtPrivate::minifyValue<StringLength>());

    // prepend the first offset (zero) pointing to the *start* of the first element
    size_t offset = 0;
    std::array offsetList = {
        OffsetType(0),
        OffsetType(offset += Nx)...
    };

    // append an extra null terminator
    std::array<Char, StringLength + 1> staticString = {};
    const Char *strings[] = { entries... };
    for (size_t i = 0; i < std::size(strings); ++i) {
        size_t length = offsetList[i + 1] - offsetList[i];
        q20::copy_n(strings[i], length, staticString.begin() + offsetList[i]);
    }

    return QOffsetStringArray(staticString, offsetList);
}
} // namespace QtPrivate

template<typename Char, int ... Nx>
#ifdef __cpp_concepts
requires std::is_same_v<Char, char> || std::is_same_v<Char, char16_t>
#  ifdef __cpp_char8_t
    || std::is_same_v<Char, char8_t>
#  endif
#endif
constexpr auto qOffsetStringArray(const Char (&...strings)[Nx]) noexcept
{
    return QtPrivate::makeOffsetStringArray<Char>(strings...);
}

QT_WARNING_POP
QT_END_NAMESPACE

#endif // QOFFSETSTRINGARRAY_P_H