-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathjson.cc
More file actions
4947 lines (4258 loc) · 92.4 KB
/
Copy pathjson.cc
File metadata and controls
4947 lines (4258 loc) · 92.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma GCC visibility push(internal)
namespace ircd::json
{
using namespace ircd::spirit;
// Instantiations of the grammars
struct parser extern const parser;
struct printer extern const printer;
}
#pragma GCC visibility pop
#pragma GCC visibility push(internal)
BOOST_FUSION_ADAPT_STRUCT
(
ircd::json::member,
( decltype(ircd::json::member::first), first )
( decltype(ircd::json::member::second), second )
)
#pragma GCC visibility pop
#pragma GCC visibility push(internal)
BOOST_FUSION_ADAPT_STRUCT
(
ircd::json::object::member,
( decltype(ircd::json::object::member::first), first )
( decltype(ircd::json::object::member::second), second )
)
#pragma GCC visibility pop
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
struct [[gnu::visibility("internal")]]
ircd::json::parser
:qi::grammar<const char *, unused_type>
{
using it = const char *;
template<class T = unused_type,
class... A>
using rule = qi::rule<it, T, A...>;
const rule<> NUL { lit('\0') ,"nul" };
// insignificant whitespaces
const rule<> SP { lit('\x20') ,"space" };
const rule<> HT { lit('\x09') ,"horizontal tab" };
const rule<> CR { lit('\x0D') ,"carriage return" };
const rule<> LF { lit('\x0A') ,"line feed" };
// whitespace skipping
const rule<> WS { SP | HT | CR | LF ,"whitespace" };
const rule<> ws { *(WS) ,"whitespace monoid" };
const rule<> wsp { +(WS) ,"whitespace semigroup" };
// structural
const rule<> object_begin { lit('{') ,"object begin" };
const rule<> object_end { lit('}') ,"object end" };
const rule<> array_begin { lit('[') ,"array begin" };
const rule<> array_end { lit(']') ,"array end" };
const rule<> name_sep { lit(':') ,"name sep" };
const rule<> value_sep { lit(',') ,"value sep" };
const rule<> escape { lit('\\') ,"escape" };
const rule<> quote { lit('"') ,"quote" };
// literal
const rule<> lit_false { lit("false") ,"literal false" };
const rule<> lit_true { lit("true") ,"literal true" };
const rule<> lit_null { lit("null") ,"null" };
const rule<> boolean { lit_true | lit_false ,"boolean" };
const rule<> literal { lit_true | lit_false | lit_null ,"literal" };
// numerical
const rule<> number_int
{
(char_("1-9") >> repeat(0, 18)[char_("0-9")]) | lit('0')
,"integer"
};
const rule<> number_frac
{
lit('.') >> repeat(1, 18)[char_("0-9")] >> -char_("1-9")
,"fraction"
};
const rule<> number_exp
{
char_("eE") >> -char_("+-") >> repeat(1, 4)[char_("0-9")]
,"exponent"
};
const rule<> number
{
-lit('-') >> number_int >> -number_frac >> -number_exp
,"number"
};
// string
const rule<> utf16_surrogate
{
qi::uint_parser
<
uint16_t, // 16 bit width
16U, // base-16 (hex)
4U, // minimum digits
4U // maximum digits
>{}
,"UTF-16 surrogate"
};
const rule<> unicode
{
lit('u') >> utf16_surrogate
,"escaped unicode"
};
const rule<> control
{
char_('\x00', '\x1F')
,"control character"
};
// characters that must be escaped
const rule<> escaped
{
quote | escape | control
,"escaped character"
};
// characters that should appear after an escaping solidus
const rule<> escaper
{
char_("btnfr0\"\\") | unicode
,"escaper"
};
// cscapers supersetting the rule above with addl non-canonical chars
const rule<> escaper_nc
{
escaper | lit('/')
,"escaper"
};
const rule<> escape_sequence
{
escape >> escaper_nc
,"escape sequence"
};
const rule<string_view> chars
{
//raw[*((char_ - escaped) | (escape >> escaper_nc))]
raw[*((~char_('\x00', '\x1F') - char_("\x22\x5C")) | (escape >> escaper_nc))]
,"characters"
};
template<class block_t> static u64x2 string_content_block(const block_t, const block_t) noexcept;
const custom_parser string_content{};
const rule<string_view> string
{
//quote >> chars >> (!escape >> quote)
string_content
,"string"
};
// container
const rule<string_view> name
{
string.alias()
,"name"
};
// recursion depth
_r1_type depth;
[[noreturn]] static void throws_exceeded();
rule<unused_type(uint)> member
{
name >> -ws >> name_sep >> -ws >> value(depth)
,"member"
};
rule<unused_type(uint)> object
{
(eps(depth < json::object::max_recursion_depth) | eps[throws_exceeded]) >>
object_begin >> -((-ws >> member(depth)) % (-ws >> value_sep)) >> -ws >> object_end
,"object"
};
rule<unused_type(uint)> array
{
(eps(depth < json::array::max_recursion_depth) | eps[throws_exceeded]) >>
array_begin >> -((-ws >> value(depth)) % (-ws >> value_sep)) >> -ws >> array_end
,"array"
};
// primary recursive rule
rule<unused_type(uint)> value
{
("e >> string)
| (&object_begin >> object(depth + 1))
| (&array_begin >> array(depth + 1))
| number
| lit_true
| lit_false
| lit_null
,"value"
};
template<class gen,
class... attr>
bool operator()(const char *&start, const char *const &stop, gen&&, attr&&...) const;
template<class gen,
class... attr>
bool operator()(const char *const &start, const char *const &stop, gen&&, attr&&...) const;
parser() noexcept
:parser::base_type{rule<>{}} // required by spirit
{
// synthesized repropagation of recursive rules
value %= ("e >> string)
| (&object_begin >> object(depth + 1))
| (&array_begin >> array(depth + 1))
| number
| lit_true
| lit_false
| lit_null
;
}
}
const ircd::json::parser;
#pragma GCC diagnostic pop
struct [[gnu::visibility("internal")]]
ircd::json::printer
:karma::grammar<char *, unused_type>
{
using it = char *;
template<class T = unused_type,
class... A>
using rule = karma::rule<it, T, A...>;
const rule<> NUL { lit('\0') ,"nul" };
// insignificant whitespaces
const rule<> SP { lit('\x20') ,"space" };
const rule<> HT { lit('\x09') ,"horizontal tab" };
const rule<> CR { lit('\x0D') ,"carriage return" };
const rule<> LF { lit('\x0A') ,"line feed" };
// whitespace skipping
const rule<> WS { SP | HT | CR | LF ,"whitespace" };
const rule<> ws { *(WS) ,"whitespace monoid" };
const rule<> wsp { +(WS) ,"whitespace semigroup" };
// structural
const rule<> object_begin { lit('{') ,"object begin" };
const rule<> object_end { lit('}') ,"object end" };
const rule<> array_begin { lit('[') ,"array begin" };
const rule<> array_end { lit(']') ,"array end" };
const rule<> name_sep { lit(':') ,"name separator" };
const rule<> value_sep { lit(',') ,"value separator" };
const rule<> quote { lit('"') ,"quote" };
const rule<> escape { lit('\\') ,"escape" };
// literal
const rule<string_view> lit_true { karma::string("true") ,"literal true" };
const rule<string_view> lit_false { karma::string("false") ,"literal false" };
const rule<string_view> lit_null { karma::string("null") ,"literal null" };
const rule<string_view> boolean { lit_true | lit_false ,"boolean" };
const rule<string_view> literal { lit_true | lit_false | lit_null ,"literal" };
// number
const rule<string_view> number
{
double_
,"number"
};
// string
using string_context = boost::spirit::context<fusion::cons<const string_view &>, fusion::vector<>>;
static void string_generate(unused_type, string_context &, bool &) noexcept;
const rule<string_view()> string
{
quote << eps[std::bind(&printer::string_generate, ph::_1, ph::_2, ph::_3)] << quote
,"string"
};
const rule<string_view()> name
{
string.alias()
,"name"
};
// primary recursive rule
rule<string_view> value
{
rule<string_view>{}
,"value"
};
rule<json::object::member> member
{
rule<json::object::member>{}
,"member"
};
rule<json::object> object
{
rule<json::object>{}
,"object"
};
rule<json::array> array
{
rule<json::array>{}
,"array"
};
template<class it_a,
class it_b,
class closure>
static void list_protocol(mutable_buffer &, it_a begin, const it_b &end, closure&&);
template<class gen,
class... attr>
void operator()(mutable_buffer &out, gen&&, attr&&...) const;
printer() noexcept
:printer::base_type{rule<>{}}
{
// synthesized repropagation of recursive rules
member %= name << name_sep << value;
object %= object_begin << -(member % value_sep) << object_end;
array %= array_begin << -(value % value_sep) << array_end;
value %= (&object << object)
| (&array << array)
| (&literal << literal)
| (&number << number)
| string
;
}
}
const ircd::json::printer;
decltype(ircd::json::stats)
ircd::json::stats;
template<class gen,
class... attr>
[[gnu::always_inline]]
inline void
ircd::json::printer::operator()(mutable_buffer &out,
gen&& g,
attr&&... a)
const
{
#ifdef IRCD_JSON_PRINTER_STATS
++stats.print_calls;
const prof::scope_cycles timer{stats.print_cycles};
#endif
if(unlikely(!ircd::generate(out, std::forward<gen>(g), std::forward<attr>(a)...)))
throw print_error
{
"Failed to generate JSON"
};
}
template<class it_a,
class it_b,
class closure>
[[gnu::always_inline]]
inline void
ircd::json::printer::list_protocol(mutable_buffer &out,
it_a it,
const it_b &end,
closure&& lambda)
{
if(likely(it != end))
{
lambda(out, *it);
for(++it; it != end; ++it)
{
const auto &printer(json::printer);
printer(out, printer.value_sep);
lambda(out, *it);
}
}
}
inline void
ircd::json::printer::string_generate(unused_type,
string_context &g,
bool &ret)
noexcept
{
#if __has_builtin(__builtin_assume)
__builtin_assume(ret == true);
#endif
assert(generator_state);
auto &state
{
*generator_state
};
const string_view &input
{
attr_at<0>(g)
};
const size_t output_length
{
json::string::stringify(state.out, input)
};
const size_t consumed
{
std::min(output_length, size(state.out))
};
state.consumed += consume(state.out, consumed);
state.generated += output_length;
ret = state.generated == state.consumed;
}
template<class gen,
class... attr>
[[gnu::always_inline]]
inline bool
ircd::json::parser::operator()(const char *const &start_,
const char *const &stop,
gen&& g,
attr&&...a)
const
{
const char *start(start_);
return operator()(start, stop, std::forward<gen>(g), std::forward<attr>(a)...);
}
template<class gen,
class... attr>
[[gnu::always_inline]]
inline bool
ircd::json::parser::operator()(const char *&start,
const char *const &stop,
gen&& g,
attr&&...a)
const
{
#ifdef IRCD_JSON_PARSER_STATS
++stats.parse_calls;
const prof::scope_cycles timer{stats.parse_cycles};
#endif
return ircd::parse<parse_error>(start, stop, std::forward<gen>(g), std::forward<attr>(a)...);
}
/// The input covers everything from the alleged start of our alleged string
/// to the end of whatever the user provided. Returns true if successful and
/// the result string_view is set in the context attribute; the iterator is
/// advanced.
template<class iterator,
class context,
class skipper,
class attr>
inline bool
ircd::json::custom_parser::parse(iterator &__restrict__ start,
const iterator &__restrict__ stop,
context &g,
const skipper &,
attr &)
const
{
// Clang scales between 128bit and 256bit systems when we use the 256 bit
// type (note that performance even improves on some 128 bit systems). GCC
// falls back to scalar instead, so we have to case 128bit systems on GCC.
#if defined(__AVX__) || defined(__clang__)
using block_t = u8x32;
using block_t_u = u256x1_u;
#else
using block_t = u8x16;
using block_t_u = u128x1_u;
#endif
assert(start <= stop);
const size_t input_max
{
size_t(std::distance(start, stop))
};
// The input is a priori invalid if the length is not greater than "" or
// the first character is not quote.
const bool input_valid
{
input_max >= 2 && start[0] == '"'
};
// When the input is valid subtract one for the new max length. Otherwise
// we mask this length to zero to void the remainder of this frame.
const u64x2 max
{
0, (input_max - 1) & boolmask<u64>(input_valid)
};
static const auto each_block
{
json::parser::string_content_block<block_t>
};
const auto count
{
simd::stream<block_t_u, block_t>(start + 1, max, each_block)
};
const bool ok
{
count[0] == 1
};
// Set the result in the context attribute. This covers the string content
// without surrounding quotes.
attr_at<0>(g) = string_view
{
start + ok, count[1] & boolmask<u64>(ok)
};
// Advance the iterator the length of the full string including quotes
// iff this parser was successful.
start += (1 + count[1] + 1) & boolmask<u64>(ok);
return ok;
}
template<class block_t>
inline ircd::u64x2
ircd::json::parser::string_content_block(const block_t block,
const block_t block_mask)
noexcept
{
assert(block_mask[0] == 0xff);
const block_t is_esc
(
block == '\\'
);
const block_t is_quote
(
block == '"'
);
const block_t is_ctrl
(
block < 0x20
);
const block_t is_special
{
is_esc | is_quote | is_ctrl
};
const block_t is_regular
{
simd::lateral<std::bit_and>(~is_special)
};
if(likely(is_regular[0]))
return u64x2
{
0, sizeof(block)
};
const u64 regular_prefix_count
{
simd::lzcnt(is_special | ~block_mask) / 8
};
if(likely(regular_prefix_count))
return u64x2
{
0, regular_prefix_count
};
const u64 err
{
popmask<u64>(is_quote[0])
| boolmask<u64>(is_ctrl[0])
| boolmask<u64>(is_esc[0] & ~block_mask[1])
};
const u64 add
{
1UL + popmask<u64>(is_esc[0] & (is_quote[1] | is_esc[1]) & block_mask[1])
};
return u64x2
{
err, add & boolmask<u64>(err == 0)
};
}
[[gnu::noinline]]
void
ircd::json::parser::throws_exceeded()
{
throw recursion_limit
{
"Maximum recursion depth exceeded"
};
}
///////////////////////////////////////////////////////////////////////////////
//
// json/tool.h
//
ircd::json::strung
ircd::json::replace(const strung &s,
const json::members &r)
{
static const auto in
{
[](const json::members &r, const object::member &m)
{
return std::any_of(begin(r), end(r), [&m]
(const json::member &r)
{
return string_view{r.first} == m.first;
});
}
};
if(!empty(s) && type(s) != type::OBJECT)
throw type_error
{
"Cannot replace member into JSON of type %s",
reflect(type(s))
};
size_t mctr {0};
thread_local std::array<member, iov::max_size> mb;
for(const object::member &m : object{s})
if(!in(r, m))
mb.at(mctr++) = member{m};
for(const json::member &m : r)
mb.at(mctr++) = m;
return strung
{
mb.data(), mb.data() + mctr
};
}
ircd::json::strung
ircd::json::replace(const strung &s,
const json::member &m_)
{
if(!empty(s) && type(s) != type::OBJECT)
throw type_error
{
"Cannot replace member into JSON of type %s",
reflect(type(s))
};
size_t mctr {0};
thread_local std::array<member, iov::max_size> mb;
for(const object::member &m : object{s})
if(m.first != string_view{m_.first})
mb.at(mctr++) = member{m};
mb.at(mctr++) = m_;
return strung
{
mb.data(), mb.data() + mctr
};
}
ircd::json::strung
ircd::json::insert(const strung &s,
const json::member &m)
{
if(!empty(s) && type(s) != type::OBJECT)
throw type_error
{
"Cannot insert member into JSON of type %s",
reflect(type(s))
};
size_t mctr {0};
thread_local std::array<member, iov::max_size> mb;
for(const object::member &m : object{s})
mb.at(mctr++) = member{m};
mb.at(mctr++) = m;
return strung
{
mb.data(), mb.data() + mctr
};
}
ircd::json::strung
ircd::json::remove(const strung &s,
const string_view &key)
{
if(empty(s))
return s;
if(type(s) != type::OBJECT)
throw type_error
{
"Cannot remove object member '%s' from JSON of type %s",
key,
reflect(type(s))
};
size_t mctr {0};
thread_local std::array<object::member, iov::max_size> mb;
for(const object::member &m : object{s})
if(m.first != key)
mb.at(mctr++) = m;
return strung
{
mb.data(), mb.data() + mctr
};
}
ircd::json::strung
ircd::json::remove(const strung &s,
const size_t &idx)
{
if(empty(s))
return s;
if(type(s) != type::ARRAY)
throw type_error
{
"Cannot remove array element [%zu] from JSON of type %s",
idx,
reflect(type(s))
};
size_t mctr{0}, i{0};
thread_local std::array<string_view, iov::max_size> mb;
for(const string_view &m : array{s})
if(i++ != idx)
mb.at(mctr++) = m;
return strung
{
mb.data(), mb.data() + mctr
};
}
void
ircd::json::merge(stack::object &out,
const vector &v)
{
struct val
{
//TODO: optimize with std::pmr::monotonic_buffer_resource et al
std::map<string_view, val, std::less<>> o;
std::vector<string_view> a;
string_view v;
void _merge_object(const json::object &o)
{
for(const auto &m : o)
{
val &v(this->o[m.first]);
v.merge(m.second);
}
}
void _merge_array(const json::array &a)
{
for(const auto &v : a)
this->a.emplace_back(v);
}
void merge(const string_view &v)
{
switch(json::type(v))
{
case json::OBJECT: _merge_object(v); break;
case json::ARRAY: _merge_array(v); break;
default: this->v = v; break;
}
}
void _compose_object(json::stack &out, json::stack::object &object) const
{
for(const auto &m : o)
{
json::stack::member member{object, m.first};
m.second.compose(out);
}
}
void _compose_object(json::stack &out, json::stack::member &member) const
{
json::stack::object object{member};
_compose_object(out, object);
}
void _compose_object(json::stack &out) const
{
json::stack::chase c{out, true};
if(c.m)
_compose_object(out, *c.m);
else if(c.o)
_compose_object(out, *c.o);
}
void _compose_array(json::stack &out) const
{
json::stack::array array{out};
for(const auto &v : a)
array.append(v);
}
void _compose_value(json::stack &out) const
{
json::stack::chase c{out, true};
if(c.a)
c.a->append(v);
else if(c.m)
c.m->append(v);
else
assert(0);
}
void compose(json::stack &out) const
{
if(!o.empty())
_compose_object(out);
else if(!a.empty())
_compose_array(out);
else if(!v.empty())
_compose_value(out);
}
val() = default;
val(const string_view &v)
{
merge(v);
}
};
val top;
for(const auto &o : v)
top.merge(o);
assert(out.s);
top.compose(*out.s);
}
///////////////////////////////////////////////////////////////////////////////
//
// json/stack.h
//
ircd::json::stack::stack(const mutable_buffer &buf,
flush_callback flusher,
const size_t &hiwat,
const size_t &lowat)
:buf{buf}
,flusher{std::move(flusher)}
,hiwat{hiwat}
,lowat{lowat}
{
}
ircd::json::stack::stack(stack &&other)
noexcept
:buf{std::move(other.buf)}
,flusher{std::move(other.flusher)}
,eptr{std::move(other.eptr)}
,cp{std::move(other.cp)}
,appended{std::move(other.appended)}
,flushed{std::move(other.flushed)}
,level{std::move(other.level)}
,hiwat{std::move(other.hiwat)}
,lowat{std::move(other.lowat)}
,co{std::move(other.co)}
,ca{std::move(other.ca)}
{
other.cp = nullptr;
other.co = nullptr;
other.ca = nullptr;
if(cp)
{
assert(cp->s == &other);
cp->s = this;
}
if(co)
{
assert(co->s == &other);
co->s = this;
}
if(ca)
{
assert(ca->s == &other);
ca->s = this;
}
}
ircd::json::stack::~stack()
noexcept
{
assert(closed());
if(buf.consumed())
flush(true);
assert(clean() || done());
}
void
ircd::json::stack::append(const char &c)
noexcept
{
append(1, [&c]
(const mutable_buffer &buf)
noexcept
{
buf[0] = c;
return 1;
});
}
void
ircd::json::stack::append(const string_view &s)
noexcept
{
append(s.size(), [&s]
(const mutable_buffer &buf)
noexcept
{
assert(ircd::size(buf) >= s.size());
return ircd::copy(buf, s);
});
}
void
ircd::json::stack::append(const size_t &expect,
const window_buffer::closure &closure)
noexcept try
{
if(!expect || failed())
return;
// Minimum bytes we keep available all times to allow the JSON to close
// correctly without complication on the user's stack unwind; hinted by
// the recursion level.
const size_t buf_min
{
level + 8
};
// Calculated buffer bytes required.
const size_t buf_req
{
expect + buf_min
};
// Since all appends are atomic, we need to have buffer available to print
// the JSON without having to flush while doing so. If we're low on buffer,
// this branch triggers a flush. Afterward, if there is still not enough
// buffer that's an error so the user needs to flush enough when called.
if(buf_req > buf.remaining())
{
if(unlikely(!flusher))
throw print_panic
{
"Insufficient buffer. I need %zu more bytes; you only have %zu left (of %zu).",
buf_req,
buf.remaining(),
size(buf.base)
};
if(!flush(true))
return;