Short live q20::copy{,_n,_if}!

All they do is add constexpr.

Use them in QOffsetStringArray where they replace a custom
implementation, except, as usual, the custom implementation does one
tiny thing more, so not all uses can be replaced.

Explicitly not use it in QStaticByteArrayMatcher to not cause
conflicts with the introduction of QStaticLatin1StringMatcher.

Pick-to: 6.4
Change-Id: I3c102a7e934a1d7d5fae4cbc629a4fabf2c47c92
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2022-10-07 11:31:55 +02:00
parent e17372e723
commit d6250e2a0d
2 changed files with 46 additions and 3 deletions

View File

@ -27,11 +27,53 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace q20 { namespace q20 {
// like std::is_sorted{,_until} (ie. constexpr) // like std::is_sorted{,_until}, std::copy (ie. constexpr)
#ifdef __cpp_lib_constexpr_algorithms #ifdef __cpp_lib_constexpr_algorithms
using std::copy;
using std::copy_if;
using std::copy_n;
using std::is_sorted_until; using std::is_sorted_until;
using std::is_sorted; using std::is_sorted;
#else #else
template <typename InputIterator, typename OutputIterator>
constexpr OutputIterator
copy(InputIterator first, InputIterator last, OutputIterator dest)
{
while (first != last) {
*dest = *first;
++first;
++dest;
}
return dest;
}
template <typename InputIterator, typename OutputIterator, typename UnaryPredicate>
constexpr OutputIterator
copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred)
{
while (first != last) {
if (pred(*first)) {
*dest = *first;
++dest;
}
++first;
}
return dest;
}
template <typename InputIterator, typename Size, typename OutputIterator>
constexpr OutputIterator
copy_n(InputIterator first, Size n, OutputIterator dest)
{
while (n > Size{0}) {
*dest = *first;
++first;
++dest;
--n;
}
return dest;
}
template <typename ForwardIterator, typename BinaryPredicate = std::less<>> template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
constexpr ForwardIterator constexpr ForwardIterator
is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {}) is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})

View File

@ -20,6 +20,7 @@
#include <QByteArrayView> #include <QByteArrayView>
#include <QtCore/q20algorithm.h>
#include <array> #include <array>
#include <limits> #include <limits>
#include <string_view> #include <string_view>
@ -100,7 +101,7 @@ constexpr auto makeStaticString(Extractor extract, const T &... entries)
const char *strings[] = { extract(entries).operator const char *()... }; const char *strings[] = { extract(entries).operator const char *()... };
size_t lengths[] = { sizeof(extract(T{}))... }; size_t lengths[] = { sizeof(extract(T{}))... };
for (size_t i = 0; i < std::size(strings); ++i) { for (size_t i = 0; i < std::size(strings); ++i) {
copyData(strings[i], lengths[i], result.begin() + offset); q20::copy_n(strings[i], lengths[i], result.begin() + offset);
offset += lengths[i]; offset += lengths[i];
} }
return result; return result;
@ -110,7 +111,7 @@ template <size_t N> struct StaticString
{ {
char value[N] = {}; char value[N] = {};
constexpr StaticString() = default; constexpr StaticString() = default;
constexpr StaticString(const char (&s)[N]) { copyData(s, N, value); } constexpr StaticString(const char (&s)[N]) { q20::copy_n(s, N, value); }
constexpr operator const char *() const { return value; } constexpr operator const char *() const { return value; }
}; };