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 <thiago.macieira@intel.com>
This commit is contained in:
Mikhail Svetkin 2018-08-28 15:56:09 +02:00
parent fde0adc3c7
commit 6b4e603c62
2 changed files with 21 additions and 12 deletions

View File

@ -62,26 +62,23 @@ namespace QtPrivate {
template<int N, int O, int I, int ... Idx> template<int N, int O, int I, int ... Idx>
struct OffsetSequenceHelper : OffsetSequenceHelper<N - 1, O + I, Idx..., O> { }; struct OffsetSequenceHelper : OffsetSequenceHelper<N - 1, O + I, Idx..., O> { };
template<int O, int I, int ... Idx> template<int Last, int I, int S, int ... Idx>
struct OffsetSequenceHelper<0, O, I, Idx...> : IndexesList<O, Idx...> struct OffsetSequenceHelper<1, Last, I, S, Idx...> : IndexesList<Last + I, Idx..., Last>
{ {
static const constexpr auto Length = O; static const constexpr auto Length = Last + I;
};
template<int I, int ... Idx>
struct OffsetSequence : OffsetSequenceHelper<sizeof ... (Idx) + 1, 0, I, Idx..., 0>
{
static const constexpr auto Count = sizeof ... (Idx) + 1;
using Type = typename QConditional< using Type = typename QConditional<
Count <= std::numeric_limits<quint8>::max() + 1, Last <= std::numeric_limits<quint8>::max(),
quint8, quint8,
typename QConditional< typename QConditional<
Count <= std::numeric_limits<quint16>::max() + 1, Last <= std::numeric_limits<quint16>::max(),
quint16, quint16,
int>::Type int>::Type
>::Type; >::Type;
}; };
template<int ... Idx>
struct OffsetSequence : OffsetSequenceHelper<sizeof ... (Idx), 0, Idx..., 0> { };
template<int N> template<int N>
struct StaticString struct StaticString
{ {
@ -168,7 +165,7 @@ public:
} }
constexpr inline const char *str() const { return m_string; } 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; }; constexpr inline int count() const { return SizeOffsets; };
static constexpr const auto sizeString = SizeString; static constexpr const auto sizeString = SizeString;

View File

@ -81,6 +81,13 @@ constexpr const auto messages257 = qOffsetStringArray(
"", "", "", "", "", "", "end" "", "", "", "", "", "", "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() void tst_QOffsetStringArray::init()
{ {
static_assert(messages.sizeString == 51, "message.sizeString"); static_assert(messages.sizeString == 51, "message.sizeString");
@ -91,6 +98,11 @@ void tst_QOffsetStringArray::init()
static_assert(messages257.sizeString == 260, "messages257.sizeString"); static_assert(messages257.sizeString == 260, "messages257.sizeString");
static_assert(std::is_same<decltype(messages257)::Type, quint16>::value, static_assert(std::is_same<decltype(messages257)::Type, quint16>::value,
"messages257::Type != quint16"); "messages257::Type != quint16");
static_assert(messagesBigOffsets.sizeOffsets == 4, "messagesBigOffsets.sizeOffsets");
static_assert(messagesBigOffsets.sizeString == 364, "messagesBigOffsets.sizeString");
static_assert(std::is_same<decltype(messagesBigOffsets)::Type, quint16>::value,
"messagesBigOffsets::Type != quint16");
} }
void tst_QOffsetStringArray::access() void tst_QOffsetStringArray::access()