diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2020-09-16 12:04:31 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2020-09-18 23:34:52 +0200 |
commit | 107ff4c1d6b5da2cb11c65b2bd9106817f7fdb02 (patch) | |
tree | e0c79bac217c10fd6d25fa9c4155f2003f6636df /src/corelib/text/qanystringview.h | |
parent | bbe7570ddcc6fcce707a355b76d2d0024c44ea38 (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/qanystringview.h')
-rw-r--r-- | src/corelib/text/qanystringview.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index 61761c6a82..242efc6e5c 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -109,9 +109,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)); } static QChar toQChar(char ch) noexcept { return toQChar(QLatin1Char{ch}); } // we don't handle UTF-8 multibytes @@ -180,6 +182,10 @@ public: constexpr QAnyStringView(QBasicUtf8StringView<UseChar8T> v) noexcept : QAnyStringView(std::data(v), lengthHelperContainer(v)) {} + template <typename Char, size_t Size, if_compatible_char<Char> = true> + [[nodiscard]] constexpr static QAnyStringView fromArray(const Char (&string)[Size]) noexcept + { return QAnyStringView(string, Size); } + // defined in qstring.h: template <typename Visitor> inline constexpr decltype(auto) visit(Visitor &&v) const; |