From 48d6990e418cffb09cd76c0d48b8ffa63490d6cf Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 22 Jan 2018 19:29:04 -0800 Subject: 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 Reviewed-by: Thiago Macieira --- src/corelib/serialization/qcborvalue.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/corelib/serialization/qcborvalue.cpp') diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp index 96d3feaf1b..970ae23352 100644 --- a/src/corelib/serialization/qcborvalue.cpp +++ b/src/corelib/serialization/qcborvalue.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include @@ -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(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) -- cgit v1.2.3