Debian Codegraph @

vendor/re/src/fmt.ml

raw
1 (** Very small tooling for format printers. *)
2
3 include Format
4
5 type 'a t = Format.formatter -> 'a -> unit
6
7 let list = pp_print_list
8 let array ?pp_sep f fmt arr = list ?pp_sep f fmt (Array.to_list arr)
9 let str = pp_print_string
10 let sexp fmt s pp x = fprintf fmt "@[<3>(%s@ %a)@]" s pp x
11 let bytes fmt t = Format.fprintf fmt "%S" (Bytes.to_string t)
12
13 let pair pp1 pp2 fmt (v1, v2) =
14 pp1 fmt v1;
15 pp_print_space fmt ();
16 pp2 fmt v2
17 ;;
18
19 let triple pp1 pp2 pp3 fmt (v1, v2, v3) =
20 pp1 fmt v1;
21 pp_print_space fmt ();
22 pp2 fmt v2;
23 pp_print_space fmt ();
24 pp3 fmt v3
25 ;;
26
27 let opt f fmt x =
28 match x with
29 | None -> pp_print_string fmt "<None>"
30 | Some x -> fprintf fmt "%a" f x
31 ;;
32
33 let int = pp_print_int
34
35 let optint fmt = function
36 | None -> ()
37 | Some i -> fprintf fmt "@ %d" i
38 ;;
39
40 let quote fmt s = Format.fprintf fmt "\"%s\"" s
41
42 let pp_olist pp_elem fmt =
43 Format.fprintf
44 fmt
45 "@[<3>[@ %a@ ]@]"
46 (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ";@ ") pp_elem)
47 ;;
48
49 let char fmt c = Format.fprintf fmt "%c" c
50 let bool = Format.pp_print_bool
51 let pp_str_list = pp_olist quote
52 let lit s fmt () = pp_print_string fmt s
53
54 let to_to_string pp x =
55 let b = Buffer.create 16 in
56 let fmt = Format.formatter_of_buffer b in
57 pp fmt x;
58 Buffer.contents b
59 ;;
60
61 let quoted_string fmt s = Format.fprintf fmt "%S" s