Cbor: Add overloads for QStringView

Change-Id: I8d22d0741c4d3852b438b12099f81ed87244871d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ulf Hermann 2019-08-13 16:42:48 +02:00
parent 4c6e549b25
commit c8c724a3ce
3 changed files with 55 additions and 26 deletions

View File

@ -915,7 +915,7 @@ 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)
Q_NEVER_INLINE void QCborContainerPrivate::appendAsciiString(QStringView s)
{
qsizetype len = s.size();
QtCbor::Element e;
@ -926,7 +926,7 @@ Q_NEVER_INLINE void QCborContainerPrivate::appendAsciiString(const QString &s)
char *ptr = data.data() + e.value + sizeof(ByteData);
uchar *l = reinterpret_cast<uchar *>(ptr);
const ushort *uc = (const ushort *)s.unicode();
const ushort *uc = (const ushort *)s.utf16();
qt_to_latin1_unchecked(l, uc, len);
}
@ -1646,13 +1646,23 @@ QCborValue::QCborValue(const QByteArray &ba)
container->ref.storeRelaxed(1);
}
#if QT_STRINGVIEW_LEVEL < 2
/*!
Creates a QCborValue with string value \a s. The value can later be
retrieved using toString().
\sa toString(), isString(), isByteArray()
*/
QCborValue::QCborValue(const QString &s)
QCborValue::QCborValue(const QString &s) : QCborValue(qToStringViewIgnoringNull(s)) {}
#endif
/*!
Creates a QCborValue with string value \a s. The value can later be
retrieved using toString().
\sa toString(), isString(), isByteArray()
*/
QCborValue::QCborValue(QStringView s)
: n(0), container(new QCborContainerPrivate), t(String)
{
container->append(s);

View File

@ -143,7 +143,10 @@ public:
QCborValue(QCborSimpleType st) : t(type_helper(st)) {}
QCborValue(const QByteArray &ba);
#if QT_STRINGVIEW_LEVEL < 2
QCborValue(const QString &s);
#endif
QCborValue(QStringView s);
QCborValue(QLatin1String s);
#ifndef QT_NO_CAST_FROM_ASCII
QT_ASCII_CAST_WARN QCborValue(const char *s) : QCborValue(QString::fromUtf8(s)) {}

View File

@ -245,13 +245,21 @@ public:
appendByteData(s.latin1(), s.size(), QCborValue::String,
QtCbor::Element::StringIsAscii);
}
void appendAsciiString(const QString &s);
void appendAsciiString(QStringView s);
#if QT_STRINGVIEW_LEVEL < 2
void append(const QString &s)
{
append(qToStringViewIgnoringNull(s));
}
#endif
void append(QStringView s)
{
if (QtPrivate::isAscii(s))
appendAsciiString(s);
else
appendByteData(reinterpret_cast<const char *>(s.constData()), s.size() * 2,
appendByteData(reinterpret_cast<const char *>(s.utf16()), s.size() * 2,
QCborValue::String, QtCbor::Element::StringIsUtf16);
}
void append(const QCborValue &v)
@ -345,33 +353,41 @@ public:
return e;
}
bool stringEqualsElement(qsizetype idx, QLatin1String s) const
static int compareUtf8(const QtCbor::ByteData *b, const QLatin1String &s)
{
const auto &e = elements.at(idx);
if (e.type != QCborValue::String)
return false;
const QtCbor::ByteData *b = byteData(idx);
if (!b)
return s.isEmpty();
if (e.flags & QtCbor::Element::StringIsUtf16)
return QtPrivate::compareStrings(b->asStringView(), s) == 0;
return QUtf8::compareUtf8(b->byte(), b->len, s) == 0;
return QUtf8::compareUtf8(b->byte(), b->len, s);
}
bool stringEqualsElement(qsizetype idx, const QString &s) const
{
const auto &e = elements.at(idx);
if (e.type != QCborValue::String)
return false;
const QtCbor::ByteData *b = byteData(idx);
static int compareUtf8(const QtCbor::ByteData *b, QStringView s)
{
return QUtf8::compareUtf8(b->byte(), b->len, s.data(), s.size());
}
template<typename String>
int stringCompareElement(const QtCbor::Element &e, String s) const
{
if (e.type != QCborValue::String)
return int(e.type) - int(QCborValue::String);
const QtCbor::ByteData *b = byteData(e);
if (!b)
return s.isEmpty();
return s.isEmpty() ? 0 : -1;
if (e.flags & QtCbor::Element::StringIsUtf16)
return QtPrivate::compareStrings(b->asStringView(), s) == 0;
return QUtf8::compareUtf8(b->byte(), b->len, s.data(), s.size()) == 0;
return QtPrivate::compareStrings(b->asStringView(), s);
return compareUtf8(b, s);
}
template<typename String>
bool stringEqualsElement(const QtCbor::Element &e, String s) const
{
return stringCompareElement(e, s) == 0;
}
template<typename String>
bool stringEqualsElement(qsizetype idx, String s) const
{
return stringEqualsElement(elements.at(idx), s);
}
static int compareElement_helper(const QCborContainerPrivate *c1, QtCbor::Element e1,