4#ifndef QANYSTRINGVIEW_H
5#define QANYSTRINGVIEW_H
7#include <QtCore/qlatin1stringview.h>
8#include <QtCore/qstringview.h>
9#include <QtCore/qutf8stringview.h>
11#ifdef __cpp_impl_three_way_comparison
14#include <QtCore/q20type_traits.h>
17class tst_QAnyStringView;
23template <
typename Tag,
typename Result>
26template <
typename Tag,
typename Result>
37 static constexpr size_t SizeMask = (std::numeric_limits<size_t>::max)() / 4;
38#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
39 static constexpr int SizeShift = 2;
40 static constexpr size_t Latin1Flag = 1;
42 static constexpr int SizeShift = 0;
43 static constexpr size_t Latin1Flag = SizeMask + 1;
45 static constexpr size_t TwoByteCodePointFlag = Latin1Flag << 1;
46 static constexpr size_t TypeMask = ~(SizeMask << SizeShift);
47 static_assert(TypeMask == (Latin1Flag|TwoByteCodePointFlag));
59 Utf16 = TwoByteCodePointFlag,
63 template <
typename Char>
64 using if_compatible_char = std::enable_if_t<std::disjunction_v<
69 template <
typename Po
inter>
70 using if_compatible_pointer = std::enable_if_t<std::disjunction_v<
77 using if_compatible_container = std::enable_if_t<std::disjunction_v<
82 template <
typename QStringOrQByteArray,
typename T>
83 using if_convertible_to = std::enable_if_t<std::conjunction_v<
85 std::negation<std::disjunction<
86 std::is_same<q20::remove_cvref_t<T>, QAnyStringView::Tag>,
88 std::is_pointer<std::decay_t<T>>,
89 std::is_same<q20::remove_cvref_t<T>,
QByteArray>,
90 std::is_same<q20::remove_cvref_t<T>,
QString>
93 std::is_convertible<T, QStringOrQByteArray>
100 template<
typename Char>
101 static constexpr bool isAsciiOnlyCharsAtCompileTime(
Char *
str,
qsizetype sz)
noexcept
104#if !defined(QT_SUPPORTS_IS_CONSTANT_EVALUATED)
109 if (!qIsConstantEvaluated())
111 if constexpr (
sizeof(
Char) !=
sizeof(
char)) {
125 template<
typename Char>
126 static constexpr std::size_t encodeType(
const Char *
str,
qsizetype sz)
noexcept
132 return (std::size_t(sz) << SizeShift)
133 |
uint(
sizeof(
Char) ==
sizeof(
char16_t)) * Tag::Utf16
134 |
uint(isAsciiOnlyCharsAtCompileTime(
str, sz)) * Tag::Latin1;
137 template <
typename Char>
140#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
141 if (qIsConstantEvaluated())
144 if constexpr (
sizeof(
Char) ==
sizeof(
char16_t))
147 return qsizetype(strlen(
reinterpret_cast<const char*
>(
str)));
158 :
m_data{
nullptr}, m_size{0} {}
162 template <
typename Char, if_compatible_
char<Char> = true>
168 template <
typename Char, if_compatible_
char<Char> = true>
173 template <
typename Char,
size_t N>
176 template <
typename Char>
180 template <
typename Po
inter, if_compatible_po
inter<Po
inter> = true>
190 template <
typename Container, if_compatible_container<Container> = true>
194 template <
typename Container, if_convertible_to<QString, Container> = true>
199 template <
typename Container, if_convertible_to<QByteArray, Container> = true>
204 template <
typename Char, if_compatible_
char<Char> = true>
211 std::enable_if_t<std::is_same_v<Char, char32_t>,
bool> =
true>
218 template <
bool UseChar8T>
222 template <
typename Char,
size_t Size, if_compatible_
char<Char> = true>
227 template <
typename Visitor>
228 inline constexpr decltype(
auto)
visit(Visitor &&
v)
const;
240 if (
size_t(
n) >=
size_t(
size()))
247 if (
size_t(
n) >=
size_t(
size()))
253 { verify(
pos);
auto r = *
this;
r.advanceData(
pos);
r.setSize(
size() -
pos);
return r; }
255 { verify(
pos,
n);
auto r = *
this;
r.advanceData(
pos);
r.setSize(
n);
return r; }
264 { verify(
n); setSize(
n); }
266 { verify(
n); setSize(
size() -
n); }
272 {
return qsizetype((m_size >> SizeShift) & SizeMask); }
273 [[nodiscard]]
constexpr const void *
data() const noexcept {
return m_data; }
282 [[nodiscard]]
constexpr QChar back()
const;
283 [[nodiscard]]
constexpr bool empty() const noexcept {
return size() == 0; }
285 {
return size() * charSize(); }
290 [[nodiscard]]
constexpr bool isNull() const noexcept {
return !
m_data; }
291 [[nodiscard]]
constexpr bool isEmpty() const noexcept {
return empty(); }
301#if defined(__cpp_impl_three_way_comparison) && !defined(Q_QDOC)
315#ifndef QT_NO_DEBUG_STREAM
319 [[nodiscard]]
constexpr Tag tag() const noexcept {
return Tag{m_size & TypeMask}; }
320 [[nodiscard]]
constexpr bool isUtf16() const noexcept {
return tag() == Tag::Utf16; }
321 [[nodiscard]]
constexpr bool isUtf8() const noexcept {
return tag() == Tag::Utf8; }
322 [[nodiscard]]
constexpr bool isLatin1() const noexcept {
return tag() == Tag::Latin1; }
323 [[nodiscard]]
constexpr QStringView asStringView()
const
325 [[nodiscard]]
constexpr q_no_char8_t::QUtf8StringView asUtf8StringView()
const
328 [[nodiscard]]
constexpr size_t charSize() const noexcept {
return isUtf16() ? 2 : 1; }
329 constexpr void setSize(
qsizetype sz)
noexcept { m_size = size_t(sz) | tag(); }
330 constexpr void advanceData(
qsizetype delta)
noexcept
345 friend class ::tst_QAnyStringView;
349template <
typename QStringLike, std::enable_if_t<std::disjunction_v<
350 std::is_same<QStringLike, QString>,
351 std::is_same<QStringLike, QByteArray>
constexpr QAnyStringView(const QChar &c) noexcept
constexpr QAnyStringView(std::nullptr_t) noexcept
Constructs a null string view.
constexpr QAnyStringView() noexcept
Constructs a null string view.
constexpr QChar back() const
Returns the last character in the string view.
constexpr bool isNull() const noexcept
Returns whether this string view is null - that is, whether {data() == nullptr}.
constexpr QAnyStringView(const Char *str, qsizetype len)
Constructs a string view on str with length len.
friend bool operator>=(QAnyStringView lhs, QAnyStringView rhs) noexcept
constexpr qsizetype size_bytes() const noexcept
Returns the size of this string view, but in bytes, not code-points.
qsizetype size_type
Alias for qsizetype.
friend bool operator<=(QAnyStringView lhs, QAnyStringView rhs) noexcept
constexpr qsizetype size() const noexcept
Returns the size of this string view, in the encoding's code points.
constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const
constexpr QAnyStringView sliced(qsizetype pos) const
QString toString() const
Returns a deep copy of this string view's data as a QString.
friend bool operator==(QAnyStringView lhs, QAnyStringView rhs) noexcept
friend bool operator!=(QAnyStringView lhs, QAnyStringView rhs) noexcept
static Q_CORE_EXPORT int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Returns an integer that compares to zero as lhs compares to rhs.
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
static constexpr QAnyStringView fromArray(const Char(&string)[Size]) noexcept
constexpr QAnyStringView right(qsizetype n) const
constexpr QAnyStringView(const Char &c) noexcept
static Q_CORE_EXPORT bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept
constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t< Container, QByteArray > &&capacity={})
Q_CORE_EXPORT friend QDebug operator<<(QDebug d, QAnyStringView s)
constexpr qsizetype length() const noexcept
Same as size().
constexpr QAnyStringView last(qsizetype n) const
constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t< Container, QString > &&capacity={})
const char16_t * m_data_utf16
qptrdiff difference_type
Alias for {std::ptrdiff_t}.
friend bool operator>(QAnyStringView lhs, QAnyStringView rhs) noexcept
Operators that compare lhs to rhs.
constexpr QChar front() const
Returns the first character in the string view.
constexpr QAnyStringView left(qsizetype n) const
constexpr QAnyStringView(QStringView v) noexcept
constexpr QAnyStringView first(qsizetype n) const
constexpr QAnyStringView mid(qsizetype pos, qsizetype n=-1) const
constexpr QAnyStringView chopped(qsizetype n) const
constexpr void chop(qsizetype n)
constexpr const void * data() const noexcept
Returns a const pointer to the first character in the string view.
constexpr void truncate(qsizetype n)
constexpr QAnyStringView(Char c, Container &&capacity={})
friend bool operator<(QAnyStringView lhs, QAnyStringView rhs) noexcept
constexpr QAnyStringView(const Char *f, const Char *l)
Constructs a string view on first with length (last - first).
constexpr Q_ALWAYS_INLINE QAnyStringView(const Container &c) noexcept
constexpr QAnyStringView(const Pointer &str) noexcept
constexpr QAnyStringView(QBasicUtf8StringView< UseChar8T > v) noexcept
constexpr bool empty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
constexpr decltype(auto) visit(Visitor &&v) const
Calls v with either a QUtf8StringView, QLatin1String, or QStringView, depending on the encoding of th...
static constexpr auto fromUcs4(char32_t c) noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Combined button and popup list for selecting options.
constexpr Q_ALWAYS_INLINE std::enable_if_t< sizeof(Char)==sizeof(char16_t), qsizetype > lengthHelperContainer(const Char(&str)[N])
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept
IsCompatibleChar8TypeHelper< q20::remove_cvref_t< Char > > IsCompatibleChar8Type
typename wrapped< Tag, Result >::type wrapped_t
QAnyStringView qToAnyStringViewIgnoringNull(const QStringLike &s) noexcept
GLsizei const GLfloat * v
[13]
GLenum GLuint GLintptr GLsizeiptr size
[1]
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
\inmodule QtCore \reentrant