Source file json_stream.ml
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
type jsonm_lexeme =
[ `Null
| `Bool of bool
| `String of string
| `Float of float
| `Name of string
| `As
| `Ae
| `Os
| `Oe ]
let string_of_float f =
let fract, intr = modf f in
if fract = 0.0 then Format.asprintf "%.0f" intr else Format.asprintf "%g" f
let string_needs_escaping_at index s =
let exception At of int in
try
for i = index to String.length s - 1 do
match s.[i] with
| '\"' | '\n' | '\r' | '\b' | '\t' | '\\' | '\x00' .. '\x1F' ->
raise (At i)
| _ -> ()
done ;
-1
with At i -> i
let do_escape_string s =
let buff = Buffer.create (String.length s) in
for i = 0 to String.length s - 1 do
match s.[i] with
| '\"' -> Buffer.add_string buff "\\\""
| '\n' -> Buffer.add_string buff "\\n"
| '\r' -> Buffer.add_string buff "\\r"
| '\b' -> Buffer.add_string buff "\\b"
| '\t' -> Buffer.add_string buff "\\t"
| '\\' -> Buffer.add_string buff "\\\\"
| '\x00' .. '\x1F' as c ->
Format.kasprintf (Buffer.add_string buff) "\\u%04x" (Char.code c)
| c -> Buffer.add_char buff c
done ;
Buffer.contents buff
let escape_string s =
if string_needs_escaping_at 0 s >= 0 then do_escape_string s else s
(** small_string_seq_of_jsonm_lexeme_seq: converts a seq of lexeme into a naive
seq of small strings. This may or may not be appripriate depending on the
way the resulting seq is consumed. *)
let small_string_seq_of_jsonm_lexeme_seq ~newline (s : jsonm_lexeme Seq.t) :
string Seq.t =
let rec sseq first depth seq () =
match seq () with
| Seq.Nil ->
assert (depth = 0) ;
if newline then Seq.Cons ("\n", Seq.empty) else Seq.Nil
| Seq.Cons (`Null, seq) ->
let tail = Seq.Cons ("null", sseq false depth seq) in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`Bool true, seq) ->
let tail = Seq.Cons ("true", sseq false depth seq) in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`Bool false, seq) ->
let tail = Seq.Cons ("false", sseq false depth seq) in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`As, seq) ->
let tail = Seq.Cons ("[", sseq true (depth + 1) seq) in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`Ae, seq) -> Seq.Cons ("]", sseq false (depth - 1) seq)
| Seq.Cons (`Os, seq) ->
let tail = Seq.Cons ("{", sseq true (depth + 1) seq) in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`Oe, seq) -> Seq.Cons ("}", sseq false (depth - 1) seq)
| Seq.Cons (`String s, seq) ->
let tail =
Seq.Cons
( "\"",
fun () ->
Seq.Cons
( escape_string s,
fun () -> Seq.Cons ("\"", sseq false depth seq) ) )
in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`Float f, seq) ->
let f = string_of_float f in
let tail = Seq.Cons (f, sseq false depth seq) in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
| Seq.Cons (`Name n, seq) ->
let tail =
Seq.Cons
( "\"",
fun () ->
Seq.Cons
( escape_string n,
fun () ->
Seq.Cons
("\"", fun () -> Seq.Cons (":", sseq true depth seq)) )
)
in
if (not first) && depth > 0 then Seq.Cons (",", fun () -> tail)
else tail
in
sseq false 0 s
let dump_unescaped_string chunk_size_hint buff str index seq =
let rec aux bytes_left_in_buff index =
if bytes_left_in_buff > String.length str - index then (
Buffer.add_substring buff str index (String.length str - index) ;
seq ())
else (
Buffer.add_substring buff str index bytes_left_in_buff ;
let s = Buffer.contents buff in
Buffer.clear buff ;
Seq.Cons (s, fun () -> aux chunk_size_hint (index + bytes_left_in_buff)))
in
aux (chunk_size_hint - Buffer.length buff) index
let dump_escaped_string chunk_size_hint buff str seq =
let rec aux_outer bytes_left_in_buff index =
let next_escape = string_needs_escaping_at index str in
if bytes_left_in_buff <= 6 then (
let s = Buffer.contents buff in
Buffer.clear buff ;
Seq.Cons (s, fun () -> aux_outer chunk_size_hint index))
else if next_escape < 0 then
dump_unescaped_string chunk_size_hint buff str index seq
else if next_escape = 0 then (
(match str.[index] with
| '\"' -> Buffer.add_string buff "\\\""
| '\n' -> Buffer.add_string buff "\\n"
| '\r' -> Buffer.add_string buff "\\r"
| '\b' -> Buffer.add_string buff "\\b"
| '\t' -> Buffer.add_string buff "\\t"
| '\\' -> Buffer.add_string buff "\\\\"
| '\x00' .. '\x1F' as c ->
Format.kasprintf (Buffer.add_string buff) "\\u%04x" (Char.code c)
| c -> Buffer.add_char buff c) ;
aux_outer (chunk_size_hint - Buffer.length buff) (index + 1))
else
let to_write_unescaped = next_escape - index in
if bytes_left_in_buff > to_write_unescaped then (
Buffer.add_substring buff str index to_write_unescaped ;
aux_outer
(bytes_left_in_buff - to_write_unescaped)
(index + to_write_unescaped))
else
let rec aux_inner bytes_left_in_buff index to_write_unescaped continue =
if bytes_left_in_buff < to_write_unescaped then (
Buffer.add_substring buff str index bytes_left_in_buff ;
let s = Buffer.contents buff in
Seq.Cons
( s,
fun () ->
aux_inner
chunk_size_hint
(index + bytes_left_in_buff)
(to_write_unescaped - bytes_left_in_buff)
continue ))
else (
Buffer.add_substring buff str index to_write_unescaped ;
continue (index + to_write_unescaped))
in
aux_inner bytes_left_in_buff index to_write_unescaped (fun index ->
aux_outer (chunk_size_hint - Buffer.length buff) index)
in
aux_outer (chunk_size_hint - Buffer.length buff) 0
let dump_string_literal chunk_size_hint buff literal seq =
Buffer.add_char buff '"' ;
dump_escaped_string chunk_size_hint buff literal (fun () ->
Buffer.add_char buff '"' ;
seq ())
let string_seq_of_jsonm_lexeme_seq ~newline ~chunk_size_hint
(s : jsonm_lexeme Seq.t) : string Seq.t =
let chunk_size_hint = min chunk_size_hint 16 in
let buff_size = chunk_size_hint + 16 in
let buff = Buffer.create buff_size in
let rec sseq first depth seq () =
if Buffer.length buff >= buff_size then (
let b = Buffer.contents buff in
Buffer.clear buff ;
Seq.Cons (b, fun () -> (sseq [@ocaml.tailcall]) first depth seq ()))
else
match seq () with
| Seq.Nil ->
assert (depth = 0) ;
if newline then Buffer.add_char buff '\n' ;
if Buffer.length buff = 0 then
Seq.Nil
else
let b = Buffer.contents buff in
Buffer.clear buff ;
Seq.Cons (b, Seq.empty)
| Seq.Cons (`Null, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
Buffer.add_string buff "null" ;
(sseq [@ocaml.tailcall]) false depth seq ()
| Seq.Cons (`Bool true, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
Buffer.add_string buff "true" ;
(sseq [@ocaml.tailcall]) false depth seq ()
| Seq.Cons (`Bool false, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
Buffer.add_string buff "false" ;
(sseq [@ocaml.tailcall]) false depth seq ()
| Seq.Cons (`As, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
Buffer.add_char buff '[' ;
(sseq [@ocaml.tailcall]) true (depth + 1) seq ()
| Seq.Cons (`Ae, seq) ->
assert (depth > 0) ;
Buffer.add_char buff ']' ;
(sseq [@ocaml.tailcall]) false (depth - 1) seq ()
| Seq.Cons (`Os, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
Buffer.add_char buff '{' ;
(sseq [@ocaml.tailcall]) true (depth + 1) seq ()
| Seq.Cons (`Oe, seq) ->
assert (depth > 0) ;
Buffer.add_char buff '}' ;
(sseq [@ocaml.tailcall]) false (depth - 1) seq ()
| Seq.Cons (`String s, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
dump_string_literal chunk_size_hint buff s (fun () ->
(sseq [@ocaml.tailcall]) false depth seq ())
| Seq.Cons (`Float f, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
let f = string_of_float f in
Buffer.add_string buff f ;
(sseq [@ocaml.tailcall]) false depth seq ()
| Seq.Cons (`Name n, seq) ->
if (not first) && depth > 0 then Buffer.add_char buff ',' ;
dump_string_literal chunk_size_hint buff n (fun () ->
Buffer.add_char buff ':' ;
(sseq [@ocaml.tailcall]) true depth seq ())
in
sseq false 0 s
let biseq_escaped_string_content buffer offset s k =
let can_be_written = Bytes.length buffer - offset in
if String.length s + 1 > can_be_written + Bytes.length buffer then
if
offset < Bytes.length buffer / 2
then (
Bytes.blit_string s 0 buffer offset can_be_written ;
let offset = offset + can_be_written in
assert (offset = Bytes.length buffer) ;
Seq.Cons
( (buffer, 0, offset),
fun () ->
let s = Bytes.unsafe_of_string s in
Seq.Cons
( (s, can_be_written, Bytes.length s - can_be_written),
fun () -> k 0 ) ))
else
Seq.Cons
( (buffer, 0, offset),
fun () ->
let s = Bytes.unsafe_of_string s in
Seq.Cons ((s, 0, Bytes.length s), fun () -> k 0) )
else if String.length s + 1 <= can_be_written then (
Bytes.blit_string s 0 buffer offset (String.length s) ;
let offset = offset + String.length s in
k offset)
else (
Bytes.blit_string s 0 buffer offset can_be_written ;
let offset = offset + can_be_written in
assert (offset = Bytes.length buffer) ;
Seq.Cons
( (buffer, 0, offset),
fun () ->
let remain_to_be_written = String.length s - can_be_written in
Bytes.blit_string s can_be_written buffer 0 remain_to_be_written ;
let offset = remain_to_be_written in
k offset ))
let biseq_string_literal buffer offset s k =
Bytes.set buffer offset '"' ;
let offset = offset + 1 in
let first_escape = string_needs_escaping_at 0 s in
if first_escape < 0 then
biseq_escaped_string_content buffer offset s (fun offset ->
Bytes.set buffer offset '"' ;
k (offset + 1))
else
let s = do_escape_string s in
biseq_escaped_string_content buffer offset s (fun offset ->
Bytes.set buffer offset '"' ;
k (offset + 1))
let blit_instructions_seq_of_jsonm_lexeme_seq ~newline ~buffer lexeme_seq =
let buffer_size = Bytes.length buffer in
if buffer_size < 32 then
raise
(Invalid_argument
"Data_encoding.blit_instructions_seq_of_jsonm_lexeme_seq") ;
let flush_at = buffer_size - 16 in
let[@ocaml.inline] sep first depth offset =
if (not first) && depth > 0 then (
Bytes.set buffer offset ',' ;
offset + 1)
else offset
in
let rec biseq first depth offset seq () =
if offset >= flush_at then
Seq.Cons
( (buffer, 0, offset),
fun () -> (biseq [@ocaml.tailcall]) first depth 0 seq () )
else
match seq () with
| Seq.Nil ->
assert (depth = 0) ;
let offset =
if newline then (
Bytes.set buffer offset '\n' ;
offset + 1)
else offset
in
if offset = 0 then
Seq.Nil
else Seq.Cons ((buffer, 0, offset), fun () -> Seq.Nil)
| Seq.Cons (`Null, seq) ->
let offset = sep first depth offset in
Bytes.blit_string "null" 0 buffer offset 4 ;
let offset = offset + 4 in
(biseq [@ocaml.tailcall]) false depth offset seq ()
| Seq.Cons (`Bool true, seq) ->
let offset = sep first depth offset in
Bytes.blit_string "true" 0 buffer offset 4 ;
let offset = offset + 4 in
(biseq [@ocaml.tailcall]) false depth offset seq ()
| Seq.Cons (`Bool false, seq) ->
let offset = sep first depth offset in
Bytes.blit_string "false" 0 buffer offset 5 ;
let offset = offset + 5 in
(biseq [@ocaml.tailcall]) false depth offset seq ()
| Seq.Cons (`As, seq) ->
let offset = sep first depth offset in
Bytes.set buffer offset '[' ;
let offset = offset + 1 in
(biseq [@ocaml.tailcall]) true (depth + 1) offset seq ()
| Seq.Cons (`Ae, seq) ->
Bytes.set buffer offset ']' ;
let offset = offset + 1 in
(biseq [@ocaml.tailcall]) false (depth - 1) offset seq ()
| Seq.Cons (`Os, seq) ->
let offset = sep first depth offset in
Bytes.set buffer offset '{' ;
let offset = offset + 1 in
(biseq [@ocaml.tailcall]) true (depth + 1) offset seq ()
| Seq.Cons (`Oe, seq) ->
Bytes.set buffer offset '}' ;
let offset = offset + 1 in
(biseq [@ocaml.tailcall]) false (depth - 1) offset seq ()
| Seq.Cons (`String "", seq) ->
let offset = sep first depth offset in
Bytes.blit_string "\"\"" 0 buffer offset 2 ;
let offset = offset + 2 in
(biseq [@ocaml.tailcall]) false depth offset seq ()
| Seq.Cons (`String s, seq) ->
let offset = sep first depth offset in
biseq_string_literal buffer offset s (fun offset ->
(biseq [@ocaml.tailcall]) false depth offset seq ())
| Seq.Cons (`Float f, seq) ->
let offset = sep first depth offset in
let f = string_of_float f in
biseq_escaped_string_content buffer offset f (fun offset ->
(biseq [@ocaml.tailcall]) false depth offset seq ())
| Seq.Cons (`Name n, seq) ->
let offset = sep first depth offset in
biseq_string_literal buffer offset n (fun offset ->
if offset = buffer_size then
Seq.Cons
( (buffer, 0, offset),
fun () ->
Bytes.set buffer 0 ':' ;
let offset = 1 in
(biseq [@ocaml.tailcall]) true depth offset seq () )
else (
Bytes.set buffer offset ':' ;
let offset = offset + 1 in
(biseq [@ocaml.tailcall]) true depth offset seq ()))
in
biseq false 0 0 lexeme_seq