diff options
author | Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> | 2011-04-27 13:53:28 (GMT) |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> | 2011-04-27 14:49:22 (GMT) |
commit | feabda665de62a0f6a82d831b45926697f30b45b (patch) | |
tree | bef0c40e7e6043130532b60060bd835e06784634 /src | |
parent | 4671c273edb87e55436dd3bf0b371267c5e34ff7 (diff) | |
download | Qt-feabda665de62a0f6a82d831b45926697f30b45b.zip Qt-feabda665de62a0f6a82d831b45926697f30b45b.tar.gz Qt-feabda665de62a0f6a82d831b45926697f30b45b.tar.bz2 |
Added QStringRef::toLatin1 and QStringRef::toUtf8
These helper functions make it convenient to avoid making an unnecessary
copy of the string before converting it to a QByteArray. The current
most obvious way to do this would be:
// QStringRef text
QByteArray latin1 = text.toString().toLatin1();
Though the copy can also be avoided by doing:
const QString textData =
QString::fromRawData(text.unicode(), text.size());
QByteArray latin1 = textData.toLatin1();
Now the faster method can be achieved using the new obvious way:
QByteArray latin1 = text.toLatin1();
Reviewed-by: Thiago Macieira
Reviewed-by: Robin Burchell
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 41 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 2 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 5493ba9..7569555 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8014,6 +8014,47 @@ QString QStringRef::toString() const { return QString(m_string->unicode() + m_position, m_size); } +/*! + Returns a Latin-1 representation of the string reference as a QByteArray. + + The returned byte array is undefined if the string reference contains + non-Latin1 characters. Those characters may be suppressed or replaced with a + question mark. + + \sa QString::toLatin1(), toUtf8() + \since 4.8 +*/ +QByteArray QStringRef::toLatin1() const +{ + if (!m_string) + return QByteArray(); + return toLatin1_helper(m_string->unicode() + m_position, m_size); +} + +/*! + Returns a UTF-8 representation of the string reference as a QByteArray. + + UTF-8 is a Unicode codec and can represent all characters in a Unicode + string like QString. + + However, in the Unicode range, there are certain codepoints that are not + considered characters. The Unicode standard reserves the last two + codepoints in each Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, + U+2FFFE, etc.), as well as 16 codepoints in the range U+FDD0..U+FDDF, + inclusive, as non-characters. If any of those appear in the string, they + may be discarded and will not appear in the UTF-8 representation, or they + may be replaced by one or more replacement characters. + + \sa QString::toUtf8(), toLatin1(), QTextCodec + \since 4.8 +*/ +QByteArray QStringRef::toUtf8() const +{ + if (isNull()) + return QByteArray(); + return QUtf8::convertFromUnicode(m_string->unicode() + m_position, m_size, 0); +} + /*! \relates QStringRef diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 66cfa74..2b3026c 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1167,6 +1167,8 @@ public: inline void clear() { m_string = 0; m_position = m_size = 0; } QString toString() const; + QByteArray toLatin1() const; + QByteArray toUtf8() const; inline bool isEmpty() const { return m_size == 0; } inline bool isNull() const { return m_string == 0 || m_string->isNull(); } |