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// QStringConverter(Base) class are handled in qstringconverter
10#pragma qt_sync_stop_processing
11#endif
12
13#include <optional>
14
15#include <QtCore/qglobal.h> // QT_{BEGIN,END}_NAMESPACE
16#include <QtCore/qflags.h> // Q_DECLARE_FLAGS
17#include <QtCore/qcontainerfwd.h>
18#include <QtCore/qstringfwd.h>
19
20#include <cstring>
21
22QT_BEGIN_NAMESPACE
23
24class QByteArrayView;
25class QChar;
26class QByteArrayView;
27class QStringView;
28
29#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(Q_QDOC) && !defined(QT_BOOTSTRAPPED)
30class QStringConverterBase
31#else
33#endif
34{
35public:
36 enum class Flag {
38 Stateless = 0x1,
40 WriteBom = 0x4,
42 UsesIcu = 0x10,
43 };
44 Q_DECLARE_FLAGS(Flags, Flag)
45
46 struct State {
47 constexpr State(Flags f = Flag::Default) noexcept
48 : flags(f), state_data{0, 0, 0, 0} {}
49 ~State() { clear(); }
50
59 State &operator=(State &&other) noexcept
60 {
61 clear();
65 std::memmove(state_data, other.state_data, sizeof state_data); // self-assignment-safe
67 other.clearFn = nullptr;
68 return *this;
69 }
70 Q_CORE_EXPORT void clear() noexcept;
71 Q_CORE_EXPORT void reset() noexcept;
72
77
78 union {
80 void *d[2];
81 };
82 using ClearDataFn = void (*)(State *) noexcept;
84 private:
86 };
87
88#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(Q_QDOC) && !defined(QT_BOOTSTRAPPED)
89protected:
90 QStringConverterBase() = default;
91 ~QStringConverterBase() = default;
94};
95
97{
98public:
99#endif // Qt 6 compat for QStringConverterBase
100
115
116protected:
117
119 {
120 using DecoderFn = QChar * (*)(QChar *out, QByteArrayView in, State *state);
122 using EncoderFn = char * (*)(char *out, QStringView in, State *state);
123 const char *name = nullptr;
124 DecoderFn toUtf16 = nullptr;
126 EncoderFn fromUtf16 = nullptr;
128 };
129
130 constexpr QStringConverter() noexcept
131 : iface(nullptr)
132 {}
133 constexpr explicit QStringConverter(Encoding encoding, Flags f)
135 {}
136 constexpr explicit QStringConverter(const Interface *i) noexcept
137 : iface(i)
138 {}
139#if QT_CORE_REMOVED_SINCE(6, 8)
140 Q_CORE_EXPORT explicit QStringConverter(const char *name, Flags f);
141#endif
143
144
145 ~QStringConverter() = default;
146
147public:
150
151 bool isValid() const noexcept { return iface != nullptr; }
152
153 void resetState() noexcept
154 {
155 state.reset();
156 }
157 bool hasError() const noexcept { return state.invalidChars != 0; }
158
159 Q_CORE_EXPORT const char *name() const noexcept;
160
161#if QT_CORE_REMOVED_SINCE(6, 8)
162 Q_CORE_EXPORT static std::optional<Encoding> encodingForName(const char *name) noexcept;
163#endif
165 Q_CORE_EXPORT static const char *nameForEncoding(Encoding e);
169
170 Q_CORE_EXPORT static QStringList availableCodecs();
171
172protected:
175private:
176 Q_CORE_EXPORT static const Interface encodingInterfaces[Encoding::LastEncoding + 1];
177};
178
179Q_DECLARE_OPERATORS_FOR_FLAGS(QStringConverter::Flags)
180
181QT_END_NAMESPACE
182
183#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.
static Q_CORE_EXPORT const char * nameForEncoding(Encoding e)
Returns the canonical name for encoding e.
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