QAnyStringView Class

QAnyStringView 类通过QString API 的只读子集为 Latin-1、UTF-8 或 UTF-16 字符串提供了统一的视图。更多

Header: #include <QAnyStringView>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Qt 6.0

该类具有很强的可比性

该类与 char16_t,QChar, const char16_t *, const char *,QByteArray,QByteArrayView,QString,QStringView,QUtf8StringView, 和QLatin1StringView强烈同类。

注意:该类中的所有函数都是可重入的

公共类型

公共函数

QAnyStringView()
QAnyStringView(const Char (&)[N] string)
QAnyStringView(const Char *str)
QAnyStringView(const Container &str)
QAnyStringView(const QByteArray &str)
QAnyStringView(const QString &str)
QAnyStringView(std::nullptr_t)
QAnyStringView(const Char *first, const Char *last)
QAnyStringView(const Char *str, qsizetype len)
(since 6.9) QString arg(Args &&... args) const
QChar back() const
(since 6.5) void chop(qsizetype n)
(since 6.5) QAnyStringView chopped(qsizetype n) const
const void *data() const
bool empty() const
(since 6.5) QAnyStringView first(qsizetype n) const
QChar front() const
bool isEmpty() const
bool isNull() const
(since 6.5) QAnyStringView last(qsizetype n) const
qsizetype length() const
(since 6.8) qsizetype max_size() const
qsizetype size() const
qsizetype size_bytes() const
(since 6.8) QAnyStringView &slice(qsizetype pos, qsizetype n)
(since 6.8) QAnyStringView &slice(qsizetype pos)
(since 6.5) QAnyStringView sliced(qsizetype pos) const
(since 6.5) QAnyStringView sliced(qsizetype pos, qsizetype n) const
QString toString() const
(since 6.5) void truncate(qsizetype n)
decltype(auto) visit(Visitor &&v) const

静态公共成员

int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QAnyStringView fromArray(const Char (&)[Size] string)
bool operator!=(const QAnyStringView &lhs, const QAnyStringView &rhs)
bool operator<(const QAnyStringView &lhs, const QAnyStringView &rhs)
(since 6.7) QDebug operator<<(QDebug d, QAnyStringView s)
bool operator<=(const QAnyStringView &lhs, const QAnyStringView &rhs)
bool operator==(const QAnyStringView &lhs, const QAnyStringView &rhs)
bool operator>(const QAnyStringView &lhs, const QAnyStringView &rhs)
bool operator>=(const QAnyStringView &lhs, const QAnyStringView &rhs)

详细说明

QAnyStringView 引用了它不拥有的字符串的连续部分。它是所有类型字符串的接口类型,无需首先构建QString

QStringViewQUtf8StringView 不同,QAnyStringView 可以保存以下任何编码的字符串:UTF-8、UTF-16 和 Latin-1。之所以支持后者,是因为 Latin-1 与 UTF-8 不同,可以有效地与 UTF-16 数据进行比较:长度不匹配已意味着字符串不可能相等。UTF-8/UTF-16比较则不然,因为UTF-8是一种可变长度编码。

字符串可表示为char,char8_t,QChar,ushort,char16_t 或(在 Windows 等平台上,字符串是 16 位类型)wchar_t 的数组(或数组兼容的数据结构,如QString, std::basic_string 等)。

QAnyStringView 被设计为接口类型;其主要用途是作为函数参数类型。当 QAnyStringView 被用作自动变量或数据成员时,必须注意确保所引用的字符串数据(例如,由QString 所拥有)在所有代码路径上都比 QAnyStringView 长,以免字符串视图最终引用了已删除的数据。

当作为接口类型使用时,QAnyStringView 允许一个函数接受多种字符串数据源。这样,一个接受 QAnyStringView 的函数就取代了五个函数重载(接受QString(const QChar*, qsizetype)QUtf8StringViewQLatin1StringView (但见上文)和QChar ),同时还能向函数传递更多的字符串数据源,如u8"Hello World" 、一个char8_t 字符串字面。

与 Qt XML 的其他地方一样,QAnyStringView 假定char 数据以 UTF-8 编码,除非它以QLatin1StringView 的形式呈现。

不过,自 Qt 6.4 起,纯 US-ASCII 的 UTF-8 字符串字面量将自动存储为 Latin-1。这是一种编译时检查,没有运行时开销。该功能要求使用 C++20 或最新的 GCC 进行编译。

QAnyStringView 应按值传递,而不是按引用-const 传递:

    void myfun1(QAnyStringView sv);        // preferred
    void myfun2(const QAnyStringView &sv); // compiles and works, but slower

QAnyStringView 也可以用作函数的返回值,但不建议这样做。QUtf8StringViewQStringView 更适合用作函数返回值。如果你调用一个返回 QAnyStringView 的函数,请格外小心,不要让 QAnyStringView 的存在时间超过函数承诺的保持引用字符串数据存活的时间。如果有疑问,可调用toString() 将 QAnyStringView 转换为QString ,从而获得对数据的强引用。

QAnyStringView 是一种字面类型

兼容字符类型

QAnyStringView 接受多种字符类型的字符串:

  • char (有符号和无符号)
  • char8_t (仅适用于 C++20)
  • char16_t
  • wchar_t (16位字符类型,如 Windows)
  • ushort
  • QChar

8 位字符类型被解释为 UTF-8 数据(以QLatin1StringView 形式显示时除外),而 16 位字符类型被解释为按主机字节顺序排列的 UTF-16 数据(与QString 相同)。

大小和子字符串

QAnyStringView 函数中的所有大小和位置都以编码的代码单位为单位(也就是说,在这些函数中,UTF-16 代理对被算作两个,与QString 中相同;UTF-8 多字节序列被算作两个、三个或四个,取决于它们的长度)。

另请参阅 QUtf8StringViewQStringView

成员类型文档

QAnyStringView::difference_type

std::ptrdiff_t 的别名。与 STL 兼容。

QAnyStringView::size_type

qsizetype 的别名。提供该别名是为了与 STL 兼容。

成员函数文档

[constexpr noexcept] QAnyStringView::QAnyStringView()

构造空字符串视图。

另请参阅 isNull()。

[constexpr noexcept] template <typename Char, size_t N> QAnyStringView::QAnyStringView(const Char (&)[N] string)

在字符串字面string 上构建字符串视图。视图覆盖数组,直到遇到第一个Char(0)N ,以先到者为准。如果需要完整的数组,请使用fromArray() 代替。

string 必须在该字符串视图对象的生命周期内保持有效。

限制条件

只有当string 是实际数组且Char 是兼容字符类型时,才参与重载解析。

另请参阅 Compatible Character Types

[constexpr noexcept] template <typename Char> QAnyStringView::QAnyStringView(const Char *str)

str 上构建字符串视图。长度通过扫描第一个Char(0) 来确定。

str 必须在该字符串视图对象的生命周期内保持有效。

nullptr 作为str 传递是安全的,会产生一个空字符串视图。

限制条件

只有当str 不是数组且Char 是兼容字符类型时,才会参与重载解析。

另请参阅 isNull() 和Compatible Character Types

[constexpr noexcept] template <typename Container, QAnyStringView::if_compatible_container<Container> = true> QAnyStringView::QAnyStringView(const Container &str)

str 上构建字符串视图。长度取自std::size(str)

std::data(str) 必须在该字符串视图对象的生命周期内保持有效。

当且仅当std::size(str) == 0 时,字符串视图将为空。未说明该构造函数是否会导致字符串视图为空(std::data(str) 必须返回nullptr )。

限制条件

只有当Container 是一个具有与value_type 兼容的字符类型的容器时,才会参与重载解析。

另请参阅 isNull() 和isEmpty()。

[noexcept] QAnyStringView::QAnyStringView(const QByteArray &str)

str 上构建字符串视图。str 中的数据被解释为 UTF-8。

str.data() 必须在该字符串视图对象的生命周期内保持有效。

只有当str.isNull() 时,字符串视图才会为空。

[noexcept] QAnyStringView::QAnyStringView(const QString &str)

str 上构造一个字符串视图。

str.data() 必须在该字符串视图对象的生命周期内保持有效。

当且仅当str.isNull().

[constexpr noexcept] QAnyStringView::QAnyStringView(std::nullptr_t)

构造空字符串视图。

另请参阅 isNull()。

[constexpr] template <typename Char, QAnyStringView::if_compatible_char<Char> = true> QAnyStringView::QAnyStringView(const Char *first, const Char *last)

first 上构建长度为 (last -first) 的字符串视图。

[first,last) 的范围必须在该字符串视图对象的生命周期内保持有效。

如果last 也是nullptr ,则以first 的形式传递nullptr 是安全的,但会导致字符串视图为空。

如果lastfirst 之前,或firstnullptrlast 不是,则行为未定义。

限制条件

只有当Char 是兼容字符类型时,才会参与重载解析。

另请参阅 isNull() 和Compatible Character Types

[constexpr] template <typename Char, QAnyStringView::if_compatible_char<Char> = true> QAnyStringView::QAnyStringView(const Char *str, qsizetype len)

str 上构建长度为len 的字符串视图。

[str,len) 的范围必须在该字符串视图对象的生命周期内保持有效。

如果len 也为 0,将nullptr 作为str 传递是安全的,但会导致字符串视图为空。

如果len 为负数,或str 为正数,则行为未定义nullptr

限制条件

只有当Char 是兼容字符类型时,才会参与重载解析。

另请参阅 isNull() 和Compatible Character Types

[since 6.9] template <typename... Args> QString QAnyStringView::arg(Args &&... args) const

args 中的相应参数替换该字符串中出现的%N 。参数不分位置:args 的第一个参数用最低的N (全部)替换%Nargs 的第二个参数用次低的N 替换%N ,等等。

Args 可以由隐式转换为 的任何内容组成。QAnyStringView

此函数在 Qt 6.9 中引入。

另请参阅 QString::arg(Args&&...)。

[constexpr] QChar QAnyStringView::back() const

返回字符串视图中的最后一个字符。

提供此函数是为了与 STL 兼容。

警告 在空字符串视图中调用此函数会导致未定义的行为。

另请参阅 front() 和Sizes and Sub-Strings

[constexpr, since 6.5] void QAnyStringView::chop(qsizetype n)

通过n 代码点截断该字符串视图。

*this = first(size() - n) 相同。

注意: n < 0 或n >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 sliced(),first(),last(),chopped(),truncate(),slice() 和Sizes and Sub-Strings

[constexpr, since 6.5] QAnyStringView QAnyStringView::chopped(qsizetype n) const

返回从此对象开始的长度为size() -n 的子字符串。

first(size() - n) 相同。

注意: n < 0 或n >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 sliced(),first(),last(),chop(),truncate(),slice(), 和Sizes and Sub-Strings

[static noexcept] int QAnyStringView::compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)

将字符串视图lhs 与字符串视图rhs 进行比较,如果lhs 小于rhs ,则返回一个负整数;如果大于rhs ,则返回一个正整数;如果两者相等,则返回 0。

如果csQt::CaseSensitive (默认值),则比较区分大小写;否则比较不区分大小写。

另请参阅 operator==()、operator<() 和operator>()。

[constexpr noexcept] const void *QAnyStringView::data() const

返回指向字符串视图中第一个字符的常量指针。

注意: 返回值所代表的字符数组不是空端。

另请参阅 size_bytes()。

[constexpr noexcept] bool QAnyStringView::empty() const

返回该字符串视图是否为空,即size() == 0 是否为空。

提供此函数是为了与 STL 兼容。

另请参阅 isEmpty()、isNull() 和size()。

[constexpr, since 6.5] QAnyStringView QAnyStringView::first(qsizetype n) const

返回一个字符串视图,其中包含该字符串视图的第一个n 代码点。

注意: n < 0 或n >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 last(),sliced(),chopped(),chop(),truncate(),slice() 和Sizes and Sub-Strings

[static constexpr noexcept] template <typename Char, size_t Size, QAnyStringView::if_compatible_char<Char> = true> QAnyStringView QAnyStringView::fromArray(const Char (&)[Size] string)

在完整字符串字面string 上构建字符串视图,包括任何尾部Char(0) 。如果不想在视图中包含 null 终结符,可以在确定它位于末尾时将其chop() 掉。另外,您也可以使用构造函数重载数组字面量,这样就可以创建一个视图,直到但不包括数据中的第一个空结束符。

string 必须在该字符串视图对象的生命周期内保持有效。

如果Char 是兼容字符类型,则该函数可用于任何数组字面量。兼容字符类型包括QChar,ushort,char16_t 以及(在诸如 Windows 等平台上为 16 位类型)wchar_t

[constexpr] QChar QAnyStringView::front() const

返回字符串视图中的第一个字符。

提供此函数是为了与 STL 兼容。

警告 在空字符串视图中调用此函数将导致未定义的行为。

另请参阅 back() 和Sizes and Sub-Strings

[constexpr noexcept] bool QAnyStringView::isEmpty() const

返回该字符串视图是否为空,即size() == 0 是否为空。

提供此函数是为了与其他 Qt 容器兼容。

另请参阅 empty()、isNull() 和size()。

[constexpr noexcept] bool QAnyStringView::isNull() const

返回该字符串视图是否为空,即data() == nullptr 是否为空。

提供此函数是为了与其他 Qt 容器兼容。

另请参阅 empty()、isEmpty() 和size()。

[constexpr, since 6.5] QAnyStringView QAnyStringView::last(qsizetype n) const

返回包含该字符串视图最后n 个代码点的字符串视图。

注意: n < 0 或n >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 first(),sliced(),chopped(),chop(),truncate(),slice(), 和Sizes and Sub-Strings

[constexpr noexcept] qsizetype QAnyStringView::length() const

size() 相同。

提供该函数是为了与其他 Qt 容器兼容。

另请参阅 size()。

[constexpr noexcept, since 6.8] qsizetype QAnyStringView::max_size() const

该函数与 STL 兼容。

它返回字符串视图理论上可表示的元素的最大数量。实际上,受系统可用内存量的限制,这个数字可能要小得多。

注意: 返回值是根据当前使用的字符类型计算的,因此在两个不同的视图上调用此函数可能会返回不同的结果。

此函数在 Qt 6.8 中引入。

[constexpr noexcept] qsizetype QAnyStringView::size() const

以编码的码位为单位,返回该字符串视图的大小。

另请参阅 empty(),isEmpty(),isNull(),size_bytes() 和Sizes and Sub-Strings

[constexpr noexcept] qsizetype QAnyStringView::size_bytes() const

返回该字符串视图的大小,但单位是字节,而不是代码点。

您可以将此函数与data() 一起用于散列或序列化。

提供此函数是为了与 STL 兼容。

另请参阅 size() 和data()。

[constexpr, since 6.8] QAnyStringView &QAnyStringView::slice(qsizetype pos, qsizetype n)

修改字符串视图,使其从pos 位置开始,并扩展至n 代码点。

注意: pos < 0、n < 0 或pos +n >size() 时,行为未定义。

此函数在 Qt 6.8 中引入。

另请参阅 sliced(),first(),last(),chopped(),chop(),truncate(), 和Sizes and Sub-Strings

[constexpr, since 6.8] QAnyStringView &QAnyStringView::slice(qsizetype pos)

这是一个重载函数。

修改字符串视图,使其从pos 位置开始,一直延伸到其末尾。

注意: pos < 0 或pos >size() 时,行为未定义。

此函数在 Qt 6.8 中引入。

另请参阅 sliced(),first(),last(),chopped(),chop(),truncate(), 和Sizes and Sub-Strings

[constexpr, since 6.5] QAnyStringView QAnyStringView::sliced(qsizetype pos) const

返回一个字符串视图,该视图从pos 开始,一直延伸到该对象的末尾。

注意: pos < 0 或pos >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 first(),last(),chopped(),chop(),truncate(),slice(), 和Sizes and Sub-Strings

[constexpr, since 6.5] QAnyStringView QAnyStringView::sliced(qsizetype pos, qsizetype n) const

返回包含n 代码点的字符串视图,从位置pos 开始。

注意: pos < 0、n < 0 或pos +n >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 first(),last(),chopped(),chop(),truncate(),slice(), 和Sizes and Sub-Strings

QString QAnyStringView::toString() const

QString 的形式返回该字符串视图数据的深度副本。

如果且仅当该字符串视图为空,返回值将是一个空QString

[constexpr, since 6.5] void QAnyStringView::truncate(qsizetype n)

将字符串视图截断为n 代码点。

*this = first(n) 相同。

注意: n < 0 或n >size() 时,行为未定义。

此函数在 Qt 6.5 中引入。

另请参阅 sliced(),first(),last(),chopped(),chop() 和Sizes and Sub-Strings

[constexpr] template <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const

根据字符串视图引用字符串数据的编码,使用QUtf8StringViewQLatin1StringQStringView 调用v

大多数使用QAnyStringView 的函数都是这样分叉成按编码的函数的:

void processImpl(QLatin1String s) { ~~~ }
void processImpl(QUtf8StringView s) { ~~~ }
void processImpl(QStringView s) { ~~~ }

void process(QAnyStringView s)
{
    s.visit([](auto s) { processImpl(s); });
}

在这里,我们对QAnyStringView 对象和 lambda 的参数都使用了相同的名称s 。这是惯用的代码,有助于通过 visit() 调用跟踪对象的身份,例如在更复杂的情况下,如

bool equal(QAnyStringView lhs, QAnyStringView rhs)
{
    // assuming operator==(QAnyStringView, QAnyStringView) didn't, yet, exist:
    return lhs.visit([rhs](auto lhs) {
        rhs.visit([lhs](auto rhs) {
            return lhs == rhs;
        });
    });
}

visit() 要求所有 lambda 实例具有相同的返回类型。如果它们不同,即使存在共同的类型,也会出现编译错误。要解决这个问题,可以在 lambda 上使用显式返回类型,或在返回语句中进行转换:

// wrong:
QAnyStringView firstHalf(QAnyStringView input)
{
    return input.visit([](auto input) {   // ERROR: lambdas return different types
        return input.sliced(0, input.size() / 2);
    });
}
// correct:
QAnyStringView firstHalf(QAnyStringView input)
{
    return input.visit([](auto input) -> QAnyStringView { // OK, explicit return type
        return input.sliced(0, input.size() / 2);
    });
}
// also correct:
QAnyStringView firstHalf(QAnyStringView input)
{
    return input.visit([](auto input) {
        return QAnyStringView(input.sliced(0, input.size() / 2)); // OK, cast to common type
    });
}

相关非成员

[noexcept] bool operator!=(const QAnyStringView &lhs, const QAnyStringView &rhs)

[noexcept] bool operator<(const QAnyStringView &lhs, const QAnyStringView &rhs)

[noexcept] bool operator<=(const QAnyStringView &lhs, const QAnyStringView &rhs)

[noexcept] bool operator==(const QAnyStringView &lhs, const QAnyStringView &rhs)

[noexcept] bool operator>(const QAnyStringView &lhs, const QAnyStringView &rhs)

[noexcept] bool operator>=(const QAnyStringView &lhs, const QAnyStringView &rhs)

lhsrhs 进行比较的运算符。

另请参见 compare().

[since 6.7] QDebug operator<<(QDebug d, QAnyStringView s)

s 输出到调试流d

如果d.quotedString()true ,则表示字符串使用的是哪种编码。如果只需要字符串数据,可使用visit() 这样的函数:

s.visit([&d) (auto s) { d << s; });

此函数在 Qt 6.7 中引入。

另请参阅 QAnyStringView::visit()。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.