6.0.0-rc4Source

QStringConverter Class

The QStringConverter class provides a base class for encoding and decoding text. More…

Header#include <QStringConverter>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since6.0
InheritsQStringConverterBase
Inherited byQStringDecoder, QStringEncoder

List of all members, including inherited members

Public Types

enumEncoding

Public Functions

QStringConverter(QStringConverter &&other)
QStringConverter &operator=(QStringConverter &&other)
boolisValid() const
voidresetState()
boolhasError() const
const char *name() const

Static Public Members

std::optional<Encoding>encodingForName(QAnyStringView name)
const char *nameForEncoding(Encoding e)
std::optional<Encoding>encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0)
std::optional<Encoding>encodingForHtml(QByteArrayView data)
QStringListavailableCodecs()

Protected Functions

QStringConverter()
QStringConverter(Encoding encoding, Flags f)
~QStringConverter()

Detailed Description

QStringConverter holds the encoding and the conversion state shared by QStringEncoder and QStringDecoder, and the static functions that look up encodings by name or detect them from data.

See also QStringEncoder and QStringDecoder.

Differences from Qt

Only the encodings of the Encoding enum are supported; there is no ICU backend, so names such as "Shift_JIS" give an invalid converter. The System encoding is UTF-8. Utf16 and Utf32 without a byte order mark use the host byte order. Converters can be moved but not copied.

Member Type Documentation

enum QStringConverter::Encoding

ConstantDescription
QStringConverter::Utf8UTF-8.
QStringConverter::Utf16UTF-16. Decoding detects the byte order from a byte order mark and otherwise uses the host order; encoding uses the host order.
QStringConverter::Utf16LEUTF-16, little endian.
QStringConverter::Utf16BEUTF-16, big endian.
QStringConverter::Utf32UTF-32, with the byte order handled like Utf16.
QStringConverter::Utf32LEUTF-32, little endian.
QStringConverter::Utf32BEUTF-32, big endian.
QStringConverter::Latin1ISO-8859-1. Characters above U+00FF are encoded as '?'.
QStringConverter::SystemThe encoding of the system, which is UTF-8 in Nt.

Member Function Documentation

QStringConverter::QStringConverter(QStringConverter &&other)

Move-constructs a converter from other.

[protected] QStringConverter::QStringConverter()

Constructs an invalid converter.

[protected] QStringConverter::QStringConverter(Encoding encoding, Flags f)

Constructs a converter for encoding with the flags f.

[protected] QStringConverter::~QStringConverter()

Destroys the converter.

[static] QStringList QStringConverter::availableCodecs()

Returns the names of the encodings that the converters support.

This function was introduced in 6.7.

See also encodingForName().

[static] std::optional<Encoding> QStringConverter::encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0)

Returns the encoding of data as indicated by a byte order mark at its start. Without one, expectedFirstCharacter (if not 0) is used: the position of its zero bytes tells UTF-16 and UTF-32 and their byte order apart. Returns an empty optional if the encoding cannot be told.

See also encodingForHtml().

[static] std::optional<Encoding> QStringConverter::encodingForHtml(QByteArrayView data)

Returns the encoding of the HTML document data: the one given by a byte order mark, or else by a charset attribute in the first 1024 bytes. Returns Utf8 if neither is present, and an empty optional if the document names an encoding that is not supported.

See also QStringDecoder::decoderForHtml() and encodingForData().

[static] std::optional<Encoding> QStringConverter::encodingForName(QAnyStringView name)

Returns the encoding called name, or an empty optional if it is not supported. Case and the characters other than letters and digits are ignored, so "utf8", "UTF-8" and "Utf_8" are the same. The names "latin1", "ISO-8859-1" and "locale" are recognized.

See also nameForEncoding().

bool QStringConverter::hasError() const

Returns true if invalid input was seen since the converter was created or resetState() was called; otherwise returns false.

bool QStringConverter::isValid() const

Returns true if the converter has a supported encoding; otherwise returns false.

const char * QStringConverter::name() const

Returns the canonical name of the converter's encoding, or nullptr if the converter is invalid.

See also nameForEncoding().

[static] const char * QStringConverter::nameForEncoding(Encoding e)

Returns the canonical name of the encoding e, such as "UTF-8".

See also encodingForName().

QStringConverter & QStringConverter::operator=(QStringConverter &&other)

Move-assigns other to this converter and returns a reference to it.

void QStringConverter::resetState()

Resets the conversion state: pending partial sequences are dropped, the error state is cleared and the next input is treated as the start of the data.

See also hasError().