diff --git a/src/corelib/tools/qoffsetstringarray_p.h b/src/corelib/tools/qoffsetstringarray_p.h new file mode 100644 index 0000000000..a1117b1d47 --- /dev/null +++ b/src/corelib/tools/qoffsetstringarray_p.h @@ -0,0 +1,211 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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 +#include + +QT_BEGIN_NAMESPACE + +namespace QtPrivate { +template +struct OffsetSequenceHelper : OffsetSequenceHelper { }; + +template +struct OffsetSequenceHelper<0, O, I, Idx...> : IndexesList +{ + static const constexpr auto Length = O; +}; + +template +struct OffsetSequence : OffsetSequenceHelper +{ + static const constexpr auto Count = sizeof ... (Idx) + 1; + using Type = typename QConditional< + Count <= std::numeric_limits::max() + 1, + quint8, + typename QConditional< + Count <= std::numeric_limits::max() + 1, + quint16, + int>::Type + >::Type; +}; + +template +struct StaticString +{ + const char data[N]; + + constexpr StaticString(const char (&literal)[N]) noexcept + : StaticString(literal, makeIndexSequence {}) + { } + + template + constexpr StaticString(const char (&literal)[N], + IndexesList) noexcept + : data{literal[Idx]...} + { } + + template + constexpr StaticString(const T ... c) noexcept : data{c...} { } + + constexpr char operator[](int i) const noexcept + { + return data[i]; + } + + template + constexpr StaticString operator+(const StaticString &rs) const noexcept + { + return concatenate(rs, makeIndexSequence{}, makeIndexSequence{}); + } + + template + constexpr StaticString concatenate(const StaticString &rs, + IndexesList, + IndexesList) const noexcept + { + return StaticString(data[I1]..., rs[I2]...); + } + + constexpr int size() const noexcept + { + return N; + } +}; + +template<> +struct StaticString<0> +{ +}; + +template +constexpr StaticString<0> staticString() noexcept +{ + return StaticString<0>{}; +} + +template +constexpr StaticString staticString(const char (&s)[I], const char (&...sx)[Ix]) noexcept +{ + return StaticString(s) + staticString(sx...); +} +} // namespace QtPrivate + +template +class QOffsetStringArray +{ +public: + using Type = T; + + template + constexpr QOffsetStringArray(const QtPrivate::StaticString &str, + QtPrivate::IndexesList, + QtPrivate::IndexesList) noexcept + : m_string{str[Cx]...}, + m_offsets{Ox...} + { } + + constexpr inline const char *operator[](const int index) const noexcept + { + return m_string + m_offsets[qBound(int(0), index, SizeOffsets - 1)]; + } + + constexpr inline const char *at(const int index) const noexcept + { + return m_string + m_offsets[index]; + } + + constexpr inline const char *str() const { return m_string; } + constexpr inline const int *offsets() const { return m_offsets; } + constexpr inline int count() const { return SizeOffsets; }; + + static constexpr const auto sizeString = SizeString; + static constexpr const auto sizeOffsets = SizeOffsets; + +private: + const char m_string[SizeString]; + const T m_offsets[SizeOffsets]; +}; + +template +constexpr QOffsetStringArray qOffsetStringArray( + const QtPrivate::StaticString &string, + QtPrivate::IndexesList offsets) noexcept +{ + return QOffsetStringArray( + string, + QtPrivate::makeIndexSequence {}, + offsets); +} + +template +struct QOffsetStringArrayRet +{ + using Offsets = QtPrivate::OffsetSequence; + using Type = QOffsetStringArray; +}; + +template +constexpr auto qOffsetStringArray(const char (&...strings)[Nx]) noexcept -> typename QOffsetStringArrayRet::Type +{ + using Offsets = QtPrivate::OffsetSequence; + return qOffsetStringArray( + QtPrivate::staticString(strings...), Offsets{}); +} + + +QT_END_NAMESPACE + +#endif // QOFFSETSTRINGARRAY_P_H diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri index 9ec6ea4894..6b180602c2 100644 --- a/src/corelib/tools/tools.pri +++ b/src/corelib/tools/tools.pri @@ -39,6 +39,7 @@ HEADERS += \ tools/qmargins.h \ tools/qmessageauthenticationcode.h \ tools/qcontiguouscache.h \ + tools/qoffsetstringarray_p.h \ tools/qpair.h \ tools/qpoint.h \ tools/qqueue.h \ diff --git a/tests/auto/corelib/tools/qoffsetstringarray/qoffsetstringarray.pro b/tests/auto/corelib/tools/qoffsetstringarray/qoffsetstringarray.pro new file mode 100644 index 0000000000..c8e6a8e05a --- /dev/null +++ b/tests/auto/corelib/tools/qoffsetstringarray/qoffsetstringarray.pro @@ -0,0 +1,6 @@ +CONFIG += testcase +TARGET = tst_qoffsetstringarray +QT = core testlib core-private +CONFIG += c++11 +CONFIG += strict_c++ +SOURCES = $$PWD/tst_qoffsetstringarray.cpp diff --git a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp new file mode 100644 index 0000000000..9174308a9a --- /dev/null +++ b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include + + +class tst_QOffsetStringArray : public QObject +{ + Q_OBJECT + +private slots: + void init(); + void access(); +}; + + +constexpr const auto messages = qOffsetStringArray( + "level - 0", + "level - 1", + "level - 2", + "level - 3", + "level - 4", + "" +); + +constexpr const auto messages257 = qOffsetStringArray( + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "end" +); + +void tst_QOffsetStringArray::init() +{ + static_assert(messages.sizeString == 51, "message.sizeString"); + static_assert(messages.sizeOffsets == 6, "message.sizeOffsets"); + static_assert(std::is_same::value, "messages::Type != quint8"); + + static_assert(messages257.sizeOffsets == 257, "messages257.sizeOffsets"); + static_assert(messages257.sizeString == 260, "messages257.sizeString"); + static_assert(std::is_same::value, + "messages257::Type != quint16"); +} + +void tst_QOffsetStringArray::access() +{ + QCOMPARE(messages[0], "level - 0"); + QCOMPARE(messages[1], "level - 1"); + QCOMPARE(messages[2], "level - 2"); + QCOMPARE(messages[3], "level - 3"); + QCOMPARE(messages[4], "level - 4"); + QCOMPARE(messages[5], ""); + QCOMPARE(messages[6], ""); +} + + +QTEST_APPLESS_MAIN(tst_QOffsetStringArray) +#include "tst_qoffsetstringarray.moc" diff --git a/tests/auto/corelib/tools/tools.pro b/tests/auto/corelib/tools/tools.pro index f28cf21b8b..2a975e67d1 100644 --- a/tests/auto/corelib/tools/tools.pro +++ b/tests/auto/corelib/tools/tools.pro @@ -35,6 +35,7 @@ SUBDIRS=\ qmap_strictiterators \ qmargins \ qmessageauthenticationcode \ + qoffsetstringarray \ qpair \ qpoint \ qpointf \