@@ -110,7 +110,7 @@ struct ParsedJson {
110110 // return false if the tape is likely wrong (e.g., you did not parse a valid
111111 // JSON).
112112 WARN_UNUSED
113- bool printjson () {
113+ bool printjson (std::ostream &os ) {
114114 if (!isvalid) return false ;
115115 size_t tapeidx = 0 ;
116116 u64 tape_val = tape[tapeidx];
@@ -138,64 +138,64 @@ struct ParsedJson {
138138 type = (tape_val >> 56 );
139139 if (!inobject[depth]) {
140140 if ((inobjectidx[depth] > 0 ) && (type != ' ]' ))
141- printf ( " , " ) ;
141+ os << " , " ;
142142 inobjectidx[depth]++;
143143 } else { // if (inobject) {
144144 if ((inobjectidx[depth] > 0 ) && ((inobjectidx[depth] & 1 ) == 0 ) &&
145145 (type != ' }' ))
146- printf ( " , " ) ;
146+ os << " , " ;
147147 if (((inobjectidx[depth] & 1 ) == 1 ))
148- printf ( " : " ) ;
148+ os << " : " ;
149149 inobjectidx[depth]++;
150150 }
151151 switch (type) {
152152 case ' "' : // we have a string
153- putchar ( ' "' ) ;
153+ os << ' "' ;
154154 print_with_escapes ((const unsigned char *)(string_buf + payload));
155- putchar ( ' "' ) ;
155+ os << ' "' ;
156156 break ;
157157 case ' l' : // we have a long int
158158 if (tapeidx + 1 >= howmany)
159159 return false ;
160- printf ( " % " PRId64, (int64_t )tape[++tapeidx]) ;
160+ os << (int64_t )tape[++tapeidx];
161161 break ;
162162 case ' d' : // we have a double
163163 if (tapeidx + 1 >= howmany)
164164 return false ;
165165 double answer;
166166 memcpy (&answer, &tape[++tapeidx], sizeof (answer));
167- printf ( " %f " , answer) ;
167+ os << answer;
168168 break ;
169169 case ' n' : // we have a null
170- printf ( " null" ) ;
170+ os << " null" ;
171171 break ;
172172 case ' t' : // we have a true
173- printf ( " true" ) ;
173+ os << " true" ;
174174 break ;
175175 case ' f' : // we have a false
176- printf ( " false" ) ;
176+ os << " false" ;
177177 break ;
178178 case ' {' : // we have an object
179- printf ( " \n " ) ;
180- printf ( " %*s \n %*s " , depth, " { " , depth + 1 , " " ) ;
179+ os << ' \n ' ;
180+ os << ' { ' ;
181181 depth++;
182182 inobject[depth] = true ;
183183 inobjectidx[depth] = 0 ;
184184 break ;
185185 case ' }' : // we end an object
186186 depth--;
187- printf ( " \n %*s} \n %*s " , depth - 1 , " " , depth, " " ) ;
187+ os << ' } ' ;
188188 break ;
189189 case ' [' : // we start an array
190- printf ( " \n " ) ;
191- printf ( " %*s \n %*s " , depth, " [ " , depth + 1 , " " );
190+ os << ' \n ' ;
191+ os << ' [ ' ;
192192 depth++;
193193 inobject[depth] = false ;
194194 inobjectidx[depth] = 0 ;
195195 break ;
196196 case ' ]' : // we end an array
197197 depth--;
198- printf ( " \n %*s] \n %*s " , depth - 1 , " " , depth, " " );
198+ os << ' ] ' ;
199199 break ;
200200 case ' r' : // we start and end with the root node
201201 printf (" should we be hitting the root node?\n " );
@@ -215,7 +215,7 @@ struct ParsedJson {
215215 }
216216
217217 WARN_UNUSED
218- bool dump_raw_tape () {
218+ bool dump_raw_tape (std::ostream &os ) {
219219 if (!isvalid) return false ;
220220 size_t tapeidx = 0 ;
221221 u64 tape_val = tape[tapeidx++];
@@ -228,52 +228,49 @@ struct ParsedJson {
228228 return false ;
229229 }
230230 for (; tapeidx < howmany; tapeidx++) {
231- printf ( " %zu : " , tapeidx);
231+ os << tapeidx << " : " ;
232232 tape_val = tape[tapeidx];
233233 u64 payload = tape_val & JSONVALUEMASK;
234234 type = (tape_val >> 56 );
235235 switch (type) {
236236 case ' "' : // we have a string
237- printf (" string: " );
238- putchar (' "' );
237+ os << " string \" " ;
239238 print_with_escapes ((const unsigned char *)(string_buf + payload));
240- putchar (' "' );
241- printf (" \n " );
239+ os << ' "' ;
242240 break ;
243241 case ' l' : // we have a long int
244242 if (tapeidx + 1 >= howmany)
245243 return false ;
246- printf (" integer: " );
247- printf (" %" PRId64" \n " , (int64_t )tape[++tapeidx]);
244+ os << " integer " << (int64_t )tape[++tapeidx] << " \n " ;
248245 break ;
249246 case ' d' : // we have a double
250- printf ( " float: " ) ;
247+ os << " float " ;
251248 if (tapeidx + 1 >= howmany)
252249 return false ;
253250 double answer;
254251 memcpy (&answer, &tape[++tapeidx], sizeof (answer));
255- printf ( " %f \n " , answer) ;
252+ os << answer << ' \n ' ;
256253 break ;
257254 case ' n' : // we have a null
258- printf ( " null\n " ) ;
255+ os << " null\n " ;
259256 break ;
260257 case ' t' : // we have a true
261- printf ( " true\n " ) ;
258+ os << " true\n " ;
262259 break ;
263260 case ' f' : // we have a false
264- printf ( " false\n " ) ;
261+ os << " false\n " ;
265262 break ;
266263 case ' {' : // we have an object
267- printf ( " {\t // pointing to next tape location %llu \n " , payload) ;
264+ os << " {\t // pointing to next tape location " << payload << " \n " ;
268265 break ;
269266 case ' }' : // we end an object
270- printf ( " }\t // pointing to previous tape location %llu \n " , payload) ;
267+ os << " }\t // pointing to previous tape location " << payload << " \n " ;
271268 break ;
272269 case ' [' : // we start an array
273- printf ( " [\t // pointing to next tape location %llu \n " , payload) ;
270+ os << " [\t // pointing to next tape location " << payload << " \n " ;
274271 break ;
275272 case ' ]' : // we end an array
276- printf ( " ]\t // pointing to previous tape location %llu \n " , payload) ;
273+ os << " ]\t // pointing to previous tape location " << payload << " \n " ;
277274 break ;
278275 case ' r' : // we start and end with the root node
279276 printf (" end of root\n " );
0 commit comments