Source file utils.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
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
open Ppxlib
open Ast_builder.Default
let verbose = match Sys.getenv_opt "PPX_ENCODING_DEBUG" with
| None | Some "0" | Some "false" | Some "no" -> 0
| Some s ->
match s with
| "true" -> 1
| s -> match int_of_string_opt s with
| Some i -> i
| None -> 0
let fake = match Sys.getenv_opt "PPX_ENCODING_FAKE" with
| Some "1" | Some "true" | Some "yes" -> true
| _ -> false
let remove_prefix_var = match Sys.getenv_opt "PPX_ENCODING_RM_PREFIX" with
| Some "false" | Some "0" -> false
| _ -> true
let debug ?(v=1) ?(force=false) fmt =
if force || verbose >= v then Format.ksprintf (fun s -> Format.eprintf "%s@." s) fmt
else Printf.ifprintf () fmt
let json_encoding_dot = ref "Json_encoding."
let json_encoding_tmp = ref !json_encoding_dot
let wrap = function
| None -> json_encoding_tmp := !json_encoding_dot
| Some "" -> json_encoding_tmp := !json_encoding_dot; json_encoding_dot := ""
| Some s -> json_encoding_tmp := !json_encoding_dot; json_encoding_dot := String.capitalize_ascii s ^ "."
let unwrap () =
json_encoding_dot := !json_encoding_tmp
let () = wrap (Sys.getenv_opt "PPX_ENCODING_MODULE")
let raise_error ~loc s = Location.raise_errorf ~loc s
let enc_names : (string, string) Hashtbl.t = Hashtbl.create 256
let enc_name ?(search=true) type_name =
let aux name =
match List.rev @@ String.split_on_char '.' name with
| [] -> assert false
| "t" :: q -> String.concat "." @@ List.rev @@ "enc" :: q
| _ -> name ^ "_enc" in
if not search then aux type_name
else match Hashtbl.find_opt enc_names type_name with
| None -> aux type_name
| Some name -> name
let add_enc_name type_name enc_name = Hashtbl.add enc_names type_name enc_name
let enc_mod s = !json_encoding_dot ^ s
let enc_var ~loc s = evar ~loc (enc_mod s)
let enc_apply ~loc s l = eapply ~loc (enc_var ~loc s) l
let enc_apply0 ~loc s l = pexp_apply ~loc (enc_var ~loc s) l
let pexp_fun p e = pexp_fun ~loc:e.pexp_loc Nolabel None p e
let llid ~loc s = {txt=Longident.parse s; loc}
let esome e =
let loc = e.pexp_loc in
pexp_construct ~loc (llid ~loc "Some") (Some e)
let eoption ~loc = function
| None -> [%expr None]
| Some e -> esome e
let rec add_params_fun expr = function
| [] -> expr
| {ptyp_desc = Ptyp_var x; ptyp_loc=loc; _} :: t ->
pexp_fun
(ppat_constraint ~loc (pvar ~loc ("_" ^ enc_name x))
(ptyp_constr ~loc (llid ~loc (enc_mod "encoding")) [ptyp_var ~loc x]))
(add_params_fun expr t)
| _ :: t -> add_params_fun expr t
let rec add_params_fun_sig typ = function
| [] -> typ
| {ptyp_desc = Ptyp_var x; ptyp_loc=loc; _} :: t ->
ptyp_arrow ~loc Nolabel
(ptyp_constr ~loc (llid ~loc (enc_mod "encoding")) [ptyp_var ~loc x])
(add_params_fun_sig typ t)
| _ :: t -> add_params_fun_sig typ t
let param_names params =
List.rev @@ List.fold_left (fun acc (p, _) -> match p.ptyp_desc with
| Ptyp_var x -> x :: acc
| _ -> acc) [] params
let string_literal = function
| Ppxlib.Pconst_string (s, _, _) -> Some s
| _ -> None
let get_expr_attr = function
| PStr [{pstr_desc = Pstr_eval (e, _); _}] -> Some e
| _ -> None
let get_string_attr = function
| PStr [{pstr_desc = Pstr_eval ({pexp_desc = Pexp_constant cst; _}, _); _}] ->
string_literal cst
| _ -> None
let rm_prefix_of_expr e = match e.pexp_desc with
| Pexp_construct ({txt=Lident "true"; _}, _) -> Some (`bool true)
| Pexp_construct ({txt=Lident "false"; _}, _) -> Some (`bool false)
| Pexp_constant (Pconst_integer (s, None)) -> Some (`length (int_of_string s))
| Pexp_constant cst ->
begin match string_literal cst with
| Some s -> Some (`prefix s)
| None -> None
end
| _ -> None
let get_rm_prefix_attr pl =
match get_expr_attr pl with
| None -> None
| Some e -> rm_prefix_of_expr e
let schema_enc e =
let loc = e.pexp_loc in
let aux sch x =
enc_apply0 ~loc "conv" [
Labelled "schema", sch;
Nolabel, [%expr Fun.id];
Nolabel, [%expr Fun.id];
Nolabel, x ] in
match e.pexp_desc with
| Pexp_fun (Nolabel, None, ({ppat_desc=Ppat_var {txt; _}; _} as p), schema) ->
(fun e -> [%expr let [%p p] = [%e e] in [%e aux schema (evar ~loc txt)]])
| _ -> aux e
let read_only loc =
schema_enc [%expr
fun enc -> Json_schema.(create {
(root [%e enc_apply ~loc "schema" [ [%expr enc] ]]) with access = Some `read_only })
]
let write_only loc =
schema_enc [%expr
fun enc -> Json_schema.(create {
(root [%e enc_apply ~loc "schema" [ [%expr enc] ]]) with access = Some `write_only })
]
let to_snake ?(sep='_') s =
let n = String.length s in
let b = Bytes.create (2*n) in
let rec aux i j =
if i = n then j
else
let c = String.get s i in
let code = Char.code c in
if i=0 && code >= 65 && code <= 90 then (
Bytes.set b j (Char.chr (code + 32));
aux (i+1) (j+1))
else if code >= 65 && code <= 90 then (
Bytes.set b j sep;
Bytes.set b (j+1) (Char.chr (code + 32));
aux (i+1) (j+2))
else if c = '-' || c = '_' then (
Bytes.set b j sep;
aux (i+1) (j+1))
else (
Bytes.set b j c;
aux (i+1) (j+1)) in
let m = aux 0 0 in
Bytes.(to_string @@ sub b 0 m)
let to_camel ?(pascal=false) s =
let n = String.length s in
let b = Bytes.create n in
let rec aux i j =
if i = n then j
else
let c = String.get s i in
if (c = '_' || c = '-') && i <> (n-1) then (
let code = Char.code @@ String.get s (i+1) in
let c =
if code >= 97 && code <= 122 then Char.chr (code - 32)
else Char.chr code in
Bytes.set b j c;
aux (i+2) (j+1))
else (
Bytes.set b j c;
aux (i+1) (j+1)) in
let m = aux 0 0 in
let s = Bytes.(to_string @@ sub b 0 m) in
if pascal then String.capitalize_ascii s else String.uncapitalize_ascii s
type field_attributes = {
fa_field: string * bool * expression option;
fa_key: string;
fa_title: expression option;
fa_description: expression option;
fa_assoc: bool option;
fa_enum: bool option;
fa_exclude: expression option;
fa_obj: bool option;
fa_enc: expression option;
fa_obj1: string option;
fa_merge: bool;
fa_construct_default: bool;
fa_set: expression option;
fa_map: expression option option;
fa_schema: (expression -> expression) option;
}
let transform_case ?case ?(typ=`cons) s = match case with
| None -> begin match typ with
| `cons -> if String.for_all (fun c -> let code = Char.code c in code >= 65 && code <= 90) s then s else String.uncapitalize_ascii s
| `variant -> s end
| Some `camel -> to_camel s
| Some `snake -> to_snake s
| Some `pascal -> to_camel ~pascal:true s
| Some `kebab -> to_snake ~sep:'-' s
| Some `upper -> String.uppercase_ascii s
| Some `lower -> String.lowercase_ascii s
| Some `cap -> String.capitalize_ascii s
let field_attrs ~key ?(opt=false) ?(option="opt") ?case ?set ?map ?schema l =
let fa_field = match opt, option with
| false, _ -> ("req", false, None)
| _, "opt" -> ("opt", true, None)
| _, "req" -> ("req", false, None)
| _ ->
let loc = !Ast_helper.default_loc in
("dft", false, Some [%expr None]) in
let fa_key = transform_case ?case key in
List.fold_left (fun fa a -> match a.attr_name.txt with
| "req" | "enc.req" -> {fa with fa_field = ("req", false, None)}
| "opt" | "enc.opt" -> {fa with fa_field = ("opt", true, None)}
| "dft" | "enc.dft" -> {fa with fa_field = ("dft", false, get_expr_attr a.attr_payload)}
| "ddft" | "enc.ddft" -> {fa with fa_field = ("dft", false, get_expr_attr a.attr_payload);
fa_construct_default = true}
| "key" | "enc.key" -> begin match get_string_attr a.attr_payload with
| None -> failwith "key expression must be a string constant"
| Some fa_key -> {fa with fa_key} end
| "title" | "enc.title" -> {fa with fa_title = get_expr_attr a.attr_payload}
| "description" | "enc.description" -> {fa with fa_description = get_expr_attr a.attr_payload}
| "assoc" | "enc.assoc" -> {fa with fa_assoc = Some true}
| "enum" | "enc.enum" -> {fa with fa_enum = Some true}
| "union" | "enc.union" -> {fa with fa_enum = Some false}
| "exclude" | "enc.exclude" -> {fa with fa_exclude = get_expr_attr a.attr_payload}
| "object" | "enc.object" -> {fa with fa_obj = Some true}
| "encoding" | "enc.encoding" -> {fa with fa_enc = get_expr_attr a.attr_payload}
| "obj1" | "enc.obj1" | "wrap" | "enc.wrap" -> {fa with fa_obj1 = get_string_attr a.attr_payload}
| "merge" | "enc.merge" -> {fa with fa_merge = true}
| "camel" | "enc.camel" -> {fa with fa_key = to_camel fa.fa_key}
| "snake" | "enc.snake" -> {fa with fa_key = to_snake fa.fa_key}
| "pascal" | "enc.pascal" -> {fa with fa_key = to_camel ~pascal:true fa.fa_key}
| "kebab" | "enc.kebab" -> {fa with fa_key = to_snake ~sep:'-' fa.fa_key}
| "set" | "enc.set" -> {fa with fa_set = get_expr_attr a.attr_payload}
| "map" | "enc.map" -> {fa with fa_map = Some (get_expr_attr a.attr_payload)}
| "upper" | "enc.upper" -> {fa with fa_key = String.uppercase_ascii fa.fa_key}
| "lower" | "enc.lower" -> {fa with fa_key = String.lowercase_ascii fa.fa_key}
| "cap" | "enc.cap" -> {fa with fa_key = String.capitalize_ascii fa.fa_key}
| "schema" | "enc.schema" -> {fa with fa_schema = Option.map schema_enc @@ get_expr_attr a.attr_payload}
| "readonly" | "enc.readonly" -> {fa with fa_schema = Some (read_only a.attr_loc)}
| "writeonly" | "enc.writeonly" -> {fa with fa_schema = Some (read_only a.attr_loc)}
| _ -> fa
) { fa_field; fa_key; fa_title=None; fa_description=None; fa_assoc=None;
fa_enum=None; fa_exclude=None; fa_obj=None; fa_enc=None; fa_obj1=None;
fa_merge=false; fa_construct_default=false; fa_set=set; fa_map=map; fa_schema=schema } l
let pp_expression = Pprintast.expression
type cs_attributes = {
cs_kind : string option;
cs_kind_label : string option;
cs_assoc : bool option;
cs_enum : bool option;
cs_transform : (string -> string);
cs_obj : bool option;
cs_enc : expression option;
cs_title : expression option;
cs_description : expression option;
cs_ignore : bool;
cs_rm_prefix : [`bool of bool | `prefix of string | `length of int];
cs_obj1 : string option;
cs_empty : bool option;
cs_case : expression option;
cs_schema : (expression -> expression) option;
}
let constructor_attrs ?case ?typ ?kind l =
let cs_transform s = transform_case ?case ?typ s in
let cs_kind, cs_kind_label = match kind with
| Some (Some k) -> Some "", Some k
| Some None -> Some "", None
| _ -> None, None in
List.fold_left (fun cs a -> match a.attr_name.txt with
| "kind" | "enc.kind" ->
let cs_kind = match get_string_attr a.attr_payload with
| None -> Some "" | k -> k in
{cs with cs_kind}
| "kind_label" | "enc.kind_label" -> {cs with cs_kind_label = get_string_attr a.attr_payload}
| "assoc" | "enc.assoc" -> {cs with cs_assoc = Some true}
| "enum" | "enc.enum" -> {cs with cs_enum = Some true}
| "union" | "enc.union" -> {cs with cs_enum = Some false}
| "key" | "enc.key" -> begin match get_string_attr a.attr_payload with
| None -> failwith "key expression must be a string constant"
| Some s -> {cs with cs_transform = fun _ -> s}
end
| "object" | "enc.object" -> {cs with cs_obj = Some true}
| "encoding" | "enc.encoding" -> {cs with cs_enc = get_expr_attr a.attr_payload}
| "title" | "enc.title" -> {cs with cs_title = get_expr_attr a.attr_payload}
| "description" | "enc.desccription" -> {cs with cs_description = get_expr_attr a.attr_payload}
| "ignore" | "enc.ignore" -> {cs with cs_ignore = true}
| "remove_prefix" | "enc.remove_prefix" ->
let cs_rm_prefix = match get_rm_prefix_attr a.attr_payload with
| None -> `bool true
| Some x -> x in
{cs with cs_rm_prefix}
| "obj1" | "enc.obj1" | "wrap" | "enc.wrap" ->
{cs with cs_obj1 = get_string_attr a.attr_payload}
| "empty" | "enc.empty" -> {cs with cs_empty = Some true}
| "camel" | "enc.camel" -> {cs with cs_transform = to_camel}
| "snake" | "enc.snake" -> {cs with cs_transform = to_snake}
| "pascal" | "enc.pascal" -> {cs with cs_transform = to_camel ~pascal:true}
| "kebab" | "enc.kebab" -> {cs with cs_transform = to_snake ~sep:'-'}
| "upper" | "enc.upper" -> {cs with cs_transform = String.uppercase_ascii}
| "lower" | "enc.lower" -> {cs with cs_transform = String.lowercase_ascii}
| "cap" | "enc.cap" -> {cs with cs_transform = String.capitalize_ascii}
| "case" | "enc.case" -> {cs with cs_case = get_expr_attr a.attr_payload}
| "schema" | "enc.schema" -> {cs with cs_schema = Option.map schema_enc @@ get_expr_attr a.attr_payload}
| "readonly" | "enc.readonly" -> {cs with cs_schema = Some (read_only a.attr_loc)}
| "writeonly" | "enc.writeonly" -> {cs with cs_schema = Some (read_only a.attr_loc)}
| _ -> cs
) { cs_kind; cs_assoc=None; cs_enum=None; cs_transform; cs_obj=None;
cs_enc=None; cs_title=None; cs_description=None; cs_ignore=false;
cs_rm_prefix=`bool false; cs_obj1=None; cs_kind_label; cs_empty=None;
cs_case=None; cs_schema=None } l
type core_attributes = {
co_assoc : bool option;
co_enum : bool option;
co_exclude : expression option;
co_obj : bool option;
co_enc : expression option;
co_obj1 : string option;
co_merge : bool;
co_rm_prefix : [`bool of bool | `prefix of string | `length of int] option;
co_set: expression option;
co_map: expression option option;
co_opt: expression option option;
co_schema: (expression -> expression) option;
}
let core_attrs ?assoc ?enum ?obj ?enc ?obj1 ?set ?map ?schema l =
List.fold_left (fun co a -> match a.attr_name.txt with
| "assoc" | "enc.assoc" -> {co with co_assoc = Some true}
| "enum" | "enc.enum" -> {co with co_enum = Some true}
| "union" | "enc.union" -> {co with co_enum = Some false}
| "exclude" | "enc.exclude" -> {co with co_exclude = get_expr_attr a.attr_payload}
| "object" | "enc.object" -> {co with co_obj = Some true}
| "encoding" | "enc.encoding" -> {co with co_enc = get_expr_attr a.attr_payload}
| "obj1" | "enc.obj1" | "wrap" | "enc.wrap" -> {co with co_obj1 = get_string_attr a.attr_payload}
| "merge" | "enc.merge" -> {co with co_merge = true}
| "remove_prefix" | "enc.remove_prefix" -> {co with co_rm_prefix = get_rm_prefix_attr a.attr_payload}
| "set" | "enc.set" -> { co with co_set = get_expr_attr a.attr_payload }
| "map" | "enc.map" -> { co with co_map = Some (get_expr_attr a.attr_payload) }
| "opt" | "enc.opt" -> { co with co_opt = Some None }
| "dft" | "enc.dft" -> { co with co_opt = Some (get_expr_attr a.attr_payload) }
| "schema" | "enc.schema" ->
{ co with co_schema = Option.map schema_enc @@ get_expr_attr a.attr_payload }
| "readonly" | "enc.readonly" -> {co with co_schema = Some (read_only a.attr_loc)}
| "writeonly" | "enc.writeonly" -> {co with co_schema = Some (read_only a.attr_loc)}
| _ -> co
) {co_assoc=assoc; co_enum=enum; co_exclude=None; co_obj=obj; co_enc=enc;
co_obj1=obj1; co_merge=false; co_rm_prefix=None; co_set=set; co_map=map;
co_opt=None; co_schema=schema} l
let new_var = let i = ref (-1) in fun () -> incr i; "v" ^ string_of_int !i
let str_of_structure e = Pprintast.string_of_structure e
let str_of_signature e =
Pprintast.signature Format.str_formatter e;
Format.flush_str_formatter ()
let str_of_core e =
Pprintast.core_type Format.str_formatter e;
Format.flush_str_formatter ()
let rec flat_tuple_length = function
| [] -> 0
| `merge _ :: tl -> 1 + flat_tuple_length tl
| `obj l :: tl -> List.length l + flat_tuple_length tl
let simple_obj_tuple ?(v="x") ?(i=0) ~loc var tuple l =
let n = List.length l in
let q = n / 10 in
let r = n mod 10 in
let rec aux ~i q =
if q = 1 && r = 0 then tuple ~loc @@ List.init 10 (fun j -> var ~loc (v ^ string_of_int (i+j)))
else if q = 0 then tuple ~loc @@ List.init r (fun j -> var ~loc (v ^ string_of_int (i+j)))
else tuple ~loc [
tuple ~loc @@ List.init 10 (fun j -> var ~loc (v ^ string_of_int (i+j)));
aux ~i:(i+10) (q-1) ] in
aux ~i q
let rec encaps_tuple ?(v="x") ?(i=0) ~loc var tuple = function
| [] -> assert false
| [ `merge _ ] -> var ~loc (v ^ string_of_int i)
| [ `obj l ] -> simple_obj_tuple ~i ~v ~loc var tuple l
| `merge _ :: tl -> tuple ~loc [var ~loc (v ^ string_of_int i); encaps_tuple ~i:(i+1) ~loc var tuple tl]
| `obj l :: tl ->
tuple ~loc [
simple_obj_tuple ~i ~v ~loc var tuple l;
encaps_tuple ~i:(i+List.length l) ~loc var tuple tl]
let simple_obj ~loc ~kind l =
let a = Array.of_list l in
let n = Array.length a in
let q = n / 10 in
let r = n mod 10 in
let rec aux q a =
if q = 1 && r = 0 then eapply ~loc (evar ~loc (enc_mod (kind ^ string_of_int 10))) (Array.to_list a)
else if q = 0 then eapply ~loc (evar ~loc (enc_mod (kind ^ string_of_int r))) (Array.to_list a)
else enc_apply ~loc ("merge_" ^ kind ^ "s") [
eapply ~loc (evar ~loc (enc_mod (kind ^ "10"))) (Array.to_list @@ Array.sub a 0 10);
aux (q-1) (Array.sub a 10 ((q-1)*10 +r))
] in
aux q a
let rec encaps_merge ~loc ?(kind="obj") = function
| [] -> assert false
| [ `merge h ] -> h
| [ `obj l ] -> simple_obj ~loc ~kind l
| `merge h :: tl ->
enc_apply ~loc ("merge_" ^ kind ^ "s") [ h; encaps_merge ~loc ~kind tl ]
| `obj l :: tl ->
enc_apply ~loc ("merge_" ^ kind ^ "s") [ simple_obj ~loc ~kind l; encaps_merge ~loc ~kind tl ]
let obj_expr ~loc ?(kind="obj") ?(v="x") l =
let rec aux acc1 acc2 l = match l, acc2 with
| [], None -> List.rev acc1
| [], Some o -> List.rev (`obj (List.rev o) :: acc1)
| (h, merge) :: tl , None ->
if merge then aux ((`merge h) :: acc1) None tl
else aux acc1 (Some [h]) tl
| (h, merge) :: tl , Some o ->
if merge then aux (`merge h :: (`obj (List.rev o)) :: acc1) None tl
else aux acc1 (Some (h :: o)) tl in
let lgrouped = aux [] None l in
match lgrouped with
| [] -> enc_var ~loc "empty"
| [ `obj o ] when List.length o <= 10 ->
simple_obj ~loc ~kind o
| _ ->
let n = flat_tuple_length lgrouped in
enc_apply ~loc "conv" [
pexp_fun
(ppat_tuple ~loc (List.init n (fun i -> pvar ~loc (v ^ string_of_int i))))
(encaps_tuple ~v ~loc evar pexp_tuple lgrouped);
pexp_fun
(encaps_tuple ~loc pvar ppat_tuple lgrouped)
(pexp_tuple ~loc (List.init n (fun i -> evar ~loc (v ^ string_of_int i))));
encaps_merge ~loc ~kind lgrouped ]
let remove_prefix s n = String.sub s n (String.length s - n)
let same_prefix l =
let common_prefix s1 s2 =
let n1 = String.length s1 in
let n2 = String.length s2 in
let rec aux i =
if i < n1 && i < n2 && s1.[i] = s2.[i] then aux (i+1)
else i, String.sub s1 0 i in
aux 0 in
let rec aux n pr = function
| [] -> n, pr
| h :: t ->
let n, pr = common_prefix h pr in
aux n pr t in
let check n l = List.for_all (fun s -> String.length s > n) l in
match l with
| [] | [ _ ] -> 0
| h :: t ->
let n = fst (aux (String.length h) h t) in
if check n l then n else 0
let remove_prefix_options l = function
| Some (`bool false) -> 0
| Some (`length n) -> n
| Some (`prefix s) -> String.length s
| _ -> if not remove_prefix_var then 0 else same_prefix l
module type S = sig
type 'a encoding
type 'a field
type 'a case
val unit : unit encoding
val empty : unit encoding
val int : int encoding
val int32 : int32 encoding
val int53 : int64 encoding
val bool : bool encoding
val string : string encoding
val string_enum : (string * 'a) list -> 'a encoding
val constant : string -> unit encoding
val bytes : bytes encoding
val float : float encoding
val option : 'a encoding -> 'a option encoding
val req : ?title:string -> ?description:string -> string -> 't encoding -> 't field
val opt : ?title:string -> ?description:string -> string -> 't encoding -> 't option field
val dft : ?title:string -> ?description:string -> string -> 't encoding -> 't -> 't field
val obj1 : 'f1 field -> 'f1 encoding
val obj2 : 'f1 field -> 'f2 field -> ('f1 * 'f2) encoding
val obj3 : 'f1 field -> 'f2 field -> 'f3 field -> ('f1 * 'f2 * 'f3) encoding
val obj4 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> ('f1 * 'f2 * 'f3 * 'f4) encoding
val obj5 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5) encoding
val obj6 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6) encoding
val obj7 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7) encoding
val obj8 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> 'f8 field -> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8) encoding
val obj9 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> 'f8 field -> 'f9 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9) encoding
val obj10 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> 'f8 field -> 'f9 field -> 'f10 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9 * 'f10) encoding
val merge_objs : 'a encoding -> 'b encoding -> ('a * 'b) encoding
val tup1 : 'f1 field -> 'f1 encoding
val tup2 : 'f1 field -> 'f2 field -> ('f1 * 'f2) encoding
val tup3 : 'f1 field -> 'f2 field -> 'f3 field -> ('f1 * 'f2 * 'f3) encoding
val tup4 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> ('f1 * 'f2 * 'f3 * 'f4) encoding
val tup5 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5) encoding
val tup6 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6) encoding
val tup7 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7) encoding
val tup8 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> 'f8 field -> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8) encoding
val tup9 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> 'f8 field -> 'f9 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9) encoding
val tup10 : 'f1 field -> 'f2 field -> 'f3 field -> 'f4 field -> 'f5 field -> 'f6 field
-> 'f7 field -> 'f8 field -> 'f9 field -> 'f10 field
-> ('f1 * 'f2 * 'f3 * 'f4 * 'f5 * 'f6 * 'f7 * 'f8 * 'f9 * 'f10) encoding
val merge_tups : 'a encoding -> 'b encoding -> ('a * 'b) encoding
val array : 'a encoding -> 'a array encoding
val list : 'a encoding -> 'a list encoding
val assoc : 'a encoding -> (string * 'a) list encoding
val case : ?title:string -> ?description:string -> ('t encoding -> ('a -> 't option) -> ('t -> 'a)) list -> 'a case
val union : 'a case list -> 'a encoding
val conv : ('a -> 'b) -> ('b -> 'a) -> 'b encoding -> 'a encoding
val mu : string -> ?title:string -> ?description:string -> ('a encoding -> 'a encoding) -> 'a encoding
val any_ezjson_value : 'a encoding
val def : string -> ?title:string -> ?description:string -> 'a encoding -> 'a encoding
end