From 107ff4c1d6b5da2cb11c65b2bd9106817f7fdb02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 16 Sep 2020 12:04:31 +0200 Subject: Q(Any|Utf8)StringView: move array size deduction feature to fromArray The constructor taking an array literal will now stop at the first null-terminator encountered. And fromArray is introduced which only supports array literals. Constructs a view of the full size. Explicit so it shouldn't be surprising. Change-Id: I1497c33a5c12453a95e87c990abe6335b2817081 Reviewed-by: Andrei Golubev Reviewed-by: Lars Knoll --- src/corelib/text/qstringview.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/corelib/text/qstringview.h') diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index f88871ff5d..a4d0a78e5b 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -172,9 +172,11 @@ private: } template - static constexpr qsizetype lengthHelperContainer(const Char (&)[N]) noexcept + static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept { - return qsizetype(N - 1); + const auto it = std::char_traits::find(str, N, Char(0)); + const auto end = it ? it : std::next(str, N); + return qsizetype(std::distance(str, end)); } template @@ -223,6 +225,10 @@ public: constexpr QStringView(const Container &c) noexcept : QStringView(std::data(c), lengthHelperContainer(c)) {} + template = true> + [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept + { return QStringView(string, Size); } + Q_REQUIRED_RESULT inline QString toString() const; // defined in qstring.h #if defined(Q_OS_DARWIN) || defined(Q_QDOC) // defined in qcore_foundation.mm -- cgit v1.2.3