Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:critical reason:data-parser
4
5#ifndef QSTRINGCONVERTER_BASE_H
6#define QSTRINGCONVERTER_BASE_H
7
8#if 0
9// IWYU pragma: private, include "qstringconverter.h"
10// QStringConverter(Base) class are handled in qstringconverter
11#pragma qt_sync_stop_processing
12#endif
13
14#include <optional>
15
16#include <QtCore/qglobal.h> // QT_{BEGIN,END}_NAMESPACE
17#include <QtCore/qflags.h> // Q_DECLARE_FLAGS
18#include <QtCore/qcontainerfwd.h>
19#include <QtCore/qstringfwd.h>
20
21#include <cstring>
22
23QT_BEGIN_NAMESPACE
24
25class QByteArrayView;
26class QChar;
27class QByteArrayView;
28class QStringView;
29
30#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(Q_QDOC) && !defined(QT_BOOTSTRAPPED)
31class QStringConverterBase
32#else
34#endif
35{
36public:
37 enum class Flag {
39 Stateless = 0x1,
41 WriteBom = 0x4,
43 UsesIcu = 0x10,
44 };
45 Q_DECLARE_FLAGS(Flags, Flag)
46
47 struct State {
48 constexpr State(Flags f = Flag::Default) noexcept
49 : flags(f), state_data{0, 0, 0, 0} {}
50 ~State() { clear(); }
51
60 State &operator=(State &&other) noexcept
61 {
62 clear();
66 std::memmove(state_data, other.state_data, sizeof state_data); // self-assignment-safe
68 other.clearFn = nullptr;
69 return *this;
70 }
71 Q_CORE_EXPORT void clear() noexcept;
72 Q_CORE_EXPORT void reset() noexcept;
73
78
79 union {
81 void *d[2];
82 };
83 using ClearDataFn = void (*)(State *) noexcept;
85 private:
87 };
88
89#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(Q_QDOC) && !defined(QT_BOOTSTRAPPED)
90protected:
91 QStringConverterBase() = default;
92 ~QStringConverterBase() = default;
95};
96
98{
99public:
100#endif // Qt 6 compat for QStringConverterBase
101
116
117protected:
118
120 {
121 using DecoderFn = QChar * (*)(QChar *out, QByteArrayView in, State *state);
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)
136 {}
137 constexpr explicit QStringConverter(const Interface *i) noexcept
138 : iface(i)
139 {}
140#if QT_CORE_REMOVED_SINCE(6, 8)
141 Q_CORE_EXPORT explicit QStringConverter(const char *name, Flags f);
142#endif
144
145
146 ~QStringConverter() = default;
147
148public:
151
152 bool isValid() const noexcept { return iface != nullptr; }
153
154 void resetState() noexcept
155 {
156 state.reset();
157 }
158 bool hasError() const noexcept { return state.invalidChars != 0; }
159
160 Q_CORE_EXPORT const char *name() const noexcept;
161
162#if QT_CORE_REMOVED_SINCE(6, 8)
163 Q_CORE_EXPORT static std::optional<Encoding> encodingForName(const char *name) noexcept;
164#endif
166 Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static const char *nameForEncoding(Encoding e) noexcept;
170
171 Q_CORE_EXPORT static QStringList availableCodecs();
172
173
182 template <typename Char>
191
192protected:
195private:
196 Q_CORE_EXPORT static const Interface encodingInterfaces[Encoding::LastEncoding + 1];
197};
198
199Q_DECLARE_OPERATORS_FOR_FLAGS(QStringConverter::Flags)
200
201QT_END_NAMESPACE
202
203#endif
Q_CORE_EXPORT const char * name() const noexcept
Returns the canonical name of the encoding this QStringConverter can encode or decode.
bool hasError() const noexcept
Returns true if a conversion could not correctly convert a character.
constexpr QStringConverter(Encoding encoding, Flags f)
QStringConverter(QStringConverter &&)=default
Flag
\value Default Default conversion rules apply.
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
const Interface * iface
constexpr QStringConverter() noexcept
\inmodule QtCore
Definition qstringview.h:77
QChar *(*)(QChar *out, QByteArrayView in, State *state) DecoderFn
char *(*)(char *out, QStringView in, State *state) EncoderFn