QUtf8StringView Class
QUtf8StringView 类通过QString API 的一个只读子集提供了一个统一的 UTF-8 字符串视图。更多
Header: | #include <QUtf8StringView> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
自 | Qt 6.0 |
- 所有成员的列表,包括继承成员
- 已废弃成员
- QUtf8StringView 属于字符串数据类。
该类具有很强的可比性。
该类与 char16_t,QChar, const char16_t *,QString,QStringView, 和QLatin1StringView 具有很强的可比性。
该类与 const char *、QByteArray 和QByteArrayView 具有很强的可比性。
字节数组的内容解释为 UTF-8。
注意:该类中的所有函数都是可重入的。
公共类型
const_iterator | |
const_pointer | |
const_reference | |
const_reverse_iterator | |
difference_type | |
iterator | |
pointer | |
reference | |
reverse_iterator | |
size_type | |
storage_type | |
value_type |
公共函数
QUtf8StringView() | |
QUtf8StringView(const Char (&)[N] string) | |
QUtf8StringView(const Char *str) | |
QUtf8StringView(const Container &str) | |
QUtf8StringView(std::nullptr_t) | |
QUtf8StringView(const Char *first, const Char *last) | |
QUtf8StringView(const Char *str, qsizetype len) | |
(since 6.9) QString | arg(Args &&... args) const |
QUtf8StringView::storage_type | at(qsizetype n) const |
QUtf8StringView::storage_type | back() const |
QUtf8StringView::const_iterator | begin() const |
QUtf8StringView::const_iterator | cbegin() const |
QUtf8StringView::const_iterator | cend() const |
void | chop(qsizetype n) |
QUtf8StringView | chopped(qsizetype n) const |
(since 6.5) int | compare(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
(since 6.5) int | compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
(since 6.5) int | compare(QUtf8StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QUtf8StringView::const_reverse_iterator | crbegin() const |
QUtf8StringView::const_reverse_iterator | crend() const |
QUtf8StringView::const_pointer | data() const |
bool | empty() const |
QUtf8StringView::const_iterator | end() const |
QUtf8StringView | first(qsizetype n) const |
QUtf8StringView::storage_type | front() const |
bool | isEmpty() const |
bool | isNull() const |
(since 6.3) bool | isValidUtf8() const |
QUtf8StringView | last(qsizetype n) const |
qsizetype | length() const |
(since 6.8) qsizetype | max_size() const |
QUtf8StringView::const_reverse_iterator | rbegin() const |
QUtf8StringView::const_reverse_iterator | rend() const |
qsizetype | size() const |
(since 6.8) QUtf8StringView & | slice(qsizetype pos, qsizetype n) |
(since 6.8) QUtf8StringView & | slice(qsizetype pos) |
QUtf8StringView | sliced(qsizetype pos) const |
QUtf8StringView | sliced(qsizetype pos, qsizetype n) const |
QString | toString() const |
void | truncate(qsizetype n) |
const char8_t * | utf8() const |
(since 6.7) std::basic_string_view<QUtf8StringView::storage_type> | operator std::basic_string_view<QUtf8StringView::storage_type>() const |
QUtf8StringView::storage_type | operator[](qsizetype n) const |
静态公共成员
详细说明
QUtf8StringView 引用了它不拥有的 UTF-8 字符串的连续部分。它是所有类型 UTF-8 字符串的接口类型,无需先构建QString 或QByteArray 。
UTF-8 字符串可表示为char8_t
,char
,signed char
或unsigned char
的数组(或数组兼容的数据结构,如 std::basic_string 等)。
QUtf8StringView 被设计为接口类型;其主要用途是作为函数参数类型。当 QUtf8StringView 被用作自动变量或数据成员时,必须注意确保所引用的字符串数据(例如,由 std::u8string 所拥有)在所有代码路径上都比 QUtf8StringView 长,以免字符串视图最终引用了已删除的数据。
当作为接口类型使用时,QUtf8StringView 允许一个函数接受多种 UTF-8 字符串数据源。因此,一个接受 QUtf8StringView 的函数可以取代多个函数重载(例如QByteArray ),同时还能向函数传递更多字符串数据源,例如u8"Hello World"
、一个char8_t
(C++20) 或char
(C++17) 字符串字面量。使用 QUtf8StringView 时,C++17 和 C++20 之间的char8_t
不兼容问题将不复存在。
与所有视图一样,QUtf8StringView 应通过值传递,而不是通过引用-const 传递:
void myfun1(QUtf8StringView sv); // preferred void myfun2(const QUtf8StringView &sv); // compiles and works, but slower
如果您想让用户在向函数传递字符串时拥有最大的自由度,请考虑使用QAnyStringView 。
QUtf8StringView 也可用作函数的返回值。如果你调用一个返回 QUtf8StringView 的函数,请格外注意不要让 QUtf8StringView 的存在时间超过函数承诺的保持引用字符串数据存活的时间。如果有疑问,可调用toString() 将 QUtf8StringView 转换为QString ,从而获得对数据的强引用。
QUtf8StringView 是一种字面类型。
兼容字符类型
QUtf8StringView 接受多种字符类型的字符串:
char
(有符号和无符号)char8_t
(仅适用于 C++20)
大小和子字符串
QUtf8StringView 函数中的所有大小和位置都是以 UTF-8 代码点为单位的(也就是说,UTF-8 多字节序列根据其长度被视为 2、3 或 4)。QUtf8StringView 不会试图检测或防止直接切入 UTF-8 多字节序列。这与QStringView 和代理对的情况类似。
C++20、char8_t 和 QUtf8StringView
在 C++20 中,u8""
字符串字面量的类型从const char[]
变为const char8_t[]
。如果 Qt 6 可以依赖 C++20,QUtf8StringView 将原生存储char8_t
,以下函数和别名将使用(指向)char8_t
:
- storage_type,value_type, 等
- begin(),end(),data(), etc
- front(),back(),at(), operator[]()
这就是 Qt 7 中 QUtf8StringView 的预期外观,但在 Qt 6 中却无法实现。Qt 在不同的(内联)命名空间中提供了两个 QUtf8StringView 类,而不是将用户锁定在 C++17 时代的接口上。第一个 QUtf8StringView 类位于命名空间q_no_char8_t
中,其value_type 为const char
,并且是通用的。第二个 QUtf8StringView 类位于命名空间q_has_char8_t
中,其value_type 的值为const char8_t
,仅在以 C++20 模式编译时可用。
q_no_char8_t
为了避免意外的二进制不兼容,C++ 20 是一个内联命名空间,与 C++ 版本无关。要使用 版本,需要用 明确命名。char8_t
q_has_char8_t::QUtf8StringView
在内部,两者都是同一个模板类 QBasicUtf8StringView 的实例化。请不要在源代码中使用模板类的名称。
另请参阅 QAnyStringView,QUtf8StringView, 和QString 。
成员类型文档
QUtf8StringView::const_iterator
该类型定义为QUtf8StringView 提供了 STL 风格的常量迭代器。
另请参见 iterator 和const_reverse_iterator 。
QUtf8StringView::const_pointer
value_type *
的别名。与 STL 兼容。
QUtf8StringView::const_reference
value_type &
的别名。与 STL 兼容。
QUtf8StringView::const_reverse_iterator
该类型定义为QUtf8StringView 提供了 STL 风格的 const 反向迭代器。
另请参见 reverse_iterator 和const_iterator 。
QUtf8StringView::difference_type
std::ptrdiff_t
的别名。与 STL 兼容。
QUtf8StringView::iterator
该类型定义为QUtf8StringView 提供了 STL 风格的常量迭代器。
QUtf8StringView 迭代器不支持可变迭代器,因此它与 相同。const_iterator
另请参见 const_iterator 和reverse_iterator 。
QUtf8StringView::pointer
value_type *
的别名。为与 STL 兼容而提供。
QUtf8StringView 不支持可变指针,因此与 相同。const_pointer
QUtf8StringView::reference
value_type &
的别名。为与 STL 兼容而提供。
QUtf8StringView 不支持可变引用,因此与 相同。const_reference
QUtf8StringView::reverse_iterator
该类型定义为QUtf8StringView 提供了 STL 风格的 const 反向迭代器。
QUtf8StringView 迭代器不支持可变反向迭代器,因此它与 相同。const_reverse_iterator
另请参见 const_reverse_iterator 和iterator 。
QUtf8StringView::size_type
qsizetype 的别名。与 STL 兼容。
[alias]
QUtf8StringView::storage_type
char
的别名 。
QUtf8StringView::value_type
const char
的别名。为与 STL 兼容而提供。
成员函数文档
[noexcept, since 6.5]
int QUtf8StringView::compare(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[noexcept, since 6.5]
int QUtf8StringView::compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[noexcept, since 6.5]
int QUtf8StringView::compare(QUtf8StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
将此字符串视图与str 进行比较,如果此字符串视图小于str ,则返回一个负整数;如果大于str ,则返回一个正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive (默认值),则比较区分大小写;否则比较不区分大小写。
此函数在 Qt 6.5 中引入。
[constexpr noexcept]
QUtf8StringView::QUtf8StringView()
构造空字符串视图。
另请参阅 isNull()。
[constexpr noexcept]
template <typename Char, size_t N> QUtf8StringView::QUtf8StringView(const Char (&)[N] string)
在字符串字面string 上构建字符串视图。视图覆盖数组,直到遇到第一个Char(0)
或N
,以先到者为准。如果需要完整的数组,请使用fromArray() 代替。
string 必须在该字符串视图对象的生命周期内保持有效。
限制条件
只有当string 是一个实际数组且Char
是一个兼容字符类型时,才会参与重载解析。兼容字符类型有char8_t
,char
,signed char
和unsigned char
。
另请参阅 fromArray()。
[constexpr noexcept]
template <typename Char> QUtf8StringView::QUtf8StringView(const Char *str)
在str 上构建字符串视图。长度通过扫描第一个Char(0)
来确定。
str 必须在该字符串视图对象的生命周期内保持有效。
将nullptr
作为str 传递是安全的,会产生一个空字符串视图。
限制条件
只有当str 不是数组且Char
是兼容字符类型时,才会参与重载解析。兼容字符类型有char8_t
,char
,signed char
和unsigned char
。
[constexpr noexcept]
template <typename Container, QUtf8StringView::if_compatible_container<Container> = true> QUtf8StringView::QUtf8StringView(const Container &str)
在str 上构建字符串视图。长度取自std::size(str)
。
std::data(str)
必须在该字符串视图对象的生命周期内保持有效。
当且仅当std::size(str) == 0
时,字符串视图将为空。未说明该构造函数是否会导致字符串视图为空(std::data(str)
必须返回nullptr
)。
限制条件
只有当Container
是一个具有与value_type
兼容的字符类型的容器时,才会参与重载解析。兼容的字符类型有char8_t
,char
,signed char
和unsigned char
。
[constexpr noexcept]
QUtf8StringView::QUtf8StringView(std::nullptr_t)
构造空字符串视图。
另请参阅 isNull()。
[constexpr]
template <typename Char, QUtf8StringView::if_compatible_char<Char> = true> QUtf8StringView::QUtf8StringView(const Char *first, const Char *last)
在first 上构建长度为 (last -first) 的字符串视图。
[first,last)
的范围必须在该字符串视图对象的生命周期内保持有效。
如果last 也是nullptr
,则以first 的形式传递\nullptr
是安全的,但会导致字符串视图为空。
如果last 在first 之前,或first 是nullptr
而last 不是,则行为未定义。
限制条件
只有当Char
是兼容字符类型时,才会参与重载解析。兼容的字符类型有char8_t
,char
,signed char
和unsigned char
。
[constexpr]
template <typename Char, QUtf8StringView::if_compatible_char<Char> = true> QUtf8StringView::QUtf8StringView(const Char *str, qsizetype len)
在str 上构建长度为len 的字符串视图。
[str,len)
的范围必须在该字符串视图对象的生命周期内保持有效。
如果len 也为 0,将nullptr
作为str 传递是安全的,但会导致字符串视图为空。
如果len 为负数,或str 为正数,则行为未定义nullptr
。
限制条件
只有当Char
是兼容字符类型时,才会参与重载解析。兼容字符类型有char8_t
,char
,signed char
和unsigned char
。
[since 6.9]
template <typename... Args> QString QUtf8StringView::arg(Args &&... args) const
用args 中的相应参数替换该字符串中出现的%N
。参数不分位置:args 的第一个参数用最低的N
(全部)替换%N
,args 的第二个参数用次低的N
替换%N
,等等。
Args
可以由隐式转换为 的任何内容组成。QAnyStringView
此函数在 Qt 6.9 中引入。
另请参阅 QString::arg(Args&&...)。
[constexpr]
QUtf8StringView::storage_type QUtf8StringView::at(qsizetype n) const
返回该字符串视图中n 位置的代码点。
如果n 为负数或不小于size(),则行为未定义。
另请参阅 operator[]()、front() 和back()。
[constexpr]
QUtf8StringView::storage_type QUtf8StringView::back() const
返回字符串视图中的最后一个代码点。与last() 相同。
提供此函数是为了与 STL 兼容。
警告 在空字符串视图中调用此函数会导致未定义的行为。
另请参见 front()。
[noexcept]
QUtf8StringView::const_iterator QUtf8StringView::begin() const
返回指向字符串视图中第一个代码点的STL 风格迭代器。
提供此函数是为了与 STL 兼容。
另请参阅 end()、cbegin()、rbegin() 和data()。
[noexcept]
QUtf8StringView::const_iterator QUtf8StringView::cbegin() const
与begin() 相同。
提供此函数是为了与 STL 兼容。
另请参见 cend()、begin()、crbegin() 和data()。
[noexcept]
QUtf8StringView::const_iterator QUtf8StringView::cend() const
与end() 相同。
提供此函数是为了与 STL 兼容。
[constexpr]
void QUtf8StringView::chop(qsizetype n)
通过n 代码点截断该字符串视图。
与*this = first(size() - n)
相同。
注: 当n < 0 或n >size() 时,行为未定义。
另请参阅 sliced()、first()、last()、chopped() 和truncate()。
[constexpr]
QUtf8StringView QUtf8StringView::chopped(qsizetype n) const
返回从此对象开始的长度为size() -n 的子字符串。
与first(size() - n)
相同。
注意: 当n < 0 或n >size() 时,行为未定义。
另请参阅 sliced()、first()、last()、chop()、truncate() 和slice()。
[noexcept]
QUtf8StringView::const_reverse_iterator QUtf8StringView::crbegin() const
与rbegin() 相同。
提供此函数是为了与 STL 兼容。
另请参见 crend()、rbegin() 和cbegin()。
[noexcept]
QUtf8StringView::const_reverse_iterator QUtf8StringView::crend() const
与rend() 相同。
提供此函数是为了与 STL 兼容。
另请参见 crbegin()、rend() 和cend()。
[constexpr noexcept]
QUtf8StringView::const_pointer QUtf8StringView::data() const
返回指向字符串视图中第一个码位的常量指针。
注意: 返回值所代表的字符数组不是空端。
[constexpr noexcept]
bool QUtf8StringView::empty() const
返回该字符串视图是否为空,即size() == 0
是否为空。
提供此函数是为了与 STL 兼容。
另请参阅 isEmpty()、isNull()、size() 和length() 。
[noexcept]
QUtf8StringView::const_iterator QUtf8StringView::end() const
返回指向列表中最后一个码位之后的虚数码位的 ConstSTL 样式迭代器。
提供此函数是为了与 STL 兼容。
[constexpr]
QUtf8StringView QUtf8StringView::first(qsizetype n) const
返回一个字符串视图,其中包含该字符串视图的第一个n 代码点。
注意: 当n < 0 或n >size() 时,行为未定义。
另请参阅 last()、sliced()、chopped()、chop()、truncate() 和slice()。
[static constexpr noexcept]
template <typename Char, size_t Size, QUtf8StringView::if_compatible_char<Char> = true> QUtf8StringView QUtf8StringView::fromArray(const Char (&)[Size] string)
在完整字符串字面string 上构建字符串视图,包括任何尾部Char(0)
。如果不想在视图中包含 null 终结符,可以在确定它位于末尾时将其chop() 掉。另外,您也可以使用构造函数重载数组字面量,这样就可以创建一个视图,直到但不包括数据中的第一个空结束符。
string 必须在该字符串视图对象的生命周期内保持有效。
如果Char
是兼容字符类型,则该函数可用于任何数组字面量。兼容字符类型包括char8_t
,char
,signed char
和unsigned char
。
[constexpr]
QUtf8StringView::storage_type QUtf8StringView::front() const
返回字符串视图中的第一个代码点。与first() 相同。
提供此函数是为了与 STL 兼容。
警告 在空字符串视图中调用此函数会导致未定义的行为。
另请参见 back()。
[constexpr noexcept]
bool QUtf8StringView::isEmpty() const
返回该字符串视图是否为空,即size() == 0
是否为空。
提供此函数是为了与其他 Qt 容器兼容。
另请参阅 empty(),isNull(),size() 和length() 。
[constexpr noexcept]
bool QUtf8StringView::isNull() const
返回该字符串视图是否为空,即data() == nullptr
是否为空。
提供此函数是为了与其他 Qt 容器兼容。
另请参阅 empty(),isEmpty(),size() 和length() 。
[noexcept, since 6.3]
bool QUtf8StringView::isValidUtf8() const
如果此字符串包含有效的 UTF-8 编码数据,则返回true
,否则返回false
。
此函数在 Qt 6.3 中引入。
[constexpr]
QUtf8StringView QUtf8StringView::last(qsizetype n) const
返回包含该字符串视图最后n 个代码点的字符串视图。
注意: 当n < 0 或n >size() 时,行为未定义。
另请参阅 first()、sliced()、chopped()、chop()、truncate() 和slice()。
[constexpr noexcept]
qsizetype QUtf8StringView::length() const
与size() 相同。
提供此函数是为了与其他 Qt 容器兼容。
另请参阅 empty()、isEmpty()、isNull() 和size()。
[static constexpr noexcept, since 6.8]
qsizetype QUtf8StringView::maxSize()
它返回视图理论上可表示的最大元素数。在实际应用中,受系统可用内存量的限制,该数量可能会小得多。
此函数在 Qt 6.8 中引入。
[constexpr noexcept, since 6.8]
qsizetype QUtf8StringView::max_size() const
提供此函数是为了与 STL 兼容。
返回maxSize()。
此函数在 Qt 6.8 中引入。
[noexcept]
QUtf8StringView::const_reverse_iterator QUtf8StringView::rbegin() const
按相反顺序返回指向字符串视图中第一个代码点的 ConstSTL 样式反向迭代器。
提供此函数是为了与 STL 兼容。
另请参见 rend()、crbegin() 和begin()。
[noexcept]
QUtf8StringView::const_reverse_iterator QUtf8StringView::rend() const
返回一个STL 样式的反向迭代器,按相反顺序指向字符串视图中最后一个代码点之后的一个代码点。
提供此函数是为了与 STL 兼容。
[constexpr noexcept]
qsizetype QUtf8StringView::size() const
以 UTF-8 代码点为单位返回此字符串视图的大小(也就是说,在此函数中,多字节序列算作一个以上,与QString 和QStringView 中的代理对相同)。
另请参阅 empty()、isEmpty()、isNull() 和length()。
[constexpr, since 6.8]
QUtf8StringView &QUtf8StringView::slice(qsizetype pos, qsizetype n)
修改字符串视图,使其从pos 位置开始,并扩展至n 代码点。
注意: 当pos < 0、n < 0 或pos +n >size() 时,行为未定义。
此函数在 Qt 6.8 中引入。
另请参见 sliced()、first()、last()、chopped()、chop() 和truncate()。
[constexpr, since 6.8]
QUtf8StringView &QUtf8StringView::slice(qsizetype pos)
这是一个重载函数。
修改字符串视图,使其从pos 位置开始,一直延伸到其末尾。
注意: 当pos < 0 或pos >size() 时,行为未定义。
此函数在 Qt 6.8 中引入。
另请参阅 sliced()、first()、last()、chopped()、chop() 和truncate() 。
[constexpr]
QUtf8StringView QUtf8StringView::sliced(qsizetype pos) const
返回一个字符串视图,该视图从pos 开始,一直延伸到该对象的末尾。
注意: 当pos < 0 或pos >size() 时,行为未定义。
另请参阅 first()、last()、chopped()、chop()、truncate() 和slice()。
[constexpr]
QUtf8StringView QUtf8StringView::sliced(qsizetype pos, qsizetype n) const
返回包含n 代码点的字符串视图,从位置pos 开始。
注意: 当pos < 0、n < 0 或pos +n >size() 时,行为未定义。
另请参阅 first()、last()、chopped()、chop()、truncate() 和slice()。
QString QUtf8StringView::toString() const
以QString 的形式返回该字符串视图数据的深度副本。
如果且仅当该字符串视图为空,返回值将是一个空QString 。
[constexpr]
void QUtf8StringView::truncate(qsizetype n)
将字符串视图截断为n 代码点。
与*this = first(n)
相同。
注: 当n < 0 或n >size() 时,行为未定义。
另请参阅 sliced()、first()、last()、chopped() 和chop()。
[noexcept]
const char8_t *QUtf8StringView::utf8() const
返回指向字符串视图中第一个代码点的常量指针。
结果以const char8_t*
的形式返回,因此只有在 C++20 模式下编译时才能使用此函数。
注意: 返回值所代表的字符数组不是空端。
[noexcept, since 6.7]
std::basic_string_view<QUtf8StringView::storage_type> QUtf8StringView::operator std::basic_string_view<QUtf8StringView::storage_type>() const
将QUtf8StringView 对象转换为std::basic_string_view
对象。返回的视图将具有与此视图相同的数据指针和长度。返回视图的字符类型为storage_type
。
此函数在 Qt 6.7 中引入。
[constexpr]
QUtf8StringView::storage_type QUtf8StringView::operator[](qsizetype n) const
返回该字符串视图中n 位置的代码点。
如果n 为负数或不小于size(),则行为未定义。
© 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.