2 * Eddie Kohler, Yandong Mao, Robert Morris
3 * Copyright (c) 2012-2014 President and Fellows of Harvard College
4 * Copyright (c) 2012-2014 Massachusetts Institute of Technology
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Masstree LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Masstree LICENSE file; the license in that file
16 // -*- c-basic-offset: 4 -*-
18 #include "compiler.hh"
25 The Json class represents Json data: null values, booleans, numbers,
26 strings, and combinations of these primitives into arrays and objects.
28 Json objects are not references, and two Json values cannot share
29 subobjects. This differs from Javascript. For example:
32 Json j1 = Json::make_object(), j2 = Json::make_object();
33 j1.set("a", j2); // stores a COPY of j2 in j1
35 assert(j1.unparse() == "{\"a\":{}}");
36 assert(j2.unparse() == "{\"b\":1}");
39 Compare this with the Javascript code:
43 j1.a = j2; // stores a REFERENCE to j2 in j1
45 assert(JSON.stringify(j1) == "{\"a\":{\"b\":1}}");
48 Most Json functions for extracting components and typed values behave
49 liberally. For example, objects silently convert to integers, and
50 extracting properties from non-objects is allowed. This should make it
51 easier to work with untrusted objects. (Json objects often originate
52 from untrusted sources, and may not have the types you expect.) If you
53 prefer an assertion to fail when a Json object has an unexpected type,
54 use the checked <tt>as_</tt> and <code>at</code> functions, rather than
55 the liberal <tt>to_</tt>, <tt>get</tt>, and <tt>operator[]</code>
58 const Json::null_t Json::null;
59 const Json Json::null_json;
60 static const String array_string("[Array]", 7);
61 static const String object_string("[Object]", 8);
65 Json::ArrayJson* Json::ArrayJson::make(int n) {
66 int cap = n < 8 ? 8 : n;
67 char* buf = new char[sizeof(ArrayJson) + cap * sizeof(Json)];
68 return new((void*) buf) ArrayJson(cap);
71 void Json::ArrayJson::destroy(ArrayJson* aj) {
73 for (int i = 0; i != aj->size; ++i)
75 delete[] reinterpret_cast<char*>(aj);
81 Json::ObjectJson::ObjectJson(const ObjectJson &x)
82 : ComplexJson(), os_(x.os_), n_(x.n_), capacity_(x.capacity_),
89 Json::ObjectJson::~ObjectJson()
91 ObjectItem *ob = os_, *oe = ob + n_;
92 for (; ob != oe; ++ob)
95 delete[] reinterpret_cast<char *>(os_);
98 void Json::ObjectJson::grow(bool copy)
100 if (copy && !capacity_)
104 new_capacity = capacity_;
106 new_capacity = capacity_ * 2;
109 ObjectItem *new_os = reinterpret_cast<ObjectItem *>(operator new[](sizeof(ObjectItem) * new_capacity));
110 ObjectItem *ob = os_, *oe = ob + n_;
111 for (ObjectItem *oi = new_os; ob != oe; ++oi, ++ob) {
115 new((void*) oi) ObjectItem(ob->v_.first, ob->v_.second, ob->next_);
117 memcpy(oi, ob, sizeof(ObjectItem));
120 operator delete[](reinterpret_cast<void *>(os_));
122 capacity_ = new_capacity;
125 void Json::ObjectJson::rehash()
127 hash_.assign(hash_.size() * 2, -1);
128 for (int i = n_ - 1; i >= 0; --i) {
129 ObjectItem &oi = item(i);
131 int b = bucket(oi.v_.first.data(), oi.v_.first.length());
138 int Json::ObjectJson::find_insert(const String &key, const Json &value)
142 int *b = &hash_[bucket(key.data(), key.length())], chain = 0;
143 while (*b >= 0 && os_[*b].v_.first != key) {
153 // NB 'b' is invalid now
154 new ((void *) &os_[n_]) ObjectItem(key, value, -1);
163 Json &Json::ObjectJson::get_insert(Str key)
167 int *b = &hash_[bucket(key.data(), key.length())], chain = 0;
168 while (*b >= 0 && os_[*b].v_.first != key) {
173 return os_[*b].v_.second;
178 // NB 'b' is invalid now
179 new ((void *) &os_[n_]) ObjectItem(String(key.data(), key.length()), null_json, -1);
184 return os_[n_ - 1].v_.second;
188 void Json::ObjectJson::erase(int p) {
189 const ObjectItem& oi = item(p);
190 int* b = &hash_[bucket(oi.v_.first.data(), oi.v_.first.length())];
191 while (*b >= 0 && *b != p)
195 os_[p].~ObjectItem();
200 Json::size_type Json::ObjectJson::erase(Str key) {
201 int* b = &hash_[bucket(key.data(), key.length())];
202 while (*b >= 0 && os_[*b].v_.first != key)
207 os_[p].~ObjectItem();
216 template <typename T> bool string_to_int_key(const char *first,
217 const char *last, T& x)
219 if (first == last || !isdigit((unsigned char) *first)
220 || (first[0] == '0' && first + 1 != last))
222 // XXX integer overflow
224 for (++first; first != last && isdigit((unsigned char) *first); ++first)
225 x = 10 * x + *first - '0';
226 return first == last;
230 void Json::hard_uniqueify_array(bool convert, int ncap_in) {
232 precondition(is_null() || is_array());
236 unsigned ncap = std::max(ncap_in, 8);
237 if (old_u.x.type == j_array && old_u.a.x)
238 ncap = std::max(ncap, unsigned(old_u.a.x->size));
239 // New capacity: Round up to a power of 2, up to multiples of 1<<14.
240 unsigned xcap = iceil_log2(ncap);
241 if (xcap <= (1U << 14))
244 ncap = ((ncap - 1) | ((1U << 14) - 1)) + 1;
245 u_.a.x = ArrayJson::make(ncap);
248 if (old_u.x.type == j_array && old_u.a.x && old_u.a.x->refcount == 1) {
249 u_.a.x->size = old_u.a.x->size;
250 memcpy(u_.a.x->a, old_u.a.x->a, sizeof(Json) * u_.a.x->size);
251 delete[] reinterpret_cast<char*>(old_u.a.x);
252 } else if (old_u.x.type == j_array && old_u.a.x) {
253 u_.a.x->size = old_u.a.x->size;
254 Json* last = u_.a.x->a + u_.a.x->size;
255 for (Json* it = u_.a.x->a, *oit = old_u.a.x->a; it != last; ++it, ++oit)
256 new((void*) it) Json(*oit);
257 old_u.a.x->deref(j_array);
258 } else if (old_u.x.type == j_object && old_u.o.x) {
259 ObjectItem *ob = old_u.o.x->os_, *oe = ob + old_u.o.x->n_;
261 for (; ob != oe; ++ob)
263 && string_to_int_key(ob->v_.first.begin(),
264 ob->v_.first.end(), i)) {
265 if (i >= unsigned(u_.a.x->capacity))
266 hard_uniqueify_array(false, i + 1);
267 if (i >= unsigned(u_.a.x->size)) {
268 memset(&u_.a.x->a[u_.a.x->size], 0, sizeof(Json) * (i + 1 - u_.a.x->size));
269 u_.a.x->size = i + 1;
271 u_.a.x->a[i] = ob->v_.second;
273 old_u.o.x->deref(j_object);
274 } else if (old_u.x.type < 0)
278 void Json::hard_uniqueify_object(bool convert) {
280 precondition(is_null() || is_object());
282 if (u_.x.type == j_object && u_.o.x) {
283 noj = new ObjectJson(*u_.o.x);
284 u_.o.x->deref(j_object);
285 } else if (u_.x.type == j_array && u_.a.x) {
286 noj = new ObjectJson;
287 for (int i = 0; i != u_.a.x->size; ++i)
288 noj->find_insert(String(i), u_.a.x->a[i]);
289 u_.a.x->deref(j_array);
291 noj = new ObjectJson;
296 u_.o.type = j_object;
300 static_assert(offsetof(rep_type, i.type) == offsetof(rep_type, x.type), "odd Json::rep_type.i.type offset");
301 static_assert(offsetof(rep_type, u.type) == offsetof(rep_type, x.type), "odd Json::rep_type.u.type offset");
302 static_assert(offsetof(rep_type, d.type) == offsetof(rep_type, x.type), "odd Json::rep_type.d.type offset");
303 static_assert(offsetof(rep_type, str.memo_offset) == offsetof(rep_type, x.type), "odd Json::rep_type.str.memo_offset offset");
304 static_assert(offsetof(rep_type, a.type) == offsetof(rep_type, x.type), "odd Json::rep_type.a.type offset");
305 static_assert(offsetof(rep_type, o.type) == offsetof(rep_type, x.type), "odd Json::rep_type.o.type offset");
307 if (u_.x.type == j_array) {
308 if (u_.a.x && u_.a.x->refcount == 1) {
309 Json* last = u_.a.x->a + u_.a.x->size;
310 for (Json* it = u_.a.x->a; it != last; ++it)
314 u_.a.x->deref(j_array);
317 } else if (u_.x.type == j_object) {
318 if (u_.o.x && u_.o.x->refcount == 1) {
319 ObjectItem* last = u_.o.x->os_ + u_.o.x->n_;
320 for (ObjectItem* it = u_.o.x->os_; it != last; ++it)
323 u_.o.x->n_ = u_.o.x->size = 0;
324 u_.o.x->hash_.assign(u_.o.x->hash_.size(), -1);
326 u_.o.x->deref(j_object);
332 memset(&u_, 0, sizeof(u_));
336 void* Json::uniqueify_array_insert(bool convert, size_type pos) {
337 size_type size = u_.a.x ? u_.a.x->size : 0;
338 uniqueify_array(convert, size + 1);
339 if (pos == (size_type) -1)
341 precondition(pos >= 0 && pos <= size);
343 memmove(&u_.a.x->a[pos + 1], &u_.a.x->a[pos], (size - pos) * sizeof(Json));
345 return (void*) &u_.a.x->a[pos];
348 Json::array_iterator Json::erase(array_iterator first, array_iterator last) {
350 uniqueify_array(false, 0);
351 size_type fpos = first - abegin();
352 size_type lpos = last - abegin();
353 size_type size = u_.a.x->size;
354 for (size_type pos = fpos; pos != lpos; ++pos)
355 u_.a.x->a[pos].~Json();
357 memmove(&u_.a.x->a[fpos], &u_.a.x->a[lpos],
358 (size - lpos) * sizeof(Json));
359 u_.a.x->size -= lpos - fpos;
364 /** @brief Reserve the array Json to hold at least @a n items. */
365 void Json::reserve(size_type n) {
366 uniqueify_array(false, n);
369 /** @brief Resize the array Json to size @a n. */
370 void Json::resize(size_type n) {
371 uniqueify_array(false, n);
372 while (u_.a.x->size > n && u_.a.x->size > 0) {
374 u_.a.x->a[u_.a.x->size].~Json();
376 while (u_.a.x->size < n)
383 int64_t Json::hard_to_i() const {
394 return int64_t(u_.d.x);
400 invariant(u_.x.type <= 0);
401 const char *b = reinterpret_cast<const String&>(u_.str).c_str();
404 long x = strtol(b, &s, 0);
406 long long x = strtoll(b, &s, 0);
408 if (s == b + u_.str.length)
411 return (long) strtod(b, 0);
415 uint64_t Json::hard_to_u() const {
426 return uint64_t(u_.d.x);
432 const char* b = reinterpret_cast<const String&>(u_.str).c_str();
435 unsigned long x = strtoul(b, &s, 0);
437 unsigned long long x = strtoull(b, &s, 0);
439 if (s == b + u_.str.length)
442 return (uint64_t) strtod(b, 0);
446 double Json::hard_to_d() const {
464 return strtod(reinterpret_cast<const String&>(u_.str).c_str(), 0);
468 bool Json::hard_to_b() const {
482 return u_.str.length != 0;
486 String Json::hard_to_s() const {
491 return object_string;
493 return String(bool(u_.i.x));
495 return String(u_.i.x);
497 return String(u_.u.x);
499 return String(u_.d.x);
504 return String::make_empty();
506 return String(u_.str);
510 const Json& Json::hard_get(Str key) const {
513 if (is_array() && (aj = ajson())
514 && string_to_int_key(key.begin(), key.end(), i)
515 && i < unsigned(aj->size))
521 const Json& Json::hard_get(size_type x) const {
522 if (is_object() && u_.o.x)
523 return get(String(x));
528 Json& Json::hard_get_insert(size_type x) {
530 return get_insert(String(x));
532 uniqueify_array(true, x + 1);
533 if (u_.a.x->size <= x) {
534 memset(&u_.a.x->a[u_.a.x->size], 0, sizeof(Json) * (x + 1 - u_.a.x->size));
535 u_.a.x->size = x + 1;
541 bool operator==(const Json& a, const Json& b) {
542 if ((a.u_.x.type > 0 || b.u_.x.type > 0)
543 && a.u_.x.type != b.u_.x.type)
544 return a.u_.u.x == b.u_.u.x
548 else if (a.u_.x.type == Json::j_int
549 || a.u_.x.type == Json::j_unsigned
550 || a.u_.x.type == Json::j_bool)
551 return a.u_.u.x == b.u_.u.x;
552 else if (a.u_.x.type == Json::j_double)
553 return a.u_.d.x == b.u_.d.x;
554 else if (a.u_.x.type > 0 || !a.u_.x.x || !b.u_.x.x)
555 return a.u_.x.x == b.u_.x.x;
557 return String(a.u_.str) == String(b.u_.str);
563 Json::unparse_manipulator Json::default_manipulator;
565 bool Json::unparse_is_complex() const {
567 if (ObjectJson *oj = ojson()) {
570 ObjectItem *ob = oj->os_, *oe = ob + oj->n_;
571 for (; ob != oe; ++ob)
572 if (ob->next_ > -2 && !ob->v_.second.empty() && !ob->v_.second.is_primitive())
575 } else if (is_array()) {
576 if (ArrayJson *aj = ajson()) {
579 for (Json* it = aj->a; it != aj->a + aj->size; ++it)
580 if (!it->empty() && !it->is_primitive())
587 void Json::unparse_indent(StringAccum &sa, const unparse_manipulator &m, int depth)
590 depth *= (m.tab_width() ? m.tab_width() : 8);
591 sa.append_fill('\t', depth / 8);
592 sa.append_fill(' ', depth % 8);
596 const char* const upx_normal[] = {":", ","};
597 const char* const upx_expanded[] = {": ", ","};
598 const char* const upx_separated[] = {": ", ", "};
601 void Json::hard_unparse(StringAccum &sa, const unparse_manipulator &m, int depth) const
603 if (is_object() || is_array()) {
604 bool expanded = depth < m.indent_depth() && unparse_is_complex();
605 const char* const* upx;
608 else if (m.space_separator())
613 if (is_object() && !u_.x.x)
615 else if (is_object()) {
618 ObjectJson *oj = ojson();
619 ObjectItem *ob = oj->os_, *oe = ob + oj->n_;
620 for (; ob != oe; ++ob)
621 if (ob->next_ > -2) {
625 unparse_indent(sa, m, depth + 1);
627 ob->v_.first.encode_json(sa);
628 sa << '\"' << upx[0];
629 ob->v_.second.hard_unparse(sa, m, depth + 1);
633 unparse_indent(sa, m, depth);
640 ArrayJson* aj = ajson();
641 for (Json* it = aj->a; it != aj->a + aj->size; ++it) {
645 unparse_indent(sa, m, depth + 1);
646 it->hard_unparse(sa, m, depth + 1);
650 unparse_indent(sa, m, depth);
653 } else if (u_.x.type == j_null && !u_.x.x)
654 sa.append("null", 4);
655 else if (u_.x.type <= 0) {
657 reinterpret_cast<const String&>(u_.str).encode_json(sa);
659 } else if (u_.x.type == j_bool) {
661 sa.append(&"false\0true"[-b & 6], 5 - b);
662 } else if (u_.x.type == j_int)
664 else if (u_.x.type == j_unsigned)
666 else if (u_.x.type == j_double)
669 if (depth == 0 && m.newline_terminator())
675 Json::assign_parse(const char* first, const char* last, const String& str)
678 Json::streaming_parser jsp;
679 first = jsp.consume(first, last, str, true);
680 if (first != last && (*first == ' ' || *first == '\n' || *first == '\r'
683 if (first == last && jsp.success()) {
684 swap(jsp.result(), *this);
690 static inline bool in_range(uint8_t x, unsigned low, unsigned high) {
691 return (unsigned) x - low < high - low;
694 static inline bool in_range(int x, unsigned low, unsigned high) {
695 return (unsigned) x - low < high - low;
698 inline const uint8_t* Json::streaming_parser::error_at(const uint8_t* here) {
703 inline Json* Json::streaming_parser::current() {
704 return stack_.empty() ? &json_ : stack_.back();
708 Json::streaming_parser::consume(const uint8_t* first,
715 if (state_ >= 0 && (state_ & st_partmask)) {
716 if ((state_ & st_stringpart) && state_ >= st_object_colon)
717 goto string_object_key;
718 else if (state_ & st_stringpart)
720 else if (state_ & st_primitivepart)
726 while (state_ >= 0 && first != last)
732 while (first != last && *first <= 32
733 && (*first == ' ' || *first == '\n' || *first == '\r'
739 if (state_ == st_array_delim)
740 state_ = st_array_value;
741 else if (state_ == st_object_delim)
742 state_ = st_object_key;
749 if (state_ == st_object_colon) {
750 state_ = st_object_value;
757 if (state_ <= st_object_value) {
758 if (stack_.size() == max_depth)
761 if (state_ == st_initial && json_.is_o()) {
765 j = Json::make_object();
771 if (state_ == st_object_initial || state_ == st_object_delim) {
778 if (state_ <= st_object_value) {
779 if (stack_.size() == max_depth)
782 if (state_ == st_initial && json_.is_a()) {
786 j = Json::make_array();
792 if (state_ == st_array_initial || state_ == st_array_delim) {
799 if (state_ <= st_object_value) {
803 first = consume_string(first, last, str);
804 if (state_ >= 0 && !(state_ & st_stringpart)) {
805 j = Json(std::move(str_));
808 } else if (state_ == st_object_initial || state_ == st_object_key) {
809 state_ = st_object_colon;
813 first = consume_string(first, last, str);
814 if (state_ >= 0 && !(state_ & st_stringpart)) {
815 stack_.push_back(¤t()->get_insert(std::move(str_)));
825 if (state_ <= st_object_value) {
827 first = consume_primitive(first, last, j);
828 if (state_ >= 0 && !(state_ & st_primitivepart))
845 if (state_ <= st_object_value) {
847 first = consume_number(first, last, str, complete, j);
848 if (state_ >= 0 && !(state_ & st_numberpart))
856 return error_at(first);
859 Json* jp = current();
860 if (state_ != st_initial && jp->is_a()) {
861 jp->push_back(std::move(j));
866 if (state_ == st_object_value)
868 if (jp->is_a() || jp->is_o()) {
869 if (state_ != st_initial)
870 stack_.push_back(jp);
871 state_ = jp->is_a() ? st_array_initial : st_object_initial;
872 } else if (state_ == st_object_value)
873 state_ = st_object_delim;
874 else if (state_ == st_array_initial || state_ == st_array_value)
875 state_ = st_array_delim;
884 if (stack_.empty()) {
889 state_ = current()->is_a() ? st_array_delim : st_object_delim;
898 Json::streaming_parser::consume_string(const uint8_t* first,
901 StringAccum sa = StringAccum::make_transfer(str_);
903 if (unlikely(state_ & st_partlenmask)) {
904 first = consume_stringpart(sa, first, last);
905 if (state_ == st_error)
909 const uint8_t* prev = first;
910 while (first != last) {
911 if (likely(in_range(*first, 32, 128)) && *first != '\\' && *first != '\"')
913 else if (*first == '\\') {
914 sa.append(prev, first);
915 prev = first = consume_backslash(sa, first, last);
916 if (state_ == st_error)
918 } else if (*first == '\"')
920 else if (unlikely(!in_range(*first, 0xC2, 0xF5)))
921 return error_at(first);
923 int want = 2 + (*first >= 0xE0) + (*first >= 0xF0);
924 int n = last - first < want ? last - first : want;
925 if ((n > 1 && unlikely(!in_range(first[1], 0x80, 0xC0)))
927 && ((*first == 0xE0 && first[1] < 0xA0) /* overlong */
928 || (*first == 0xED && first[1] >= 0xA0) /* surrogate */
929 || (*first == 0xF0 && first[1] < 0x90) /* overlong */
930 || (*first == 0xF4 && first[1] >= 0x90) /* not a char */
932 return error_at(first + 1);
933 else if (n > 2 && unlikely(!in_range(first[2], 0x80, 0xC0)))
934 return error_at(first + 2);
935 else if (n > 3 && unlikely(!in_range(first[3], 0x80, 0xC0)))
936 return error_at(first + 3);
945 if (!sa.empty() || first == last)
946 sa.append(prev, first);
949 str_ = sa.take_string();
950 else if (prev >= str.ubegin() && first <= str.uend())
951 str_ = str.fast_substring(prev, first);
953 str_ = String(prev, first);
954 state_ &= ~st_stringpart;
957 state_ |= st_stringpart;
958 str_ = sa.take_string();
964 Json::streaming_parser::consume_stringpart(StringAccum& sa,
965 const uint8_t* first,
966 const uint8_t* last) {
967 while ((state_ & st_partlenmask) && first != last) {
968 int part = state_ & st_partlenmask;
969 uint8_t tag = sa[sa.length() - part];
970 if ((tag != '\\' && (*first & 0xC0) != 0x80)
971 || (tag == '\\' && part == 6 && *first != '\\')
972 || (tag == '\\' && part == 7 && *first != 'u')
973 || (tag == '\\' && part != 1 && part != 6 && part != 7
974 && !isxdigit(*first))) {
979 if ((tag != '\\' && part == 1 + (tag >= 0xE0) + (tag >= 0xF0))
980 || (tag == '\\' && (part == 1 || part == 5 || part == 11))) {
982 memcpy(buf, sa.end() - part - 1, part + 1);
983 sa.adjust_length(-part - 1);
984 state_ -= st_stringpart | part;
985 str_ = sa.take_string();
986 (void) consume_string(buf, buf + part + 1, String());
987 if (state_ == st_error)
989 sa = StringAccum::make_transfer(str_);
998 Json::streaming_parser::consume_backslash(StringAccum& sa,
999 const uint8_t* first,
1000 const uint8_t* last) {
1001 const uint8_t* prev = first;
1004 if (first + 1 == last)
1006 else if (first[1] == '\"' || first[1] == '\\' || first[1] == '/')
1008 else if (first[1] == 'b')
1010 else if (first[1] == 'f')
1012 else if (first[1] == 'n')
1014 else if (first[1] == 'r')
1016 else if (first[1] == 't')
1018 else if (first[1] == 'u') {
1019 for (int i = 2; i < 6; ++i) {
1020 if (first + i == last)
1022 else if (in_range(first[i], '0', '9' + 1))
1023 ch = 16 * ch + first[i] - '0';
1024 else if (in_range(first[i], 'A', 'F' + 1))
1025 ch = 16 * ch + first[i] - 'A' + 10;
1026 else if (in_range(first[i], 'a', 'f' + 1))
1027 ch = 16 * ch + first[i] - 'a' + 10;
1029 return error_at(&first[i]);
1032 // special handling required for surrogate pairs
1033 if (unlikely(in_range(ch, 0xD800, 0xE000))) {
1035 return error_at(&first[1]);
1036 else if (first + 2 == last)
1038 else if (first[2] != '\\')
1039 return error_at(&first[2]);
1040 else if (first + 3 == last)
1042 else if (first[3] != 'u')
1043 return error_at(&first[3]);
1045 for (int i = 4; i < 8; ++i) {
1046 if (first + i == last)
1048 else if (in_range(first[i], '0', '9' + 1))
1049 ch2 = 16 * ch2 + first[i] - '0';
1050 else if (in_range(first[i], 'A', 'F' + 1))
1051 ch2 = 16 * ch2 + first[i] - 'A' + 10;
1052 else if (in_range(first[i], 'A', 'F' + 1))
1053 ch2 = 16 * ch2 + first[i] - 'a' + 10;
1055 return error_at(&first[i]);
1057 if (!in_range(ch2, 0xDC00, 0xE000))
1058 return error_at(&first[7]);
1059 ch = 0x10000 + (ch - 0xD800) * 0x400 + (ch2 - 0xDC00);
1064 if (!ch || !sa.append_utf8(ch))
1065 return error_at(&first[1]);
1069 state_ |= last - prev;
1070 sa.append(prev, last);
1075 Json::streaming_parser::consume_primitive(const uint8_t* first,
1076 const uint8_t* last,
1078 const char* t = "null\0false\0true";
1080 if (unlikely(state_ & st_primitivepart)) {
1081 n = state_ & st_partlenmask;
1082 state_ &= ~st_partmask;
1084 n = (*first == 'n' ? 1 : (*first == 'f' ? 6 : 12));
1088 for (; first != last && t[n]; ++n, ++first)
1090 return error_at(first);
1093 state_ |= st_primitivepart | n;
1104 Json::streaming_parser::consume_number(const uint8_t* first,
1105 const uint8_t* last,
1109 const uint8_t* prev = first;
1110 int position = state_ & st_partlenmask;
1118 if (first != last && *first == '0') {
1121 } else if (first != last && in_range(*first, '1', '9' + 1)) {
1125 while (first != last && in_range(*first, '0', '9' + 1))
1131 if (first != last && *first == '.') {
1136 if (first != last && (*first == 'e' || *first == 'E')) {
1145 if (first != last && in_range(*first, '0', '9' + 1)) {
1151 while (first != last && in_range(*first, '0', '9' + 1))
1153 if (first != last && position == 5)
1154 goto maybe_exponent;
1160 if (first != last && (*first == '+' || *first == '-'))
1162 else if (first == last)
1167 if (first != last && in_range(*first, '0', '9' + 1)) {
1173 while (first != last && in_range(*first, '0', '9' + 1))
1178 if (first != last || complete) {
1179 if (!(position & 1))
1182 if (state_ & st_partlenmask) {
1183 str_.append(prev, first);
1184 prev = str_.ubegin();
1185 first = str_.uend();
1187 if (prev + 1 == first)
1188 j = Json(int(*prev - '0'));
1189 else if (position < 4) {
1190 bool negative = *prev == '-';
1191 prev += int(negative);
1193 while (prev != first) {
1194 x = (x * 10) + *prev - '0';
1198 j = Json(-int64_t(x));
1202 if (!(state_ & st_partlenmask))
1203 str_ = String(prev, first);
1204 double x = strtod(str_.c_str(), 0);
1207 state_ &= ~st_partmask;
1212 if (state_ & st_partmask)
1213 str_.append(prev, first);
1214 else if (prev >= str.ubegin() && first <= str.uend())
1215 str_ = str.substring(prev, first);
1217 str_ = String(prev, first);
1218 state_ = (state_ & ~st_partmask) | st_numberpart | position;
1223 return error_at(first);