Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qstringconverter_base.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSTRINGCONVERTER_BASE_H
5#define QSTRINGCONVERTER_BASE_H
6
7#if 0
8// QStringConverter(Base) class are handled in qstringconverter
9#pragma qt_sync_stop_processing
10#endif
11
12#include <optional>
13
14#include <QtCore/qglobal.h> // QT_{BEGIN,END}_NAMESPACE
15#include <QtCore/qflags.h> // Q_DECLARE_FLAGS
16#include <QtCore/qcontainerfwd.h>
17
18#include <cstring>
19
21
22class QByteArrayView;
23class QChar;
24class QByteArrayView;
25class QStringView;
26
28{
29public:
30 enum class Flag {
31 Default = 0,
32 Stateless = 0x1,
34 WriteBom = 0x4,
36 UsesIcu = 0x10,
37 };
39
40 struct State {
41 constexpr State(Flags f = Flag::Default) noexcept
42 : flags(f), state_data{0, 0, 0, 0} {}
43 ~State() { clear(); }
44
45 State(State &&other) noexcept
46 : flags(other.flags),
47 remainingChars(other.remainingChars),
48 invalidChars(other.invalidChars),
49 state_data{other.state_data[0], other.state_data[1],
50 other.state_data[2], other.state_data[3]},
51 clearFn(other.clearFn)
52 { other.clearFn = nullptr; }
54 {
55 clear();
56 flags = other.flags;
57 remainingChars = other.remainingChars;
58 invalidChars = other.invalidChars;
59 std::memmove(state_data, other.state_data, sizeof state_data); // self-assignment-safe
60 clearFn = other.clearFn;
61 other.clearFn = nullptr;
62 return *this;
63 }
64 Q_CORE_EXPORT void clear() noexcept;
65 Q_CORE_EXPORT void reset() noexcept;
66
69 qsizetype remainingChars = 0;
70 qsizetype invalidChars = 0;
71
72 union {
73 uint state_data[4];
74 void *d[2];
75 };
76 using ClearDataFn = void (*)(State *) noexcept;
77 ClearDataFn clearFn = nullptr;
78 private:
79 Q_DISABLE_COPY(State)
80 };
81protected:
83};
84Q_DECLARE_OPERATORS_FOR_FLAGS(QStringConverterBase::Flags)
85
87{
88public:
89
90 enum Encoding {
92#ifndef QT_BOOTSTRAPPED
99#endif
102 LastEncoding = System
103 };
104#ifdef Q_QDOC
105 // document the flags here
106 enum class Flag {
107 Default = 0,
108 Stateless = 0x1,
109 ConvertInvalidToNull = 0x2,
110 WriteBom = 0x4,
111 ConvertInitialBom = 0x8,
112 UsesIcu = 0x10,
113 };
115#endif
116
117protected:
118
120 {
122 using LengthFn = qsizetype (*)(qsizetype inLength);
123 using EncoderFn = char * (*)(char *out, QStringView in, State *state);
124 const char *name = nullptr;
129 };
130
131 constexpr QStringConverter() noexcept
132 : iface(nullptr)
133 {}
134 constexpr explicit QStringConverter(Encoding encoding, Flags f)
135 : iface(&encodingInterfaces[qsizetype(encoding)]), state(f)
136 {}
137 constexpr explicit QStringConverter(const Interface *i) noexcept
138 : iface(i)
139 {}
140 Q_CORE_EXPORT explicit QStringConverter(const char *name, Flags f);
141
142
143 ~QStringConverter() = default;
144
145public:
148
149 bool isValid() const noexcept { return iface != nullptr; }
150
151 void resetState() noexcept
152 {
153 state.reset();
154 }
155 bool hasError() const noexcept { return state.invalidChars != 0; }
156
157 Q_CORE_EXPORT const char *name() const noexcept;
158
159 Q_CORE_EXPORT static std::optional<Encoding> encodingForName(const char *name) noexcept;
160 Q_CORE_EXPORT static const char *nameForEncoding(Encoding e);
161 Q_CORE_EXPORT static std::optional<Encoding>
162 encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0) noexcept;
163 Q_CORE_EXPORT static std::optional<Encoding> encodingForHtml(QByteArrayView data);
164
165 Q_CORE_EXPORT static QStringList availableCodecs();
166
167protected:
168 const Interface *iface;
170private:
171 Q_CORE_EXPORT static const Interface encodingInterfaces[Encoding::LastEncoding + 1];
172};
173
175
176#endif
LEInquiryState internalState
\inmodule QtCore
~QStringConverterBase()=default
bool hasError() const noexcept
Returns true if a conversion could not correctly convert a character.
constexpr QStringConverter(Encoding encoding, Flags f)
QStringConverter(QStringConverter &&)=default
bool isValid() const noexcept
Returns true if this is a valid string converter that can be used for encoding or decoding text.
void resetState() noexcept
Resets the internal state of the converter, clearing potential errors or partial conversions.
~QStringConverter()=default
Encoding
\value Utf8 Create a converter to or from UTF-8 \value Utf16 Create a converter to or from UTF-16.
constexpr QStringConverter(const Interface *i) noexcept
QStringConverter & operator=(QStringConverter &&)=default
constexpr QStringConverter() noexcept
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
b clear()
else opt state
[0]
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
Flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLbitfield flags
GLuint name
GLboolean reset
GLuint in
static QT_BEGIN_NAMESPACE const uint Default
Definition qsplitter_p.h:27
static qsizetype toUtf16Len(qsizetype l)
static qsizetype fromUtf16Len(qsizetype l)
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
QTextStream out(stdout)
[7]
QSharedPointer< T > other(t)
[5]
void(*)(State *) noexcept ClearDataFn
State & operator=(State &&other) noexcept
State(State &&other) noexcept
constexpr State(Flags f=Flag::Default) noexcept
QChar *(*)(QChar *out, QByteArrayView in, State *state) DecoderFn
qsizetype(*)(qsizetype inLength) LengthFn
char *(*)(char *out, QStringView in, State *state) EncoderFn