diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-11-04 12:50:31 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-11-19 11:30:37 +0000 |
commit | db9a0befca2415574e3bef8a0e07cf3fef62361c (patch) | |
tree | 317f2d3da7a829ed00608e2a7dbc7f18814e1d39 /src/corelib/codecs | |
parent | 110e82f5e916b6171ca257777ce5cbbbceb35714 (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')
-rw-r--r-- | src/corelib/codecs/qutfcodec.cpp | 29 | ||||
-rw-r--r-- | src/corelib/codecs/qutfcodec_p.h | 1 |
2 files changed, 27 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) diff --git a/src/corelib/codecs/qutfcodec_p.h b/src/corelib/codecs/qutfcodec_p.h index d97145c6fc..62540213f9 100644 --- a/src/corelib/codecs/qutfcodec_p.h +++ b/src/corelib/codecs/qutfcodec_p.h @@ -279,6 +279,7 @@ enum DataEndianness struct QUtf8 { + static QChar *convertToUnicode(QChar *, const char *, int) Q_DECL_NOTHROW; static QString convertToUnicode(const char *, int); static QString convertToUnicode(const char *, int, QTextCodec::ConverterState *); static QByteArray convertFromUnicode(const QChar *, int); |