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.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// Qt-Security score:critical reason:data-parser
4
5#if 0
6// keep existing syncqt header working after the move of the class
7// into qstringconverter_base
8#pragma qt_class(QStringConverter)
9#endif
10
11#ifndef QSTRINGCONVERTER_H
12#define QSTRINGCONVERTER_H
13
14#include <QtCore/qstringconverter_base.h>
15#include <QtCore/qstring.h>
16#include <QtCore/qstringbuilder.h>
17
18QT_BEGIN_NAMESPACE
19
20class QStringEncoder : public QStringConverter
21{
22protected:
23 constexpr explicit QStringEncoder(const Interface *i) noexcept
24 : QStringConverter(i)
25 {}
26public:
27 constexpr QStringEncoder() noexcept
28 : QStringConverter()
29 {}
30 constexpr explicit QStringEncoder(Encoding encoding, Flags flags = Flag::Default)
31 : QStringConverter(encoding, flags)
32 {}
33 explicit QStringEncoder(QAnyStringView name, Flags flags = Flag::Default)
34 : QStringConverter(name, flags)
35 {}
36
37 template<typename T>
38 struct DecodedData
39 {
40 QStringEncoder *encoder;
41 T data;
42 operator QByteArray() const { return encoder->encodeAsByteArray(data); }
43 };
44 Q_WEAK_OVERLOAD
45 DecodedData<const QString &> operator()(const QString &str)
46 { return DecodedData<const QString &>{this, str}; }
47 DecodedData<QStringView> operator()(QStringView in)
48 { return DecodedData<QStringView>{this, in}; }
49 Q_WEAK_OVERLOAD
50 DecodedData<const QString &> encode(const QString &str)
51 { return DecodedData<const QString &>{this, str}; }
52 DecodedData<QStringView> encode(QStringView in)
53 { return DecodedData<QStringView>{this, in}; }
54
55 qsizetype requiredSpace(qsizetype inputLength) const
56 { return iface ? iface->fromUtf16Len(inputLength) : 0; }
57 char *appendToBuffer(char *out, QStringView in)
58 {
59 if (!iface) {
60 state.invalidChars = 1;
61 return out;
62 }
63 return iface->fromUtf16(out, in, &state);
64 }
65
66 using FinalizeResult = FinalizeResultChar<char>;
67 Q_REQUIRED_RESULT
68 Q_CORE_EXPORT FinalizeResult finalize(char *out, qsizetype maxlen);
69 Q_REQUIRED_RESULT
70 FinalizeResult finalize() { return finalize(nullptr, 0); }
71
72private:
73 QByteArray invalidateAndReturnNull()
74 {
75 // ensure that hasError returns true
76 state.invalidChars = 1;
77 return QByteArray();
78 }
79 QByteArray encodeAsByteArrayImpl(QStringView in)
80 {
81 QByteArray result(iface->fromUtf16Len(in.size()), Qt::Uninitialized);
82 char *out = result.data();
83 out = iface->fromUtf16(out, in, &state);
84 result.truncate(out - result.constData());
85 return result;
86 }
87 QByteArray encodeAsByteArray(QStringView in)
88 {
89 return iface ? encodeAsByteArrayImpl(in) : invalidateAndReturnNull();
90 }
91};
92
94{
95protected:
96 constexpr explicit QStringDecoder(const Interface *i) noexcept
98 {}
99public:
100 constexpr explicit QStringDecoder(Encoding encoding, Flags flags = Flag::Default)
102 {}
103 constexpr QStringDecoder() noexcept
105 {}
106 explicit QStringDecoder(QAnyStringView name, Flags f = Flag::Default)
108 {}
109
110 template<typename T>
119 { return EncodedData<const QByteArray &>{this, ba}; }
124 { return EncodedData<const QByteArray &>{this, ba}; }
127
131 {
132 if (!iface) {
134 return out;
135 }
136 return iface->toUtf16(out, ba, &state);
137 }
138 char16_t *appendToBuffer(char16_t *out, QByteArrayView ba)
139 { return reinterpret_cast<char16_t *>(appendToBuffer(reinterpret_cast<QChar *>(out), ba)); }
140
141
144 FinalizeResultQChar finalize(QChar *out, qsizetype maxlen)
145 {
146 auto r = finalize(reinterpret_cast<char16_t *>(out), maxlen);
147 return { {}, reinterpret_cast<QChar *>(r.next), r.invalidChars, r.error };
148 }
149 Q_REQUIRED_RESULT
150 Q_CORE_EXPORT FinalizeResult finalize(char16_t *out, qsizetype maxlen);
151 Q_REQUIRED_RESULT
152 FinalizeResult finalize()
153 {
154 return finalize(static_cast<char16_t *>(nullptr), 0);
155 }
156
158
159private:
160 QString invalidateAndReturnNull()
161 {
162 // ensure that hasError returns true
163 state.invalidChars = 1;
164 return QString();
165 }
166 QString decodeAsStringImpl(QByteArrayView in)
167 {
168 QString result(iface->toUtf16Len(in.size()), Qt::Uninitialized);
169 const QChar *out = iface->toUtf16(result.data(), in, &state);
170 result.truncate(out - result.constData());
171 return result;
172 }
173 QString decodeAsString(QByteArrayView in)
174 {
175 return iface ? decodeAsStringImpl(in) : invalidateAndReturnNull();
176 }
177};
178
179
180#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
181template <typename T>
182struct QConcatenable<QStringDecoder::EncodedData<T>>
183 : private QAbstractConcatenable
184{
185 typedef QChar type;
186 typedef QString ConvertTo;
187 enum { ExactSize = false };
188 static qsizetype size(const QStringDecoder::EncodedData<T> &s) { return s.decoder->requiredSpace(s.data.size()); }
189 static inline void appendTo(const QStringDecoder::EncodedData<T> &s, QChar *&out)
190 {
191 out = s.decoder->appendToBuffer(out, s.data);
192 }
193};
194
195template <typename T>
196struct QConcatenable<QStringEncoder::DecodedData<T>>
197 : private QAbstractConcatenable
198{
199 typedef char type;
200 typedef QByteArray ConvertTo;
201 enum { ExactSize = false };
202 static qsizetype size(const QStringEncoder::DecodedData<T> &s) { return s.encoder->requiredSpace(s.data.size()); }
203 static inline void appendTo(const QStringEncoder::DecodedData<T> &s, char *&out)
204 {
205 out = s.encoder->appendToBuffer(out, s.data);
206 }
207};
208
209template <typename T>
210QString &operator+=(QString &a, const QStringDecoder::EncodedData<T> &b)
211{
212 qsizetype len = a.size() + QConcatenable<QStringDecoder::EncodedData<T>>::size(b);
213 a.reserve(len);
214 QChar *it = a.data() + a.size();
215 QConcatenable<QStringDecoder::EncodedData<T>>::appendTo(b, it);
216 a.resize(qsizetype(it - a.constData())); //may be smaller than len
217 return a;
218}
219
220template <typename T>
221QByteArray &operator+=(QByteArray &a, const QStringEncoder::DecodedData<T> &b)
222{
223 qsizetype len = a.size() + QConcatenable<QStringEncoder::DecodedData<T>>::size(b);
224 a.reserve(len);
225 char *it = a.data() + a.size();
226 QConcatenable<QStringEncoder::DecodedData<T>>::appendTo(b, it);
227 a.resize(qsizetype(it - a.constData())); //may be smaller than len
228 return a;
229}
230#endif
231
232template <typename InputIterator>
233void QString::assign_helper_char8(InputIterator first, InputIterator last)
234{
235 static_assert(!QString::is_contiguous_iterator_v<InputIterator>,
236 "Internal error: Should have been handed over to the QAnyStringView overload."
237 );
238
239 using ValueType = typename std::iterator_traits<InputIterator>::value_type;
240 constexpr bool IsFwdIt = std::is_convertible_v<
241 typename std::iterator_traits<InputIterator>::iterator_category,
242 std::forward_iterator_tag
243 >;
244
245 resize(0);
246 // In case of not being shared, there is the possibility of having free space at begin
247 // even after the resize to zero.
248 if (const auto offset = d.freeSpaceAtBegin())
249 d.setBegin(d.begin() - offset);
250
251 if constexpr (IsFwdIt)
252 reserve(static_cast<qsizetype>(std::distance(first, last)));
253
254 auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
255 auto availableCapacity = d.constAllocatedCapacity();
256 auto *dst = d.data();
257 auto *dend = d.data() + availableCapacity;
258
259 while (true) {
260 if (first == last) { // ran out of input elements
261 Q_ASSERT(!std::less<>{}(dend, dst));
262 d.size = dst - d.begin();
263 return;
264 }
265 const ValueType next = *first; // decays proxies, if any
266 const auto chunk = QUtf8StringView(&next, 1);
267 // UTF-8 characters can have a maximum size of 4 bytes and may result in a surrogate
268 // pair of UTF-16 code units. In the input-iterator case, we don't know the size
269 // and would need to always reserve space for 2 code units. To keep our promise
270 // of 'not allocating if it fits', we have to pre-check this condition.
271 // We know that it fits in the forward-iterator case.
272 if constexpr (!IsFwdIt) {
273 constexpr qsizetype Pair = 2;
274 char16_t buf[Pair];
275 const qptrdiff n = toUtf16.appendToBuffer(buf, chunk) - buf;
276 if (dend - dst < n) { // ran out of allocated memory
277 const auto offset = dst - d.begin();
278 reallocData(d.constAllocatedCapacity() + Pair, QArrayData::Grow);
279 // update the pointers since we've re-allocated
280 availableCapacity = d.constAllocatedCapacity();
281 dst = d.data() + offset;
282 dend = d.data() + availableCapacity;
283 }
284 dst = std::copy_n(buf, n, dst);
285 } else { // take the fast path
286 dst = toUtf16.appendToBuffer(dst, chunk);
287 }
288 ++first;
289 }
290}
291
292QT_END_NAMESPACE
293
294#endif
\inmodule QtCore
constexpr QStringDecoder(Encoding encoding, Flags flags=Flag::Default)
Creates an decoder object using encoding and flags.
FinalizeResultQChar finalize(QChar *out, qsizetype maxlen)
constexpr QStringDecoder(const Interface *i) noexcept
constexpr QStringDecoder() noexcept
Default constructs an decoder.