diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-04 13:17:07 +0100 |
---|---|---|
committer | Jamey Hicks <jamey.hicks@nokia.com> | 2012-01-04 21:07:09 +0100 |
commit | dcf8f7c16a1b309efbecde384990580514cdfdc0 (patch) | |
tree | 597d4bdf11a140a888af69f3667cd90896cdbd9f | |
parent | d769c6d22ef74579bbb73a9b0fe669c5a7384fec (diff) |
Simplify the String/Latin1String classes
String and Latin1String are now laid out as the
binary data, so we can directly cast memory to
these classes.
Change-Id: Ief229f8d79241f68bb167bde7a4fab6fdfd152a9
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
-rw-r--r-- | src/qjson_p.h | 169 |
1 files changed, 64 insertions, 105 deletions
diff --git a/src/qjson_p.h b/src/qjson_p.h index 545a7dd..4014dbd 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -59,6 +59,7 @@ #include <qjsonarray.h> #include <qbasicatomic.h> #include <qstring.h> +#include <qendian.h> /* This defines a binary data structure for Json data. The data structure is optimised for fast reading and minimum allocations. @@ -117,7 +118,7 @@ struct Object; struct Value; struct Entry; -const uint QBJS_Tag = ('q' << 24) | ('b' << 16) | ('j' << 8) | 's'; +const uint QBJS_Tag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24); static inline int alignedSize(int size) { return (size + 3) & ~3; } @@ -143,25 +144,6 @@ static inline int qStringSize(const QString &string, bool compress) return alignedSize(l); } -static inline void copyString(char *dest, const QString &str, bool compress) -{ - if (compress) { - *((ushort *)dest) = str.length(); - uchar *d = (uchar *)dest + sizeof(ushort); - const ushort *uc = (const ushort *)str.unicode(); - for (int i = 0; i < str.length(); ++i) - *d++ = uc[i]; - while ((quintptr)d & 0x3) - *d++ = 0; - } else { - *((int *)dest) = str.length(); - ushort *d = (ushort *)(dest + sizeof(int)); - memcpy(d, str.constData(), str.length()*sizeof(ushort)); - if (str.length() & 1) - d[str.length()] = 0; - } -} - // returns INT_MAX if it can't compress it into 28 bits static inline int compressedNumber(double d) @@ -195,109 +177,94 @@ struct Latin1String; struct String { - String(const ushort *utf16, int length) - : length(length), utf16(utf16) {} - int length; - const ushort *utf16; + String(const char *data) { d = (Data *)data; } + + struct Data { + int length; + ushort utf16[1]; + }; + + Data *d; - inline bool equals(const ushort *s, int slen) const + inline String &operator=(const QString &str) { - if (slen != length) - return false; - int l = length; - const ushort *a = utf16; - const ushort *b = s; - while (l-- && *a == *b) - a++,b++; - return (l == -1); + d->length = str.length(); + memcpy(d->utf16, str.unicode(), d->length*sizeof(ushort)); + if (d->length & 1) + d->utf16[d->length] = 0; + return *this; } - inline bool equals(const uchar *s, int slen) const - { - if (slen != length) + bool operator ==(const QString &str) { + int slen = str.length(); + const ushort *s = (const ushort *)str.constData(); + if (slen != d->length) return false; - int l = length; - const ushort *a = utf16; - const uchar *b = s; + int l = d->length; + const ushort *a = d->utf16; + const ushort *b = s; while (l-- && *a == *b) a++,b++; return (l == -1); } - - bool operator ==(const QString &str) { - return equals((const ushort *)str.constData(), str.length()); - } bool operator !=(const QString &str) { - return !equals((const ushort *)str.constData(), str.length()); - } - bool operator ==(const String &str) { - return equals(str.utf16, str.length); + return !operator ==(str); } - bool operator !=(const String &str) { - return !equals(str.utf16, str.length); - } - inline bool operator ==(const Latin1String &str); - inline bool operator !=(const Latin1String &str); }; struct Latin1String { - Latin1String(const uchar *latin1, int length) - : length(length), latin1(latin1) {} - short length; - const uchar *latin1; + Latin1String(const char *data) { d = (Data *)data; } - inline bool equals(const ushort *s, int slen) const + struct Data { + short length; + uchar latin1[1]; + }; + Data *d; + + inline Latin1String &operator=(const QString &str) { - if (slen != length) - return false; - int l = length; - const uchar *a = latin1; - const ushort *b = s; - while (l-- && *a == *b) - a++,b++; - return (l == -1); + d->length = str.length(); + uchar *l = d->latin1; + const ushort *uc = (const ushort *)str.unicode(); + for (int i = 0; i < str.length(); ++i) + *l++ = uc[i]; + while ((quintptr)l & 0x3) + *l++ = 0; + return *this; } - inline bool equals(const uchar *s, int slen) const - { - if (slen != length) + bool operator ==(const QString &str) { + int slen = str.length(); + const ushort *s = (const ushort *)str.constData(); + if (slen != d->length) return false; - int l = length; - const uchar *a = latin1; - const uchar *b = s; + int l = d->length; + const uchar *a = d->latin1; + const ushort *b = s; while (l-- && *a == *b) a++,b++; return (l == -1); } - - bool operator ==(const QString &str) { - return equals((const ushort *)str.constData(), str.length()); - } bool operator !=(const QString &str) { - return !equals((const ushort *)str.constData(), str.length()); - } - bool operator ==(const String &str) { - return equals(str.utf16, str.length); - } - bool operator !=(const String &str) { - return !equals(str.utf16, str.length); - } - bool operator ==(const Latin1String &str) { - return equals(str.latin1, str.length); - } - bool operator !=(const Latin1String &str) { - return !equals(str.latin1, str.length); + return !operator ==(str); } }; -inline bool String::operator ==(const Latin1String &str) { - return equals(str.latin1, str.length); -} -inline bool String::operator !=(const Latin1String &str) { - return !equals(str.latin1, str.length); + +static inline void copyString(char *dest, const QString &str, bool compress) +{ + if (compress) { + Latin1String string(dest); + string = str; + } else { + String string(dest); + string = str; + } } + + struct Base { uint size; @@ -404,14 +371,12 @@ struct Entry { String shallowKey() const { Q_ASSERT(!value.latinKey); - int length = *(int *) ((const char *)this + sizeof(Entry)); - return String(keyData(), length); + return String((const char *)this + sizeof(Entry)); } Latin1String shallowLatin1Key() const { Q_ASSERT(value.latinKey); - int length = *(ushort *) ((const char *)this + sizeof(Entry)); - return Latin1String(latin1KeyData(), length); + return Latin1String((const char *)this + sizeof(Entry)); } QString key() const { @@ -474,19 +439,13 @@ inline QString Value::toString(const Base *b) const inline String Value::asString(const Base *b) const { Q_ASSERT(type == StringValue && !latinOrIntValue); - char *d = data(b); - int l = *(int *)d; - const ushort *c = (const ushort *)(d + sizeof(int)); - return String(c, l); + return String(data(b)); } inline Latin1String Value::asLatin1String(const Base *b) const { Q_ASSERT(type == StringValue && latinOrIntValue); - char *d = data(b); - ushort l = *(ushort *)d; - const uchar *c = (const uchar *)(d + sizeof(ushort)); - return Latin1String(c, l); + return Latin1String(data(b)); } inline Base *Value::objectOrArray(const Base *b) const |