Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkJSON.h" |
| 9 | |
Florin Malita | d7bfcaf | 2018-06-14 18:03:26 -0400 | [diff] [blame] | 10 | #include "SkMalloc.h" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 11 | #include "SkStream.h" |
Hal Canary | 8b68110 | 2018-09-05 22:32:41 -0400 | [diff] [blame^] | 12 | #include "SkStreamPriv.h" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 13 | #include "SkString.h" |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 14 | |
| 15 | #include <cmath> |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 16 | #include <tuple> |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | namespace skjson { |
| 20 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 21 | // #define SK_JSON_REPORT_ERRORS |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 22 | |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 23 | static_assert( sizeof(Value) == 8, ""); |
| 24 | static_assert(alignof(Value) == 8, ""); |
| 25 | |
| 26 | static constexpr size_t kRecAlign = alignof(Value); |
| 27 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 28 | void Value::init_tagged(Tag t) { |
| 29 | memset(fData8, 0, sizeof(fData8)); |
| 30 | fData8[Value::kTagOffset] = SkTo<uint8_t>(t); |
| 31 | SkASSERT(this->getTag() == t); |
| 32 | } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 33 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 34 | // Pointer values store a type (in the upper kTagBits bits) and a pointer. |
| 35 | void Value::init_tagged_pointer(Tag t, void* p) { |
| 36 | *this->cast<uintptr_t>() = reinterpret_cast<uintptr_t>(p); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 37 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 38 | if (sizeof(Value) == sizeof(uintptr_t)) { |
| 39 | // For 64-bit, we rely on the pointer upper bits being unused/zero. |
| 40 | SkASSERT(!(fData8[kTagOffset] & kTagMask)); |
| 41 | fData8[kTagOffset] |= SkTo<uint8_t>(t); |
| 42 | } else { |
| 43 | // For 32-bit, we need to zero-initialize the upper 32 bits |
| 44 | SkASSERT(sizeof(Value) == sizeof(uintptr_t) * 2); |
| 45 | this->cast<uintptr_t>()[kTagOffset >> 2] = 0; |
| 46 | fData8[kTagOffset] = SkTo<uint8_t>(t); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 49 | SkASSERT(this->getTag() == t); |
| 50 | SkASSERT(this->ptr<void>() == p); |
| 51 | } |
| 52 | |
| 53 | NullValue::NullValue() { |
| 54 | this->init_tagged(Tag::kNull); |
| 55 | SkASSERT(this->getTag() == Tag::kNull); |
| 56 | } |
| 57 | |
| 58 | BoolValue::BoolValue(bool b) { |
| 59 | this->init_tagged(Tag::kBool); |
| 60 | *this->cast<bool>() = b; |
| 61 | SkASSERT(this->getTag() == Tag::kBool); |
| 62 | } |
| 63 | |
| 64 | NumberValue::NumberValue(int32_t i) { |
| 65 | this->init_tagged(Tag::kInt); |
| 66 | *this->cast<int32_t>() = i; |
| 67 | SkASSERT(this->getTag() == Tag::kInt); |
| 68 | } |
| 69 | |
| 70 | NumberValue::NumberValue(float f) { |
| 71 | this->init_tagged(Tag::kFloat); |
| 72 | *this->cast<float>() = f; |
| 73 | SkASSERT(this->getTag() == Tag::kFloat); |
| 74 | } |
| 75 | |
| 76 | // Vector recs point to externally allocated slabs with the following layout: |
| 77 | // |
| 78 | // [size_t n] [REC_0] ... [REC_n-1] [optional extra trailing storage] |
| 79 | // |
| 80 | // Long strings use extra_alloc_size == 1 to store the \0 terminator. |
| 81 | // |
| 82 | template <typename T, size_t extra_alloc_size = 0> |
| 83 | static void* MakeVector(const void* src, size_t size, SkArenaAlloc& alloc) { |
| 84 | // The Ts are already in memory, so their size should be safe. |
| 85 | const auto total_size = sizeof(size_t) + size * sizeof(T) + extra_alloc_size; |
| 86 | auto* size_ptr = reinterpret_cast<size_t*>(alloc.makeBytesAlignedTo(total_size, kRecAlign)); |
Florin Malita | 28f5dd8 | 2018-06-14 13:56:53 -0400 | [diff] [blame] | 87 | |
Florin Malita | d7bfcaf | 2018-06-14 18:03:26 -0400 | [diff] [blame] | 88 | *size_ptr = size; |
| 89 | sk_careful_memcpy(size_ptr + 1, src, size * sizeof(T)); |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 90 | |
| 91 | return size_ptr; |
| 92 | } |
| 93 | |
| 94 | ArrayValue::ArrayValue(const Value* src, size_t size, SkArenaAlloc& alloc) { |
| 95 | this->init_tagged_pointer(Tag::kArray, MakeVector<Value>(src, size, alloc)); |
| 96 | SkASSERT(this->getTag() == Tag::kArray); |
| 97 | } |
| 98 | |
| 99 | // Strings have two flavors: |
| 100 | // |
| 101 | // -- short strings (len <= 7) -> these are stored inline, in the record |
| 102 | // (one byte reserved for null terminator/type): |
| 103 | // |
| 104 | // [str] [\0]|[max_len - actual_len] |
| 105 | // |
| 106 | // Storing [max_len - actual_len] allows the 'len' field to double-up as a |
| 107 | // null terminator when size == max_len (this works 'cause kShortString == 0). |
| 108 | // |
| 109 | // -- long strings (len > 7) -> these are externally allocated vectors (VectorRec<char>). |
| 110 | // |
| 111 | // The string data plus a null-char terminator are copied over. |
| 112 | // |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 113 | namespace { |
| 114 | |
| 115 | // An internal string builder with a fast 8 byte short string load path |
| 116 | // (for the common case where the string is not at the end of the stream). |
| 117 | class FastString final : public Value { |
| 118 | public: |
| 119 | FastString(const char* src, size_t size, const char* eos, SkArenaAlloc& alloc) { |
| 120 | SkASSERT(src <= eos); |
| 121 | |
| 122 | if (size > kMaxInlineStringSize) { |
| 123 | this->initLongString(src, size, alloc); |
| 124 | SkASSERT(this->getTag() == Tag::kString); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | static_assert(static_cast<uint8_t>(Tag::kShortString) == 0, "please don't break this"); |
| 129 | static_assert(sizeof(Value) == 8, ""); |
| 130 | |
| 131 | // TODO: LIKELY |
| 132 | if (src + 7 <= eos) { |
| 133 | this->initFastShortString(src, size); |
| 134 | } else { |
| 135 | this->initShortString(src, size); |
| 136 | } |
| 137 | |
| 138 | SkASSERT(this->getTag() == Tag::kShortString); |
| 139 | } |
| 140 | |
| 141 | private: |
Florin Malita | d7bfcaf | 2018-06-14 18:03:26 -0400 | [diff] [blame] | 142 | static constexpr size_t kMaxInlineStringSize = sizeof(Value) - 1; |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 143 | |
| 144 | void initLongString(const char* src, size_t size, SkArenaAlloc& alloc) { |
| 145 | SkASSERT(size > kMaxInlineStringSize); |
| 146 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 147 | this->init_tagged_pointer(Tag::kString, MakeVector<char, 1>(src, size, alloc)); |
| 148 | |
| 149 | auto* data = this->cast<VectorValue<char, Value::Type::kString>>()->begin(); |
| 150 | const_cast<char*>(data)[size] = '\0'; |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 153 | void initShortString(const char* src, size_t size) { |
| 154 | SkASSERT(size <= kMaxInlineStringSize); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 155 | |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 156 | this->init_tagged(Tag::kShortString); |
| 157 | sk_careful_memcpy(this->cast<char>(), src, size); |
| 158 | // Null terminator provided by init_tagged() above (fData8 is zero-initialized). |
| 159 | } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 160 | |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 161 | void initFastShortString(const char* src, size_t size) { |
| 162 | SkASSERT(size <= kMaxInlineStringSize); |
| 163 | |
| 164 | // Load 8 chars and mask out the tag and \0 terminator. |
| 165 | uint64_t* s64 = this->cast<uint64_t>(); |
| 166 | memcpy(s64, src, 8); |
| 167 | |
| 168 | #if defined(SK_CPU_LENDIAN) |
| 169 | *s64 &= 0x00ffffffffffffffULL >> ((kMaxInlineStringSize - size) * 8); |
| 170 | #else |
| 171 | static_assert(false, "Big-endian builds are not supported at this time."); |
| 172 | #endif |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | } // namespace |
| 177 | |
| 178 | StringValue::StringValue(const char* src, size_t size, SkArenaAlloc& alloc) { |
| 179 | new (this) FastString(src, size, src, alloc); |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 180 | } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 181 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 182 | ObjectValue::ObjectValue(const Member* src, size_t size, SkArenaAlloc& alloc) { |
| 183 | this->init_tagged_pointer(Tag::kObject, MakeVector<Member>(src, size, alloc)); |
| 184 | SkASSERT(this->getTag() == Tag::kObject); |
| 185 | } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 186 | |
| 187 | |
| 188 | // Boring public Value glue. |
| 189 | |
Mike Reed | 0edad3e | 2018-08-13 17:18:47 -0400 | [diff] [blame] | 190 | static int inline_strcmp(const char a[], const char b[]) { |
| 191 | for (;;) { |
| 192 | char c = *a++; |
| 193 | if (c == 0) { |
| 194 | break; |
| 195 | } |
| 196 | if (c != *b++) { |
| 197 | return 1; |
| 198 | } |
| 199 | } |
| 200 | return *b != 0; |
| 201 | } |
| 202 | |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 203 | const Value& ObjectValue::operator[](const char* key) const { |
| 204 | // Reverse search for duplicates resolution (policy: return last). |
| 205 | const auto* begin = this->begin(); |
| 206 | const auto* member = this->end(); |
| 207 | |
| 208 | while (member > begin) { |
| 209 | --member; |
Mike Reed | 0edad3e | 2018-08-13 17:18:47 -0400 | [diff] [blame] | 210 | if (0 == inline_strcmp(key, member->fKey.as<StringValue>().begin())) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 211 | return member->fValue; |
| 212 | } |
| 213 | } |
| 214 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 215 | static const Value g_null = NullValue(); |
| 216 | return g_null; |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | namespace { |
| 220 | |
| 221 | // Lexer/parser inspired by rapidjson [1], sajson [2] and pjson [3]. |
| 222 | // |
| 223 | // [1] https://github.com/Tencent/rapidjson/ |
| 224 | // [2] https://github.com/chadaustin/sajson |
| 225 | // [3] https://pastebin.com/hnhSTL3h |
| 226 | |
| 227 | |
| 228 | // bit 0 (0x01) - plain ASCII string character |
| 229 | // bit 1 (0x02) - whitespace |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 230 | // bit 2 (0x04) - string terminator (" \0 [control chars] **AND } ]** <- see matchString notes) |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 231 | // bit 3 (0x08) - 0-9 |
| 232 | // bit 4 (0x10) - 0-9 e E . |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 233 | // bit 5 (0x20) - scope terminator (} ]) |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 234 | static constexpr uint8_t g_token_flags[256] = { |
| 235 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 236 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 4, 4, 6, 4, 4, // 0 |
| 237 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 1 |
| 238 | 3, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x11,1, // 2 |
| 239 | 0x19,0x19,0x19,0x19,0x19,0x19,0x19,0x19, 0x19,0x19, 1, 1, 1, 1, 1, 1, // 3 |
| 240 | 1, 1, 1, 1, 1, 0x11,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 241 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,0x25, 1, 1, // 5 |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 242 | 1, 1, 1, 1, 1, 0x11,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 243 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,0x25, 1, 1, // 7 |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 244 | |
| 245 | // 128-255 |
| 246 | 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, |
| 247 | 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, |
| 248 | 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, |
| 249 | 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 |
| 250 | }; |
| 251 | |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 252 | static inline bool is_ws(char c) { return g_token_flags[static_cast<uint8_t>(c)] & 0x02; } |
| 253 | static inline bool is_eostring(char c) { return g_token_flags[static_cast<uint8_t>(c)] & 0x04; } |
| 254 | static inline bool is_digit(char c) { return g_token_flags[static_cast<uint8_t>(c)] & 0x08; } |
| 255 | static inline bool is_numeric(char c) { return g_token_flags[static_cast<uint8_t>(c)] & 0x10; } |
| 256 | static inline bool is_eoscope(char c) { return g_token_flags[static_cast<uint8_t>(c)] & 0x20; } |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 257 | |
| 258 | static inline const char* skip_ws(const char* p) { |
| 259 | while (is_ws(*p)) ++p; |
| 260 | return p; |
| 261 | } |
| 262 | |
| 263 | static inline float pow10(int32_t exp) { |
| 264 | static constexpr float g_pow10_table[63] = |
| 265 | { |
| 266 | 1.e-031f, 1.e-030f, 1.e-029f, 1.e-028f, 1.e-027f, 1.e-026f, 1.e-025f, 1.e-024f, |
| 267 | 1.e-023f, 1.e-022f, 1.e-021f, 1.e-020f, 1.e-019f, 1.e-018f, 1.e-017f, 1.e-016f, |
| 268 | 1.e-015f, 1.e-014f, 1.e-013f, 1.e-012f, 1.e-011f, 1.e-010f, 1.e-009f, 1.e-008f, |
| 269 | 1.e-007f, 1.e-006f, 1.e-005f, 1.e-004f, 1.e-003f, 1.e-002f, 1.e-001f, 1.e+000f, |
| 270 | 1.e+001f, 1.e+002f, 1.e+003f, 1.e+004f, 1.e+005f, 1.e+006f, 1.e+007f, 1.e+008f, |
| 271 | 1.e+009f, 1.e+010f, 1.e+011f, 1.e+012f, 1.e+013f, 1.e+014f, 1.e+015f, 1.e+016f, |
| 272 | 1.e+017f, 1.e+018f, 1.e+019f, 1.e+020f, 1.e+021f, 1.e+022f, 1.e+023f, 1.e+024f, |
| 273 | 1.e+025f, 1.e+026f, 1.e+027f, 1.e+028f, 1.e+029f, 1.e+030f, 1.e+031f |
| 274 | }; |
| 275 | |
| 276 | static constexpr int32_t k_exp_offset = SK_ARRAY_COUNT(g_pow10_table) / 2; |
| 277 | |
| 278 | // We only support negative exponents for now. |
| 279 | SkASSERT(exp <= 0); |
| 280 | |
| 281 | return (exp >= -k_exp_offset) ? g_pow10_table[exp + k_exp_offset] |
| 282 | : std::pow(10.0f, static_cast<float>(exp)); |
| 283 | } |
| 284 | |
| 285 | class DOMParser { |
| 286 | public: |
| 287 | explicit DOMParser(SkArenaAlloc& alloc) |
| 288 | : fAlloc(alloc) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 289 | fValueStack.reserve(kValueStackReserve); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 292 | const Value parse(const char* p, size_t size) { |
| 293 | if (!size) { |
| 294 | return this->error(NullValue(), p, "invalid empty input"); |
| 295 | } |
| 296 | |
| 297 | const char* p_stop = p + size - 1; |
| 298 | |
| 299 | // We're only checking for end-of-stream on object/array close('}',']'), |
| 300 | // so we must trim any whitespace from the buffer tail. |
| 301 | while (p_stop > p && is_ws(*p_stop)) --p_stop; |
| 302 | |
| 303 | SkASSERT(p_stop >= p && p_stop < p + size); |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 304 | if (!is_eoscope(*p_stop)) { |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 305 | return this->error(NullValue(), p_stop, "invalid top-level value"); |
| 306 | } |
| 307 | |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 308 | p = skip_ws(p); |
| 309 | |
| 310 | switch (*p) { |
| 311 | case '{': |
| 312 | goto match_object; |
| 313 | case '[': |
| 314 | goto match_array; |
| 315 | default: |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 316 | return this->error(NullValue(), p, "invalid top-level value"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | match_object: |
| 320 | SkASSERT(*p == '{'); |
| 321 | p = skip_ws(p + 1); |
| 322 | |
| 323 | this->pushObjectScope(); |
| 324 | |
| 325 | if (*p == '}') goto pop_object; |
| 326 | |
| 327 | // goto match_object_key; |
| 328 | match_object_key: |
| 329 | p = skip_ws(p); |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 330 | if (*p != '"') return this->error(NullValue(), p, "expected object key"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 331 | |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 332 | p = this->matchString(p, p_stop, [this](const char* key, size_t size, const char* eos) { |
| 333 | this->pushObjectKey(key, size, eos); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 334 | }); |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 335 | if (!p) return NullValue(); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 336 | |
| 337 | p = skip_ws(p); |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 338 | if (*p != ':') return this->error(NullValue(), p, "expected ':' separator"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 339 | |
| 340 | ++p; |
| 341 | |
| 342 | // goto match_value; |
| 343 | match_value: |
| 344 | p = skip_ws(p); |
| 345 | |
| 346 | switch (*p) { |
| 347 | case '\0': |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 348 | return this->error(NullValue(), p, "unexpected input end"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 349 | case '"': |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 350 | p = this->matchString(p, p_stop, [this](const char* str, size_t size, const char* eos) { |
| 351 | this->pushString(str, size, eos); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 352 | }); |
| 353 | break; |
| 354 | case '[': |
| 355 | goto match_array; |
| 356 | case 'f': |
| 357 | p = this->matchFalse(p); |
| 358 | break; |
| 359 | case 'n': |
| 360 | p = this->matchNull(p); |
| 361 | break; |
| 362 | case 't': |
| 363 | p = this->matchTrue(p); |
| 364 | break; |
| 365 | case '{': |
| 366 | goto match_object; |
| 367 | default: |
| 368 | p = this->matchNumber(p); |
| 369 | break; |
| 370 | } |
| 371 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 372 | if (!p) return NullValue(); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 373 | |
| 374 | // goto match_post_value; |
| 375 | match_post_value: |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 376 | SkASSERT(!this->inTopLevelScope()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 377 | |
| 378 | p = skip_ws(p); |
| 379 | switch (*p) { |
| 380 | case ',': |
| 381 | ++p; |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 382 | if (this->inObjectScope()) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 383 | goto match_object_key; |
| 384 | } else { |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 385 | SkASSERT(this->inArrayScope()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 386 | goto match_value; |
| 387 | } |
| 388 | case ']': |
| 389 | goto pop_array; |
| 390 | case '}': |
| 391 | goto pop_object; |
| 392 | default: |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 393 | return this->error(NullValue(), p - 1, "unexpected value-trailing token"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // unreachable |
| 397 | SkASSERT(false); |
| 398 | |
| 399 | pop_object: |
| 400 | SkASSERT(*p == '}'); |
| 401 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 402 | if (this->inArrayScope()) { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 403 | return this->error(NullValue(), p, "unexpected object terminator"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | this->popObjectScope(); |
| 407 | |
| 408 | // goto pop_common |
| 409 | pop_common: |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 410 | SkASSERT(is_eoscope(*p)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 411 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 412 | if (this->inTopLevelScope()) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 413 | SkASSERT(fValueStack.size() == 1); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 414 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 415 | // Success condition: parsed the top level element and reached the stop token. |
| 416 | return p == p_stop |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 417 | ? fValueStack.front() |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 418 | : this->error(NullValue(), p + 1, "trailing root garbage"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Florin Malita | 587f5a9 | 2018-06-15 09:21:36 -0400 | [diff] [blame] | 421 | if (p == p_stop) { |
| 422 | return this->error(NullValue(), p, "unexpected end-of-input"); |
| 423 | } |
| 424 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 425 | ++p; |
| 426 | |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 427 | goto match_post_value; |
| 428 | |
| 429 | match_array: |
| 430 | SkASSERT(*p == '['); |
| 431 | p = skip_ws(p + 1); |
| 432 | |
| 433 | this->pushArrayScope(); |
| 434 | |
| 435 | if (*p != ']') goto match_value; |
| 436 | |
| 437 | // goto pop_array; |
| 438 | pop_array: |
| 439 | SkASSERT(*p == ']'); |
| 440 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 441 | if (this->inObjectScope()) { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 442 | return this->error(NullValue(), p, "unexpected array terminator"); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | this->popArrayScope(); |
| 446 | |
| 447 | goto pop_common; |
| 448 | |
| 449 | SkASSERT(false); |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 450 | return NullValue(); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 453 | std::tuple<const char*, const SkString> getError() const { |
| 454 | return std::make_tuple(fErrorToken, fErrorMessage); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | private: |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 458 | SkArenaAlloc& fAlloc; |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 459 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 460 | // Pending values stack. |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 461 | static constexpr size_t kValueStackReserve = 256; |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 462 | std::vector<Value> fValueStack; |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 463 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 464 | // Tracks the current object/array scope, as an index into fStack: |
| 465 | // |
| 466 | // - for objects: fScopeIndex = (index of first value in scope) |
| 467 | // - for arrays : fScopeIndex = -(index of first value in scope) |
| 468 | // |
| 469 | // fScopeIndex == 0 IFF we are at the top level (no current/active scope). |
| 470 | intptr_t fScopeIndex = 0; |
| 471 | |
| 472 | // Error reporting. |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 473 | const char* fErrorToken = nullptr; |
| 474 | SkString fErrorMessage; |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 475 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 476 | bool inTopLevelScope() const { return fScopeIndex == 0; } |
| 477 | bool inObjectScope() const { return fScopeIndex > 0; } |
| 478 | bool inArrayScope() const { return fScopeIndex < 0; } |
| 479 | |
| 480 | // Helper for masquerading raw primitive types as Values (bypassing tagging, etc). |
| 481 | template <typename T> |
| 482 | class RawValue final : public Value { |
| 483 | public: |
| 484 | explicit RawValue(T v) { |
| 485 | static_assert(sizeof(T) <= sizeof(Value), ""); |
| 486 | *this->cast<T>() = v; |
| 487 | } |
| 488 | |
| 489 | T operator *() const { return *this->cast<T>(); } |
| 490 | }; |
| 491 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 492 | template <typename VectorT> |
| 493 | void popScopeAsVec(size_t scope_start) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 494 | SkASSERT(scope_start > 0); |
| 495 | SkASSERT(scope_start <= fValueStack.size()); |
| 496 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 497 | using T = typename VectorT::ValueT; |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 498 | static_assert( sizeof(T) >= sizeof(Value), ""); |
| 499 | static_assert( sizeof(T) % sizeof(Value) == 0, ""); |
| 500 | static_assert(alignof(T) == alignof(Value), ""); |
| 501 | |
| 502 | const auto scope_count = fValueStack.size() - scope_start, |
| 503 | count = scope_count / (sizeof(T) / sizeof(Value)); |
| 504 | SkASSERT(scope_count % (sizeof(T) / sizeof(Value)) == 0); |
| 505 | |
| 506 | const auto* begin = reinterpret_cast<const T*>(fValueStack.data() + scope_start); |
| 507 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 508 | // Restore the previous scope index from saved placeholder value, |
| 509 | // and instantiate as a vector of values in scope. |
| 510 | auto& placeholder = fValueStack[scope_start - 1]; |
| 511 | fScopeIndex = *static_cast<RawValue<intptr_t>&>(placeholder); |
| 512 | placeholder = VectorT(begin, count, fAlloc); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 513 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 514 | // Drop the (consumed) values in scope. |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 515 | fValueStack.resize(scope_start); |
| 516 | } |
| 517 | |
| 518 | void pushObjectScope() { |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 519 | // Save a scope index now, and then later we'll overwrite this value as the Object itself. |
| 520 | fValueStack.push_back(RawValue<intptr_t>(fScopeIndex)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 521 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 522 | // New object scope. |
| 523 | fScopeIndex = SkTo<intptr_t>(fValueStack.size()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void popObjectScope() { |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 527 | SkASSERT(this->inObjectScope()); |
| 528 | this->popScopeAsVec<ObjectValue>(SkTo<size_t>(fScopeIndex)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 529 | |
| 530 | SkDEBUGCODE( |
| 531 | const auto& obj = fValueStack.back().as<ObjectValue>(); |
| 532 | SkASSERT(obj.is<ObjectValue>()); |
| 533 | for (const auto& member : obj) { |
| 534 | SkASSERT(member.fKey.is<StringValue>()); |
| 535 | } |
| 536 | ) |
| 537 | } |
| 538 | |
| 539 | void pushArrayScope() { |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 540 | // Save a scope index now, and then later we'll overwrite this value as the Array itself. |
| 541 | fValueStack.push_back(RawValue<intptr_t>(fScopeIndex)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 542 | |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 543 | // New array scope. |
| 544 | fScopeIndex = -SkTo<intptr_t>(fValueStack.size()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void popArrayScope() { |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 548 | SkASSERT(this->inArrayScope()); |
| 549 | this->popScopeAsVec<ArrayValue>(SkTo<size_t>(-fScopeIndex)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 550 | |
| 551 | SkDEBUGCODE( |
| 552 | const auto& arr = fValueStack.back().as<ArrayValue>(); |
| 553 | SkASSERT(arr.is<ArrayValue>()); |
| 554 | ) |
| 555 | } |
| 556 | |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 557 | void pushObjectKey(const char* key, size_t size, const char* eos) { |
Florin Malita | 2e5c1ae | 2018-06-20 14:23:23 -0400 | [diff] [blame] | 558 | SkASSERT(this->inObjectScope()); |
| 559 | SkASSERT(fValueStack.size() >= SkTo<size_t>(fScopeIndex)); |
| 560 | SkASSERT(!((fValueStack.size() - SkTo<size_t>(fScopeIndex)) & 1)); |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 561 | this->pushString(key, size, eos); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void pushTrue() { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 565 | fValueStack.push_back(BoolValue(true)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void pushFalse() { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 569 | fValueStack.push_back(BoolValue(false)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void pushNull() { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 573 | fValueStack.push_back(NullValue()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 576 | void pushString(const char* s, size_t size, const char* eos) { |
| 577 | fValueStack.push_back(FastString(s, size, eos, fAlloc)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void pushInt32(int32_t i) { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 581 | fValueStack.push_back(NumberValue(i)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void pushFloat(float f) { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 585 | fValueStack.push_back(NumberValue(f)); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | template <typename T> |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 589 | T error(T&& ret_val, const char* p, const char* msg) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 590 | #if defined(SK_JSON_REPORT_ERRORS) |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 591 | fErrorToken = p; |
| 592 | fErrorMessage.set(msg); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 593 | #endif |
| 594 | return ret_val; |
| 595 | } |
| 596 | |
| 597 | const char* matchTrue(const char* p) { |
| 598 | SkASSERT(p[0] == 't'); |
| 599 | |
| 600 | if (p[1] == 'r' && p[2] == 'u' && p[3] == 'e') { |
| 601 | this->pushTrue(); |
| 602 | return p + 4; |
| 603 | } |
| 604 | |
| 605 | return this->error(nullptr, p, "invalid token"); |
| 606 | } |
| 607 | |
| 608 | const char* matchFalse(const char* p) { |
| 609 | SkASSERT(p[0] == 'f'); |
| 610 | |
| 611 | if (p[1] == 'a' && p[2] == 'l' && p[3] == 's' && p[4] == 'e') { |
| 612 | this->pushFalse(); |
| 613 | return p + 5; |
| 614 | } |
| 615 | |
| 616 | return this->error(nullptr, p, "invalid token"); |
| 617 | } |
| 618 | |
| 619 | const char* matchNull(const char* p) { |
| 620 | SkASSERT(p[0] == 'n'); |
| 621 | |
| 622 | if (p[1] == 'u' && p[2] == 'l' && p[3] == 'l') { |
| 623 | this->pushNull(); |
| 624 | return p + 4; |
| 625 | } |
| 626 | |
| 627 | return this->error(nullptr, p, "invalid token"); |
| 628 | } |
| 629 | |
| 630 | template <typename MatchFunc> |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 631 | const char* matchString(const char* p, const char* p_stop, MatchFunc&& func) { |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 632 | SkASSERT(*p == '"'); |
| 633 | const auto* s_begin = p + 1; |
| 634 | |
| 635 | // TODO: unescape |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 636 | |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 637 | do { |
| 638 | // Consume string chars. |
| 639 | for (p = p + 1; !is_eostring(*p); ++p); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 640 | |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 641 | if (*p == '"') { |
| 642 | // Valid string found. |
Florin Malita | fb3beb0 | 2018-06-18 22:25:31 -0400 | [diff] [blame] | 643 | func(s_begin, p - s_begin, p_stop); |
Florin Malita | 0052a31 | 2018-06-15 16:42:09 -0400 | [diff] [blame] | 644 | return p + 1; |
| 645 | } |
| 646 | |
| 647 | // End-of-scope chars are special: we use them to tag the end of the input. |
| 648 | // Thus they cannot be consumed indiscriminately -- we need to check if we hit the |
| 649 | // end of the input. To that effect, we treat them as string terminators above, |
| 650 | // then we catch them here. |
| 651 | } while (is_eoscope(*p) && (p != p_stop)); // Safe scope terminator char, keep going. |
| 652 | |
| 653 | // Premature end-of-input, or illegal string char. |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 654 | return this->error(nullptr, s_begin - 1, "invalid string"); |
| 655 | } |
| 656 | |
| 657 | const char* matchFastFloatDecimalPart(const char* p, int sign, float f, int exp) { |
| 658 | SkASSERT(exp <= 0); |
| 659 | |
| 660 | for (;;) { |
| 661 | if (!is_digit(*p)) break; |
| 662 | f = f * 10.f + (*p++ - '0'); --exp; |
| 663 | if (!is_digit(*p)) break; |
| 664 | f = f * 10.f + (*p++ - '0'); --exp; |
| 665 | } |
| 666 | |
| 667 | if (is_numeric(*p)) { |
| 668 | SkASSERT(*p == '.' || *p == 'e' || *p == 'E'); |
| 669 | // We either have malformed input, or an (unsupported) exponent. |
| 670 | return nullptr; |
| 671 | } |
| 672 | |
| 673 | this->pushFloat(sign * f * pow10(exp)); |
| 674 | |
| 675 | return p; |
| 676 | } |
| 677 | |
| 678 | const char* matchFastFloatPart(const char* p, int sign, float f) { |
| 679 | for (;;) { |
| 680 | if (!is_digit(*p)) break; |
| 681 | f = f * 10.f + (*p++ - '0'); |
| 682 | if (!is_digit(*p)) break; |
| 683 | f = f * 10.f + (*p++ - '0'); |
| 684 | } |
| 685 | |
| 686 | if (!is_numeric(*p)) { |
| 687 | // Matched (integral) float. |
| 688 | this->pushFloat(sign * f); |
| 689 | return p; |
| 690 | } |
| 691 | |
| 692 | return (*p == '.') ? this->matchFastFloatDecimalPart(p + 1, sign, f, 0) |
| 693 | : nullptr; |
| 694 | } |
| 695 | |
| 696 | const char* matchFast32OrFloat(const char* p) { |
| 697 | int sign = 1; |
| 698 | if (*p == '-') { |
| 699 | sign = -1; |
| 700 | ++p; |
| 701 | } |
| 702 | |
| 703 | const auto* digits_start = p; |
| 704 | |
| 705 | int32_t n32 = 0; |
| 706 | |
| 707 | // This is the largest absolute int32 value we can handle before |
| 708 | // risking overflow *on the next digit* (214748363). |
| 709 | static constexpr int32_t kMaxInt32 = (std::numeric_limits<int32_t>::max() - 9) / 10; |
| 710 | |
| 711 | if (is_digit(*p)) { |
| 712 | n32 = (*p++ - '0'); |
| 713 | for (;;) { |
| 714 | if (!is_digit(*p) || n32 > kMaxInt32) break; |
| 715 | n32 = n32 * 10 + (*p++ - '0'); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if (!is_numeric(*p)) { |
| 720 | // Did we actually match any digits? |
| 721 | if (p > digits_start) { |
| 722 | this->pushInt32(sign * n32); |
| 723 | return p; |
| 724 | } |
| 725 | return nullptr; |
| 726 | } |
| 727 | |
| 728 | if (*p == '.') { |
| 729 | const auto* decimals_start = ++p; |
| 730 | |
| 731 | int exp = 0; |
| 732 | |
| 733 | for (;;) { |
| 734 | if (!is_digit(*p) || n32 > kMaxInt32) break; |
| 735 | n32 = n32 * 10 + (*p++ - '0'); --exp; |
| 736 | if (!is_digit(*p) || n32 > kMaxInt32) break; |
| 737 | n32 = n32 * 10 + (*p++ - '0'); --exp; |
| 738 | } |
| 739 | |
| 740 | if (!is_numeric(*p)) { |
| 741 | // Did we actually match any digits? |
| 742 | if (p > decimals_start) { |
| 743 | this->pushFloat(sign * n32 * pow10(exp)); |
| 744 | return p; |
| 745 | } |
| 746 | return nullptr; |
| 747 | } |
| 748 | |
| 749 | if (n32 > kMaxInt32) { |
| 750 | // we ran out on n32 bits |
| 751 | return this->matchFastFloatDecimalPart(p, sign, n32, exp); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | return this->matchFastFloatPart(p, sign, n32); |
| 756 | } |
| 757 | |
| 758 | const char* matchNumber(const char* p) { |
| 759 | if (const auto* fast = this->matchFast32OrFloat(p)) return fast; |
| 760 | |
| 761 | // slow fallback |
| 762 | char* matched; |
| 763 | float f = strtof(p, &matched); |
| 764 | if (matched > p) { |
| 765 | this->pushFloat(f); |
| 766 | return matched; |
| 767 | } |
| 768 | return this->error(nullptr, p, "invalid numeric token"); |
| 769 | } |
| 770 | }; |
| 771 | |
| 772 | void Write(const Value& v, SkWStream* stream) { |
| 773 | switch (v.getType()) { |
| 774 | case Value::Type::kNull: |
| 775 | stream->writeText("null"); |
| 776 | break; |
| 777 | case Value::Type::kBool: |
| 778 | stream->writeText(*v.as<BoolValue>() ? "true" : "false"); |
| 779 | break; |
| 780 | case Value::Type::kNumber: |
Hal Canary | 8b68110 | 2018-09-05 22:32:41 -0400 | [diff] [blame^] | 781 | SkWStreamWriteScalarAsText(stream, *v.as<NumberValue>()); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 782 | break; |
| 783 | case Value::Type::kString: |
| 784 | stream->writeText("\""); |
| 785 | stream->writeText(v.as<StringValue>().begin()); |
| 786 | stream->writeText("\""); |
| 787 | break; |
| 788 | case Value::Type::kArray: { |
| 789 | const auto& array = v.as<ArrayValue>(); |
| 790 | stream->writeText("["); |
| 791 | bool first_value = true; |
| 792 | for (const auto& v : array) { |
| 793 | if (!first_value) stream->writeText(","); |
| 794 | Write(v, stream); |
| 795 | first_value = false; |
| 796 | } |
| 797 | stream->writeText("]"); |
| 798 | break; |
| 799 | } |
| 800 | case Value::Type::kObject: |
| 801 | const auto& object = v.as<ObjectValue>(); |
| 802 | stream->writeText("{"); |
| 803 | bool first_member = true; |
| 804 | for (const auto& member : object) { |
| 805 | SkASSERT(member.fKey.getType() == Value::Type::kString); |
| 806 | if (!first_member) stream->writeText(","); |
| 807 | Write(member.fKey, stream); |
| 808 | stream->writeText(":"); |
| 809 | Write(member.fValue, stream); |
| 810 | first_member = false; |
| 811 | } |
| 812 | stream->writeText("}"); |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | } // namespace |
| 818 | |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 819 | SkString Value::toString() const { |
| 820 | SkDynamicMemoryWStream wstream; |
| 821 | Write(*this, &wstream); |
| 822 | const auto data = wstream.detachAsData(); |
| 823 | // TODO: is there a better way to pass data around without copying? |
| 824 | return SkString(static_cast<const char*>(data->data()), data->size()); |
| 825 | } |
| 826 | |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 827 | static constexpr size_t kMinChunkSize = 4096; |
| 828 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 829 | DOM::DOM(const char* data, size_t size) |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 830 | : fAlloc(kMinChunkSize) { |
| 831 | DOMParser parser(fAlloc); |
| 832 | |
Florin Malita | fedfd54 | 2018-06-14 15:03:21 -0400 | [diff] [blame] | 833 | fRoot = parser.parse(data, size); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | void DOM::write(SkWStream* stream) const { |
Florin Malita | ae25279 | 2018-06-14 11:24:50 -0400 | [diff] [blame] | 837 | Write(fRoot, stream); |
Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | } // namespace skjson |