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