diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-10-07 16:50:42 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-10-11 21:17:17 +0000 |
commit | fd2685c2f0a219c091e028a98ba6cdd154986fec (patch) | |
tree | a376fcb4b978420f11cb4c66c532566a8005fd52 | |
parent | e1d21fe813c4c9c3f53e59c08190f80b38e385da (diff) |
Short live q20::fill{,_n}!
It just adds constexpr to it (we're ignoring the range version).
Apply it to QStaticByteArrayMatcher, where it replaces rather
lengthy initialization code.
Pick-to: 6.4
Change-Id: I1d60216fb04c94fa66fce5cc01313b3e9ba856ac
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
-rw-r--r-- | src/corelib/global/q20algorithm.h | 24 | ||||
-rw-r--r-- | src/corelib/text/qbytearraymatcher.h | 29 |
2 files changed, 28 insertions, 25 deletions
diff --git a/src/corelib/global/q20algorithm.h b/src/corelib/global/q20algorithm.h index 1d2e0117f4..69dc2d2446 100644 --- a/src/corelib/global/q20algorithm.h +++ b/src/corelib/global/q20algorithm.h @@ -32,6 +32,8 @@ namespace q20 { using std::copy; using std::copy_if; using std::copy_n; +using std::fill; +using std::fill_n; using std::is_sorted_until; using std::is_sorted; using std::transform; @@ -75,6 +77,28 @@ copy_n(InputIterator first, Size n, OutputIterator dest) return dest; } +template <typename ForwardIterator, typename Value> +constexpr void +fill(ForwardIterator first, ForwardIterator last, const Value &value) +{ + while (first != last) { + *first = value; + ++first; + } +} + +template <typename OutputIterator, typename Size, typename Value> +constexpr OutputIterator +fill_n(OutputIterator first, Size n, const Value &value) +{ + while (n > Size{0}) { + *first = value; + ++first; + --n; + } + return first; +} + template <typename ForwardIterator, typename BinaryPredicate = std::less<>> constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {}) diff --git a/src/corelib/text/qbytearraymatcher.h b/src/corelib/text/qbytearraymatcher.h index 1ac4356e0a..1de9c23f10 100644 --- a/src/corelib/text/qbytearraymatcher.h +++ b/src/corelib/text/qbytearraymatcher.h @@ -6,6 +6,8 @@ #include <QtCore/qbytearray.h> +#include <QtCore/q20algorithm.h> +#include <iterator> #include <limits> QT_BEGIN_NAMESPACE @@ -83,31 +85,8 @@ private: { const auto uchar_max = (std::numeric_limits<uchar>::max)(); uchar max = n > uchar_max ? uchar_max : uchar(n); - Skiptable table = { - // this verbose initialization code aims to avoid some opaque error messages - // even on powerful compilers such as GCC 5.3. Even though for GCC a loop - // format can be found that v5.3 groks, it's probably better to go with this - // for the time being: - { - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, - } - }; + Skiptable table = {}; + q20::fill(std::begin(table.data), std::end(table.data), max); pattern += n - max; while (max--) table.data[uchar(*pattern++)] = max; |