Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qstringconverter.h
Go to the documentation of this file.
1// Copyright (C) 2020 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#if 0
5// keep existing syncqt header working after the move of the class
6// into qstringconverter_base
7#pragma qt_class(QStringConverter)
8#pragma qt_class(QStringConverterBase)
9#endif
10
11#ifndef QSTRINGCONVERTER_H
12#define QSTRINGCONVERTER_H
13
14#include <QtCore/qstringconverter_base.h>
15#include <QtCore/qstring.h>
16#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
17#include <QtCore/qstringbuilder.h>
18#endif
19
21
23{
24protected:
25 constexpr explicit QStringEncoder(const Interface *i) noexcept
27 {}
28public:
29 constexpr QStringEncoder() noexcept
31 {}
32 constexpr explicit QStringEncoder(Encoding encoding, Flags flags = Flag::Default)
33 : QStringConverter(encoding, flags)
34 {}
35 explicit QStringEncoder(const char *name, Flags flags = Flag::Default)
37 {}
38
39#if defined(Q_QDOC)
44#else
45 template<typename T>
47 {
50 operator QByteArray() const { return encoder->encodeAsByteArray(data); }
51 };
54 { return DecodedData<const QString &>{this, str}; }
56 { return DecodedData<QStringView>{this, in}; }
59 { return DecodedData<const QString &>{this, str}; }
61 { return DecodedData<QStringView>{this, in}; }
62#endif
63
65 { return iface ? iface->fromUtf16Len(inputLength) : 0; }
67 {
68 if (!iface) {
70 return out;
71 }
72 return iface->fromUtf16(out, in, &state);
73 }
74private:
75 QByteArray encodeAsByteArray(QStringView in)
76 {
77 if (!iface) {
78 // ensure that hasError returns true
80 return {};
81 }
83 char *out = result.data();
85 result.truncate(out - result.constData());
86 return result;
87 }
88
89};
90
92{
93protected:
94 constexpr explicit QStringDecoder(const Interface *i) noexcept
96 {}
97public:
98 constexpr explicit QStringDecoder(Encoding encoding, Flags flags = Flag::Default)
99 : QStringConverter(encoding, flags)
100 {}
101 constexpr QStringDecoder() noexcept
103 {}
104 explicit QStringDecoder(const char *name, Flags f = Flag::Default)
106 {}
107
108#if defined(Q_QDOC)
111 QString decode(const QByteArray &ba);
113#else
114 template<typename T>
116 {
119 operator QString() const { return decoder->decodeAsString(data); }
120 };
123 { return EncodedData<const QByteArray &>{this, ba}; }
125 { return EncodedData<QByteArrayView>{this, ba}; }
128 { return EncodedData<const QByteArray &>{this, ba}; }
130 { return EncodedData<QByteArrayView>{this, ba}; }
131#endif
132
134 { return iface ? iface->toUtf16Len(inputLength) : 0; }
136 {
137 if (!iface) {
139 return out;
140 }
141 return iface->toUtf16(out, ba, &state);
142 }
143 char16_t *appendToBuffer(char16_t *out, QByteArrayView ba)
144 { return reinterpret_cast<char16_t *>(appendToBuffer(reinterpret_cast<QChar *>(out), ba)); }
145
146 Q_CORE_EXPORT static QStringDecoder decoderForHtml(QByteArrayView data);
147
148private:
149 QString decodeAsString(QByteArrayView in)
150 {
151 if (!iface) {
152 // ensure that hasError returns true
154 return {};
155 }
157 const QChar *out = iface->toUtf16(result.data(), in, &state);
158 result.truncate(out - result.constData());
159 return result;
160 }
161};
162
163
164#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
165template <typename T>
166struct QConcatenable<QStringDecoder::EncodedData<T>>
167 : private QAbstractConcatenable
168{
169 typedef QChar type;
170 typedef QString ConvertTo;
171 enum { ExactSize = false };
172 static qsizetype size(const QStringDecoder::EncodedData<T> &s) { return s.decoder->requiredSpace(s.data.size()); }
173 static inline void appendTo(const QStringDecoder::EncodedData<T> &s, QChar *&out)
174 {
175 out = s.decoder->appendToBuffer(out, s.data);
176 }
177};
178
179template <typename T>
180struct QConcatenable<QStringEncoder::DecodedData<T>>
181 : private QAbstractConcatenable
182{
183 typedef char type;
184 typedef QByteArray ConvertTo;
185 enum { ExactSize = false };
186 static qsizetype size(const QStringEncoder::DecodedData<T> &s) { return s.encoder->requiredSpace(s.data.size()); }
187 static inline void appendTo(const QStringEncoder::DecodedData<T> &s, char *&out)
188 {
189 out = s.encoder->appendToBuffer(out, s.data);
190 }
191};
192
193template <typename T>
195{
197 a.reserve(len);
198 QChar *it = a.data() + a.size();
200 a.resize(qsizetype(it - a.constData())); //may be smaller than len
201 return a;
202}
203
204template <typename T>
206{
208 a.reserve(len);
209 char *it = a.data() + a.size();
211 a.resize(qsizetype(it - a.constData())); //may be smaller than len
212 return a;
213}
214#endif
215
217
218#endif
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qchar.h:48
qsizetype size() const
Definition qset.h:50
Encoding
\value Utf8 Create a converter to or from UTF-8 \value Utf16 Create a converter to or from UTF-16.
const Interface * iface
constexpr QStringConverter() noexcept
\inmodule QtCore
constexpr QStringDecoder(Encoding encoding, Flags flags=Flag::Default)
Creates an decoder object using encoding and flags.
EncodedData< QByteArrayView > decode(QByteArrayView ba)
Converts ba and returns the data as a QString.
char16_t * appendToBuffer(char16_t *out, QByteArrayView ba)
constexpr QStringDecoder(const Interface *i) noexcept
qsizetype requiredSpace(qsizetype inputLength) const
Returns the maximum amount of UTF-16 code units required to be able to process inputLength encoded da...
QChar * appendToBuffer(QChar *out, QByteArrayView ba)
Decodes the sequence of bytes viewed by in and writes the decoded result into the buffer starting at ...
static Q_CORE_EXPORT QStringDecoder decoderForHtml(QByteArrayView data)
Tries to determine the encoding of the HTML in data by looking at leading byte order marks or a chars...
Q_WEAK_OVERLOAD EncodedData< const QByteArray & > decode(const QByteArray &ba)
Q_WEAK_OVERLOAD EncodedData< const QByteArray & > operator()(const QByteArray &ba)
QStringDecoder(const char *name, Flags f=Flag::Default)
Creates an decoder object using name and flags.
EncodedData< QByteArrayView > operator()(QByteArrayView ba)
constexpr QStringDecoder() noexcept
Default constructs an decoder.
\inmodule QtCore
QStringEncoder(const char *name, Flags flags=Flag::Default)
Creates an encoder object using name and flags.
constexpr QStringEncoder() noexcept
Default constructs an encoder.
char * appendToBuffer(char *out, QStringView in)
Encodes in and writes the encoded result into the buffer starting at out.
constexpr QStringEncoder(Encoding encoding, Flags flags=Flag::Default)
Creates an encoder object using encoding and flags.
DecodedData< QStringView > operator()(QStringView in)
Converts in and returns the data as a byte array.
qsizetype requiredSpace(qsizetype inputLength) const
Returns the maximum amount of characters required to be able to process inputLength decoded data.
DecodedData< QStringView > encode(QStringView in)
Q_WEAK_OVERLOAD DecodedData< const QString & > encode(const QString &str)
Q_WEAK_OVERLOAD DecodedData< const QString & > operator()(const QString &str)
constexpr QStringEncoder(const Interface *i) noexcept
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
#define Q_WEAK_OVERLOAD
constexpr timespec & operator+=(timespec &t1, const timespec &t2)
Flags
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLfloat GLfloat f
GLenum type
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLenum GLsizei len
GLuint in
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
ptrdiff_t qsizetype
Definition qtypes.h:70
#define decode(x)
#define encode(x)
QByteArray ba
[0]
QTextStream out(stdout)
[7]