summaryrefslogtreecommitdiff
path: root/src/corelib/serialization
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-04-23 22:48:37 +0200
committerAhmad Samir <a.samirh78@gmail.com>2023-05-01 21:52:22 +0200
commit5c3888fe10f4a9009b0b8dd071a3649f6c888236 (patch)
tree3854f23331b1450d6b416a23233420c081e04bb4 /src/corelib/serialization
parentb2a95f9eeef5cc34111707c7af50eef512a07035 (diff)
downloadqtbase-5c3888fe10f4a9009b0b8dd071a3649f6c888236.tar.gz
QJsonWriter: general cleanup
Use reinterpret_cast instead of C-style cast. Take by QStringView directly so that we can use QSV::utf16(). Change-Id: I6d30669b0ed4bdf80a63043c364af695fd1d68b5 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/serialization')
-rw-r--r--src/corelib/serialization/qjsonwriter.cpp15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/corelib/serialization/qjsonwriter.cpp b/src/corelib/serialization/qjsonwriter.cpp
index 56c751426d..c9da553a93 100644
--- a/src/corelib/serialization/qjsonwriter.cpp
+++ b/src/corelib/serialization/qjsonwriter.cpp
@@ -22,23 +22,24 @@ static inline uchar hexdig(uint u)
return (u < 0xa ? '0' + u : 'a' + u - 0xa);
}
-static QByteArray escapedString(const QString &s)
+static QByteArray escapedString(QStringView s)
{
// give it a minimum size to ensure the resize() below always adds enough space
QByteArray ba(qMax(s.size(), 16), Qt::Uninitialized);
+ auto ba_const_start = [&]() { return reinterpret_cast<const uchar *>(ba.constData()); };
uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData()));
const uchar *ba_end = cursor + ba.size();
- const char16_t *src = reinterpret_cast<const char16_t *>(s.constBegin());
- const char16_t *const end = reinterpret_cast<const char16_t *>(s.constEnd());
+ const char16_t *src = s.utf16();
+ const char16_t *const end = s.utf16() + s.size();
while (src != end) {
if (cursor >= ba_end - 6) {
// ensure we have enough space
- qptrdiff pos = cursor - (const uchar *)ba.constData();
+ qptrdiff pos = cursor - ba_const_start();
ba.resize(ba.size()*2);
- cursor = (uchar *)ba.data() + pos;
- ba_end = (const uchar *)ba.constData() + ba.size();
+ cursor = reinterpret_cast<uchar *>(ba.data()) + pos;
+ ba_end = ba_const_start() + ba.size();
}
char16_t u = *src++;
@@ -88,7 +89,7 @@ static QByteArray escapedString(const QString &s)
}
}
- ba.resize(cursor - (const uchar *)ba.constData());
+ ba.resize(cursor - ba_const_start());
return ba;
}