From 6b4e603c62c71593cadb69fee4cf6e6f4555f7d7 Mon Sep 17 00:00:00 2001 From: Mikhail Svetkin Date: Tue, 28 Aug 2018 15:56:09 +0200 Subject: [PATCH] corelib/tools: Fix auto detection of QOffsetStringArray::m_offset type The previous implementation wrongly calculated the necessary data type to hold the offset indexes. It looked at the amount of elements, but instead we should look at value for the last element in the offset array Change-Id: I84c6985dc3c329df3bbc5a14f9789170877b65bb Reviewed-by: Thiago Macieira --- src/corelib/tools/qoffsetstringarray_p.h | 21 ++++++++----------- .../tst_qoffsetstringarray.cpp | 12 +++++++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/corelib/tools/qoffsetstringarray_p.h b/src/corelib/tools/qoffsetstringarray_p.h index a1117b1d478..ff195965ec4 100644 --- a/src/corelib/tools/qoffsetstringarray_p.h +++ b/src/corelib/tools/qoffsetstringarray_p.h @@ -62,26 +62,23 @@ namespace QtPrivate { template struct OffsetSequenceHelper : OffsetSequenceHelper { }; -template -struct OffsetSequenceHelper<0, O, I, Idx...> : IndexesList +template +struct OffsetSequenceHelper<1, Last, I, S, Idx...> : IndexesList { - static const constexpr auto Length = O; -}; - -template -struct OffsetSequence : OffsetSequenceHelper -{ - static const constexpr auto Count = sizeof ... (Idx) + 1; + static const constexpr auto Length = Last + I; using Type = typename QConditional< - Count <= std::numeric_limits::max() + 1, + Last <= std::numeric_limits::max(), quint8, typename QConditional< - Count <= std::numeric_limits::max() + 1, + Last <= std::numeric_limits::max(), quint16, int>::Type >::Type; }; +template +struct OffsetSequence : OffsetSequenceHelper { }; + template struct StaticString { @@ -168,7 +165,7 @@ public: } constexpr inline const char *str() const { return m_string; } - constexpr inline const int *offsets() const { return m_offsets; } + constexpr inline const T *offsets() const { return m_offsets; } constexpr inline int count() const { return SizeOffsets; }; static constexpr const auto sizeString = SizeString; diff --git a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp index 9174308a9a7..dfa0450b18c 100644 --- a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp +++ b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp @@ -81,6 +81,13 @@ constexpr const auto messages257 = qOffsetStringArray( "", "", "", "", "", "", "end" ); +constexpr const auto messagesBigOffsets = qOffsetStringArray( + " 10 20 30 40 50 60 70 80 90", + " 10 20 30 40 50 60 70 80 90", + " 10 20 30 40 50 60 70 80 90", + " 10 20 30 40 50 60 70 80 90" +); + void tst_QOffsetStringArray::init() { static_assert(messages.sizeString == 51, "message.sizeString"); @@ -91,6 +98,11 @@ void tst_QOffsetStringArray::init() static_assert(messages257.sizeString == 260, "messages257.sizeString"); static_assert(std::is_same::value, "messages257::Type != quint16"); + + static_assert(messagesBigOffsets.sizeOffsets == 4, "messagesBigOffsets.sizeOffsets"); + static_assert(messagesBigOffsets.sizeString == 364, "messagesBigOffsets.sizeString"); + static_assert(std::is_same::value, + "messagesBigOffsets::Type != quint16"); } void tst_QOffsetStringArray::access()