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 {}
36
37 template<typename T>
39 {
42 operator QByteArray() const { return encoder->encodeAsByteArray(data); }
43 };
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}; }
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
56 { return iface ? iface->fromUtf16Len(inputLength) : 0; }
58 {
59 if (!iface) {
61 return out;
62 }
63 return iface->fromUtf16(out, in, &state);
64 }
65private:
66 QByteArray encodeAsByteArray(QStringView in)
67 {
68 if (!iface) {
69 // ensure that hasError returns true
71 return {};
72 }
74 char *out = result.data();
76 result.truncate(out - result.constData());
77 return result;
78 }
79
80};
81
83{
84protected:
85 constexpr explicit QStringDecoder(const Interface *i) noexcept
87 {}
88public:
89 constexpr explicit QStringDecoder(Encoding encoding, Flags flags = Flag::Default)
90 : QStringConverter(encoding, flags)
91 {}
92 constexpr QStringDecoder() noexcept
94 {}
98
99 template<typename T>
101 {
104 operator QString() const { return decoder->decodeAsString(data); }
105 };
107 EncodedData<const QByteArray &> operator()(const QByteArray &ba)
108 { return EncodedData<const QByteArray &>{this, ba}; }
109 EncodedData<QByteArrayView> operator()(QByteArrayView ba)
110 { return EncodedData<QByteArrayView>{this, ba}; }
112 EncodedData<const QByteArray &> decode(const QByteArray &ba)
113 { return EncodedData<const QByteArray &>{this, ba}; }
114 EncodedData<QByteArrayView> decode(QByteArrayView ba)
115 { return EncodedData<QByteArrayView>{this, ba}; }
116
118 { return iface ? iface->toUtf16Len(inputLength) : 0; }
120 {
121 if (!iface) {
123 return out;
124 }
125 return iface->toUtf16(out, ba, &state);
126 }
127 char16_t *appendToBuffer(char16_t *out, QByteArrayView ba)
128 { return reinterpret_cast<char16_t *>(appendToBuffer(reinterpret_cast<QChar *>(out), ba)); }
129
130 Q_CORE_EXPORT static QStringDecoder decoderForHtml(QByteArrayView data);
131
132private:
133 QString decodeAsString(QByteArrayView in)
134 {
135 if (!iface) {
136 // ensure that hasError returns true
138 return {};
139 }
141 const QChar *out = iface->toUtf16(result.data(), in, &state);
142 result.truncate(out - result.constData());
143 return result;
144 }
145};
146
147
148#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
149template <typename T>
150struct QConcatenable<QStringDecoder::EncodedData<T>>
151 : private QAbstractConcatenable
152{
153 typedef QChar type;
154 typedef QString ConvertTo;
155 enum { ExactSize = false };
156 static qsizetype size(const QStringDecoder::EncodedData<T> &s) { return s.decoder->requiredSpace(s.data.size()); }
157 static inline void appendTo(const QStringDecoder::EncodedData<T> &s, QChar *&out)
158 {
159 out = s.decoder->appendToBuffer(out, s.data);
160 }
161};
162
163template <typename T>
164struct QConcatenable<QStringEncoder::DecodedData<T>>
165 : private QAbstractConcatenable
166{
167 typedef char type;
168 typedef QByteArray ConvertTo;
169 enum { ExactSize = false };
170 static qsizetype size(const QStringEncoder::DecodedData<T> &s) { return s.encoder->requiredSpace(s.data.size()); }
171 static inline void appendTo(const QStringEncoder::DecodedData<T> &s, char *&out)
172 {
173 out = s.encoder->appendToBuffer(out, s.data);
174 }
175};
176
177template <typename T>
179{
180 qsizetype len = a.size() + QConcatenable<QStringDecoder::EncodedData<T>>::size(b);
181 a.reserve(len);
182 QChar *it = a.data() + a.size();
183 QConcatenable<QStringDecoder::EncodedData<T>>::appendTo(b, it);
184 a.resize(qsizetype(it - a.constData())); //may be smaller than len
185 return a;
186}
187
188template <typename T>
190{
191 qsizetype len = a.size() + QConcatenable<QStringEncoder::DecodedData<T>>::size(b);
192 a.reserve(len);
193 char *it = a.data() + a.size();
194 QConcatenable<QStringEncoder::DecodedData<T>>::appendTo(b, it);
195 a.resize(qsizetype(it - a.constData())); //may be smaller than len
196 return a;
197}
198#endif
199
200template <typename InputIterator>
201void QString::assign_helper_char8(InputIterator first, InputIterator last)
202{
203 static_assert(!QString::is_contiguous_iterator_v<InputIterator>,
204 "Internal error: Should have been handed over to the QAnyStringView overload."
205 );
206
207 using ValueType = typename std::iterator_traits<InputIterator>::value_type;
208 constexpr bool IsFwdIt = std::is_convertible_v<
209 typename std::iterator_traits<InputIterator>::iterator_category,
210 std::forward_iterator_tag
211 >;
212
213 resize(0);
214 // In case of not being shared, there is the possibility of having free space at begin
215 // even after the resize to zero.
216 if (const auto offset = d.freeSpaceAtBegin())
217 d.setBegin(d.begin() - offset);
218
219 if constexpr (IsFwdIt)
220 reserve(static_cast<qsizetype>(std::distance(first, last)));
221
223 auto availableCapacity = d.constAllocatedCapacity();
224 auto *dst = d.data();
225 auto *dend = d.data() + availableCapacity;
226
227 while (true) {
228 if (first == last) { // ran out of input elements
229 Q_ASSERT(!std::less<>{}(dend, dst));
230 d.size = dst - d.begin();
231 return;
232 }
233 const ValueType next = *first; // decays proxies, if any
234 const auto chunk = QUtf8StringView(&next, 1);
235 // UTF-8 characters can have a maximum size of 4 bytes and may result in a surrogate
236 // pair of UTF-16 code units. In the input-iterator case, we don't know the size
237 // and would need to always reserve space for 2 code units. To keep our promise
238 // of 'not allocating if it fits', we have to pre-check this condition.
239 // We know that it fits in the forward-iterator case.
240 if constexpr (!IsFwdIt) {
241 constexpr qsizetype Pair = 2;
242 char16_t buf[Pair];
243 const qptrdiff n = toUtf16.appendToBuffer(buf, chunk) - buf;
244 if (dend - dst < n) { // ran out of allocated memory
245 const auto offset = dst - d.begin();
246 reallocData(d.constAllocatedCapacity() + Pair, QArrayData::Grow);
247 // update the pointers since we've re-allocated
248 availableCapacity = d.constAllocatedCapacity();
249 dst = d.data() + offset;
250 dend = d.data() + availableCapacity;
251 }
252 dst = std::copy_n(buf, n, dst);
253 } else { // take the fast path
254 dst = toUtf16.appendToBuffer(dst, chunk);
255 }
256 ++first;
257 }
258}
259
261
262#endif
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
qsizetype size() const
Definition qset.h:51
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.
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(QAnyStringView name, Flags f=Flag::Default)
Creates an decoder object using name and flags.
constexpr QStringDecoder() noexcept
Default constructs an decoder.
\inmodule QtCore
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)
QStringEncoder(QAnyStringView name, Flags flags=Flag::Default)
Creates an encoder object using name and flags.
DecodedData< QStringView > encode(QStringView in)
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:2670
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