annotate Resources/CodeGeneration/testWasmIntegrated/jsoncpp-1.8.4/jsoncpp.cpp @ 1313:f30905f5d246 broker

Added support for OnGenericSdlEvent to GuiAdapter. This makes it possible to piggyback app-specific logic to special events (here, to support the Viewport refresh events)
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 16 Mar 2020 11:12:39 +0100
parents 8a0a62189f46
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1 /// Json-cpp amalgamated source (http://jsoncpp.sourceforge.net/).
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2 /// It is intended to be used with #include "json/json.h"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5 // Beginning of content of file: LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
6 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
7
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
8 /*
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
9 The JsonCpp library's source code, including accompanying documentation,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
10 tests and demonstration applications, are licensed under the following
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
11 conditions...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
12
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
13 Baptiste Lepilleur and The JsonCpp Authors explicitly disclaim copyright in all
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
14 jurisdictions which recognize such a disclaimer. In such jurisdictions,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
15 this software is released into the Public Domain.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
16
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
17 In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
18 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
19 The JsonCpp Authors, and is released under the terms of the MIT License (see below).
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
20
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
21 In jurisdictions which recognize Public Domain property, the user of this
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
22 software may choose to accept it either as 1) Public Domain, 2) under the
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
23 conditions of the MIT License (see below), or 3) under the terms of dual
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
24 Public Domain/MIT License conditions described here, as they choose.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
25
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
26 The MIT License is about as close to Public Domain as a license can get, and is
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
27 described in clear, concise terms at:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
28
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
29 http://en.wikipedia.org/wiki/MIT_License
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
30
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
31 The full text of the MIT License follows:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
32
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
33 ========================================================================
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
34 Copyright (c) 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
35
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
36 Permission is hereby granted, free of charge, to any person
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
37 obtaining a copy of this software and associated documentation
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
38 files (the "Software"), to deal in the Software without
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
39 restriction, including without limitation the rights to use, copy,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
40 modify, merge, publish, distribute, sublicense, and/or sell copies
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
41 of the Software, and to permit persons to whom the Software is
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
42 furnished to do so, subject to the following conditions:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
43
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
44 The above copyright notice and this permission notice shall be
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
45 included in all copies or substantial portions of the Software.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
46
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
47 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
48 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
49 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
50 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
51 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
52 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
53 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
54 SOFTWARE.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
55 ========================================================================
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
56 (END LICENSE TEXT)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
57
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
58 The MIT license is compatible with both the GPL and commercial
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
59 software, affording one all of the rights of Public Domain with the
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
60 minor nuisance of being required to keep the above copyright notice
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
61 and license text in the source code. Note also that by accepting the
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
62 Public Domain "license" you can re-license your copy using whatever
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
63 license you like.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
64
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
65 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
66
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
67 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
68 // End of content of file: LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
69 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
70
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
71
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
72
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
73
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
74
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
75
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
76 #include "json/json.h"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
77
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
78 #ifndef JSON_IS_AMALGAMATION
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
79 #error "Compile with -I PATH_TO_JSON_DIRECTORY"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
80 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
81
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
82
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
83 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
84 // Beginning of content of file: src/lib_json/json_tool.h
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
85 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
86
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
87 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
88 // Distributed under MIT license, or public domain if desired and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
89 // recognized in your jurisdiction.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
90 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
91
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
92 #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
93 #define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
94
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
95 #if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
96 #include <json/config.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
97 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
98
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
99 // Also support old flag NO_LOCALE_SUPPORT
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
100 #ifdef NO_LOCALE_SUPPORT
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
101 #define JSONCPP_NO_LOCALE_SUPPORT
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
102 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
103
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
104 #ifndef JSONCPP_NO_LOCALE_SUPPORT
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
105 #include <clocale>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
106 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
107
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
108 /* This header provides common string manipulation support, such as UTF-8,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
109 * portable conversion from/to string...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
110 *
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
111 * It is an internal header that must not be exposed.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
112 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
113
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
114 namespace Json {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
115 static inline char getDecimalPoint() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
116 #ifdef JSONCPP_NO_LOCALE_SUPPORT
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
117 return '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
118 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
119 struct lconv* lc = localeconv();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
120 return lc ? *(lc->decimal_point) : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
121 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
122 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
123
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
124 /// Converts a unicode code-point to UTF-8.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
125 static inline String codePointToUTF8(unsigned int cp) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
126 String result;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
127
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
128 // based on description from http://en.wikipedia.org/wiki/UTF-8
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
129
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
130 if (cp <= 0x7f) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
131 result.resize(1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
132 result[0] = static_cast<char>(cp);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
133 } else if (cp <= 0x7FF) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
134 result.resize(2);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
135 result[1] = static_cast<char>(0x80 | (0x3f & cp));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
136 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
137 } else if (cp <= 0xFFFF) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
138 result.resize(3);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
139 result[2] = static_cast<char>(0x80 | (0x3f & cp));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
140 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
141 result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
142 } else if (cp <= 0x10FFFF) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
143 result.resize(4);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
144 result[3] = static_cast<char>(0x80 | (0x3f & cp));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
145 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
146 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
147 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
148 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
149
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
150 return result;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
151 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
152
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
153 enum {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
154 /// Constant that specify the size of the buffer that must be passed to
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
155 /// uintToString.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
156 uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
157 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
158
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
159 // Defines a char buffer for use with uintToString().
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
160 typedef char UIntToStringBuffer[uintToStringBufferSize];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
161
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
162 /** Converts an unsigned integer to string.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
163 * @param value Unsigned integer to convert to string
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
164 * @param current Input/Output string buffer.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
165 * Must have at least uintToStringBufferSize chars free.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
166 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
167 static inline void uintToString(LargestUInt value, char*& current) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
168 *--current = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
169 do {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
170 *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
171 value /= 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
172 } while (value != 0);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
173 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
174
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
175 /** Change ',' to '.' everywhere in buffer.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
176 *
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
177 * We had a sophisticated way, but it did not work in WinCE.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
178 * @see https://github.com/open-source-parsers/jsoncpp/pull/9
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
179 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
180 template <typename Iter> Iter fixNumericLocale(Iter begin, Iter end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
181 for (; begin != end; ++begin) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
182 if (*begin == ',') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
183 *begin = '.';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
184 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
185 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
186 return begin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
187 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
188
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
189 template <typename Iter> void fixNumericLocaleInput(Iter begin, Iter end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
190 char decimalPoint = getDecimalPoint();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
191 if (decimalPoint == '\0' || decimalPoint == '.') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
192 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
193 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
194 for (; begin != end; ++begin) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
195 if (*begin == '.') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
196 *begin = decimalPoint;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
197 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
198 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
199 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
200
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
201 /**
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
202 * Return iterator that would be the new end of the range [begin,end), if we
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
203 * were to delete zeros in the end of string, but not the last zero before '.'.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
204 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
205 template <typename Iter> Iter fixZerosInTheEnd(Iter begin, Iter end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
206 for (; begin != end; --end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
207 if (*(end - 1) != '0') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
208 return end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
209 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
210 // Don't delete the last zero before the decimal point.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
211 if (begin != (end - 1) && *(end - 2) == '.') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
212 return end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
213 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
214 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
215 return end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
216 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
217
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
218 } // namespace Json
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
219
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
220 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
221
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
222 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
223 // End of content of file: src/lib_json/json_tool.h
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
224 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
225
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
226
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
227
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
228
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
229
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
230
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
231 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
232 // Beginning of content of file: src/lib_json/json_reader.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
233 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
234
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
235 // Copyright 2007-2011 Baptiste Lepilleur and The JsonCpp Authors
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
236 // Copyright (C) 2016 InfoTeCS JSC. All rights reserved.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
237 // Distributed under MIT license, or public domain if desired and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
238 // recognized in your jurisdiction.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
239 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
240
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
241 #if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
242 #include "json_tool.h"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
243 #include <json/assertions.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
244 #include <json/reader.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
245 #include <json/value.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
246 #endif // if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
247 #include <cassert>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
248 #include <cstring>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
249 #include <istream>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
250 #include <limits>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
251 #include <memory>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
252 #include <set>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
253 #include <sstream>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
254 #include <utility>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
255
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
256 #include <cstdio>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
257 #if __cplusplus >= 201103L
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
258
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
259 #if !defined(sscanf)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
260 #define sscanf std::sscanf
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
261 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
262
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
263 #endif //__cplusplus
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
264
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
265 #if defined(_MSC_VER)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
266 #if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
267 #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
268 #endif //_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
269 #endif //_MSC_VER
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
270
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
271 #if defined(_MSC_VER)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
272 // Disable warning about strdup being deprecated.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
273 #pragma warning(disable : 4996)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
274 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
275
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
276 // Define JSONCPP_DEPRECATED_STACK_LIMIT as an appropriate integer at compile
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
277 // time to change the stack limit
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
278 #if !defined(JSONCPP_DEPRECATED_STACK_LIMIT)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
279 #define JSONCPP_DEPRECATED_STACK_LIMIT 1000
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
280 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
281
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
282 static size_t const stackLimit_g =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
283 JSONCPP_DEPRECATED_STACK_LIMIT; // see readValue()
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
284
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
285 namespace Json {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
286
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
287 #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
288 typedef std::unique_ptr<CharReader> CharReaderPtr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
289 #else
1298
8a0a62189f46 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 499
diff changeset
290 typedef std::unique_ptr<CharReader> CharReaderPtr;
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
291 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
292
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
293 // Implementation of class Features
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
294 // ////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
295
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
296 Features::Features() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
297
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
298 Features Features::all() { return {}; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
299
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
300 Features Features::strictMode() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
301 Features features;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
302 features.allowComments_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
303 features.strictRoot_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
304 features.allowDroppedNullPlaceholders_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
305 features.allowNumericKeys_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
306 return features;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
307 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
308
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
309 // Implementation of class Reader
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
310 // ////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
311
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
312 bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
313 for (; begin < end; ++begin)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
314 if (*begin == '\n' || *begin == '\r')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
315 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
316 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
317 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
318
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
319 // Class Reader
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
320 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
321
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
322 Reader::Reader()
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
323 : errors_(), document_(), commentsBefore_(), features_(Features::all()) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
324
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
325 Reader::Reader(const Features& features)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
326 : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
327 lastValue_(), commentsBefore_(), features_(features), collectComments_() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
328 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
329
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
330 bool Reader::parse(const std::string& document,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
331 Value& root,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
332 bool collectComments) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
333 document_.assign(document.begin(), document.end());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
334 const char* begin = document_.c_str();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
335 const char* end = begin + document_.length();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
336 return parse(begin, end, root, collectComments);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
337 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
338
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
339 bool Reader::parse(std::istream& is, Value& root, bool collectComments) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
340 // std::istream_iterator<char> begin(is);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
341 // std::istream_iterator<char> end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
342 // Those would allow streamed input from a file, if parse() were a
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
343 // template function.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
344
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
345 // Since String is reference-counted, this at least does not
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
346 // create an extra copy.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
347 String doc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
348 std::getline(is, doc, (char)EOF);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
349 return parse(doc.data(), doc.data() + doc.size(), root, collectComments);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
350 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
351
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
352 bool Reader::parse(const char* beginDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
353 const char* endDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
354 Value& root,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
355 bool collectComments) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
356 if (!features_.allowComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
357 collectComments = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
358 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
359
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
360 begin_ = beginDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
361 end_ = endDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
362 collectComments_ = collectComments;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
363 current_ = begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
364 lastValueEnd_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
365 lastValue_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
366 commentsBefore_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
367 errors_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
368 while (!nodes_.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
369 nodes_.pop();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
370 nodes_.push(&root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
371
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
372 bool successful = readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
373 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
374 skipCommentTokens(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
375 if (collectComments_ && !commentsBefore_.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
376 root.setComment(commentsBefore_, commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
377 if (features_.strictRoot_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
378 if (!root.isArray() && !root.isObject()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
379 // Set error location to start of doc, ideally should be first token found
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
380 // in doc
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
381 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
382 token.start_ = beginDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
383 token.end_ = endDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
384 addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
385 "A valid JSON document must be either an array or an object value.",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
386 token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
387 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
388 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
389 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
390 return successful;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
391 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
392
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
393 bool Reader::readValue() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
394 // readValue() may call itself only if it calls readObject() or ReadArray().
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
395 // These methods execute nodes_.push() just before and nodes_.pop)() just
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
396 // after calling readValue(). parse() executes one nodes_.push(), so > instead
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
397 // of >=.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
398 if (nodes_.size() > stackLimit_g)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
399 throwRuntimeError("Exceeded stackLimit in readValue().");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
400
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
401 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
402 skipCommentTokens(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
403 bool successful = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
404
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
405 if (collectComments_ && !commentsBefore_.empty()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
406 currentValue().setComment(commentsBefore_, commentBefore);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
407 commentsBefore_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
408 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
409
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
410 switch (token.type_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
411 case tokenObjectBegin:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
412 successful = readObject(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
413 currentValue().setOffsetLimit(current_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
414 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
415 case tokenArrayBegin:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
416 successful = readArray(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
417 currentValue().setOffsetLimit(current_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
418 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
419 case tokenNumber:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
420 successful = decodeNumber(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
421 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
422 case tokenString:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
423 successful = decodeString(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
424 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
425 case tokenTrue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
426 Value v(true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
427 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
428 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
429 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
430 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
431 case tokenFalse: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
432 Value v(false);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
433 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
434 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
435 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
436 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
437 case tokenNull: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
438 Value v;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
439 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
440 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
441 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
442 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
443 case tokenArraySeparator:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
444 case tokenObjectEnd:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
445 case tokenArrayEnd:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
446 if (features_.allowDroppedNullPlaceholders_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
447 // "Un-read" the current token and mark the current value as a null
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
448 // token.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
449 current_--;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
450 Value v;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
451 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
452 currentValue().setOffsetStart(current_ - begin_ - 1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
453 currentValue().setOffsetLimit(current_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
454 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
455 } // Else, fall through...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
456 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
457 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
458 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
459 return addError("Syntax error: value, object or array expected.", token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
460 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
461
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
462 if (collectComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
463 lastValueEnd_ = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
464 lastValue_ = &currentValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
465 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
466
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
467 return successful;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
468 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
469
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
470 void Reader::skipCommentTokens(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
471 if (features_.allowComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
472 do {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
473 readToken(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
474 } while (token.type_ == tokenComment);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
475 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
476 readToken(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
477 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
478 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
479
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
480 bool Reader::readToken(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
481 skipSpaces();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
482 token.start_ = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
483 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
484 bool ok = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
485 switch (c) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
486 case '{':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
487 token.type_ = tokenObjectBegin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
488 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
489 case '}':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
490 token.type_ = tokenObjectEnd;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
491 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
492 case '[':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
493 token.type_ = tokenArrayBegin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
494 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
495 case ']':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
496 token.type_ = tokenArrayEnd;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
497 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
498 case '"':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
499 token.type_ = tokenString;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
500 ok = readString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
501 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
502 case '/':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
503 token.type_ = tokenComment;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
504 ok = readComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
505 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
506 case '0':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
507 case '1':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
508 case '2':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
509 case '3':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
510 case '4':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
511 case '5':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
512 case '6':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
513 case '7':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
514 case '8':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
515 case '9':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
516 case '-':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
517 token.type_ = tokenNumber;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
518 readNumber();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
519 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
520 case 't':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
521 token.type_ = tokenTrue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
522 ok = match("rue", 3);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
523 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
524 case 'f':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
525 token.type_ = tokenFalse;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
526 ok = match("alse", 4);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
527 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
528 case 'n':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
529 token.type_ = tokenNull;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
530 ok = match("ull", 3);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
531 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
532 case ',':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
533 token.type_ = tokenArraySeparator;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
534 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
535 case ':':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
536 token.type_ = tokenMemberSeparator;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
537 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
538 case 0:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
539 token.type_ = tokenEndOfStream;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
540 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
541 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
542 ok = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
543 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
544 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
545 if (!ok)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
546 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
547 token.end_ = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
548 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
549 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
550
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
551 void Reader::skipSpaces() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
552 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
553 Char c = *current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
554 if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
555 ++current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
556 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
557 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
558 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
559 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
560
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
561 bool Reader::match(Location pattern, int patternLength) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
562 if (end_ - current_ < patternLength)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
563 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
564 int index = patternLength;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
565 while (index--)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
566 if (current_[index] != pattern[index])
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
567 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
568 current_ += patternLength;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
569 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
570 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
571
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
572 bool Reader::readComment() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
573 Location commentBegin = current_ - 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
574 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
575 bool successful = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
576 if (c == '*')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
577 successful = readCStyleComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
578 else if (c == '/')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
579 successful = readCppStyleComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
580 if (!successful)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
581 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
582
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
583 if (collectComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
584 CommentPlacement placement = commentBefore;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
585 if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
586 if (c != '*' || !containsNewLine(commentBegin, current_))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
587 placement = commentAfterOnSameLine;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
588 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
589
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
590 addComment(commentBegin, current_, placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
591 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
592 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
593 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
594
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
595 String Reader::normalizeEOL(Reader::Location begin, Reader::Location end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
596 String normalized;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
597 normalized.reserve(static_cast<size_t>(end - begin));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
598 Reader::Location current = begin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
599 while (current != end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
600 char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
601 if (c == '\r') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
602 if (current != end && *current == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
603 // convert dos EOL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
604 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
605 // convert Mac EOL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
606 normalized += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
607 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
608 normalized += c;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
609 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
610 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
611 return normalized;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
612 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
613
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
614 void Reader::addComment(Location begin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
615 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
616 CommentPlacement placement) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
617 assert(collectComments_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
618 const String& normalized = normalizeEOL(begin, end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
619 if (placement == commentAfterOnSameLine) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
620 assert(lastValue_ != nullptr);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
621 lastValue_->setComment(normalized, placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
622 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
623 commentsBefore_ += normalized;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
624 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
625 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
626
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
627 bool Reader::readCStyleComment() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
628 while ((current_ + 1) < end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
629 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
630 if (c == '*' && *current_ == '/')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
631 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
632 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
633 return getNextChar() == '/';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
634 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
635
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
636 bool Reader::readCppStyleComment() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
637 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
638 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
639 if (c == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
640 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
641 if (c == '\r') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
642 // Consume DOS EOL. It will be normalized in addComment.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
643 if (current_ != end_ && *current_ == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
644 getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
645 // Break on Moc OS 9 EOL.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
646 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
647 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
648 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
649 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
650 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
651
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
652 void Reader::readNumber() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
653 const char* p = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
654 char c = '0'; // stopgap for already consumed character
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
655 // integral part
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
656 while (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
657 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
658 // fractional part
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
659 if (c == '.') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
660 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
661 while (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
662 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
663 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
664 // exponential part
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
665 if (c == 'e' || c == 'E') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
666 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
667 if (c == '+' || c == '-')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
668 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
669 while (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
670 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
671 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
672 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
673
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
674 bool Reader::readString() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
675 Char c = '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
676 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
677 c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
678 if (c == '\\')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
679 getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
680 else if (c == '"')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
681 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
682 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
683 return c == '"';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
684 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
685
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
686 bool Reader::readObject(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
687 Token tokenName;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
688 String name;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
689 Value init(objectValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
690 currentValue().swapPayload(init);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
691 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
692 while (readToken(tokenName)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
693 bool initialTokenOk = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
694 while (tokenName.type_ == tokenComment && initialTokenOk)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
695 initialTokenOk = readToken(tokenName);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
696 if (!initialTokenOk)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
697 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
698 if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
699 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
700 name.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
701 if (tokenName.type_ == tokenString) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
702 if (!decodeString(tokenName, name))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
703 return recoverFromError(tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
704 } else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
705 Value numberName;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
706 if (!decodeNumber(tokenName, numberName))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
707 return recoverFromError(tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
708 name = String(numberName.asCString());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
709 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
710 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
711 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
712
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
713 Token colon;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
714 if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
715 return addErrorAndRecover("Missing ':' after object member name", colon,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
716 tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
717 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
718 Value& value = currentValue()[name];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
719 nodes_.push(&value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
720 bool ok = readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
721 nodes_.pop();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
722 if (!ok) // error already set
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
723 return recoverFromError(tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
724
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
725 Token comma;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
726 if (!readToken(comma) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
727 (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
728 comma.type_ != tokenComment)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
729 return addErrorAndRecover("Missing ',' or '}' in object declaration",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
730 comma, tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
731 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
732 bool finalizeTokenOk = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
733 while (comma.type_ == tokenComment && finalizeTokenOk)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
734 finalizeTokenOk = readToken(comma);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
735 if (comma.type_ == tokenObjectEnd)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
736 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
737 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
738 return addErrorAndRecover("Missing '}' or object member name", tokenName,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
739 tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
740 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
741
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
742 bool Reader::readArray(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
743 Value init(arrayValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
744 currentValue().swapPayload(init);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
745 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
746 skipSpaces();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
747 if (current_ != end_ && *current_ == ']') // empty array
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
748 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
749 Token endArray;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
750 readToken(endArray);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
751 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
752 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
753 int index = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
754 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
755 Value& value = currentValue()[index++];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
756 nodes_.push(&value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
757 bool ok = readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
758 nodes_.pop();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
759 if (!ok) // error already set
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
760 return recoverFromError(tokenArrayEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
761
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
762 Token currentToken;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
763 // Accept Comment after last item in the array.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
764 ok = readToken(currentToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
765 while (currentToken.type_ == tokenComment && ok) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
766 ok = readToken(currentToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
767 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
768 bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
769 currentToken.type_ != tokenArrayEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
770 if (!ok || badTokenType) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
771 return addErrorAndRecover("Missing ',' or ']' in array declaration",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
772 currentToken, tokenArrayEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
773 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
774 if (currentToken.type_ == tokenArrayEnd)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
775 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
776 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
777 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
778 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
779
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
780 bool Reader::decodeNumber(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
781 Value decoded;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
782 if (!decodeNumber(token, decoded))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
783 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
784 currentValue().swapPayload(decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
785 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
786 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
787 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
788 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
789
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
790 bool Reader::decodeNumber(Token& token, Value& decoded) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
791 // Attempts to parse the number as an integer. If the number is
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
792 // larger than the maximum supported value of an integer then
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
793 // we decode the number as a double.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
794 Location current = token.start_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
795 bool isNegative = *current == '-';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
796 if (isNegative)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
797 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
798 // TODO: Help the compiler do the div and mod at compile time or get rid of
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
799 // them.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
800 Value::LargestUInt maxIntegerValue =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
801 isNegative ? Value::LargestUInt(Value::maxLargestInt) + 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
802 : Value::maxLargestUInt;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
803 Value::LargestUInt threshold = maxIntegerValue / 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
804 Value::LargestUInt value = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
805 while (current < token.end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
806 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
807 if (c < '0' || c > '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
808 return decodeDouble(token, decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
809 auto digit(static_cast<Value::UInt>(c - '0'));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
810 if (value >= threshold) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
811 // We've hit or exceeded the max value divided by 10 (rounded down). If
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
812 // a) we've only just touched the limit, b) this is the last digit, and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
813 // c) it's small enough to fit in that rounding delta, we're okay.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
814 // Otherwise treat this number as a double to avoid overflow.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
815 if (value > threshold || current != token.end_ ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
816 digit > maxIntegerValue % 10) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
817 return decodeDouble(token, decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
818 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
819 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
820 value = value * 10 + digit;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
821 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
822 if (isNegative && value == maxIntegerValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
823 decoded = Value::minLargestInt;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
824 else if (isNegative)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
825 decoded = -Value::LargestInt(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
826 else if (value <= Value::LargestUInt(Value::maxInt))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
827 decoded = Value::LargestInt(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
828 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
829 decoded = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
830 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
831 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
832
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
833 bool Reader::decodeDouble(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
834 Value decoded;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
835 if (!decodeDouble(token, decoded))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
836 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
837 currentValue().swapPayload(decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
838 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
839 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
840 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
841 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
842
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
843 bool Reader::decodeDouble(Token& token, Value& decoded) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
844 double value = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
845 String buffer(token.start_, token.end_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
846 IStringStream is(buffer);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
847 if (!(is >> value))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
848 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
849 "'" + String(token.start_, token.end_) + "' is not a number.", token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
850 decoded = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
851 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
852 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
853
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
854 bool Reader::decodeString(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
855 String decoded_string;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
856 if (!decodeString(token, decoded_string))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
857 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
858 Value decoded(decoded_string);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
859 currentValue().swapPayload(decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
860 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
861 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
862 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
863 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
864
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
865 bool Reader::decodeString(Token& token, String& decoded) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
866 decoded.reserve(static_cast<size_t>(token.end_ - token.start_ - 2));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
867 Location current = token.start_ + 1; // skip '"'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
868 Location end = token.end_ - 1; // do not include '"'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
869 while (current != end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
870 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
871 if (c == '"')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
872 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
873 else if (c == '\\') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
874 if (current == end)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
875 return addError("Empty escape sequence in string", token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
876 Char escape = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
877 switch (escape) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
878 case '"':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
879 decoded += '"';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
880 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
881 case '/':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
882 decoded += '/';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
883 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
884 case '\\':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
885 decoded += '\\';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
886 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
887 case 'b':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
888 decoded += '\b';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
889 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
890 case 'f':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
891 decoded += '\f';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
892 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
893 case 'n':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
894 decoded += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
895 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
896 case 'r':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
897 decoded += '\r';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
898 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
899 case 't':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
900 decoded += '\t';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
901 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
902 case 'u': {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
903 unsigned int unicode;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
904 if (!decodeUnicodeCodePoint(token, current, end, unicode))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
905 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
906 decoded += codePointToUTF8(unicode);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
907 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
908 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
909 return addError("Bad escape sequence in string", token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
910 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
911 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
912 decoded += c;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
913 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
914 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
915 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
916 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
917
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
918 bool Reader::decodeUnicodeCodePoint(Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
919 Location& current,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
920 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
921 unsigned int& unicode) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
922
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
923 if (!decodeUnicodeEscapeSequence(token, current, end, unicode))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
924 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
925 if (unicode >= 0xD800 && unicode <= 0xDBFF) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
926 // surrogate pairs
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
927 if (end - current < 6)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
928 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
929 "additional six characters expected to parse unicode surrogate pair.",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
930 token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
931 if (*(current++) == '\\' && *(current++) == 'u') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
932 unsigned int surrogatePair;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
933 if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
934 unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
935 } else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
936 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
937 } else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
938 return addError("expecting another \\u token to begin the second half of "
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
939 "a unicode surrogate pair",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
940 token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
941 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
942 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
943 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
944
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
945 bool Reader::decodeUnicodeEscapeSequence(Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
946 Location& current,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
947 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
948 unsigned int& ret_unicode) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
949 if (end - current < 4)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
950 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
951 "Bad unicode escape sequence in string: four digits expected.", token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
952 current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
953 int unicode = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
954 for (int index = 0; index < 4; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
955 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
956 unicode *= 16;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
957 if (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
958 unicode += c - '0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
959 else if (c >= 'a' && c <= 'f')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
960 unicode += c - 'a' + 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
961 else if (c >= 'A' && c <= 'F')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
962 unicode += c - 'A' + 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
963 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
964 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
965 "Bad unicode escape sequence in string: hexadecimal digit expected.",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
966 token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
967 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
968 ret_unicode = static_cast<unsigned int>(unicode);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
969 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
970 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
971
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
972 bool Reader::addError(const String& message, Token& token, Location extra) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
973 ErrorInfo info;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
974 info.token_ = token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
975 info.message_ = message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
976 info.extra_ = extra;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
977 errors_.push_back(info);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
978 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
979 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
980
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
981 bool Reader::recoverFromError(TokenType skipUntilToken) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
982 size_t const errorCount = errors_.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
983 Token skip;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
984 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
985 if (!readToken(skip))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
986 errors_.resize(errorCount); // discard errors caused by recovery
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
987 if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
988 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
989 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
990 errors_.resize(errorCount);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
991 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
992 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
993
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
994 bool Reader::addErrorAndRecover(const String& message,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
995 Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
996 TokenType skipUntilToken) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
997 addError(message, token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
998 return recoverFromError(skipUntilToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
999 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1000
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1001 Value& Reader::currentValue() { return *(nodes_.top()); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1002
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1003 Reader::Char Reader::getNextChar() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1004 if (current_ == end_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1005 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1006 return *current_++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1007 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1008
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1009 void Reader::getLocationLineAndColumn(Location location,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1010 int& line,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1011 int& column) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1012 Location current = begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1013 Location lastLineStart = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1014 line = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1015 while (current < location && current != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1016 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1017 if (c == '\r') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1018 if (*current == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1019 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1020 lastLineStart = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1021 ++line;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1022 } else if (c == '\n') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1023 lastLineStart = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1024 ++line;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1025 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1026 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1027 // column & line start at 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1028 column = int(location - lastLineStart) + 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1029 ++line;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1030 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1031
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1032 String Reader::getLocationLineAndColumn(Location location) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1033 int line, column;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1034 getLocationLineAndColumn(location, line, column);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1035 char buffer[18 + 16 + 16 + 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1036 jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1037 return buffer;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1038 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1039
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1040 // Deprecated. Preserved for backward compatibility
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1041 String Reader::getFormatedErrorMessages() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1042 return getFormattedErrorMessages();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1043 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1044
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1045 String Reader::getFormattedErrorMessages() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1046 String formattedMessage;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1047 for (const auto& error : errors_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1048 formattedMessage +=
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1049 "* " + getLocationLineAndColumn(error.token_.start_) + "\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1050 formattedMessage += " " + error.message_ + "\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1051 if (error.extra_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1052 formattedMessage +=
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1053 "See " + getLocationLineAndColumn(error.extra_) + " for detail.\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1054 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1055 return formattedMessage;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1056 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1057
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1058 std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1059 std::vector<Reader::StructuredError> allErrors;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1060 for (const auto& error : errors_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1061 Reader::StructuredError structured;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1062 structured.offset_start = error.token_.start_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1063 structured.offset_limit = error.token_.end_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1064 structured.message = error.message_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1065 allErrors.push_back(structured);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1066 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1067 return allErrors;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1068 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1069
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1070 bool Reader::pushError(const Value& value, const String& message) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1071 ptrdiff_t const length = end_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1072 if (value.getOffsetStart() > length || value.getOffsetLimit() > length)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1073 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1074 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1075 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1076 token.start_ = begin_ + value.getOffsetStart();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1077 token.end_ = end_ + value.getOffsetLimit();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1078 ErrorInfo info;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1079 info.token_ = token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1080 info.message_ = message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1081 info.extra_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1082 errors_.push_back(info);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1083 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1084 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1085
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1086 bool Reader::pushError(const Value& value,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1087 const String& message,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1088 const Value& extra) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1089 ptrdiff_t const length = end_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1090 if (value.getOffsetStart() > length || value.getOffsetLimit() > length ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1091 extra.getOffsetLimit() > length)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1092 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1093 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1094 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1095 token.start_ = begin_ + value.getOffsetStart();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1096 token.end_ = begin_ + value.getOffsetLimit();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1097 ErrorInfo info;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1098 info.token_ = token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1099 info.message_ = message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1100 info.extra_ = begin_ + extra.getOffsetStart();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1101 errors_.push_back(info);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1102 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1103 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1104
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1105 bool Reader::good() const { return errors_.empty(); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1106
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1107 // exact copy of Features
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1108 class OurFeatures {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1109 public:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1110 static OurFeatures all();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1111 bool allowComments_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1112 bool strictRoot_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1113 bool allowDroppedNullPlaceholders_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1114 bool allowNumericKeys_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1115 bool allowSingleQuotes_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1116 bool failIfExtra_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1117 bool rejectDupKeys_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1118 bool allowSpecialFloats_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1119 size_t stackLimit_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1120 }; // OurFeatures
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1121
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1122 // exact copy of Implementation of class Features
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1123 // ////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1124
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1125 OurFeatures OurFeatures::all() { return {}; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1126
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1127 // Implementation of class Reader
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1128 // ////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1129
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1130 // exact copy of Reader, renamed to OurReader
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1131 class OurReader {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1132 public:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1133 typedef char Char;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1134 typedef const Char* Location;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1135 struct StructuredError {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1136 ptrdiff_t offset_start;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1137 ptrdiff_t offset_limit;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1138 String message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1139 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1140
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1141 OurReader(OurFeatures const& features);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1142 bool parse(const char* beginDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1143 const char* endDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1144 Value& root,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1145 bool collectComments = true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1146 String getFormattedErrorMessages() const;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1147 std::vector<StructuredError> getStructuredErrors() const;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1148 bool pushError(const Value& value, const String& message);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1149 bool pushError(const Value& value, const String& message, const Value& extra);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1150 bool good() const;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1151
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1152 private:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1153 OurReader(OurReader const&); // no impl
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1154 void operator=(OurReader const&); // no impl
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1155
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1156 enum TokenType {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1157 tokenEndOfStream = 0,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1158 tokenObjectBegin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1159 tokenObjectEnd,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1160 tokenArrayBegin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1161 tokenArrayEnd,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1162 tokenString,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1163 tokenNumber,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1164 tokenTrue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1165 tokenFalse,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1166 tokenNull,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1167 tokenNaN,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1168 tokenPosInf,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1169 tokenNegInf,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1170 tokenArraySeparator,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1171 tokenMemberSeparator,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1172 tokenComment,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1173 tokenError
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1174 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1175
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1176 class Token {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1177 public:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1178 TokenType type_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1179 Location start_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1180 Location end_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1181 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1182
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1183 class ErrorInfo {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1184 public:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1185 Token token_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1186 String message_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1187 Location extra_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1188 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1189
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1190 typedef std::deque<ErrorInfo> Errors;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1191
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1192 bool readToken(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1193 void skipSpaces();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1194 bool match(Location pattern, int patternLength);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1195 bool readComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1196 bool readCStyleComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1197 bool readCppStyleComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1198 bool readString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1199 bool readStringSingleQuote();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1200 bool readNumber(bool checkInf);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1201 bool readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1202 bool readObject(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1203 bool readArray(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1204 bool decodeNumber(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1205 bool decodeNumber(Token& token, Value& decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1206 bool decodeString(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1207 bool decodeString(Token& token, String& decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1208 bool decodeDouble(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1209 bool decodeDouble(Token& token, Value& decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1210 bool decodeUnicodeCodePoint(Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1211 Location& current,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1212 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1213 unsigned int& unicode);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1214 bool decodeUnicodeEscapeSequence(Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1215 Location& current,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1216 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1217 unsigned int& unicode);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1218 bool addError(const String& message, Token& token, Location extra = nullptr);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1219 bool recoverFromError(TokenType skipUntilToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1220 bool addErrorAndRecover(const String& message,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1221 Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1222 TokenType skipUntilToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1223 void skipUntilSpace();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1224 Value& currentValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1225 Char getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1226 void
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1227 getLocationLineAndColumn(Location location, int& line, int& column) const;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1228 String getLocationLineAndColumn(Location location) const;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1229 void addComment(Location begin, Location end, CommentPlacement placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1230 void skipCommentTokens(Token& token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1231
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1232 static String normalizeEOL(Location begin, Location end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1233 static bool containsNewLine(Location begin, Location end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1234
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1235 typedef std::stack<Value*> Nodes;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1236 Nodes nodes_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1237 Errors errors_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1238 String document_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1239 Location begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1240 Location end_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1241 Location current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1242 Location lastValueEnd_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1243 Value* lastValue_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1244 String commentsBefore_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1245
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1246 OurFeatures const features_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1247 bool collectComments_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1248 }; // OurReader
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1249
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1250 // complete copy of Read impl, for OurReader
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1251
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1252 bool OurReader::containsNewLine(OurReader::Location begin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1253 OurReader::Location end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1254 for (; begin < end; ++begin)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1255 if (*begin == '\n' || *begin == '\r')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1256 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1257 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1258 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1259
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1260 OurReader::OurReader(OurFeatures const& features)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1261 : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1262 lastValue_(), commentsBefore_(), features_(features), collectComments_() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1263 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1264
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1265 bool OurReader::parse(const char* beginDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1266 const char* endDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1267 Value& root,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1268 bool collectComments) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1269 if (!features_.allowComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1270 collectComments = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1271 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1272
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1273 begin_ = beginDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1274 end_ = endDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1275 collectComments_ = collectComments;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1276 current_ = begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1277 lastValueEnd_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1278 lastValue_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1279 commentsBefore_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1280 errors_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1281 while (!nodes_.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1282 nodes_.pop();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1283 nodes_.push(&root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1284
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1285 bool successful = readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1286 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1287 skipCommentTokens(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1288 if (features_.failIfExtra_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1289 if ((features_.strictRoot_ || token.type_ != tokenError) &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1290 token.type_ != tokenEndOfStream) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1291 addError("Extra non-whitespace after JSON value.", token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1292 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1293 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1294 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1295 if (collectComments_ && !commentsBefore_.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1296 root.setComment(commentsBefore_, commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1297 if (features_.strictRoot_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1298 if (!root.isArray() && !root.isObject()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1299 // Set error location to start of doc, ideally should be first token found
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1300 // in doc
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1301 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1302 token.start_ = beginDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1303 token.end_ = endDoc;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1304 addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1305 "A valid JSON document must be either an array or an object value.",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1306 token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1307 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1308 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1309 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1310 return successful;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1311 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1312
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1313 bool OurReader::readValue() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1314 // To preserve the old behaviour we cast size_t to int.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1315 if (nodes_.size() > features_.stackLimit_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1316 throwRuntimeError("Exceeded stackLimit in readValue().");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1317 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1318 skipCommentTokens(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1319 bool successful = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1320
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1321 if (collectComments_ && !commentsBefore_.empty()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1322 currentValue().setComment(commentsBefore_, commentBefore);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1323 commentsBefore_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1324 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1325
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1326 switch (token.type_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1327 case tokenObjectBegin:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1328 successful = readObject(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1329 currentValue().setOffsetLimit(current_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1330 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1331 case tokenArrayBegin:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1332 successful = readArray(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1333 currentValue().setOffsetLimit(current_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1334 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1335 case tokenNumber:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1336 successful = decodeNumber(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1337 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1338 case tokenString:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1339 successful = decodeString(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1340 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1341 case tokenTrue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1342 Value v(true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1343 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1344 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1345 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1346 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1347 case tokenFalse: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1348 Value v(false);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1349 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1350 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1351 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1352 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1353 case tokenNull: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1354 Value v;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1355 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1356 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1357 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1358 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1359 case tokenNaN: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1360 Value v(std::numeric_limits<double>::quiet_NaN());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1361 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1362 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1363 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1364 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1365 case tokenPosInf: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1366 Value v(std::numeric_limits<double>::infinity());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1367 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1368 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1369 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1370 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1371 case tokenNegInf: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1372 Value v(-std::numeric_limits<double>::infinity());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1373 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1374 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1375 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1376 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1377 case tokenArraySeparator:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1378 case tokenObjectEnd:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1379 case tokenArrayEnd:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1380 if (features_.allowDroppedNullPlaceholders_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1381 // "Un-read" the current token and mark the current value as a null
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1382 // token.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1383 current_--;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1384 Value v;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1385 currentValue().swapPayload(v);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1386 currentValue().setOffsetStart(current_ - begin_ - 1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1387 currentValue().setOffsetLimit(current_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1388 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1389 } // else, fall through ...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1390 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1391 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1392 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1393 return addError("Syntax error: value, object or array expected.", token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1394 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1395
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1396 if (collectComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1397 lastValueEnd_ = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1398 lastValue_ = &currentValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1399 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1400
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1401 return successful;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1402 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1403
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1404 void OurReader::skipCommentTokens(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1405 if (features_.allowComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1406 do {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1407 readToken(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1408 } while (token.type_ == tokenComment);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1409 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1410 readToken(token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1411 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1412 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1413
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1414 bool OurReader::readToken(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1415 skipSpaces();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1416 token.start_ = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1417 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1418 bool ok = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1419 switch (c) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1420 case '{':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1421 token.type_ = tokenObjectBegin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1422 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1423 case '}':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1424 token.type_ = tokenObjectEnd;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1425 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1426 case '[':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1427 token.type_ = tokenArrayBegin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1428 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1429 case ']':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1430 token.type_ = tokenArrayEnd;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1431 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1432 case '"':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1433 token.type_ = tokenString;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1434 ok = readString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1435 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1436 case '\'':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1437 if (features_.allowSingleQuotes_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1438 token.type_ = tokenString;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1439 ok = readStringSingleQuote();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1440 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1441 } // else fall through
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1442 case '/':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1443 token.type_ = tokenComment;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1444 ok = readComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1445 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1446 case '0':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1447 case '1':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1448 case '2':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1449 case '3':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1450 case '4':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1451 case '5':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1452 case '6':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1453 case '7':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1454 case '8':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1455 case '9':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1456 token.type_ = tokenNumber;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1457 readNumber(false);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1458 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1459 case '-':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1460 if (readNumber(true)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1461 token.type_ = tokenNumber;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1462 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1463 token.type_ = tokenNegInf;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1464 ok = features_.allowSpecialFloats_ && match("nfinity", 7);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1465 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1466 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1467 case 't':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1468 token.type_ = tokenTrue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1469 ok = match("rue", 3);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1470 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1471 case 'f':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1472 token.type_ = tokenFalse;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1473 ok = match("alse", 4);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1474 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1475 case 'n':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1476 token.type_ = tokenNull;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1477 ok = match("ull", 3);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1478 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1479 case 'N':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1480 if (features_.allowSpecialFloats_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1481 token.type_ = tokenNaN;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1482 ok = match("aN", 2);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1483 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1484 ok = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1485 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1486 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1487 case 'I':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1488 if (features_.allowSpecialFloats_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1489 token.type_ = tokenPosInf;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1490 ok = match("nfinity", 7);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1491 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1492 ok = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1493 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1494 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1495 case ',':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1496 token.type_ = tokenArraySeparator;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1497 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1498 case ':':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1499 token.type_ = tokenMemberSeparator;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1500 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1501 case 0:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1502 token.type_ = tokenEndOfStream;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1503 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1504 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1505 ok = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1506 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1507 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1508 if (!ok)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1509 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1510 token.end_ = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1511 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1512 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1513
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1514 void OurReader::skipSpaces() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1515 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1516 Char c = *current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1517 if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1518 ++current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1519 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1520 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1521 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1522 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1523
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1524 bool OurReader::match(Location pattern, int patternLength) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1525 if (end_ - current_ < patternLength)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1526 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1527 int index = patternLength;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1528 while (index--)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1529 if (current_[index] != pattern[index])
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1530 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1531 current_ += patternLength;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1532 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1533 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1534
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1535 bool OurReader::readComment() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1536 Location commentBegin = current_ - 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1537 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1538 bool successful = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1539 if (c == '*')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1540 successful = readCStyleComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1541 else if (c == '/')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1542 successful = readCppStyleComment();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1543 if (!successful)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1544 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1545
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1546 if (collectComments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1547 CommentPlacement placement = commentBefore;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1548 if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1549 if (c != '*' || !containsNewLine(commentBegin, current_))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1550 placement = commentAfterOnSameLine;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1551 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1552
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1553 addComment(commentBegin, current_, placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1554 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1555 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1556 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1557
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1558 String OurReader::normalizeEOL(OurReader::Location begin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1559 OurReader::Location end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1560 String normalized;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1561 normalized.reserve(static_cast<size_t>(end - begin));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1562 OurReader::Location current = begin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1563 while (current != end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1564 char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1565 if (c == '\r') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1566 if (current != end && *current == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1567 // convert dos EOL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1568 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1569 // convert Mac EOL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1570 normalized += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1571 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1572 normalized += c;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1573 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1574 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1575 return normalized;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1576 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1577
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1578 void OurReader::addComment(Location begin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1579 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1580 CommentPlacement placement) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1581 assert(collectComments_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1582 const String& normalized = normalizeEOL(begin, end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1583 if (placement == commentAfterOnSameLine) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1584 assert(lastValue_ != nullptr);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1585 lastValue_->setComment(normalized, placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1586 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1587 commentsBefore_ += normalized;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1588 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1589 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1590
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1591 bool OurReader::readCStyleComment() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1592 while ((current_ + 1) < end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1593 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1594 if (c == '*' && *current_ == '/')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1595 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1596 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1597 return getNextChar() == '/';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1598 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1599
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1600 bool OurReader::readCppStyleComment() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1601 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1602 Char c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1603 if (c == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1604 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1605 if (c == '\r') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1606 // Consume DOS EOL. It will be normalized in addComment.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1607 if (current_ != end_ && *current_ == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1608 getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1609 // Break on Moc OS 9 EOL.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1610 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1611 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1612 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1613 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1614 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1615
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1616 bool OurReader::readNumber(bool checkInf) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1617 const char* p = current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1618 if (checkInf && p != end_ && *p == 'I') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1619 current_ = ++p;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1620 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1621 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1622 char c = '0'; // stopgap for already consumed character
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1623 // integral part
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1624 while (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1625 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1626 // fractional part
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1627 if (c == '.') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1628 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1629 while (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1630 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1631 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1632 // exponential part
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1633 if (c == 'e' || c == 'E') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1634 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1635 if (c == '+' || c == '-')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1636 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1637 while (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1638 c = (current_ = p) < end_ ? *p++ : '\0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1639 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1640 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1641 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1642 bool OurReader::readString() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1643 Char c = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1644 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1645 c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1646 if (c == '\\')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1647 getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1648 else if (c == '"')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1649 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1650 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1651 return c == '"';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1652 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1653
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1654 bool OurReader::readStringSingleQuote() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1655 Char c = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1656 while (current_ != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1657 c = getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1658 if (c == '\\')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1659 getNextChar();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1660 else if (c == '\'')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1661 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1662 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1663 return c == '\'';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1664 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1665
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1666 bool OurReader::readObject(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1667 Token tokenName;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1668 String name;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1669 Value init(objectValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1670 currentValue().swapPayload(init);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1671 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1672 while (readToken(tokenName)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1673 bool initialTokenOk = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1674 while (tokenName.type_ == tokenComment && initialTokenOk)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1675 initialTokenOk = readToken(tokenName);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1676 if (!initialTokenOk)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1677 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1678 if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1679 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1680 name.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1681 if (tokenName.type_ == tokenString) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1682 if (!decodeString(tokenName, name))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1683 return recoverFromError(tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1684 } else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1685 Value numberName;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1686 if (!decodeNumber(tokenName, numberName))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1687 return recoverFromError(tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1688 name = numberName.asString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1689 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1690 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1691 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1692
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1693 Token colon;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1694 if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1695 return addErrorAndRecover("Missing ':' after object member name", colon,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1696 tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1697 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1698 if (name.length() >= (1U << 30))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1699 throwRuntimeError("keylength >= 2^30");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1700 if (features_.rejectDupKeys_ && currentValue().isMember(name)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1701 String msg = "Duplicate key: '" + name + "'";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1702 return addErrorAndRecover(msg, tokenName, tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1703 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1704 Value& value = currentValue()[name];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1705 nodes_.push(&value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1706 bool ok = readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1707 nodes_.pop();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1708 if (!ok) // error already set
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1709 return recoverFromError(tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1710
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1711 Token comma;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1712 if (!readToken(comma) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1713 (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1714 comma.type_ != tokenComment)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1715 return addErrorAndRecover("Missing ',' or '}' in object declaration",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1716 comma, tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1717 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1718 bool finalizeTokenOk = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1719 while (comma.type_ == tokenComment && finalizeTokenOk)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1720 finalizeTokenOk = readToken(comma);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1721 if (comma.type_ == tokenObjectEnd)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1722 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1723 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1724 return addErrorAndRecover("Missing '}' or object member name", tokenName,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1725 tokenObjectEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1726 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1727
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1728 bool OurReader::readArray(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1729 Value init(arrayValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1730 currentValue().swapPayload(init);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1731 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1732 skipSpaces();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1733 if (current_ != end_ && *current_ == ']') // empty array
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1734 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1735 Token endArray;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1736 readToken(endArray);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1737 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1738 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1739 int index = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1740 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1741 Value& value = currentValue()[index++];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1742 nodes_.push(&value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1743 bool ok = readValue();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1744 nodes_.pop();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1745 if (!ok) // error already set
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1746 return recoverFromError(tokenArrayEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1747
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1748 Token currentToken;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1749 // Accept Comment after last item in the array.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1750 ok = readToken(currentToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1751 while (currentToken.type_ == tokenComment && ok) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1752 ok = readToken(currentToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1753 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1754 bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1755 currentToken.type_ != tokenArrayEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1756 if (!ok || badTokenType) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1757 return addErrorAndRecover("Missing ',' or ']' in array declaration",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1758 currentToken, tokenArrayEnd);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1759 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1760 if (currentToken.type_ == tokenArrayEnd)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1761 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1762 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1763 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1764 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1765
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1766 bool OurReader::decodeNumber(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1767 Value decoded;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1768 if (!decodeNumber(token, decoded))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1769 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1770 currentValue().swapPayload(decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1771 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1772 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1773 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1774 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1775
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1776 bool OurReader::decodeNumber(Token& token, Value& decoded) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1777 // Attempts to parse the number as an integer. If the number is
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1778 // larger than the maximum supported value of an integer then
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1779 // we decode the number as a double.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1780 Location current = token.start_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1781 bool isNegative = *current == '-';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1782 if (isNegative)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1783 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1784 // TODO: Help the compiler do the div and mod at compile time or get rid of
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1785 // them.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1786 Value::LargestUInt maxIntegerValue =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1787 isNegative ? Value::LargestUInt(Value::minLargestInt)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1788 : Value::maxLargestUInt;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1789 Value::LargestUInt threshold = maxIntegerValue / 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1790 Value::LargestUInt value = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1791 while (current < token.end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1792 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1793 if (c < '0' || c > '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1794 return decodeDouble(token, decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1795 auto digit(static_cast<Value::UInt>(c - '0'));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1796 if (value >= threshold) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1797 // We've hit or exceeded the max value divided by 10 (rounded down). If
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1798 // a) we've only just touched the limit, b) this is the last digit, and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1799 // c) it's small enough to fit in that rounding delta, we're okay.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1800 // Otherwise treat this number as a double to avoid overflow.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1801 if (value > threshold || current != token.end_ ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1802 digit > maxIntegerValue % 10) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1803 return decodeDouble(token, decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1804 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1805 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1806 value = value * 10 + digit;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1807 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1808 if (isNegative)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1809 decoded = -Value::LargestInt(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1810 else if (value <= Value::LargestUInt(Value::maxInt))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1811 decoded = Value::LargestInt(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1812 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1813 decoded = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1814 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1815 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1816
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1817 bool OurReader::decodeDouble(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1818 Value decoded;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1819 if (!decodeDouble(token, decoded))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1820 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1821 currentValue().swapPayload(decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1822 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1823 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1824 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1825 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1826
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1827 bool OurReader::decodeDouble(Token& token, Value& decoded) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1828 double value = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1829 const int bufferSize = 32;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1830 int count;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1831 ptrdiff_t const length = token.end_ - token.start_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1832
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1833 // Sanity check to avoid buffer overflow exploits.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1834 if (length < 0) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1835 return addError("Unable to parse token length", token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1836 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1837 auto const ulength = static_cast<size_t>(length);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1838
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1839 // Avoid using a string constant for the format control string given to
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1840 // sscanf, as this can cause hard to debug crashes on OS X. See here for more
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1841 // info:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1842 //
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1843 // http://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/Incompatibilities.html
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1844 char format[] = "%lf";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1845
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1846 if (length <= bufferSize) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1847 Char buffer[bufferSize + 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1848 memcpy(buffer, token.start_, ulength);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1849 buffer[length] = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1850 fixNumericLocaleInput(buffer, buffer + length);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1851 count = sscanf(buffer, format, &value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1852 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1853 String buffer(token.start_, token.end_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1854 count = sscanf(buffer.c_str(), format, &value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1855 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1856
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1857 if (count != 1)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1858 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1859 "'" + String(token.start_, token.end_) + "' is not a number.", token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1860 decoded = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1861 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1862 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1863
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1864 bool OurReader::decodeString(Token& token) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1865 String decoded_string;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1866 if (!decodeString(token, decoded_string))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1867 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1868 Value decoded(decoded_string);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1869 currentValue().swapPayload(decoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1870 currentValue().setOffsetStart(token.start_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1871 currentValue().setOffsetLimit(token.end_ - begin_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1872 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1873 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1874
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1875 bool OurReader::decodeString(Token& token, String& decoded) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1876 decoded.reserve(static_cast<size_t>(token.end_ - token.start_ - 2));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1877 Location current = token.start_ + 1; // skip '"'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1878 Location end = token.end_ - 1; // do not include '"'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1879 while (current != end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1880 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1881 if (c == '"')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1882 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1883 else if (c == '\\') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1884 if (current == end)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1885 return addError("Empty escape sequence in string", token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1886 Char escape = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1887 switch (escape) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1888 case '"':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1889 decoded += '"';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1890 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1891 case '/':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1892 decoded += '/';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1893 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1894 case '\\':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1895 decoded += '\\';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1896 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1897 case 'b':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1898 decoded += '\b';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1899 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1900 case 'f':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1901 decoded += '\f';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1902 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1903 case 'n':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1904 decoded += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1905 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1906 case 'r':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1907 decoded += '\r';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1908 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1909 case 't':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1910 decoded += '\t';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1911 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1912 case 'u': {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1913 unsigned int unicode;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1914 if (!decodeUnicodeCodePoint(token, current, end, unicode))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1915 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1916 decoded += codePointToUTF8(unicode);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1917 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1918 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1919 return addError("Bad escape sequence in string", token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1920 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1921 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1922 decoded += c;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1923 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1924 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1925 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1926 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1927
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1928 bool OurReader::decodeUnicodeCodePoint(Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1929 Location& current,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1930 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1931 unsigned int& unicode) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1932
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1933 if (!decodeUnicodeEscapeSequence(token, current, end, unicode))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1934 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1935 if (unicode >= 0xD800 && unicode <= 0xDBFF) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1936 // surrogate pairs
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1937 if (end - current < 6)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1938 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1939 "additional six characters expected to parse unicode surrogate pair.",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1940 token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1941 if (*(current++) == '\\' && *(current++) == 'u') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1942 unsigned int surrogatePair;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1943 if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1944 unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1945 } else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1946 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1947 } else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1948 return addError("expecting another \\u token to begin the second half of "
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1949 "a unicode surrogate pair",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1950 token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1951 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1952 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1953 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1954
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1955 bool OurReader::decodeUnicodeEscapeSequence(Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1956 Location& current,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1957 Location end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1958 unsigned int& ret_unicode) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1959 if (end - current < 4)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1960 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1961 "Bad unicode escape sequence in string: four digits expected.", token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1962 current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1963 int unicode = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1964 for (int index = 0; index < 4; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1965 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1966 unicode *= 16;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1967 if (c >= '0' && c <= '9')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1968 unicode += c - '0';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1969 else if (c >= 'a' && c <= 'f')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1970 unicode += c - 'a' + 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1971 else if (c >= 'A' && c <= 'F')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1972 unicode += c - 'A' + 10;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1973 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1974 return addError(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1975 "Bad unicode escape sequence in string: hexadecimal digit expected.",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1976 token, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1977 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1978 ret_unicode = static_cast<unsigned int>(unicode);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1979 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1980 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1981
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1982 bool OurReader::addError(const String& message, Token& token, Location extra) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1983 ErrorInfo info;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1984 info.token_ = token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1985 info.message_ = message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1986 info.extra_ = extra;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1987 errors_.push_back(info);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1988 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1989 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1990
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1991 bool OurReader::recoverFromError(TokenType skipUntilToken) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1992 size_t errorCount = errors_.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1993 Token skip;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1994 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1995 if (!readToken(skip))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1996 errors_.resize(errorCount); // discard errors caused by recovery
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1997 if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1998 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
1999 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2000 errors_.resize(errorCount);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2001 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2002 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2003
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2004 bool OurReader::addErrorAndRecover(const String& message,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2005 Token& token,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2006 TokenType skipUntilToken) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2007 addError(message, token);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2008 return recoverFromError(skipUntilToken);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2009 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2010
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2011 Value& OurReader::currentValue() { return *(nodes_.top()); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2012
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2013 OurReader::Char OurReader::getNextChar() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2014 if (current_ == end_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2015 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2016 return *current_++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2017 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2018
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2019 void OurReader::getLocationLineAndColumn(Location location,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2020 int& line,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2021 int& column) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2022 Location current = begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2023 Location lastLineStart = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2024 line = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2025 while (current < location && current != end_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2026 Char c = *current++;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2027 if (c == '\r') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2028 if (*current == '\n')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2029 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2030 lastLineStart = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2031 ++line;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2032 } else if (c == '\n') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2033 lastLineStart = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2034 ++line;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2035 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2036 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2037 // column & line start at 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2038 column = int(location - lastLineStart) + 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2039 ++line;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2040 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2041
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2042 String OurReader::getLocationLineAndColumn(Location location) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2043 int line, column;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2044 getLocationLineAndColumn(location, line, column);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2045 char buffer[18 + 16 + 16 + 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2046 jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2047 return buffer;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2048 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2049
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2050 String OurReader::getFormattedErrorMessages() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2051 String formattedMessage;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2052 for (const auto& error : errors_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2053 formattedMessage +=
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2054 "* " + getLocationLineAndColumn(error.token_.start_) + "\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2055 formattedMessage += " " + error.message_ + "\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2056 if (error.extra_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2057 formattedMessage +=
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2058 "See " + getLocationLineAndColumn(error.extra_) + " for detail.\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2059 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2060 return formattedMessage;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2061 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2062
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2063 std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2064 std::vector<OurReader::StructuredError> allErrors;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2065 for (const auto& error : errors_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2066 OurReader::StructuredError structured;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2067 structured.offset_start = error.token_.start_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2068 structured.offset_limit = error.token_.end_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2069 structured.message = error.message_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2070 allErrors.push_back(structured);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2071 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2072 return allErrors;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2073 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2074
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2075 bool OurReader::pushError(const Value& value, const String& message) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2076 ptrdiff_t length = end_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2077 if (value.getOffsetStart() > length || value.getOffsetLimit() > length)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2078 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2079 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2080 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2081 token.start_ = begin_ + value.getOffsetStart();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2082 token.end_ = end_ + value.getOffsetLimit();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2083 ErrorInfo info;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2084 info.token_ = token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2085 info.message_ = message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2086 info.extra_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2087 errors_.push_back(info);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2088 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2089 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2090
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2091 bool OurReader::pushError(const Value& value,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2092 const String& message,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2093 const Value& extra) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2094 ptrdiff_t length = end_ - begin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2095 if (value.getOffsetStart() > length || value.getOffsetLimit() > length ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2096 extra.getOffsetLimit() > length)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2097 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2098 Token token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2099 token.type_ = tokenError;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2100 token.start_ = begin_ + value.getOffsetStart();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2101 token.end_ = begin_ + value.getOffsetLimit();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2102 ErrorInfo info;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2103 info.token_ = token;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2104 info.message_ = message;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2105 info.extra_ = begin_ + extra.getOffsetStart();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2106 errors_.push_back(info);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2107 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2108 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2109
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2110 bool OurReader::good() const { return errors_.empty(); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2111
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2112 class OurCharReader : public CharReader {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2113 bool const collectComments_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2114 OurReader reader_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2115
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2116 public:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2117 OurCharReader(bool collectComments, OurFeatures const& features)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2118 : collectComments_(collectComments), reader_(features) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2119 bool parse(char const* beginDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2120 char const* endDoc,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2121 Value* root,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2122 String* errs) override {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2123 bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2124 if (errs) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2125 *errs = reader_.getFormattedErrorMessages();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2126 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2127 return ok;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2128 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2129 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2130
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2131 CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2132 CharReaderBuilder::~CharReaderBuilder() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2133 CharReader* CharReaderBuilder::newCharReader() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2134 bool collectComments = settings_["collectComments"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2135 OurFeatures features = OurFeatures::all();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2136 features.allowComments_ = settings_["allowComments"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2137 features.strictRoot_ = settings_["strictRoot"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2138 features.allowDroppedNullPlaceholders_ =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2139 settings_["allowDroppedNullPlaceholders"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2140 features.allowNumericKeys_ = settings_["allowNumericKeys"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2141 features.allowSingleQuotes_ = settings_["allowSingleQuotes"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2142 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2143 features.stackLimit_ = settings_["stackLimit"].asUInt64();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2144 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2145 features.stackLimit_ = settings_["stackLimit"].asUInt();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2146 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2147 features.failIfExtra_ = settings_["failIfExtra"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2148 features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2149 features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2150 return new OurCharReader(collectComments, features);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2151 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2152 static void getValidReaderKeys(std::set<String>* valid_keys) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2153 valid_keys->clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2154 valid_keys->insert("collectComments");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2155 valid_keys->insert("allowComments");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2156 valid_keys->insert("strictRoot");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2157 valid_keys->insert("allowDroppedNullPlaceholders");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2158 valid_keys->insert("allowNumericKeys");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2159 valid_keys->insert("allowSingleQuotes");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2160 valid_keys->insert("stackLimit");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2161 valid_keys->insert("failIfExtra");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2162 valid_keys->insert("rejectDupKeys");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2163 valid_keys->insert("allowSpecialFloats");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2164 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2165 bool CharReaderBuilder::validate(Json::Value* invalid) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2166 Json::Value my_invalid;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2167 if (!invalid)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2168 invalid = &my_invalid; // so we do not need to test for NULL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2169 Json::Value& inv = *invalid;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2170 std::set<String> valid_keys;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2171 getValidReaderKeys(&valid_keys);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2172 Value::Members keys = settings_.getMemberNames();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2173 size_t n = keys.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2174 for (size_t i = 0; i < n; ++i) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2175 String const& key = keys[i];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2176 if (valid_keys.find(key) == valid_keys.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2177 inv[key] = settings_[key];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2178 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2179 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2180 return inv.empty();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2181 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2182 Value& CharReaderBuilder::operator[](const String& key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2183 return settings_[key];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2184 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2185 // static
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2186 void CharReaderBuilder::strictMode(Json::Value* settings) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2187 //! [CharReaderBuilderStrictMode]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2188 (*settings)["allowComments"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2189 (*settings)["strictRoot"] = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2190 (*settings)["allowDroppedNullPlaceholders"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2191 (*settings)["allowNumericKeys"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2192 (*settings)["allowSingleQuotes"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2193 (*settings)["stackLimit"] = 1000;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2194 (*settings)["failIfExtra"] = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2195 (*settings)["rejectDupKeys"] = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2196 (*settings)["allowSpecialFloats"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2197 //! [CharReaderBuilderStrictMode]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2198 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2199 // static
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2200 void CharReaderBuilder::setDefaults(Json::Value* settings) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2201 //! [CharReaderBuilderDefaults]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2202 (*settings)["collectComments"] = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2203 (*settings)["allowComments"] = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2204 (*settings)["strictRoot"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2205 (*settings)["allowDroppedNullPlaceholders"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2206 (*settings)["allowNumericKeys"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2207 (*settings)["allowSingleQuotes"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2208 (*settings)["stackLimit"] = 1000;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2209 (*settings)["failIfExtra"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2210 (*settings)["rejectDupKeys"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2211 (*settings)["allowSpecialFloats"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2212 //! [CharReaderBuilderDefaults]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2213 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2214
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2215 //////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2216 // global functions
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2217
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2218 bool parseFromStream(CharReader::Factory const& fact,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2219 IStream& sin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2220 Value* root,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2221 String* errs) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2222 OStringStream ssin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2223 ssin << sin.rdbuf();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2224 String doc = ssin.str();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2225 char const* begin = doc.data();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2226 char const* end = begin + doc.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2227 // Note that we do not actually need a null-terminator.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2228 CharReaderPtr const reader(fact.newCharReader());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2229 return reader->parse(begin, end, root, errs);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2230 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2231
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2232 IStream& operator>>(IStream& sin, Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2233 CharReaderBuilder b;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2234 String errs;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2235 bool ok = parseFromStream(b, sin, &root, &errs);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2236 if (!ok) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2237 throwRuntimeError(errs);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2238 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2239 return sin;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2240 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2241
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2242 } // namespace Json
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2243
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2244 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2245 // End of content of file: src/lib_json/json_reader.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2246 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2247
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2248
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2249
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2250
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2251
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2252
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2253 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2254 // Beginning of content of file: src/lib_json/json_valueiterator.inl
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2255 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2256
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2257 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2258 // Distributed under MIT license, or public domain if desired and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2259 // recognized in your jurisdiction.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2260 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2261
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2262 // included by json_value.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2263
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2264 namespace Json {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2265
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2266 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2267 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2268 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2269 // class ValueIteratorBase
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2270 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2271 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2272 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2273
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2274 ValueIteratorBase::ValueIteratorBase() : current_() {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2275
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2276 ValueIteratorBase::ValueIteratorBase(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2277 const Value::ObjectValues::iterator& current)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2278 : current_(current), isNull_(false) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2279
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2280 Value& ValueIteratorBase::deref() const { return current_->second; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2281
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2282 void ValueIteratorBase::increment() { ++current_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2283
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2284 void ValueIteratorBase::decrement() { --current_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2285
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2286 ValueIteratorBase::difference_type
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2287 ValueIteratorBase::computeDistance(const SelfType& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2288 #ifdef JSON_USE_CPPTL_SMALLMAP
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2289 return other.current_ - current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2290 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2291 // Iterator for null value are initialized using the default
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2292 // constructor, which initialize current_ to the default
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2293 // std::map::iterator. As begin() and end() are two instance
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2294 // of the default std::map::iterator, they can not be compared.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2295 // To allow this, we handle this comparison specifically.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2296 if (isNull_ && other.isNull_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2297 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2298 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2299
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2300 // Usage of std::distance is not portable (does not compile with Sun Studio 12
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2301 // RogueWave STL,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2302 // which is the one used by default).
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2303 // Using a portable hand-made version for non random iterator instead:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2304 // return difference_type( std::distance( current_, other.current_ ) );
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2305 difference_type myDistance = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2306 for (Value::ObjectValues::iterator it = current_; it != other.current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2307 ++it) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2308 ++myDistance;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2309 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2310 return myDistance;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2311 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2312 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2313
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2314 bool ValueIteratorBase::isEqual(const SelfType& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2315 if (isNull_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2316 return other.isNull_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2317 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2318 return current_ == other.current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2319 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2320
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2321 void ValueIteratorBase::copy(const SelfType& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2322 current_ = other.current_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2323 isNull_ = other.isNull_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2324 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2325
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2326 Value ValueIteratorBase::key() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2327 const Value::CZString czstring = (*current_).first;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2328 if (czstring.data()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2329 if (czstring.isStaticString())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2330 return Value(StaticString(czstring.data()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2331 return Value(czstring.data(), czstring.data() + czstring.length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2332 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2333 return Value(czstring.index());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2334 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2335
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2336 UInt ValueIteratorBase::index() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2337 const Value::CZString czstring = (*current_).first;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2338 if (!czstring.data())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2339 return czstring.index();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2340 return Value::UInt(-1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2341 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2342
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2343 String ValueIteratorBase::name() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2344 char const* keey;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2345 char const* end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2346 keey = memberName(&end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2347 if (!keey)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2348 return String();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2349 return String(keey, end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2350 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2351
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2352 char const* ValueIteratorBase::memberName() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2353 const char* cname = (*current_).first.data();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2354 return cname ? cname : "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2355 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2356
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2357 char const* ValueIteratorBase::memberName(char const** end) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2358 const char* cname = (*current_).first.data();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2359 if (!cname) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2360 *end = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2361 return nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2362 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2363 *end = cname + (*current_).first.length();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2364 return cname;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2365 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2366
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2367 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2368 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2369 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2370 // class ValueConstIterator
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2371 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2372 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2373 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2374
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2375 ValueConstIterator::ValueConstIterator() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2376
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2377 ValueConstIterator::ValueConstIterator(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2378 const Value::ObjectValues::iterator& current)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2379 : ValueIteratorBase(current) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2380
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2381 ValueConstIterator::ValueConstIterator(ValueIterator const& other)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2382 : ValueIteratorBase(other) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2383
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2384 ValueConstIterator& ValueConstIterator::
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2385 operator=(const ValueIteratorBase& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2386 copy(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2387 return *this;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2388 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2389
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2390 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2391 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2392 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2393 // class ValueIterator
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2394 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2395 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2396 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2397
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2398 ValueIterator::ValueIterator() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2399
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2400 ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2401 : ValueIteratorBase(current) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2402
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2403 ValueIterator::ValueIterator(const ValueConstIterator& other)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2404 : ValueIteratorBase(other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2405 throwRuntimeError("ConstIterator to Iterator should never be allowed.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2406 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2407
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2408 ValueIterator::ValueIterator(const ValueIterator& other) = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2409
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2410 ValueIterator& ValueIterator::operator=(const SelfType& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2411 copy(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2412 return *this;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2413 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2414
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2415 } // namespace Json
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2416
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2417 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2418 // End of content of file: src/lib_json/json_valueiterator.inl
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2419 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2420
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2421
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2422
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2423
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2424
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2425
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2426 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2427 // Beginning of content of file: src/lib_json/json_value.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2428 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2429
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2430 // Copyright 2011 Baptiste Lepilleur and The JsonCpp Authors
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2431 // Distributed under MIT license, or public domain if desired and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2432 // recognized in your jurisdiction.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2433 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2434
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2435 #if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2436 #include <json/assertions.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2437 #include <json/value.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2438 #include <json/writer.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2439 #endif // if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2440 #include <cassert>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2441 #include <cmath>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2442 #include <cstring>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2443 #include <sstream>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2444 #include <utility>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2445 #ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2446 #include <cpptl/conststring.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2447 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2448 #include <algorithm> // min()
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2449 #include <cstddef> // size_t
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2450
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2451 // Provide implementation equivalent of std::snprintf for older _MSC compilers
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2452 #if defined(_MSC_VER) && _MSC_VER < 1900
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2453 #include <stdarg.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2454 static int msvc_pre1900_c99_vsnprintf(char* outBuf,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2455 size_t size,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2456 const char* format,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2457 va_list ap) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2458 int count = -1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2459 if (size != 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2460 count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2461 if (count == -1)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2462 count = _vscprintf(format, ap);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2463 return count;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2464 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2465
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2466 int JSON_API msvc_pre1900_c99_snprintf(char* outBuf,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2467 size_t size,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2468 const char* format,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2469 ...) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2470 va_list ap;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2471 va_start(ap, format);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2472 const int count = msvc_pre1900_c99_vsnprintf(outBuf, size, format, ap);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2473 va_end(ap);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2474 return count;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2475 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2476 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2477
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2478 // Disable warning C4702 : unreachable code
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2479 #if defined(_MSC_VER)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2480 #pragma warning(disable : 4702)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2481 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2482
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2483 #define JSON_ASSERT_UNREACHABLE assert(false)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2484
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2485 namespace Json {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2486
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2487 // This is a walkaround to avoid the static initialization of Value::null.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2488 // kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2489 // 8 (instead of 4) as a bit of future-proofing.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2490 #if defined(__ARMEL__)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2491 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2492 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2493 #define ALIGNAS(byte_alignment)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2494 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2495 // static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2496 // const unsigned char& kNullRef = kNull[0];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2497 // const Value& Value::null = reinterpret_cast<const Value&>(kNullRef);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2498 // const Value& Value::nullRef = null;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2500 // static
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2501 Value const& Value::nullSingleton() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2502 static Value const nullStatic;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2503 return nullStatic;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2504 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2505
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2506 // for backwards compatibility, we'll leave these global references around, but
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2507 // DO NOT use them in JSONCPP library code any more!
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2508 Value const& Value::null = Value::nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2509 Value const& Value::nullRef = Value::nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2510
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2511 const Int Value::minInt = Int(~(UInt(-1) / 2));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2512 const Int Value::maxInt = Int(UInt(-1) / 2);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2513 const UInt Value::maxUInt = UInt(-1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2514 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2515 const Int64 Value::minInt64 = Int64(~(UInt64(-1) / 2));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2516 const Int64 Value::maxInt64 = Int64(UInt64(-1) / 2);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2517 const UInt64 Value::maxUInt64 = UInt64(-1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2518 // The constant is hard-coded because some compiler have trouble
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2519 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2520 // Assumes that UInt64 is a 64 bits integer.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2521 static const double maxUInt64AsDouble = 18446744073709551615.0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2522 #endif // defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2523 const LargestInt Value::minLargestInt = LargestInt(~(LargestUInt(-1) / 2));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2524 const LargestInt Value::maxLargestInt = LargestInt(LargestUInt(-1) / 2);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2525 const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2526
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2527 const UInt Value::defaultRealPrecision = 17;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2528
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2529 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2530 template <typename T, typename U>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2531 static inline bool InRange(double d, T min, U max) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2532 // The casts can lose precision, but we are looking only for
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2533 // an approximate range. Might fail on edge cases though. ~cdunn
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2534 // return d >= static_cast<double>(min) && d <= static_cast<double>(max);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2535 return d >= min && d <= max;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2536 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2537 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2538 static inline double integerToDouble(Json::UInt64 value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2539 return static_cast<double>(Int64(value / 2)) * 2.0 +
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2540 static_cast<double>(Int64(value & 1));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2541 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2542
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2543 template <typename T> static inline double integerToDouble(T value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2544 return static_cast<double>(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2545 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2546
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2547 template <typename T, typename U>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2548 static inline bool InRange(double d, T min, U max) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2549 return d >= integerToDouble(min) && d <= integerToDouble(max);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2550 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2551 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2552
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2553 /** Duplicates the specified string value.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2554 * @param value Pointer to the string to duplicate. Must be zero-terminated if
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2555 * length is "unknown".
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2556 * @param length Length of the value. if equals to unknown, then it will be
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2557 * computed using strlen(value).
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2558 * @return Pointer on the duplicate instance of string.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2559 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2560 static inline char* duplicateStringValue(const char* value, size_t length) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2561 // Avoid an integer overflow in the call to malloc below by limiting length
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2562 // to a sane value.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2563 if (length >= static_cast<size_t>(Value::maxInt))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2564 length = Value::maxInt - 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2565
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2566 char* newString = static_cast<char*>(malloc(length + 1));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2567 if (newString == nullptr) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2568 throwRuntimeError("in Json::Value::duplicateStringValue(): "
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2569 "Failed to allocate string value buffer");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2570 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2571 memcpy(newString, value, length);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2572 newString[length] = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2573 return newString;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2574 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2575
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2576 /* Record the length as a prefix.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2577 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2578 static inline char* duplicateAndPrefixStringValue(const char* value,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2579 unsigned int length) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2580 // Avoid an integer overflow in the call to malloc below by limiting length
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2581 // to a sane value.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2582 JSON_ASSERT_MESSAGE(length <= static_cast<unsigned>(Value::maxInt) -
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2583 sizeof(unsigned) - 1U,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2584 "in Json::Value::duplicateAndPrefixStringValue(): "
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2585 "length too big for prefixing");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2586 unsigned actualLength = length + static_cast<unsigned>(sizeof(unsigned)) + 1U;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2587 char* newString = static_cast<char*>(malloc(actualLength));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2588 if (newString == nullptr) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2589 throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): "
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2590 "Failed to allocate string value buffer");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2591 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2592 *reinterpret_cast<unsigned*>(newString) = length;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2593 memcpy(newString + sizeof(unsigned), value, length);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2594 newString[actualLength - 1U] =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2595 0; // to avoid buffer over-run accidents by users later
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2596 return newString;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2597 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2598 inline static void decodePrefixedString(bool isPrefixed,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2599 char const* prefixed,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2600 unsigned* length,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2601 char const** value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2602 if (!isPrefixed) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2603 *length = static_cast<unsigned>(strlen(prefixed));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2604 *value = prefixed;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2605 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2606 *length = *reinterpret_cast<unsigned const*>(prefixed);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2607 *value = prefixed + sizeof(unsigned);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2608 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2609 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2610 /** Free the string duplicated by
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2611 * duplicateStringValue()/duplicateAndPrefixStringValue().
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2612 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2613 #if JSONCPP_USING_SECURE_MEMORY
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2614 static inline void releasePrefixedStringValue(char* value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2615 unsigned length = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2616 char const* valueDecoded;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2617 decodePrefixedString(true, value, &length, &valueDecoded);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2618 size_t const size = sizeof(unsigned) + length + 1U;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2619 memset(value, 0, size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2620 free(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2621 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2622 static inline void releaseStringValue(char* value, unsigned length) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2623 // length==0 => we allocated the strings memory
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2624 size_t size = (length == 0) ? strlen(value) : length;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2625 memset(value, 0, size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2626 free(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2627 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2628 #else // !JSONCPP_USING_SECURE_MEMORY
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2629 static inline void releasePrefixedStringValue(char* value) { free(value); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2630 static inline void releaseStringValue(char* value, unsigned) { free(value); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2631 #endif // JSONCPP_USING_SECURE_MEMORY
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2632
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2633 } // namespace Json
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2634
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2635 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2636 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2637 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2638 // ValueInternals...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2639 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2640 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2641 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2642 #if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2643
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2644 #include "json_valueiterator.inl"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2645 #endif // if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2646
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2647 namespace Json {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2648
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2649 Exception::Exception(String msg) : msg_(std::move(msg)) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2650 Exception::~Exception() JSONCPP_NOEXCEPT {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2651 char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2652 RuntimeError::RuntimeError(String const& msg) : Exception(msg) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2653 LogicError::LogicError(String const& msg) : Exception(msg) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2654 JSONCPP_NORETURN void throwRuntimeError(String const& msg) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2655 throw RuntimeError(msg);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2656 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2657 JSONCPP_NORETURN void throwLogicError(String const& msg) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2658 throw LogicError(msg);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2659 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2660
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2661 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2662 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2663 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2664 // class Value::CommentInfo
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2665 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2666 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2667 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2668
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2669 Value::CommentInfo::CommentInfo() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2670
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2671 Value::CommentInfo::~CommentInfo() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2672 if (comment_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2673 releaseStringValue(comment_, 0u);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2674 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2675
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2676 void Value::CommentInfo::setComment(const char* text, size_t len) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2677 if (comment_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2678 releaseStringValue(comment_, 0u);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2679 comment_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2680 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2681 JSON_ASSERT(text != nullptr);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2682 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2683 text[0] == '\0' || text[0] == '/',
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2684 "in Json::Value::setComment(): Comments must start with /");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2685 // It seems that /**/ style comments are acceptable as well.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2686 comment_ = duplicateStringValue(text, len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2687 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2688
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2689 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2690 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2691 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2692 // class Value::CZString
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2693 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2694 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2695 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2696
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2697 // Notes: policy_ indicates if the string was allocated when
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2698 // a string is stored.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2699
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2700 Value::CZString::CZString(ArrayIndex index) : cstr_(nullptr), index_(index) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2701
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2702 Value::CZString::CZString(char const* str,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2703 unsigned length,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2704 DuplicationPolicy allocate)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2705 : cstr_(str) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2706 // allocate != duplicate
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2707 storage_.policy_ = allocate & 0x3;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2708 storage_.length_ = length & 0x3FFFFFFF;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2709 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2710
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2711 Value::CZString::CZString(const CZString& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2712 cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ != nullptr
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2713 ? duplicateStringValue(other.cstr_, other.storage_.length_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2714 : other.cstr_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2715 storage_.policy_ =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2716 static_cast<unsigned>(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2717 other.cstr_
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2718 ? (static_cast<DuplicationPolicy>(other.storage_.policy_) ==
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2719 noDuplication
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2720 ? noDuplication
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2721 : duplicate)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2722 : static_cast<DuplicationPolicy>(other.storage_.policy_)) &
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2723 3U;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2724 storage_.length_ = other.storage_.length_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2725 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2726
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2727 #if JSON_HAS_RVALUE_REFERENCES
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2728 Value::CZString::CZString(CZString&& other)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2729 : cstr_(other.cstr_), index_(other.index_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2730 other.cstr_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2731 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2732 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2733
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2734 Value::CZString::~CZString() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2735 if (cstr_ && storage_.policy_ == duplicate) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2736 releaseStringValue(const_cast<char*>(cstr_),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2737 storage_.length_ + 1u); // +1 for null terminating
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2738 // character for sake of
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2739 // completeness but not actually
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2740 // necessary
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2741 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2742 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2743
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2744 void Value::CZString::swap(CZString& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2745 std::swap(cstr_, other.cstr_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2746 std::swap(index_, other.index_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2747 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2748
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2749 Value::CZString& Value::CZString::operator=(const CZString& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2750 cstr_ = other.cstr_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2751 index_ = other.index_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2752 return *this;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2753 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2754
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2755 #if JSON_HAS_RVALUE_REFERENCES
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2756 Value::CZString& Value::CZString::operator=(CZString&& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2757 cstr_ = other.cstr_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2758 index_ = other.index_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2759 other.cstr_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2760 return *this;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2761 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2762 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2763
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2764 bool Value::CZString::operator<(const CZString& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2765 if (!cstr_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2766 return index_ < other.index_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2767 // return strcmp(cstr_, other.cstr_) < 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2768 // Assume both are strings.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2769 unsigned this_len = this->storage_.length_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2770 unsigned other_len = other.storage_.length_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2771 unsigned min_len = std::min<unsigned>(this_len, other_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2772 JSON_ASSERT(this->cstr_ && other.cstr_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2773 int comp = memcmp(this->cstr_, other.cstr_, min_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2774 if (comp < 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2775 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2776 if (comp > 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2777 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2778 return (this_len < other_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2779 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2780
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2781 bool Value::CZString::operator==(const CZString& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2782 if (!cstr_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2783 return index_ == other.index_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2784 // return strcmp(cstr_, other.cstr_) == 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2785 // Assume both are strings.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2786 unsigned this_len = this->storage_.length_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2787 unsigned other_len = other.storage_.length_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2788 if (this_len != other_len)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2789 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2790 JSON_ASSERT(this->cstr_ && other.cstr_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2791 int comp = memcmp(this->cstr_, other.cstr_, this_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2792 return comp == 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2793 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2794
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2795 ArrayIndex Value::CZString::index() const { return index_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2796
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2797 // const char* Value::CZString::c_str() const { return cstr_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2798 const char* Value::CZString::data() const { return cstr_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2799 unsigned Value::CZString::length() const { return storage_.length_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2800 bool Value::CZString::isStaticString() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2801 return storage_.policy_ == noDuplication;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2802 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2803
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2804 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2805 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2806 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2807 // class Value::Value
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2808 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2809 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2810 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2811
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2812 /*! \internal Default constructor initialization must be equivalent to:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2813 * memset( this, 0, sizeof(Value) )
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2814 * This optimization is used in ValueInternalMap fast allocator.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2815 */
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2816 Value::Value(ValueType type) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2817 static char const emptyString[] = "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2818 initBasic(type);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2819 switch (type) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2820 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2821 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2822 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2823 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2824 value_.int_ = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2825 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2826 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2827 value_.real_ = 0.0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2828 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2829 case stringValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2830 // allocated_ == false, so this is safe.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2831 value_.string_ = const_cast<char*>(static_cast<char const*>(emptyString));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2832 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2833 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2834 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2835 value_.map_ = new ObjectValues();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2836 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2837 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2838 value_.bool_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2839 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2840 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2841 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2842 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2843 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2844
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2845 Value::Value(Int value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2846 initBasic(intValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2847 value_.int_ = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2848 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2849
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2850 Value::Value(UInt value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2851 initBasic(uintValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2852 value_.uint_ = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2853 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2854 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2855 Value::Value(Int64 value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2856 initBasic(intValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2857 value_.int_ = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2858 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2859 Value::Value(UInt64 value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2860 initBasic(uintValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2861 value_.uint_ = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2862 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2863 #endif // defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2864
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2865 Value::Value(double value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2866 initBasic(realValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2867 value_.real_ = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2868 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2869
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2870 Value::Value(const char* value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2871 initBasic(stringValue, true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2872 JSON_ASSERT_MESSAGE(value != nullptr,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2873 "Null Value Passed to Value Constructor");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2874 value_.string_ = duplicateAndPrefixStringValue(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2875 value, static_cast<unsigned>(strlen(value)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2876 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2877
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2878 Value::Value(const char* begin, const char* end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2879 initBasic(stringValue, true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2880 value_.string_ =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2881 duplicateAndPrefixStringValue(begin, static_cast<unsigned>(end - begin));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2882 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2883
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2884 Value::Value(const String& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2885 initBasic(stringValue, true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2886 value_.string_ = duplicateAndPrefixStringValue(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2887 value.data(), static_cast<unsigned>(value.length()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2888 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2889
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2890 Value::Value(const StaticString& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2891 initBasic(stringValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2892 value_.string_ = const_cast<char*>(value.c_str());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2893 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2894
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2895 #ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2896 Value::Value(const CppTL::ConstString& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2897 initBasic(stringValue, true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2898 value_.string_ = duplicateAndPrefixStringValue(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2899 value, static_cast<unsigned>(value.length()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2900 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2901 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2902
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2903 Value::Value(bool value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2904 initBasic(booleanValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2905 value_.bool_ = value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2906 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2907
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2908 Value::Value(const Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2909 dupPayload(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2910 dupMeta(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2911 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2912
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2913 Value::Value(Value&& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2914 initBasic(nullValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2915 swap(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2916 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2917
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2918 Value::~Value() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2919 releasePayload();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2920 delete[] comments_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2921 value_.uint_ = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2922 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2923
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2924 Value& Value::operator=(const Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2925 Value(other).swap(*this);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2926 return *this;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2927 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2928
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2929 Value& Value::operator=(Value&& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2930 other.swap(*this);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2931 return *this;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2932 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2933
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2934 void Value::swapPayload(Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2935 std::swap(bits_, other.bits_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2936 std::swap(value_, other.value_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2937 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2938
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2939 void Value::copyPayload(const Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2940 releasePayload();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2941 dupPayload(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2942 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2943
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2944 void Value::swap(Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2945 swapPayload(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2946 std::swap(comments_, other.comments_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2947 std::swap(start_, other.start_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2948 std::swap(limit_, other.limit_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2949 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2950
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2951 void Value::copy(const Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2952 copyPayload(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2953 delete[] comments_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2954 dupMeta(other);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2955 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2956
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2957 ValueType Value::type() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2958 return static_cast<ValueType>(bits_.value_type_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2959 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2960
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2961 int Value::compare(const Value& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2962 if (*this < other)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2963 return -1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2964 if (*this > other)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2965 return 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2966 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2967 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2968
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2969 bool Value::operator<(const Value& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2970 int typeDelta = type() - other.type();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2971 if (typeDelta)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2972 return typeDelta < 0 ? true : false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2973 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2974 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2975 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2976 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2977 return value_.int_ < other.value_.int_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2978 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2979 return value_.uint_ < other.value_.uint_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2980 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2981 return value_.real_ < other.value_.real_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2982 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2983 return value_.bool_ < other.value_.bool_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2984 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2985 if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2986 if (other.value_.string_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2987 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2988 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2989 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2990 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2991 unsigned this_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2992 unsigned other_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2993 char const* this_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2994 char const* other_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2995 decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2996 &this_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2997 decodePrefixedString(other.isAllocated(), other.value_.string_, &other_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2998 &other_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
2999 unsigned min_len = std::min<unsigned>(this_len, other_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3000 JSON_ASSERT(this_str && other_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3001 int comp = memcmp(this_str, other_str, min_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3002 if (comp < 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3003 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3004 if (comp > 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3005 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3006 return (this_len < other_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3007 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3008 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3009 case objectValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3010 int delta = int(value_.map_->size() - other.value_.map_->size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3011 if (delta)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3012 return delta < 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3013 return (*value_.map_) < (*other.value_.map_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3014 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3015 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3016 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3017 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3018 return false; // unreachable
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3019 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3020
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3021 bool Value::operator<=(const Value& other) const { return !(other < *this); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3022
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3023 bool Value::operator>=(const Value& other) const { return !(*this < other); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3024
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3025 bool Value::operator>(const Value& other) const { return other < *this; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3026
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3027 bool Value::operator==(const Value& other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3028 if (type() != other.type())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3029 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3030 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3031 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3032 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3033 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3034 return value_.int_ == other.value_.int_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3035 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3036 return value_.uint_ == other.value_.uint_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3037 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3038 return value_.real_ == other.value_.real_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3039 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3040 return value_.bool_ == other.value_.bool_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3041 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3042 if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3043 return (value_.string_ == other.value_.string_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3044 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3045 unsigned this_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3046 unsigned other_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3047 char const* this_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3048 char const* other_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3049 decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3050 &this_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3051 decodePrefixedString(other.isAllocated(), other.value_.string_, &other_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3052 &other_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3053 if (this_len != other_len)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3054 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3055 JSON_ASSERT(this_str && other_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3056 int comp = memcmp(this_str, other_str, this_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3057 return comp == 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3058 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3059 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3060 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3061 return value_.map_->size() == other.value_.map_->size() &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3062 (*value_.map_) == (*other.value_.map_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3063 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3064 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3065 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3066 return false; // unreachable
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3067 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3068
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3069 bool Value::operator!=(const Value& other) const { return !(*this == other); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3070
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3071 const char* Value::asCString() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3072 JSON_ASSERT_MESSAGE(type() == stringValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3073 "in Json::Value::asCString(): requires stringValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3074 if (value_.string_ == nullptr)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3075 return nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3076 unsigned this_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3077 char const* this_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3078 decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3079 &this_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3080 return this_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3081 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3082
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3083 #if JSONCPP_USING_SECURE_MEMORY
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3084 unsigned Value::getCStringLength() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3085 JSON_ASSERT_MESSAGE(type() == stringValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3086 "in Json::Value::asCString(): requires stringValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3087 if (value_.string_ == 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3088 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3089 unsigned this_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3090 char const* this_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3091 decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3092 &this_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3093 return this_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3094 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3095 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3096
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3097 bool Value::getString(char const** begin, char const** end) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3098 if (type() != stringValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3099 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3100 if (value_.string_ == nullptr)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3101 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3102 unsigned length;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3103 decodePrefixedString(this->isAllocated(), this->value_.string_, &length,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3104 begin);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3105 *end = *begin + length;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3106 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3107 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3108
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3109 String Value::asString() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3110 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3111 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3112 return "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3113 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3114 if (value_.string_ == nullptr)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3115 return "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3116 unsigned this_len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3117 char const* this_str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3118 decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3119 &this_str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3120 return String(this_str, this_len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3121 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3122 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3123 return value_.bool_ ? "true" : "false";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3124 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3125 return valueToString(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3126 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3127 return valueToString(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3128 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3129 return valueToString(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3130 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3131 JSON_FAIL_MESSAGE("Type is not convertible to string");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3132 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3133 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3134
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3135 #ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3136 CppTL::ConstString Value::asConstString() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3137 unsigned len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3138 char const* str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3139 decodePrefixedString(isAllocated(), value_.string_, &len, &str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3140 return CppTL::ConstString(str, len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3141 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3142 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3143
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3144 Value::Int Value::asInt() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3145 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3146 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3147 JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3148 return Int(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3149 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3150 JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3151 return Int(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3152 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3153 JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3154 "double out of Int range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3155 return Int(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3156 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3157 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3158 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3159 return value_.bool_ ? 1 : 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3160 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3161 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3162 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3163 JSON_FAIL_MESSAGE("Value is not convertible to Int.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3164 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3165
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3166 Value::UInt Value::asUInt() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3167 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3168 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3169 JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3170 return UInt(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3171 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3172 JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3173 return UInt(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3174 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3175 JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3176 "double out of UInt range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3177 return UInt(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3178 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3179 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3180 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3181 return value_.bool_ ? 1 : 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3182 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3183 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3184 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3185 JSON_FAIL_MESSAGE("Value is not convertible to UInt.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3186 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3187
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3188 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3189
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3190 Value::Int64 Value::asInt64() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3191 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3192 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3193 return Int64(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3194 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3195 JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3196 return Int64(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3197 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3198 JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3199 "double out of Int64 range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3200 return Int64(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3201 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3202 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3203 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3204 return value_.bool_ ? 1 : 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3205 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3206 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3207 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3208 JSON_FAIL_MESSAGE("Value is not convertible to Int64.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3209 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3210
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3211 Value::UInt64 Value::asUInt64() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3212 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3213 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3214 JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3215 return UInt64(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3216 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3217 return UInt64(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3218 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3219 JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3220 "double out of UInt64 range");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3221 return UInt64(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3222 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3223 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3224 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3225 return value_.bool_ ? 1 : 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3226 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3227 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3228 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3229 JSON_FAIL_MESSAGE("Value is not convertible to UInt64.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3230 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3231 #endif // if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3232
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3233 LargestInt Value::asLargestInt() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3234 #if defined(JSON_NO_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3235 return asInt();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3236 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3237 return asInt64();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3238 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3239 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3240
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3241 LargestUInt Value::asLargestUInt() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3242 #if defined(JSON_NO_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3243 return asUInt();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3244 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3245 return asUInt64();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3246 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3247 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3248
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3249 double Value::asDouble() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3250 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3251 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3252 return static_cast<double>(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3253 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3254 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3255 return static_cast<double>(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3256 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3257 return integerToDouble(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3258 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3259 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3260 return value_.real_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3261 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3262 return 0.0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3263 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3264 return value_.bool_ ? 1.0 : 0.0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3265 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3266 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3267 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3268 JSON_FAIL_MESSAGE("Value is not convertible to double.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3269 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3270
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3271 float Value::asFloat() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3272 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3273 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3274 return static_cast<float>(value_.int_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3275 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3276 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3277 return static_cast<float>(value_.uint_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3278 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3279 // This can fail (silently?) if the value is bigger than MAX_FLOAT.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3280 return static_cast<float>(integerToDouble(value_.uint_));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3281 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3282 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3283 return static_cast<float>(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3284 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3285 return 0.0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3286 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3287 return value_.bool_ ? 1.0f : 0.0f;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3288 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3289 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3290 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3291 JSON_FAIL_MESSAGE("Value is not convertible to float.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3292 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3293
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3294 bool Value::asBool() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3295 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3296 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3297 return value_.bool_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3298 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3299 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3300 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3301 return value_.int_ ? true : false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3302 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3303 return value_.uint_ ? true : false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3304 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3305 // This is kind of strange. Not recommended.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3306 return (value_.real_ != 0.0) ? true : false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3307 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3308 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3309 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3310 JSON_FAIL_MESSAGE("Value is not convertible to bool.");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3311 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3312
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3313 bool Value::isConvertibleTo(ValueType other) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3314 switch (other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3315 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3316 return (isNumeric() && asDouble() == 0.0) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3317 (type() == booleanValue && value_.bool_ == false) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3318 (type() == stringValue && asString().empty()) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3319 (type() == arrayValue && value_.map_->empty()) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3320 (type() == objectValue && value_.map_->empty()) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3321 type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3322 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3323 return isInt() ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3324 (type() == realValue && InRange(value_.real_, minInt, maxInt)) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3325 type() == booleanValue || type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3326 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3327 return isUInt() ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3328 (type() == realValue && InRange(value_.real_, 0, maxUInt)) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3329 type() == booleanValue || type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3330 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3331 return isNumeric() || type() == booleanValue || type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3332 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3333 return isNumeric() || type() == booleanValue || type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3334 case stringValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3335 return isNumeric() || type() == booleanValue || type() == stringValue ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3336 type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3337 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3338 return type() == arrayValue || type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3339 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3340 return type() == objectValue || type() == nullValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3341 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3342 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3343 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3344 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3345
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3346 /// Number of values in array or object
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3347 ArrayIndex Value::size() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3348 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3349 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3350 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3351 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3352 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3353 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3354 case stringValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3355 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3356 case arrayValue: // size of the array is highest index + 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3357 if (!value_.map_->empty()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3358 ObjectValues::const_iterator itLast = value_.map_->end();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3359 --itLast;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3360 return (*itLast).first.index() + 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3361 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3362 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3363 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3364 return ArrayIndex(value_.map_->size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3365 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3366 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3367 return 0; // unreachable;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3368 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3369
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3370 bool Value::empty() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3371 if (isNull() || isArray() || isObject())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3372 return size() == 0u;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3373 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3374 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3375 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3376
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3377 Value::operator bool() const { return !isNull(); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3378
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3379 void Value::clear() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3380 JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3381 type() == objectValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3382 "in Json::Value::clear(): requires complex value");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3383 start_ = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3384 limit_ = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3385 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3386 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3387 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3388 value_.map_->clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3389 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3390 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3391 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3392 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3393 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3394
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3395 void Value::resize(ArrayIndex newSize) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3396 JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3397 "in Json::Value::resize(): requires arrayValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3398 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3399 *this = Value(arrayValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3400 ArrayIndex oldSize = size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3401 if (newSize == 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3402 clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3403 else if (newSize > oldSize)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3404 this->operator[](newSize - 1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3405 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3406 for (ArrayIndex index = newSize; index < oldSize; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3407 value_.map_->erase(index);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3408 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3409 JSON_ASSERT(size() == newSize);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3410 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3411 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3412
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3413 Value& Value::operator[](ArrayIndex index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3414 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3415 type() == nullValue || type() == arrayValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3416 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3417 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3418 *this = Value(arrayValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3419 CZString key(index);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3420 auto it = value_.map_->lower_bound(key);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3421 if (it != value_.map_->end() && (*it).first == key)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3422 return (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3423
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3424 ObjectValues::value_type defaultValue(key, nullSingleton());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3425 it = value_.map_->insert(it, defaultValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3426 return (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3427 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3428
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3429 Value& Value::operator[](int index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3430 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3431 index >= 0,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3432 "in Json::Value::operator[](int index): index cannot be negative");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3433 return (*this)[ArrayIndex(index)];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3434 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3435
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3436 const Value& Value::operator[](ArrayIndex index) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3437 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3438 type() == nullValue || type() == arrayValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3439 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3440 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3441 return nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3442 CZString key(index);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3443 ObjectValues::const_iterator it = value_.map_->find(key);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3444 if (it == value_.map_->end())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3445 return nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3446 return (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3447 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3448
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3449 const Value& Value::operator[](int index) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3450 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3451 index >= 0,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3452 "in Json::Value::operator[](int index) const: index cannot be negative");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3453 return (*this)[ArrayIndex(index)];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3454 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3455
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3456 void Value::initBasic(ValueType type, bool allocated) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3457 setType(type);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3458 setIsAllocated(allocated);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3459 comments_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3460 start_ = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3461 limit_ = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3462 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3463
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3464 void Value::dupPayload(const Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3465 setType(other.type());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3466 setIsAllocated(false);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3467 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3468 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3469 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3470 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3471 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3472 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3473 value_ = other.value_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3474 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3475 case stringValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3476 if (other.value_.string_ && other.isAllocated()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3477 unsigned len;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3478 char const* str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3479 decodePrefixedString(other.isAllocated(), other.value_.string_, &len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3480 &str);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3481 value_.string_ = duplicateAndPrefixStringValue(str, len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3482 setIsAllocated(true);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3483 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3484 value_.string_ = other.value_.string_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3485 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3486 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3487 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3488 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3489 value_.map_ = new ObjectValues(*other.value_.map_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3490 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3491 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3492 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3493 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3494 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3495
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3496 void Value::releasePayload() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3497 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3498 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3499 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3500 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3501 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3502 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3503 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3504 case stringValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3505 if (isAllocated())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3506 releasePrefixedStringValue(value_.string_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3507 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3508 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3509 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3510 delete value_.map_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3511 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3512 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3513 JSON_ASSERT_UNREACHABLE;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3514 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3515 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3516
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3517 void Value::dupMeta(const Value& other) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3518 if (other.comments_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3519 comments_ = new CommentInfo[numberOfCommentPlacement];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3520 for (int comment = 0; comment < numberOfCommentPlacement; ++comment) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3521 const CommentInfo& otherComment = other.comments_[comment];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3522 if (otherComment.comment_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3523 comments_[comment].setComment(otherComment.comment_,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3524 strlen(otherComment.comment_));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3525 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3526 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3527 comments_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3528 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3529 start_ = other.start_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3530 limit_ = other.limit_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3531 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3532
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3533 // Access an object value by name, create a null member if it does not exist.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3534 // @pre Type of '*this' is object or null.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3535 // @param key is null-terminated.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3536 Value& Value::resolveReference(const char* key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3537 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3538 type() == nullValue || type() == objectValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3539 "in Json::Value::resolveReference(): requires objectValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3540 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3541 *this = Value(objectValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3542 CZString actualKey(key, static_cast<unsigned>(strlen(key)),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3543 CZString::noDuplication); // NOTE!
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3544 auto it = value_.map_->lower_bound(actualKey);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3545 if (it != value_.map_->end() && (*it).first == actualKey)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3546 return (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3547
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3548 ObjectValues::value_type defaultValue(actualKey, nullSingleton());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3549 it = value_.map_->insert(it, defaultValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3550 Value& value = (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3551 return value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3552 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3553
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3554 // @param key is not null-terminated.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3555 Value& Value::resolveReference(char const* key, char const* end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3556 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3557 type() == nullValue || type() == objectValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3558 "in Json::Value::resolveReference(key, end): requires objectValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3559 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3560 *this = Value(objectValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3561 CZString actualKey(key, static_cast<unsigned>(end - key),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3562 CZString::duplicateOnCopy);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3563 auto it = value_.map_->lower_bound(actualKey);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3564 if (it != value_.map_->end() && (*it).first == actualKey)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3565 return (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3566
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3567 ObjectValues::value_type defaultValue(actualKey, nullSingleton());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3568 it = value_.map_->insert(it, defaultValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3569 Value& value = (*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3570 return value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3571 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3572
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3573 Value Value::get(ArrayIndex index, const Value& defaultValue) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3574 const Value* value = &((*this)[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3575 return value == &nullSingleton() ? defaultValue : *value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3576 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3577
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3578 bool Value::isValidIndex(ArrayIndex index) const { return index < size(); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3579
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3580 Value const* Value::find(char const* begin, char const* end) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3581 JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3582 "in Json::Value::find(key, end, found): requires "
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3583 "objectValue or nullValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3584 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3585 return nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3586 CZString actualKey(begin, static_cast<unsigned>(end - begin),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3587 CZString::noDuplication);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3588 ObjectValues::const_iterator it = value_.map_->find(actualKey);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3589 if (it == value_.map_->end())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3590 return nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3591 return &(*it).second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3592 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3593 const Value& Value::operator[](const char* key) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3594 Value const* found = find(key, key + strlen(key));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3595 if (!found)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3596 return nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3597 return *found;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3598 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3599 Value const& Value::operator[](const String& key) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3600 Value const* found = find(key.data(), key.data() + key.length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3601 if (!found)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3602 return nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3603 return *found;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3604 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3605
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3606 Value& Value::operator[](const char* key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3607 return resolveReference(key, key + strlen(key));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3608 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3609
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3610 Value& Value::operator[](const String& key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3611 return resolveReference(key.data(), key.data() + key.length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3612 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3613
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3614 Value& Value::operator[](const StaticString& key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3615 return resolveReference(key.c_str());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3616 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3617
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3618 #ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3619 Value& Value::operator[](const CppTL::ConstString& key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3620 return resolveReference(key.c_str(), key.end_c_str());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3621 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3622 Value const& Value::operator[](CppTL::ConstString const& key) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3623 Value const* found = find(key.c_str(), key.end_c_str());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3624 if (!found)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3625 return nullSingleton();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3626 return *found;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3627 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3628 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3629
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3630 Value& Value::append(const Value& value) { return (*this)[size()] = value; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3631
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3632 #if JSON_HAS_RVALUE_REFERENCES
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3633 Value& Value::append(Value&& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3634 return (*this)[size()] = std::move(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3635 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3636 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3637
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3638 Value Value::get(char const* begin,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3639 char const* end,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3640 Value const& defaultValue) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3641 Value const* found = find(begin, end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3642 return !found ? defaultValue : *found;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3643 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3644 Value Value::get(char const* key, Value const& defaultValue) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3645 return get(key, key + strlen(key), defaultValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3646 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3647 Value Value::get(String const& key, Value const& defaultValue) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3648 return get(key.data(), key.data() + key.length(), defaultValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3649 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3650
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3651 bool Value::removeMember(const char* begin, const char* end, Value* removed) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3652 if (type() != objectValue) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3653 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3654 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3655 CZString actualKey(begin, static_cast<unsigned>(end - begin),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3656 CZString::noDuplication);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3657 auto it = value_.map_->find(actualKey);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3658 if (it == value_.map_->end())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3659 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3660 if (removed)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3661 #if JSON_HAS_RVALUE_REFERENCES
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3662 *removed = std::move(it->second);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3663 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3664 *removed = it->second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3665 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3666 value_.map_->erase(it);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3667 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3668 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3669 bool Value::removeMember(const char* key, Value* removed) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3670 return removeMember(key, key + strlen(key), removed);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3671 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3672 bool Value::removeMember(String const& key, Value* removed) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3673 return removeMember(key.data(), key.data() + key.length(), removed);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3674 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3675 void Value::removeMember(const char* key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3676 JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3677 "in Json::Value::removeMember(): requires objectValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3678 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3679 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3680
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3681 CZString actualKey(key, unsigned(strlen(key)), CZString::noDuplication);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3682 value_.map_->erase(actualKey);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3683 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3684 void Value::removeMember(const String& key) { removeMember(key.c_str()); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3685
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3686 bool Value::removeIndex(ArrayIndex index, Value* removed) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3687 if (type() != arrayValue) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3688 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3689 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3690 CZString key(index);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3691 auto it = value_.map_->find(key);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3692 if (it == value_.map_->end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3693 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3694 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3695 if (removed)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3696 *removed = it->second;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3697 ArrayIndex oldSize = size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3698 // shift left all items left, into the place of the "removed"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3699 for (ArrayIndex i = index; i < (oldSize - 1); ++i) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3700 CZString keey(i);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3701 (*value_.map_)[keey] = (*this)[i + 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3702 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3703 // erase the last one ("leftover")
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3704 CZString keyLast(oldSize - 1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3705 auto itLast = value_.map_->find(keyLast);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3706 value_.map_->erase(itLast);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3707 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3708 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3709
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3710 #ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3711 Value Value::get(const CppTL::ConstString& key,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3712 const Value& defaultValue) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3713 return get(key.c_str(), key.end_c_str(), defaultValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3714 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3715 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3716
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3717 bool Value::isMember(char const* begin, char const* end) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3718 Value const* value = find(begin, end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3719 return nullptr != value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3720 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3721 bool Value::isMember(char const* key) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3722 return isMember(key, key + strlen(key));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3723 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3724 bool Value::isMember(String const& key) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3725 return isMember(key.data(), key.data() + key.length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3726 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3727
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3728 #ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3729 bool Value::isMember(const CppTL::ConstString& key) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3730 return isMember(key.c_str(), key.end_c_str());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3731 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3732 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3733
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3734 Value::Members Value::getMemberNames() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3735 JSON_ASSERT_MESSAGE(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3736 type() == nullValue || type() == objectValue,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3737 "in Json::Value::getMemberNames(), value must be objectValue");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3738 if (type() == nullValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3739 return Value::Members();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3740 Members members;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3741 members.reserve(value_.map_->size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3742 ObjectValues::const_iterator it = value_.map_->begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3743 ObjectValues::const_iterator itEnd = value_.map_->end();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3744 for (; it != itEnd; ++it) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3745 members.push_back(String((*it).first.data(), (*it).first.length()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3746 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3747 return members;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3748 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3749 //
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3750 //# ifdef JSON_USE_CPPTL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3751 // EnumMemberNames
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3752 // Value::enumMemberNames() const
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3753 //{
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3754 // if ( type() == objectValue )
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3755 // {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3756 // return CppTL::Enum::any( CppTL::Enum::transform(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3757 // CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3758 // MemberNamesTransform() ) );
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3759 // }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3760 // return EnumMemberNames();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3761 //}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3762 //
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3763 //
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3764 // EnumValues
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3765 // Value::enumValues() const
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3766 //{
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3767 // if ( type() == objectValue || type() == arrayValue )
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3768 // return CppTL::Enum::anyValues( *(value_.map_),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3769 // CppTL::Type<const Value &>() );
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3770 // return EnumValues();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3771 //}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3772 //
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3773 //# endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3774
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3775 static bool IsIntegral(double d) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3776 double integral_part;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3777 return modf(d, &integral_part) == 0.0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3778 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3779
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3780 bool Value::isNull() const { return type() == nullValue; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3781
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3782 bool Value::isBool() const { return type() == booleanValue; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3783
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3784 bool Value::isInt() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3785 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3786 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3787 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3788 return value_.int_ >= minInt && value_.int_ <= maxInt;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3789 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3790 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3791 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3792 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3793 return value_.uint_ <= UInt(maxInt);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3794 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3795 return value_.real_ >= minInt && value_.real_ <= maxInt &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3796 IsIntegral(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3797 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3798 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3799 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3800 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3801 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3802
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3803 bool Value::isUInt() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3804 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3805 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3806 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3807 return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3808 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3809 return value_.int_ >= 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3810 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3811 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3812 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3813 return value_.uint_ <= maxUInt;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3814 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3815 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3816 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3817 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3818 return value_.real_ >= 0 && value_.real_ <= maxUInt &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3819 IsIntegral(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3820 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3821 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3822 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3823 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3824 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3825
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3826 bool Value::isInt64() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3827 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3828 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3829 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3830 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3831 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3832 return value_.uint_ <= UInt64(maxInt64);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3833 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3834 // Note that maxInt64 (= 2^63 - 1) is not exactly representable as a
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3835 // double, so double(maxInt64) will be rounded up to 2^63. Therefore we
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3836 // require the value to be strictly less than the limit.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3837 return value_.real_ >= double(minInt64) &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3838 value_.real_ < double(maxInt64) && IsIntegral(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3839 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3840 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3841 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3842 #endif // JSON_HAS_INT64
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3843 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3844 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3845
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3846 bool Value::isUInt64() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3847 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3848 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3849 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3850 return value_.int_ >= 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3851 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3852 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3853 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3854 // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3855 // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3856 // require the value to be strictly less than the limit.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3857 return value_.real_ >= 0 && value_.real_ < maxUInt64AsDouble &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3858 IsIntegral(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3859 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3860 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3861 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3862 #endif // JSON_HAS_INT64
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3863 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3864 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3865
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3866 bool Value::isIntegral() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3867 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3868 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3869 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3870 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3871 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3872 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3873 // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3874 // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3875 // require the value to be strictly less than the limit.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3876 return value_.real_ >= double(minInt64) &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3877 value_.real_ < maxUInt64AsDouble && IsIntegral(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3878 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3879 return value_.real_ >= minInt && value_.real_ <= maxUInt &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3880 IsIntegral(value_.real_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3881 #endif // JSON_HAS_INT64
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3882 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3883 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3884 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3885 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3886 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3887
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3888 bool Value::isDouble() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3889 return type() == intValue || type() == uintValue || type() == realValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3890 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3891
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3892 bool Value::isNumeric() const { return isDouble(); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3893
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3894 bool Value::isString() const { return type() == stringValue; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3895
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3896 bool Value::isArray() const { return type() == arrayValue; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3897
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3898 bool Value::isObject() const { return type() == objectValue; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3899
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3900 void Value::setComment(const char* comment,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3901 size_t len,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3902 CommentPlacement placement) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3903 if (!comments_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3904 comments_ = new CommentInfo[numberOfCommentPlacement];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3905 if ((len > 0) && (comment[len - 1] == '\n')) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3906 // Always discard trailing newline, to aid indentation.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3907 len -= 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3908 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3909 comments_[placement].setComment(comment, len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3910 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3911
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3912 void Value::setComment(const char* comment, CommentPlacement placement) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3913 setComment(comment, strlen(comment), placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3914 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3915
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3916 void Value::setComment(const String& comment, CommentPlacement placement) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3917 setComment(comment.c_str(), comment.length(), placement);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3918 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3919
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3920 bool Value::hasComment(CommentPlacement placement) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3921 return comments_ != nullptr && comments_[placement].comment_ != nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3922 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3923
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3924 String Value::getComment(CommentPlacement placement) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3925 if (hasComment(placement))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3926 return comments_[placement].comment_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3927 return "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3928 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3929
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3930 void Value::setOffsetStart(ptrdiff_t start) { start_ = start; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3931
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3932 void Value::setOffsetLimit(ptrdiff_t limit) { limit_ = limit; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3933
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3934 ptrdiff_t Value::getOffsetStart() const { return start_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3935
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3936 ptrdiff_t Value::getOffsetLimit() const { return limit_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3937
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3938 String Value::toStyledString() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3939 StreamWriterBuilder builder;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3940
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3941 String out = this->hasComment(commentBefore) ? "\n" : "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3942 out += Json::writeString(builder, *this);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3943 out += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3944
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3945 return out;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3946 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3947
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3948 Value::const_iterator Value::begin() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3949 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3950 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3951 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3952 if (value_.map_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3953 return const_iterator(value_.map_->begin());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3954 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3955 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3956 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3957 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3958 return {};
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3959 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3960
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3961 Value::const_iterator Value::end() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3962 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3963 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3964 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3965 if (value_.map_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3966 return const_iterator(value_.map_->end());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3967 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3968 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3969 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3970 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3971 return {};
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3972 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3973
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3974 Value::iterator Value::begin() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3975 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3976 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3977 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3978 if (value_.map_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3979 return iterator(value_.map_->begin());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3980 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3981 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3982 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3983 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3984 return iterator();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3985 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3986
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3987 Value::iterator Value::end() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3988 switch (type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3989 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3990 case objectValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3991 if (value_.map_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3992 return iterator(value_.map_->end());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3993 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3994 default:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3995 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3996 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3997 return iterator();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3998 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
3999
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4000 // class PathArgument
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4001 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4002
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4003 PathArgument::PathArgument() : key_() {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4004
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4005 PathArgument::PathArgument(ArrayIndex index)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4006 : key_(), index_(index), kind_(kindIndex) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4007
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4008 PathArgument::PathArgument(const char* key)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4009 : key_(key), index_(), kind_(kindKey) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4010
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4011 PathArgument::PathArgument(const String& key)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4012 : key_(key.c_str()), index_(), kind_(kindKey) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4013
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4014 // class Path
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4015 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4016
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4017 Path::Path(const String& path,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4018 const PathArgument& a1,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4019 const PathArgument& a2,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4020 const PathArgument& a3,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4021 const PathArgument& a4,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4022 const PathArgument& a5) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4023 InArgs in;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4024 in.reserve(5);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4025 in.push_back(&a1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4026 in.push_back(&a2);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4027 in.push_back(&a3);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4028 in.push_back(&a4);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4029 in.push_back(&a5);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4030 makePath(path, in);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4031 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4032
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4033 void Path::makePath(const String& path, const InArgs& in) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4034 const char* current = path.c_str();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4035 const char* end = current + path.length();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4036 auto itInArg = in.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4037 while (current != end) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4038 if (*current == '[') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4039 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4040 if (*current == '%')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4041 addPathInArg(path, in, itInArg, PathArgument::kindIndex);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4042 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4043 ArrayIndex index = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4044 for (; current != end && *current >= '0' && *current <= '9'; ++current)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4045 index = index * 10 + ArrayIndex(*current - '0');
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4046 args_.push_back(index);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4047 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4048 if (current == end || *++current != ']')
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4049 invalidPath(path, int(current - path.c_str()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4050 } else if (*current == '%') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4051 addPathInArg(path, in, itInArg, PathArgument::kindKey);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4052 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4053 } else if (*current == '.' || *current == ']') {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4054 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4055 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4056 const char* beginName = current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4057 while (current != end && !strchr("[.", *current))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4058 ++current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4059 args_.push_back(String(beginName, current));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4060 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4061 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4062 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4063
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4064 void Path::addPathInArg(const String& /*path*/,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4065 const InArgs& in,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4066 InArgs::const_iterator& itInArg,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4067 PathArgument::Kind kind) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4068 if (itInArg == in.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4069 // Error: missing argument %d
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4070 } else if ((*itInArg)->kind_ != kind) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4071 // Error: bad argument type
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4072 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4073 args_.push_back(**itInArg++);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4074 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4075 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4076
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4077 void Path::invalidPath(const String& /*path*/, int /*location*/) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4078 // Error: invalid path.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4079 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4080
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4081 const Value& Path::resolve(const Value& root) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4082 const Value* node = &root;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4083 for (const auto& arg : args_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4084 if (arg.kind_ == PathArgument::kindIndex) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4085 if (!node->isArray() || !node->isValidIndex(arg.index_)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4086 // Error: unable to resolve path (array value expected at position...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4087 return Value::null;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4088 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4089 node = &((*node)[arg.index_]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4090 } else if (arg.kind_ == PathArgument::kindKey) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4091 if (!node->isObject()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4092 // Error: unable to resolve path (object value expected at position...)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4093 return Value::null;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4094 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4095 node = &((*node)[arg.key_]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4096 if (node == &Value::nullSingleton()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4097 // Error: unable to resolve path (object has no member named '' at
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4098 // position...)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4099 return Value::null;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4100 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4101 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4102 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4103 return *node;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4104 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4105
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4106 Value Path::resolve(const Value& root, const Value& defaultValue) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4107 const Value* node = &root;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4108 for (const auto& arg : args_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4109 if (arg.kind_ == PathArgument::kindIndex) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4110 if (!node->isArray() || !node->isValidIndex(arg.index_))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4111 return defaultValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4112 node = &((*node)[arg.index_]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4113 } else if (arg.kind_ == PathArgument::kindKey) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4114 if (!node->isObject())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4115 return defaultValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4116 node = &((*node)[arg.key_]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4117 if (node == &Value::nullSingleton())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4118 return defaultValue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4119 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4120 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4121 return *node;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4122 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4123
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4124 Value& Path::make(Value& root) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4125 Value* node = &root;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4126 for (const auto& arg : args_) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4127 if (arg.kind_ == PathArgument::kindIndex) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4128 if (!node->isArray()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4129 // Error: node is not an array at position ...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4130 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4131 node = &((*node)[arg.index_]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4132 } else if (arg.kind_ == PathArgument::kindKey) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4133 if (!node->isObject()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4134 // Error: node is not an object at position...
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4135 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4136 node = &((*node)[arg.key_]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4137 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4138 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4139 return *node;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4140 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4141
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4142 } // namespace Json
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4143
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4144 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4145 // End of content of file: src/lib_json/json_value.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4146 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4147
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4148
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4149
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4150
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4151
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4152
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4153 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4154 // Beginning of content of file: src/lib_json/json_writer.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4155 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4156
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4157 // Copyright 2011 Baptiste Lepilleur and The JsonCpp Authors
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4158 // Distributed under MIT license, or public domain if desired and
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4159 // recognized in your jurisdiction.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4160 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4161
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4162 #if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4163 #include "json_tool.h"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4164 #include <json/writer.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4165 #endif // if !defined(JSON_IS_AMALGAMATION)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4166 #include <cassert>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4167 #include <cstring>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4168 #include <iomanip>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4169 #include <memory>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4170 #include <set>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4171 #include <sstream>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4172 #include <utility>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4173
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4174 #if __cplusplus >= 201103L
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4175 #include <cmath>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4176 #include <cstdio>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4177
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4178 #if !defined(isnan)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4179 #define isnan std::isnan
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4180 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4181
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4182 #if !defined(isfinite)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4183 #define isfinite std::isfinite
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4184 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4185
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4186 #else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4187 #include <cmath>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4188 #include <cstdio>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4189
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4190 #if defined(_MSC_VER)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4191 #if !defined(isnan)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4192 #include <float.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4193 #define isnan _isnan
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4194 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4195
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4196 #if !defined(isfinite)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4197 #include <float.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4198 #define isfinite _finite
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4199 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4200
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4201 #if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4202 #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4203 #endif //_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4204
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4205 #endif //_MSC_VER
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4206
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4207 #if defined(__sun) && defined(__SVR4) // Solaris
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4208 #if !defined(isfinite)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4209 #include <ieeefp.h>
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4210 #define isfinite finite
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4211 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4212 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4213
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4214 #if defined(__hpux)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4215 #if !defined(isfinite)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4216 #if defined(__ia64) && !defined(finite)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4217 #define isfinite(x) \
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4218 ((sizeof(x) == sizeof(float) ? _Isfinitef(x) : _IsFinite(x)))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4219 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4220 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4221 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4222
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4223 #if !defined(isnan)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4224 // IEEE standard states that NaN values will not compare to themselves
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4225 #define isnan(x) (x != x)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4226 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4227
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4228 #if !defined(__APPLE__)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4229 #if !defined(isfinite)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4230 #define isfinite finite
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4231 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4232 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4233 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4234
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4235 #if defined(_MSC_VER)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4236 // Disable warning about strdup being deprecated.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4237 #pragma warning(disable : 4996)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4238 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4239
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4240 namespace Json {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4241
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4242 #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4243 typedef std::unique_ptr<StreamWriter> StreamWriterPtr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4244 #else
1298
8a0a62189f46 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 499
diff changeset
4245 typedef std::unique_ptr<StreamWriter> StreamWriterPtr;
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4246 #endif
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4247
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4248 String valueToString(LargestInt value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4249 UIntToStringBuffer buffer;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4250 char* current = buffer + sizeof(buffer);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4251 if (value == Value::minLargestInt) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4252 uintToString(LargestUInt(Value::maxLargestInt) + 1, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4253 *--current = '-';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4254 } else if (value < 0) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4255 uintToString(LargestUInt(-value), current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4256 *--current = '-';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4257 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4258 uintToString(LargestUInt(value), current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4259 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4260 assert(current >= buffer);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4261 return current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4262 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4263
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4264 String valueToString(LargestUInt value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4265 UIntToStringBuffer buffer;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4266 char* current = buffer + sizeof(buffer);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4267 uintToString(value, current);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4268 assert(current >= buffer);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4269 return current;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4270 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4271
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4272 #if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4273
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4274 String valueToString(Int value) { return valueToString(LargestInt(value)); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4275
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4276 String valueToString(UInt value) { return valueToString(LargestUInt(value)); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4277
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4278 #endif // # if defined(JSON_HAS_INT64)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4279
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4280 namespace {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4281 String valueToString(double value,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4282 bool useSpecialFloats,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4283 unsigned int precision,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4284 PrecisionType precisionType) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4285 // Print into the buffer. We need not request the alternative representation
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4286 // that always has a decimal point because JSON doesn't distinguish the
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4287 // concepts of reals and integers.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4288 if (!isfinite(value)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4289 static const char* const reps[2][3] = {{"NaN", "-Infinity", "Infinity"},
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4290 {"null", "-1e+9999", "1e+9999"}};
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4291 return reps[useSpecialFloats ? 0 : 1]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4292 [isnan(value) ? 0 : (value < 0) ? 1 : 2];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4293 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4294
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4295 String buffer(size_t(36), '\0');
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4296 while (true) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4297 int len = jsoncpp_snprintf(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4298 &*buffer.begin(), buffer.size(),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4299 (precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f",
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4300 precision, value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4301 assert(len >= 0);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4302 auto wouldPrint = static_cast<size_t>(len);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4303 if (wouldPrint >= buffer.size()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4304 buffer.resize(wouldPrint + 1);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4305 continue;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4306 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4307 buffer.resize(wouldPrint);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4308 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4309 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4310
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4311 buffer.erase(fixNumericLocale(buffer.begin(), buffer.end()), buffer.end());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4312
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4313 // strip the zero padding from the right
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4314 if (precisionType == PrecisionType::decimalPlaces) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4315 buffer.erase(fixZerosInTheEnd(buffer.begin(), buffer.end()), buffer.end());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4316 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4317
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4318 // try to ensure we preserve the fact that this was given to us as a double on
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4319 // input
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4320 if (buffer.find('.') == buffer.npos && buffer.find('e') == buffer.npos) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4321 buffer += ".0";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4322 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4323 return buffer;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4324 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4325 } // namespace
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4326
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4327 String valueToString(double value,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4328 unsigned int precision,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4329 PrecisionType precisionType) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4330 return valueToString(value, false, precision, precisionType);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4331 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4332
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4333 String valueToString(bool value) { return value ? "true" : "false"; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4334
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4335 static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4336 assert(s || !n);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4337
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4338 char const* const end = s + n;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4339 for (char const* cur = s; cur < end; ++cur) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4340 if (*cur == '\\' || *cur == '\"' || *cur < ' ' ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4341 static_cast<unsigned char>(*cur) < 0x80)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4342 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4343 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4344 return false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4345 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4346
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4347 static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4348 const unsigned int REPLACEMENT_CHARACTER = 0xFFFD;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4349
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4350 unsigned int firstByte = static_cast<unsigned char>(*s);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4351
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4352 if (firstByte < 0x80)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4353 return firstByte;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4354
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4355 if (firstByte < 0xE0) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4356 if (e - s < 2)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4357 return REPLACEMENT_CHARACTER;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4358
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4359 unsigned int calculated =
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4360 ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4361 s += 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4362 // oversized encoded characters are invalid
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4363 return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4364 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4365
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4366 if (firstByte < 0xF0) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4367 if (e - s < 3)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4368 return REPLACEMENT_CHARACTER;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4369
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4370 unsigned int calculated = ((firstByte & 0x0F) << 12) |
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4371 ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) |
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4372 (static_cast<unsigned int>(s[2]) & 0x3F);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4373 s += 2;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4374 // surrogates aren't valid codepoints itself
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4375 // shouldn't be UTF-8 encoded
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4376 if (calculated >= 0xD800 && calculated <= 0xDFFF)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4377 return REPLACEMENT_CHARACTER;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4378 // oversized encoded characters are invalid
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4379 return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4380 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4381
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4382 if (firstByte < 0xF8) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4383 if (e - s < 4)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4384 return REPLACEMENT_CHARACTER;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4385
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4386 unsigned int calculated = ((firstByte & 0x07) << 18) |
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4387 ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) |
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4388 ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) |
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4389 (static_cast<unsigned int>(s[3]) & 0x3F);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4390 s += 3;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4391 // oversized encoded characters are invalid
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4392 return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4393 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4394
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4395 return REPLACEMENT_CHARACTER;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4396 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4397
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4398 static const char hex2[] = "000102030405060708090a0b0c0d0e0f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4399 "101112131415161718191a1b1c1d1e1f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4400 "202122232425262728292a2b2c2d2e2f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4401 "303132333435363738393a3b3c3d3e3f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4402 "404142434445464748494a4b4c4d4e4f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4403 "505152535455565758595a5b5c5d5e5f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4404 "606162636465666768696a6b6c6d6e6f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4405 "707172737475767778797a7b7c7d7e7f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4406 "808182838485868788898a8b8c8d8e8f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4407 "909192939495969798999a9b9c9d9e9f"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4408 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4409 "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4410 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4411 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4412 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4413 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4414
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4415 static String toHex16Bit(unsigned int x) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4416 const unsigned int hi = (x >> 8) & 0xff;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4417 const unsigned int lo = x & 0xff;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4418 String result(4, ' ');
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4419 result[0] = hex2[2 * hi];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4420 result[1] = hex2[2 * hi + 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4421 result[2] = hex2[2 * lo];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4422 result[3] = hex2[2 * lo + 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4423 return result;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4424 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4425
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4426 static String valueToQuotedStringN(const char* value, unsigned length) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4427 if (value == nullptr)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4428 return "";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4429
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4430 if (!isAnyCharRequiredQuoting(value, length))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4431 return String("\"") + value + "\"";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4432 // We have to walk value and escape any special characters.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4433 // Appending to String is not efficient, but this should be rare.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4434 // (Note: forward slashes are *not* rare, but I am not escaping them.)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4435 String::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4436 String result;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4437 result.reserve(maxsize); // to avoid lots of mallocs
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4438 result += "\"";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4439 char const* end = value + length;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4440 for (const char* c = value; c != end; ++c) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4441 switch (*c) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4442 case '\"':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4443 result += "\\\"";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4444 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4445 case '\\':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4446 result += "\\\\";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4447 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4448 case '\b':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4449 result += "\\b";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4450 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4451 case '\f':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4452 result += "\\f";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4453 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4454 case '\n':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4455 result += "\\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4456 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4457 case '\r':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4458 result += "\\r";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4459 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4460 case '\t':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4461 result += "\\t";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4462 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4463 // case '/':
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4464 // Even though \/ is considered a legal escape in JSON, a bare
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4465 // slash is also legal, so I see no reason to escape it.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4466 // (I hope I am not misunderstanding something.)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4467 // blep notes: actually escaping \/ may be useful in javascript to avoid </
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4468 // sequence.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4469 // Should add a flag to allow this compatibility mode and prevent this
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4470 // sequence from occurring.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4471 default: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4472 unsigned int cp = utf8ToCodepoint(c, end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4473 // don't escape non-control characters
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4474 // (short escape sequence are applied above)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4475 if (cp < 0x80 && cp >= 0x20)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4476 result += static_cast<char>(cp);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4477 else if (cp < 0x10000) { // codepoint is in Basic Multilingual Plane
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4478 result += "\\u";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4479 result += toHex16Bit(cp);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4480 } else { // codepoint is not in Basic Multilingual Plane
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4481 // convert to surrogate pair first
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4482 cp -= 0x10000;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4483 result += "\\u";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4484 result += toHex16Bit((cp >> 10) + 0xD800);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4485 result += "\\u";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4486 result += toHex16Bit((cp & 0x3FF) + 0xDC00);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4487 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4488 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4489 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4490 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4491 result += "\"";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4492 return result;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4493 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4494
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4495 String valueToQuotedString(const char* value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4496 return valueToQuotedStringN(value, static_cast<unsigned int>(strlen(value)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4497 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4498
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4499 // Class Writer
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4500 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4501 Writer::~Writer() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4502
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4503 // Class FastWriter
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4504 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4505
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4506 FastWriter::FastWriter()
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4507
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4508 = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4509
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4510 void FastWriter::enableYAMLCompatibility() { yamlCompatibilityEnabled_ = true; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4511
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4512 void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ = true; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4513
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4514 void FastWriter::omitEndingLineFeed() { omitEndingLineFeed_ = true; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4515
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4516 String FastWriter::write(const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4517 document_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4518 writeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4519 if (!omitEndingLineFeed_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4520 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4521 return document_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4522 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4523
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4524 void FastWriter::writeValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4525 switch (value.type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4526 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4527 if (!dropNullPlaceholders_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4528 document_ += "null";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4529 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4530 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4531 document_ += valueToString(value.asLargestInt());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4532 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4533 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4534 document_ += valueToString(value.asLargestUInt());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4535 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4536 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4537 document_ += valueToString(value.asDouble());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4538 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4539 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4540 // Is NULL possible for value.string_? No.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4541 char const* str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4542 char const* end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4543 bool ok = value.getString(&str, &end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4544 if (ok)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4545 document_ += valueToQuotedStringN(str, static_cast<unsigned>(end - str));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4546 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4547 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4548 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4549 document_ += valueToString(value.asBool());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4550 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4551 case arrayValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4552 document_ += '[';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4553 ArrayIndex size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4554 for (ArrayIndex index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4555 if (index > 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4556 document_ += ',';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4557 writeValue(value[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4558 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4559 document_ += ']';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4560 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4561 case objectValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4562 Value::Members members(value.getMemberNames());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4563 document_ += '{';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4564 for (auto it = members.begin(); it != members.end(); ++it) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4565 const String& name = *it;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4566 if (it != members.begin())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4567 document_ += ',';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4568 document_ += valueToQuotedStringN(name.data(),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4569 static_cast<unsigned>(name.length()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4570 document_ += yamlCompatibilityEnabled_ ? ": " : ":";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4571 writeValue(value[name]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4572 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4573 document_ += '}';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4574 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4575 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4576 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4577
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4578 // Class StyledWriter
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4579 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4580
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4581 StyledWriter::StyledWriter() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4582
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4583 String StyledWriter::write(const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4584 document_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4585 addChildValues_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4586 indentString_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4587 writeCommentBeforeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4588 writeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4589 writeCommentAfterValueOnSameLine(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4590 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4591 return document_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4592 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4593
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4594 void StyledWriter::writeValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4595 switch (value.type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4596 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4597 pushValue("null");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4598 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4599 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4600 pushValue(valueToString(value.asLargestInt()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4601 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4602 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4603 pushValue(valueToString(value.asLargestUInt()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4604 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4605 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4606 pushValue(valueToString(value.asDouble()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4607 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4608 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4609 // Is NULL possible for value.string_? No.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4610 char const* str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4611 char const* end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4612 bool ok = value.getString(&str, &end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4613 if (ok)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4614 pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4615 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4616 pushValue("");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4617 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4618 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4619 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4620 pushValue(valueToString(value.asBool()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4621 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4622 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4623 writeArrayValue(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4624 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4625 case objectValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4626 Value::Members members(value.getMemberNames());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4627 if (members.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4628 pushValue("{}");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4629 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4630 writeWithIndent("{");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4631 indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4632 auto it = members.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4633 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4634 const String& name = *it;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4635 const Value& childValue = value[name];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4636 writeCommentBeforeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4637 writeWithIndent(valueToQuotedString(name.c_str()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4638 document_ += " : ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4639 writeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4640 if (++it == members.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4641 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4642 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4643 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4644 document_ += ',';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4645 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4646 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4647 unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4648 writeWithIndent("}");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4649 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4650 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4651 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4652 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4653
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4654 void StyledWriter::writeArrayValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4655 unsigned size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4656 if (size == 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4657 pushValue("[]");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4658 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4659 bool isArrayMultiLine = isMultilineArray(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4660 if (isArrayMultiLine) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4661 writeWithIndent("[");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4662 indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4663 bool hasChildValue = !childValues_.empty();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4664 unsigned index = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4665 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4666 const Value& childValue = value[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4667 writeCommentBeforeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4668 if (hasChildValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4669 writeWithIndent(childValues_[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4670 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4671 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4672 writeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4673 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4674 if (++index == size) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4675 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4676 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4677 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4678 document_ += ',';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4679 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4680 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4681 unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4682 writeWithIndent("]");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4683 } else // output on a single line
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4684 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4685 assert(childValues_.size() == size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4686 document_ += "[ ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4687 for (unsigned index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4688 if (index > 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4689 document_ += ", ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4690 document_ += childValues_[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4691 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4692 document_ += " ]";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4693 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4694 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4695 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4696
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4697 bool StyledWriter::isMultilineArray(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4698 ArrayIndex const size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4699 bool isMultiLine = size * 3 >= rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4700 childValues_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4701 for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4702 const Value& childValue = value[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4703 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4704 !childValue.empty());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4705 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4706 if (!isMultiLine) // check if line length > max line length
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4707 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4708 childValues_.reserve(size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4709 addChildValues_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4710 ArrayIndex lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4711 for (ArrayIndex index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4712 if (hasCommentForValue(value[index])) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4713 isMultiLine = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4714 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4715 writeValue(value[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4716 lineLength += static_cast<ArrayIndex>(childValues_[index].length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4717 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4718 addChildValues_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4719 isMultiLine = isMultiLine || lineLength >= rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4720 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4721 return isMultiLine;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4722 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4723
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4724 void StyledWriter::pushValue(const String& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4725 if (addChildValues_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4726 childValues_.push_back(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4727 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4728 document_ += value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4729 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4730
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4731 void StyledWriter::writeIndent() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4732 if (!document_.empty()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4733 char last = document_[document_.length() - 1];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4734 if (last == ' ') // already indented
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4735 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4736 if (last != '\n') // Comments may add new-line
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4737 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4738 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4739 document_ += indentString_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4740 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4741
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4742 void StyledWriter::writeWithIndent(const String& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4743 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4744 document_ += value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4745 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4746
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4747 void StyledWriter::indent() { indentString_ += String(indentSize_, ' '); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4748
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4749 void StyledWriter::unindent() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4750 assert(indentString_.size() >= indentSize_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4751 indentString_.resize(indentString_.size() - indentSize_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4752 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4753
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4754 void StyledWriter::writeCommentBeforeValue(const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4755 if (!root.hasComment(commentBefore))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4756 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4757
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4758 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4759 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4760 const String& comment = root.getComment(commentBefore);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4761 String::const_iterator iter = comment.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4762 while (iter != comment.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4763 document_ += *iter;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4764 if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4765 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4766 ++iter;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4767 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4768
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4769 // Comments are stripped of trailing newlines, so add one here
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4770 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4771 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4772
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4773 void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4774 if (root.hasComment(commentAfterOnSameLine))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4775 document_ += " " + root.getComment(commentAfterOnSameLine);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4776
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4777 if (root.hasComment(commentAfter)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4778 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4779 document_ += root.getComment(commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4780 document_ += '\n';
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4781 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4782 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4783
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4784 bool StyledWriter::hasCommentForValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4785 return value.hasComment(commentBefore) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4786 value.hasComment(commentAfterOnSameLine) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4787 value.hasComment(commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4788 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4789
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4790 // Class StyledStreamWriter
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4791 // //////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4792
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4793 StyledStreamWriter::StyledStreamWriter(String indentation)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4794 : document_(nullptr), indentation_(std::move(indentation)),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4795 addChildValues_(), indented_(false) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4796
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4797 void StyledStreamWriter::write(OStream& out, const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4798 document_ = &out;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4799 addChildValues_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4800 indentString_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4801 indented_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4802 writeCommentBeforeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4803 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4804 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4805 indented_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4806 writeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4807 writeCommentAfterValueOnSameLine(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4808 *document_ << "\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4809 document_ = nullptr; // Forget the stream, for safety.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4810 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4811
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4812 void StyledStreamWriter::writeValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4813 switch (value.type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4814 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4815 pushValue("null");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4816 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4817 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4818 pushValue(valueToString(value.asLargestInt()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4819 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4820 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4821 pushValue(valueToString(value.asLargestUInt()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4822 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4823 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4824 pushValue(valueToString(value.asDouble()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4825 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4826 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4827 // Is NULL possible for value.string_? No.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4828 char const* str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4829 char const* end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4830 bool ok = value.getString(&str, &end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4831 if (ok)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4832 pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4833 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4834 pushValue("");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4835 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4836 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4837 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4838 pushValue(valueToString(value.asBool()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4839 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4840 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4841 writeArrayValue(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4842 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4843 case objectValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4844 Value::Members members(value.getMemberNames());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4845 if (members.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4846 pushValue("{}");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4847 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4848 writeWithIndent("{");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4849 indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4850 auto it = members.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4851 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4852 const String& name = *it;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4853 const Value& childValue = value[name];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4854 writeCommentBeforeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4855 writeWithIndent(valueToQuotedString(name.c_str()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4856 *document_ << " : ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4857 writeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4858 if (++it == members.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4859 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4860 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4861 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4862 *document_ << ",";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4863 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4864 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4865 unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4866 writeWithIndent("}");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4867 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4868 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4869 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4870 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4871
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4872 void StyledStreamWriter::writeArrayValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4873 unsigned size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4874 if (size == 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4875 pushValue("[]");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4876 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4877 bool isArrayMultiLine = isMultilineArray(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4878 if (isArrayMultiLine) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4879 writeWithIndent("[");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4880 indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4881 bool hasChildValue = !childValues_.empty();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4882 unsigned index = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4883 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4884 const Value& childValue = value[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4885 writeCommentBeforeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4886 if (hasChildValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4887 writeWithIndent(childValues_[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4888 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4889 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4890 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4891 indented_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4892 writeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4893 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4894 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4895 if (++index == size) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4896 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4897 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4898 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4899 *document_ << ",";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4900 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4901 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4902 unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4903 writeWithIndent("]");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4904 } else // output on a single line
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4905 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4906 assert(childValues_.size() == size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4907 *document_ << "[ ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4908 for (unsigned index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4909 if (index > 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4910 *document_ << ", ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4911 *document_ << childValues_[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4912 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4913 *document_ << " ]";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4914 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4915 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4916 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4917
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4918 bool StyledStreamWriter::isMultilineArray(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4919 ArrayIndex const size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4920 bool isMultiLine = size * 3 >= rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4921 childValues_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4922 for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4923 const Value& childValue = value[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4924 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4925 !childValue.empty());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4926 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4927 if (!isMultiLine) // check if line length > max line length
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4928 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4929 childValues_.reserve(size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4930 addChildValues_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4931 ArrayIndex lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4932 for (ArrayIndex index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4933 if (hasCommentForValue(value[index])) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4934 isMultiLine = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4935 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4936 writeValue(value[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4937 lineLength += static_cast<ArrayIndex>(childValues_[index].length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4938 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4939 addChildValues_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4940 isMultiLine = isMultiLine || lineLength >= rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4941 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4942 return isMultiLine;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4943 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4944
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4945 void StyledStreamWriter::pushValue(const String& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4946 if (addChildValues_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4947 childValues_.push_back(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4948 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4949 *document_ << value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4950 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4951
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4952 void StyledStreamWriter::writeIndent() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4953 // blep intended this to look at the so-far-written string
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4954 // to determine whether we are already indented, but
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4955 // with a stream we cannot do that. So we rely on some saved state.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4956 // The caller checks indented_.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4957 *document_ << '\n' << indentString_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4958 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4959
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4960 void StyledStreamWriter::writeWithIndent(const String& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4961 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4962 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4963 *document_ << value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4964 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4965 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4966
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4967 void StyledStreamWriter::indent() { indentString_ += indentation_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4968
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4969 void StyledStreamWriter::unindent() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4970 assert(indentString_.size() >= indentation_.size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4971 indentString_.resize(indentString_.size() - indentation_.size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4972 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4973
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4974 void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4975 if (!root.hasComment(commentBefore))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4976 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4977
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4978 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4979 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4980 const String& comment = root.getComment(commentBefore);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4981 String::const_iterator iter = comment.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4982 while (iter != comment.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4983 *document_ << *iter;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4984 if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4985 // writeIndent(); // would include newline
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4986 *document_ << indentString_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4987 ++iter;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4988 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4989 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4990 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4991
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4992 void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4993 if (root.hasComment(commentAfterOnSameLine))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4994 *document_ << ' ' << root.getComment(commentAfterOnSameLine);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4995
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4996 if (root.hasComment(commentAfter)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4997 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4998 *document_ << root.getComment(commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
4999 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5000 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5001 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5002
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5003 bool StyledStreamWriter::hasCommentForValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5004 return value.hasComment(commentBefore) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5005 value.hasComment(commentAfterOnSameLine) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5006 value.hasComment(commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5007 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5008
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5009 //////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5010 // BuiltStyledStreamWriter
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5011
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5012 /// Scoped enums are not available until C++11.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5013 struct CommentStyle {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5014 /// Decide whether to write comments.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5015 enum Enum {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5016 None, ///< Drop all comments.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5017 Most, ///< Recover odd behavior of previous versions (not implemented yet).
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5018 All ///< Keep all comments.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5019 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5020 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5021
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5022 struct BuiltStyledStreamWriter : public StreamWriter {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5023 BuiltStyledStreamWriter(String indentation,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5024 CommentStyle::Enum cs,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5025 String colonSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5026 String nullSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5027 String endingLineFeedSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5028 bool useSpecialFloats,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5029 unsigned int precision,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5030 PrecisionType precisionType);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5031 int write(Value const& root, OStream* sout) override;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5032
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5033 private:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5034 void writeValue(Value const& value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5035 void writeArrayValue(Value const& value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5036 bool isMultilineArray(Value const& value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5037 void pushValue(String const& value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5038 void writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5039 void writeWithIndent(String const& value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5040 void indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5041 void unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5042 void writeCommentBeforeValue(Value const& root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5043 void writeCommentAfterValueOnSameLine(Value const& root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5044 static bool hasCommentForValue(const Value& value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5045
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5046 typedef std::vector<String> ChildValues;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5047
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5048 ChildValues childValues_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5049 String indentString_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5050 unsigned int rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5051 String indentation_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5052 CommentStyle::Enum cs_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5053 String colonSymbol_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5054 String nullSymbol_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5055 String endingLineFeedSymbol_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5056 bool addChildValues_ : 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5057 bool indented_ : 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5058 bool useSpecialFloats_ : 1;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5059 unsigned int precision_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5060 PrecisionType precisionType_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5061 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5062 BuiltStyledStreamWriter::BuiltStyledStreamWriter(String indentation,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5063 CommentStyle::Enum cs,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5064 String colonSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5065 String nullSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5066 String endingLineFeedSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5067 bool useSpecialFloats,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5068 unsigned int precision,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5069 PrecisionType precisionType)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5070 : rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5071 colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5072 endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5073 addChildValues_(false), indented_(false),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5074 useSpecialFloats_(useSpecialFloats), precision_(precision),
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5075 precisionType_(precisionType) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5076 int BuiltStyledStreamWriter::write(Value const& root, OStream* sout) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5077 sout_ = sout;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5078 addChildValues_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5079 indented_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5080 indentString_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5081 writeCommentBeforeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5082 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5083 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5084 indented_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5085 writeValue(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5086 writeCommentAfterValueOnSameLine(root);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5087 *sout_ << endingLineFeedSymbol_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5088 sout_ = nullptr;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5089 return 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5090 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5091 void BuiltStyledStreamWriter::writeValue(Value const& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5092 switch (value.type()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5093 case nullValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5094 pushValue(nullSymbol_);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5095 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5096 case intValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5097 pushValue(valueToString(value.asLargestInt()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5098 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5099 case uintValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5100 pushValue(valueToString(value.asLargestUInt()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5101 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5102 case realValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5103 pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5104 precisionType_));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5105 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5106 case stringValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5107 // Is NULL is possible for value.string_? No.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5108 char const* str;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5109 char const* end;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5110 bool ok = value.getString(&str, &end);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5111 if (ok)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5112 pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5113 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5114 pushValue("");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5115 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5116 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5117 case booleanValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5118 pushValue(valueToString(value.asBool()));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5119 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5120 case arrayValue:
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5121 writeArrayValue(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5122 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5123 case objectValue: {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5124 Value::Members members(value.getMemberNames());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5125 if (members.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5126 pushValue("{}");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5127 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5128 writeWithIndent("{");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5129 indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5130 auto it = members.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5131 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5132 String const& name = *it;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5133 Value const& childValue = value[name];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5134 writeCommentBeforeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5135 writeWithIndent(valueToQuotedStringN(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5136 name.data(), static_cast<unsigned>(name.length())));
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5137 *sout_ << colonSymbol_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5138 writeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5139 if (++it == members.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5140 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5141 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5142 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5143 *sout_ << ",";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5144 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5145 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5146 unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5147 writeWithIndent("}");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5148 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5149 } break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5150 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5151 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5152
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5153 void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5154 unsigned size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5155 if (size == 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5156 pushValue("[]");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5157 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5158 bool isMultiLine = (cs_ == CommentStyle::All) || isMultilineArray(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5159 if (isMultiLine) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5160 writeWithIndent("[");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5161 indent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5162 bool hasChildValue = !childValues_.empty();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5163 unsigned index = 0;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5164 for (;;) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5165 Value const& childValue = value[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5166 writeCommentBeforeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5167 if (hasChildValue)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5168 writeWithIndent(childValues_[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5169 else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5170 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5171 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5172 indented_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5173 writeValue(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5174 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5175 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5176 if (++index == size) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5177 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5178 break;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5179 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5180 *sout_ << ",";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5181 writeCommentAfterValueOnSameLine(childValue);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5182 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5183 unindent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5184 writeWithIndent("]");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5185 } else // output on a single line
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5186 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5187 assert(childValues_.size() == size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5188 *sout_ << "[";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5189 if (!indentation_.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5190 *sout_ << " ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5191 for (unsigned index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5192 if (index > 0)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5193 *sout_ << ((!indentation_.empty()) ? ", " : ",");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5194 *sout_ << childValues_[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5195 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5196 if (!indentation_.empty())
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5197 *sout_ << " ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5198 *sout_ << "]";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5199 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5200 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5201 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5202
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5203 bool BuiltStyledStreamWriter::isMultilineArray(Value const& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5204 ArrayIndex const size = value.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5205 bool isMultiLine = size * 3 >= rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5206 childValues_.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5207 for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5208 Value const& childValue = value[index];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5209 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5210 !childValue.empty());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5211 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5212 if (!isMultiLine) // check if line length > max line length
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5213 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5214 childValues_.reserve(size);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5215 addChildValues_ = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5216 ArrayIndex lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5217 for (ArrayIndex index = 0; index < size; ++index) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5218 if (hasCommentForValue(value[index])) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5219 isMultiLine = true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5220 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5221 writeValue(value[index]);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5222 lineLength += static_cast<ArrayIndex>(childValues_[index].length());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5223 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5224 addChildValues_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5225 isMultiLine = isMultiLine || lineLength >= rightMargin_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5226 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5227 return isMultiLine;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5228 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5229
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5230 void BuiltStyledStreamWriter::pushValue(String const& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5231 if (addChildValues_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5232 childValues_.push_back(value);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5233 else
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5234 *sout_ << value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5235 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5236
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5237 void BuiltStyledStreamWriter::writeIndent() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5238 // blep intended this to look at the so-far-written string
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5239 // to determine whether we are already indented, but
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5240 // with a stream we cannot do that. So we rely on some saved state.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5241 // The caller checks indented_.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5242
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5243 if (!indentation_.empty()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5244 // In this case, drop newlines too.
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5245 *sout_ << '\n' << indentString_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5246 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5247 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5248
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5249 void BuiltStyledStreamWriter::writeWithIndent(String const& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5250 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5251 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5252 *sout_ << value;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5253 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5254 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5255
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5256 void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5257
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5258 void BuiltStyledStreamWriter::unindent() {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5259 assert(indentString_.size() >= indentation_.size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5260 indentString_.resize(indentString_.size() - indentation_.size());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5261 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5262
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5263 void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5264 if (cs_ == CommentStyle::None)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5265 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5266 if (!root.hasComment(commentBefore))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5267 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5268
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5269 if (!indented_)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5270 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5271 const String& comment = root.getComment(commentBefore);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5272 String::const_iterator iter = comment.begin();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5273 while (iter != comment.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5274 *sout_ << *iter;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5275 if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5276 // writeIndent(); // would write extra newline
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5277 *sout_ << indentString_;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5278 ++iter;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5279 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5280 indented_ = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5281 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5282
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5283 void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5284 Value const& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5285 if (cs_ == CommentStyle::None)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5286 return;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5287 if (root.hasComment(commentAfterOnSameLine))
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5288 *sout_ << " " + root.getComment(commentAfterOnSameLine);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5289
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5290 if (root.hasComment(commentAfter)) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5291 writeIndent();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5292 *sout_ << root.getComment(commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5293 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5294 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5295
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5296 // static
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5297 bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5298 return value.hasComment(commentBefore) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5299 value.hasComment(commentAfterOnSameLine) ||
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5300 value.hasComment(commentAfter);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5301 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5302
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5303 ///////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5304 // StreamWriter
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5305
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5306 StreamWriter::StreamWriter() : sout_(nullptr) {}
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5307 StreamWriter::~StreamWriter() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5308 StreamWriter::Factory::~Factory() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5309 StreamWriterBuilder::StreamWriterBuilder() { setDefaults(&settings_); }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5310 StreamWriterBuilder::~StreamWriterBuilder() = default;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5311 StreamWriter* StreamWriterBuilder::newStreamWriter() const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5312 String indentation = settings_["indentation"].asString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5313 String cs_str = settings_["commentStyle"].asString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5314 String pt_str = settings_["precisionType"].asString();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5315 bool eyc = settings_["enableYAMLCompatibility"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5316 bool dnp = settings_["dropNullPlaceholders"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5317 bool usf = settings_["useSpecialFloats"].asBool();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5318 unsigned int pre = settings_["precision"].asUInt();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5319 CommentStyle::Enum cs = CommentStyle::All;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5320 if (cs_str == "All") {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5321 cs = CommentStyle::All;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5322 } else if (cs_str == "None") {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5323 cs = CommentStyle::None;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5324 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5325 throwRuntimeError("commentStyle must be 'All' or 'None'");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5326 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5327 PrecisionType precisionType(significantDigits);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5328 if (pt_str == "significant") {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5329 precisionType = PrecisionType::significantDigits;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5330 } else if (pt_str == "decimal") {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5331 precisionType = PrecisionType::decimalPlaces;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5332 } else {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5333 throwRuntimeError("precisionType must be 'significant' or 'decimal'");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5334 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5335 String colonSymbol = " : ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5336 if (eyc) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5337 colonSymbol = ": ";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5338 } else if (indentation.empty()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5339 colonSymbol = ":";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5340 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5341 String nullSymbol = "null";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5342 if (dnp) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5343 nullSymbol.clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5344 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5345 if (pre > 17)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5346 pre = 17;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5347 String endingLineFeedSymbol;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5348 return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5349 endingLineFeedSymbol, usf, pre,
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5350 precisionType);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5351 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5352 static void getValidWriterKeys(std::set<String>* valid_keys) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5353 valid_keys->clear();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5354 valid_keys->insert("indentation");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5355 valid_keys->insert("commentStyle");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5356 valid_keys->insert("enableYAMLCompatibility");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5357 valid_keys->insert("dropNullPlaceholders");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5358 valid_keys->insert("useSpecialFloats");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5359 valid_keys->insert("precision");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5360 valid_keys->insert("precisionType");
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5361 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5362 bool StreamWriterBuilder::validate(Json::Value* invalid) const {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5363 Json::Value my_invalid;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5364 if (!invalid)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5365 invalid = &my_invalid; // so we do not need to test for NULL
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5366 Json::Value& inv = *invalid;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5367 std::set<String> valid_keys;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5368 getValidWriterKeys(&valid_keys);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5369 Value::Members keys = settings_.getMemberNames();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5370 size_t n = keys.size();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5371 for (size_t i = 0; i < n; ++i) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5372 String const& key = keys[i];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5373 if (valid_keys.find(key) == valid_keys.end()) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5374 inv[key] = settings_[key];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5375 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5376 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5377 return inv.empty();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5378 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5379 Value& StreamWriterBuilder::operator[](const String& key) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5380 return settings_[key];
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5381 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5382 // static
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5383 void StreamWriterBuilder::setDefaults(Json::Value* settings) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5384 //! [StreamWriterBuilderDefaults]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5385 (*settings)["commentStyle"] = "All";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5386 (*settings)["indentation"] = "\t";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5387 (*settings)["enableYAMLCompatibility"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5388 (*settings)["dropNullPlaceholders"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5389 (*settings)["useSpecialFloats"] = false;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5390 (*settings)["precision"] = 17;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5391 (*settings)["precisionType"] = "significant";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5392 //! [StreamWriterBuilderDefaults]
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5393 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5394
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5395 String writeString(StreamWriter::Factory const& factory, Value const& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5396 OStringStream sout;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5397 StreamWriterPtr const writer(factory.newStreamWriter());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5398 writer->write(root, &sout);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5399 return sout.str();
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5400 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5401
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5402 OStream& operator<<(OStream& sout, Value const& root) {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5403 StreamWriterBuilder builder;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5404 StreamWriterPtr const writer(builder.newStreamWriter());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5405 writer->write(root, &sout);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5406 return sout;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5407 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5408
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5409 } // namespace Json
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5410
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5411 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5412 // End of content of file: src/lib_json/json_writer.cpp
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5413 // //////////////////////////////////////////////////////////////////////
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5414
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5415
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5416
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5417
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents:
diff changeset
5418