Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
17#include <cstring>
18
20
21class QByteArrayView;
22class QChar;
23class QByteArrayView;
24class QStringView;
25
27{
28public:
29 enum class Flag {
30 Default = 0,
31 Stateless = 0x1,
33 WriteBom = 0x4,
35 UsesIcu = 0x10,
36 };
38
39 struct State {
40 constexpr State(Flags f = Flag::Default) noexcept
41 : flags(f), state_data{0, 0, 0, 0} {}
42 ~State() { clear(); }
43
44 State(State &&other) noexcept
45 : flags(other.flags),
46 remainingChars(other.remainingChars),
47 invalidChars(other.invalidChars),
48 state_data{other.state_data[0], other.state_data[1],
49 other.state_data[2], other.state_data[3]},
50 clearFn(other.clearFn)
51 { other.clearFn = nullptr; }
53 {
54 clear();
55 flags = other.flags;
56 remainingChars = other.remainingChars;
57 invalidChars = other.invalidChars;
58 std::memmove(state_data, other.state_data, sizeof state_data); // self-assignment-safe
59 clearFn = other.clearFn;
60 other.clearFn = nullptr;
61 return *this;
62 }
63 Q_CORE_EXPORT void clear() noexcept;
64 Q_CORE_EXPORT void reset() noexcept;
65
68 qsizetype remainingChars = 0;
69 qsizetype invalidChars = 0;
70
71 union {
72 uint state_data[4];
73 void *d[2];
74 };
75 using ClearDataFn = void (*)(State *) noexcept;
76 ClearDataFn clearFn = nullptr;
77 private:
78 Q_DISABLE_COPY(State)
79 };
80protected:
82};
83Q_DECLARE_OPERATORS_FOR_FLAGS(QStringConverterBase::Flags)
84
86{
87public:
88
89 enum Encoding {
99 LastEncoding = System
100 };
101#ifdef Q_QDOC
102 // document the flags here
103 enum class Flag {
104 Default = 0,
105 Stateless = 0x1,
106 ConvertInvalidToNull = 0x2,
107 WriteBom = 0x4,
108 ConvertInitialBom = 0x8,
109 UsesIcu = 0x10,
110 };
112#endif
113
114protected:
115
117 {
119 using LengthFn = qsizetype (*)(qsizetype inLength);
120 using EncoderFn = char * (*)(char *out, QStringView in, State *state);
121 const char *name = nullptr;
126 };
127
128 constexpr QStringConverter() noexcept
129 : iface(nullptr)
130 {}
131 constexpr explicit QStringConverter(Encoding encoding, Flags f)
132 : iface(&encodingInterfaces[qsizetype(encoding)]), state(f)
133 {}
134 constexpr explicit QStringConverter(const Interface *i) noexcept
135 : iface(i)
136 {}
137 Q_CORE_EXPORT explicit QStringConverter(const char *name, Flags f);
138
139
140 ~QStringConverter() = default;
141
142public:
145
146 bool isValid() const noexcept { return iface != nullptr; }
147
148 void resetState() noexcept
149 {
150 state.reset();
151 }
152 bool hasError() const noexcept { return state.invalidChars != 0; }
153
154 Q_CORE_EXPORT const char *name() const noexcept;
155
156 Q_CORE_EXPORT static std::optional<Encoding> encodingForName(const char *name) noexcept;
157 Q_CORE_EXPORT static const char *nameForEncoding(Encoding e);
158 Q_CORE_EXPORT static std::optional<Encoding>
159 encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0) noexcept;
160 Q_CORE_EXPORT static std::optional<Encoding> encodingForHtml(QByteArrayView data);
161
162protected:
163 const Interface *iface;
165private:
166 Q_CORE_EXPORT static const Interface encodingInterfaces[Encoding::LastEncoding + 1];
167};
168
170
171#endif
LEInquiryState internalState
\inmodule QtCore
Definition qchar.h:48
~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
Definition qstringview.h:76
b clear()
double e
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
GLfloat GLfloat f
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLboolean reset
GLuint in
static QT_BEGIN_NAMESPACE const uint Default
Definition qsplitter_p.h:25
static qsizetype toUtf16Len(qsizetype l)
static qsizetype fromUtf16Len(qsizetype l)
ptrdiff_t qsizetype
Definition qtypes.h:70
unsigned int uint
Definition qtypes.h:29
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