summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2020-09-16 12:04:31 +0200
committerMårten Nordheim <marten.nordheim@qt.io>2020-09-18 23:34:52 +0200
commit107ff4c1d6b5da2cb11c65b2bd9106817f7fdb02 (patch)
treee0c79bac217c10fd6d25fa9c4155f2003f6636df /src/corelib/text/qstringview.h
parentbbe7570ddcc6fcce707a355b76d2d0024c44ea38 (diff)
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 <andrei.golubev@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r--src/corelib/text/qstringview.h10
1 files changed, 8 insertions, 2 deletions
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 <typename Char, size_t N>
- 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<Char>::find(str, N, Char(0));
+ const auto end = it ? it : std::next(str, N);
+ return qsizetype(std::distance(str, end));
}
template <typename Char>
@@ -223,6 +225,10 @@ public:
constexpr QStringView(const Container &c) noexcept
: QStringView(std::data(c), lengthHelperContainer(c)) {}
+ template <typename Char, size_t Size, if_compatible_char<Char> = 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