summaryrefslogtreecommitdiffstats
path: root/src/corelib/json/qjson.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2016-04-28 09:40:49 +0200
committerMarc Mutz <marc.mutz@kdab.com>2016-05-10 21:18:46 +0000
commita5159cc50aa0f8a57b6f736621b359a3bcecbf7e (patch)
treeb563d6cc510c93e62f099e93922e7133c3959758 /src/corelib/json/qjson.cpp
parent5e51b15066eab5fd58f3975e5b7d47d9605ca725 (diff)
QJsonObject: add some overloads taking QLatin1String
QXmlStreamReader also has QLatin1String overloads, which greatly benefits parsers, since the vast majority of keys in both JSON and XML are US-ASCII. This patch adds such an overload to the JSON parser. The value() function is all typical parsers need, so even though many more QJsonObject functions taking QString could benefit from the same treatment, value() is the single most important one for read-only JSON access. Add some more overloads, too, for functions that don't need more internal scaffolding than value(). Requires adding a dummy op[](QL1S) (forwarding to the QString overload) so as not to make QJsonObject json; json[QLatin1String("key")]; // mutable ambiguous between const op[](QL1S) and mutable op[](QString). [ChangeLog][QtCore][QJsonObject] Added value(), op[] const, find(), constFind(), contains() overloads taking QLatin1String. Change-Id: I00883028956ad949ba5ba2b18dd8a6a25ad5085b Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/corelib/json/qjson.cpp')
-rw-r--r--src/corelib/json/qjson.cpp32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/corelib/json/qjson.cpp b/src/corelib/json/qjson.cpp
index 24fcaf0937..bb98e25fa5 100644
--- a/src/corelib/json/qjson.cpp
+++ b/src/corelib/json/qjson.cpp
@@ -179,7 +179,29 @@ void Base::removeItems(int pos, int numItems)
length -= numItems;
}
-int Object::indexOf(const QString &key, bool *exists)
+int Object::indexOf(const QString &key, bool *exists) const
+{
+ int min = 0;
+ int n = length;
+ while (n > 0) {
+ int half = n >> 1;
+ int middle = min + half;
+ if (*entryAt(middle) >= key) {
+ n = half;
+ } else {
+ min = middle + 1;
+ n -= half + 1;
+ }
+ }
+ if (min < (int)length && *entryAt(min) == key) {
+ *exists = true;
+ return min;
+ }
+ *exists = false;
+ return min;
+}
+
+int Object::indexOf(QLatin1String key, bool *exists) const
{
int min = 0;
int n = length;
@@ -248,6 +270,14 @@ bool Entry::operator ==(const QString &key) const
return (shallowKey() == key);
}
+bool Entry::operator==(QLatin1String key) const
+{
+ if (value.latinKey)
+ return shallowLatin1Key() == key;
+ else
+ return shallowKey() == key;
+}
+
bool Entry::operator ==(const Entry &other) const
{
if (value.latinKey) {