Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#include <QtCore/qstringbuilder.h>
17
19
21{
22protected:
23 constexpr explicit QStringEncoder(const Interface *i) noexcept
25 {}
26public:
27 constexpr QStringEncoder() noexcept
29 {}
30 constexpr explicit QStringEncoder(Encoding encoding, Flags flags = Flag::Default)
31 : QStringConverter(encoding, flags)
32 {}
33 explicit QStringEncoder(const char *name, Flags flags = Flag::Default)
35 {}
37 : QStringEncoder(name.toLatin1().constData(), flags)
38 {}
39
40 template<typename T>
42 {
45 operator QByteArray() const { return encoder->encodeAsByteArray(data); }
46 };
48 DecodedData<const QString &> operator()(const QString &str)
49 { return DecodedData<const QString &>{this, str}; }
50 DecodedData<QStringView> operator()(QStringView in)
51 { return DecodedData<QStringView>{this, in}; }
53 DecodedData<const QString &> encode(const QString &str)
54 { return DecodedData<const QString &>{this, str}; }
55 DecodedData<QStringView> encode(QStringView in)
56 { return DecodedData<QStringView>{this, in}; }
57
59 { return iface ? iface->fromUtf16Len(inputLength) : 0; }
61 {
62 if (!iface) {
64 return out;
65 }
66 return iface->fromUtf16(out, in, &state);
67 }
68private:
69 QByteArray encodeAsByteArray(QStringView in)
70 {
71 if (!iface) {
72 // ensure that hasError returns true
74 return {};
75 }
77 char *out = result.data();
79 result.truncate(out - result.constData());
80 return result;
81 }
82
83};
84
86{
87protected:
88 constexpr explicit QStringDecoder(const Interface *i) noexcept
90 {}
91public:
92 constexpr explicit QStringDecoder(Encoding encoding, Flags flags = Flag::Default)
93 : QStringConverter(encoding, flags)
94 {}
95 constexpr QStringDecoder() noexcept
97 {}
98 explicit QStringDecoder(const char *name, Flags f = Flag::Default)
100 {}
102 : QStringDecoder(name.toLatin1().constData(), f)
103 {}
104
105 template<typename T>
107 {
110 operator QString() const { return decoder->decodeAsString(data); }
111 };
113 EncodedData<const QByteArray &> operator()(const QByteArray &ba)
114 { return EncodedData<const QByteArray &>{this, ba}; }
115 EncodedData<QByteArrayView> operator()(QByteArrayView ba)
116 { return EncodedData<QByteArrayView>{this, ba}; }
118 EncodedData<const QByteArray &> decode(const QByteArray &ba)
119 { return EncodedData<const QByteArray &>{this, ba}; }
120 EncodedData<QByteArrayView> decode(QByteArrayView ba)
121 { return EncodedData<QByteArrayView>{this, ba}; }
122
124 { return iface ? iface->toUtf16Len(inputLength) : 0; }
126 {
127 if (!iface) {
129 return out;
130 }
131 return iface->toUtf16(out, ba, &state);
132 }
133 char16_t *appendToBuffer(char16_t *out, QByteArrayView ba)
134 { return reinterpret_cast<char16_t *>(appendToBuffer(reinterpret_cast<QChar *>(out), ba)); }
135
136 Q_CORE_EXPORT static QStringDecoder decoderForHtml(QByteArrayView data);
137
138private:
139 QString decodeAsString(QByteArrayView in)
140 {
141 if (!iface) {
142 // ensure that hasError returns true
144 return {};
145 }
147 const QChar *out = iface->toUtf16(result.data(), in, &state);
148 result.truncate(out - result.constData());
149 return result;
150 }
151};
152
153
154#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
155template <typename T>
156struct QConcatenable<QStringDecoder::EncodedData<T>>
157 : private QAbstractConcatenable
158{
159 typedef QChar type;
160 typedef QString ConvertTo;
161 enum { ExactSize = false };
162 static qsizetype size(const QStringDecoder::EncodedData<T> &s) { return s.decoder->requiredSpace(s.data.size()); }
163 static inline void appendTo(const QStringDecoder::EncodedData<T> &s, QChar *&out)
164 {
165 out = s.decoder->appendToBuffer(out, s.data);
166 }
167};
168
169template <typename T>
170struct QConcatenable<QStringEncoder::DecodedData<T>>
171 : private QAbstractConcatenable
172{
173 typedef char type;
174 typedef QByteArray ConvertTo;
175 enum { ExactSize = false };
176 static qsizetype size(const QStringEncoder::DecodedData<T> &s) { return s.encoder->requiredSpace(s.data.size()); }
177 static inline void appendTo(const QStringEncoder::DecodedData<T> &s, char *&out)
178 {
179 out = s.encoder->appendToBuffer(out, s.data);
180 }
181};
182
183template <typename T>
185{
186 qsizetype len = a.size() + QConcatenable<QStringDecoder::EncodedData<T>>::size(b);
187 a.reserve(len);
188 QChar *it = a.data() + a.size();
189 QConcatenable<QStringDecoder::EncodedData<T>>::appendTo(b, it);
190 a.resize(qsizetype(it - a.constData())); //may be smaller than len
191 return a;
192}
193
194template <typename T>
196{
197 qsizetype len = a.size() + QConcatenable<QStringEncoder::DecodedData<T>>::size(b);
198 a.reserve(len);
199 char *it = a.data() + a.size();
200 QConcatenable<QStringEncoder::DecodedData<T>>::appendTo(b, it);
201 a.resize(qsizetype(it - a.constData())); //may be smaller than len
202 return a;
203}
204#endif
205
206template <typename InputIterator>
207void QString::assign_helper_char8(InputIterator first, InputIterator last)
208{
209 static_assert(!QString::is_contiguous_iterator_v<InputIterator>,
210 "Internal error: Should have been handed over to the QAnyStringView overload."
211 );
212
213 using ValueType = typename std::iterator_traits<InputIterator>::value_type;
214 constexpr bool IsFwdIt = std::is_convertible_v<
215 typename std::iterator_traits<InputIterator>::iterator_category,
216 std::forward_iterator_tag
217 >;
218
219 resize(0);
220 // In case of not being shared, there is the possibility of having free space at begin
221 // even after the resize to zero.
222 if (const auto offset = d.freeSpaceAtBegin())
223 d.setBegin(d.begin() - offset);
224
225 if constexpr (IsFwdIt)
226 reserve(static_cast<qsizetype>(std::distance(first, last)));
227
229 auto availableCapacity = d.constAllocatedCapacity();
230 auto *dst = d.data();
231 auto *dend = d.data() + availableCapacity;
232
233 while (true) {
234 if (first == last) { // ran out of input elements
235 Q_ASSERT(!std::less<>{}(dend, dst));
236 d.size = dst - d.begin();
237 return;
238 }
239 const ValueType next = *first; // decays proxies, if any
240 const auto chunk = QUtf8StringView(&next, 1);
241 // UTF-8 characters can have a maximum size of 4 bytes and may result in a surrogate
242 // pair of UTF-16 code units. In the input-iterator case, we don't know the size
243 // and would need to always reserve space for 2 code units. To keep our promise
244 // of 'not allocating if it fits', we have to pre-check this condition.
245 // We know that it fits in the forward-iterator case.
246 if constexpr (!IsFwdIt) {
247 constexpr qsizetype Pair = 2;
248 char16_t buf[Pair];
249 const qptrdiff n = toUtf16.appendToBuffer(buf, chunk) - buf;
250 if (dend - dst < n) { // ran out of allocated memory
251 const auto offset = dst - d.begin();
252 reallocData(d.constAllocatedCapacity() + Pair, QArrayData::Grow);
253 // update the pointers since we've re-allocated
254 availableCapacity = d.constAllocatedCapacity();
255 dst = d.data() + offset;
256 dend = d.data() + availableCapacity;
257 }
258 dst = std::copy_n(buf, n, dst);
259 } else { // take the fast path
260 dst = toUtf16.appendToBuffer(dst, chunk);
261 }
262 ++first;
263 }
264}
265
267
268#endif
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
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
Q_WEAK_OVERLOAD EncodedData< const QByteArray & > decode(const QByteArray &ba)
constexpr QStringDecoder(Encoding encoding, Flags flags=Flag::Default)
Creates an decoder object using encoding and flags.
Q_WEAK_OVERLOAD QStringDecoder(const QString &name, Flags f=Flag::Default)
EncodedData< QByteArrayView > operator()(QByteArrayView ba)
Q_WEAK_OVERLOAD EncodedData< const QByteArray & > operator()(const QByteArray &ba)
char16_t * appendToBuffer(char16_t *out, QByteArrayView ba)
EncodedData< QByteArrayView > decode(QByteArrayView ba)
Converts ba and returns a struct that is implicitly convertible to QString.
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...
QStringDecoder(const char *name, Flags f=Flag::Default)
Creates an decoder object using name and flags.
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 a struct that is implicitly convertible to QByteArray.
Q_WEAK_OVERLOAD DecodedData< const QString & > operator()(const QString &str)
DecodedData< QStringView > encode(QStringView in)
Q_WEAK_OVERLOAD QStringEncoder(const QString &name, Flags flags=Flag::Default)
qsizetype requiredSpace(qsizetype inputLength) const
Returns the maximum amount of characters required to be able to process inputLength decoded data.
Q_WEAK_OVERLOAD DecodedData< const QString & > encode(const QString &str)
constexpr QStringEncoder(const Interface *i) noexcept
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void reserve(qsizetype size)
Ensures the string has space for at least size characters.
Definition qstring.h:1325
QString last(qsizetype n) const &
Definition qstring.h:392
void resize(qsizetype size)
Sets the size of the string to size characters.
Definition qstring.cpp:2668
QString str
[2]
QSet< QString >::iterator it
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
#define Q_WEAK_OVERLOAD
constexpr timespec & operator+=(timespec &t1, const timespec &t2)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
Flags
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLenum type
GLenum GLenum dst
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLenum GLuint GLintptr offset
GLuint name
GLint first
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLuint in
GLuint64EXT * result
[6]
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QBasicUtf8StringView< false > QUtf8StringView
Definition qstringfwd.h:46
ptrdiff_t qptrdiff
Definition qtypes.h:164
ptrdiff_t qsizetype
Definition qtypes.h:165
QByteArray ba
[0]
QTextStream out(stdout)
[7]
qsizetype freeSpaceAtBegin() const noexcept
void setBegin(T *begin) noexcept
qsizetype constAllocatedCapacity() const noexcept