summaryrefslogtreecommitdiffstats
path: root/src/corelib/codecs/qutfcodec.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2015-11-04 12:50:31 +0100
committerMarc Mutz <marc.mutz@kdab.com>2015-11-19 11:30:37 +0000
commitdb9a0befca2415574e3bef8a0e07cf3fef62361c (patch)
tree317f2d3da7a829ed00608e2a7dbc7f18814e1d39 /src/corelib/codecs/qutfcodec.cpp
parent110e82f5e916b6171ca257777ce5cbbbceb35714 (diff)
Add a QUtf8::convertToUnicode() overload that operates on an existing buffer
... and therefore doesn't need to allocate and thus can be marked as nothrow. Technically, the function doesn't have a wide contract, because it has the precondition that the buffer pointed to by the first argument needs to be large enough to hold the result. But that precondition can't be checked from within the function, so no failure can be generated for it and thus the nothrow guarantee is acceptable (and desireable). Change-Id: Iaf6ea6788ef6b4bbb6d8de59a3d0b14d66582307 Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/codecs/qutfcodec.cpp')
-rw-r--r--src/corelib/codecs/qutfcodec.cpp29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/corelib/codecs/qutfcodec.cpp b/src/corelib/codecs/qutfcodec.cpp
index a64b3d167e..a8a0687d7a 100644
--- a/src/corelib/codecs/qutfcodec.cpp
+++ b/src/corelib/codecs/qutfcodec.cpp
@@ -256,8 +256,32 @@ QString QUtf8::convertToUnicode(const char *chars, int len)
// The table holds for invalid sequences too: we'll insert one replacement char
// per invalid byte.
QString result(len, Qt::Uninitialized);
+ QChar *data = const_cast<QChar*>(result.constData()); // we know we're not shared
+ const QChar *end = convertToUnicode(data, chars, len);
+ result.truncate(end - data);
+ return result;
+}
- ushort *dst = reinterpret_cast<ushort *>(const_cast<QChar *>(result.constData()));
+/*!
+ \since 5.7
+ \overload
+
+ Converts the UTF-8 sequence of \a len octets beginning at \a chars to
+ a sequence of QChar starting at \a buffer. The buffer is expected to be
+ large enough to hold the result. An upper bound for the size of the
+ buffer is \a len QChars.
+
+ If, during decoding, an error occurs, a QChar::ReplacementCharacter is
+ written.
+
+ Returns a pointer to one past the last QChar written.
+
+ This function never throws.
+*/
+
+QChar *QUtf8::convertToUnicode(QChar *buffer, const char *chars, int len) Q_DECL_NOTHROW
+{
+ ushort *dst = reinterpret_cast<ushort *>(buffer);
const uchar *src = reinterpret_cast<const uchar *>(chars);
const uchar *end = src + len;
@@ -288,8 +312,7 @@ QString QUtf8::convertToUnicode(const char *chars, int len)
}
}
- result.truncate(dst - reinterpret_cast<const ushort *>(result.constData()));
- return result;
+ return reinterpret_cast<QChar *>(dst);
}
QString QUtf8::convertToUnicode(const char *chars, int len, QTextCodec::ConverterState *state)