QCborValue: store US-ASCII strings as 8-bit

They're easy to convert back to UTF-16, their length is the same, they
occupy half the memory and they're easy to encode into CBOR (no
transformation necessary).

The code was copied from QJsonPrivate::Latin1String::operator=().

Change-Id: I56b444f9d6274221a3b7fffd150c52bcb6c97f37
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Thiago Macieira 2018-01-22 19:29:04 -08:00
parent 40d528d9fd
commit 48d6990e41
2 changed files with 27 additions and 3 deletions

View File

@ -48,6 +48,7 @@
#include <qlocale.h>
#include <private/qnumeric_p.h>
#include <qscopedvaluerollback.h>
#include <private/qsimd_p.h>
#include <qstack.h>
#include <new>
@ -1082,6 +1083,24 @@ void QCborContainerPrivate::replaceAt_complex(Element &e, const QCborValue &valu
}
}
// in qstring.cpp
void qt_to_latin1_unchecked(uchar *dst, const ushort *uc, qsizetype len);
Q_NEVER_INLINE void QCborContainerPrivate::appendAsciiString(const QString &s)
{
qsizetype len = s.size();
QtCbor::Element e;
e.value = addByteData(nullptr, len);
e.type = QCborValue::String;
e.flags = Element::HasByteData | Element::StringIsAscii;
elements.append(e);
char *ptr = data.data() + e.value + sizeof(ByteData);
uchar *l = reinterpret_cast<uchar *>(ptr);
const ushort *uc = (const ushort *)s.unicode();
qt_to_latin1_unchecked(l, uc, len);
}
QT_WARNING_DISABLE_MSVC(4146) // unary minus operator applied to unsigned type, result still unsigned
static int compareContainer(const QCborContainerPrivate *c1, const QCborContainerPrivate *c2);
static int compareElementNoData(const Element &e1, const Element &e2)

View File

@ -153,7 +153,8 @@ public:
char *ptr = data.begin() + offset;
auto b = new (ptr) QtCbor::ByteData;
b->len = len;
memcpy(b->byte(), block, len);
if (block)
memcpy(b->byte(), block, len);
return offset;
}
@ -239,10 +240,14 @@ public:
appendByteData(s.latin1(), s.size(), QCborValue::String,
QtCbor::Element::StringIsAscii);
}
void appendAsciiString(const QString &s);
void append(const QString &s)
{
appendByteData(reinterpret_cast<const char *>(s.constData()), s.size() * 2,
QCborValue::String, QtCbor::Element::StringIsUtf16);
if (QtPrivate::isAscii(s))
appendAsciiString(s);
else
appendByteData(reinterpret_cast<const char *>(s.constData()), s.size() * 2,
QCborValue::String, QtCbor::Element::StringIsUtf16);
}
void append(const QCborValue &v)
{