diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2018-01-22 19:29:04 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2018-06-18 19:53:24 +0000 |
commit | 48d6990e418cffb09cd76c0d48b8ffa63490d6cf (patch) | |
tree | f426654751b0b68f7a166ce2b66344cebd75193f /src/corelib/serialization | |
parent | 40d528d9fd9a4ce3b97f79b31545e7709a5d3588 (diff) |
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>
Diffstat (limited to 'src/corelib/serialization')
-rw-r--r-- | src/corelib/serialization/qcborvalue.cpp | 19 | ||||
-rw-r--r-- | src/corelib/serialization/qcborvalue_p.h | 11 |
2 files changed, 27 insertions, 3 deletions
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 <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) diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h index 861b91eec0..c80abd68f5 100644 --- a/src/corelib/serialization/qcborvalue_p.h +++ b/src/corelib/serialization/qcborvalue_p.h @@ -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) { |