• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ocaml / odoc / 2170

10 Jul 2024 02:39PM UTC coverage: 71.864% (+0.2%) from 71.668%
2170

push

github

jonludlam
fix large odoc-nav element in two columns layout

9849 of 13705 relevant lines covered (71.86%)

3514.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.42
/src/html/utils.ml
1
(* Shared utility functions *)
2

3
let optional_elt f ?a = function [] -> [] | l -> [ f ?a l ]
107✔
4

5
module Json = struct
6
  type json =
7
    [ `Null
8
    | `Bool of bool
9
    | `Float of float
10
    | `String of string
11
    | `Array of json list
12
    | `Object of (string * json) list ]
13

14
  let rec buffer_add_json b = function
15
    | `Null -> Buffer.add_string b "null"
15✔
16
    | `Bool bool -> Buffer.add_string b (if bool then "true" else "false")
×
17
    | `Float f -> Buffer.add_string b (Printf.sprintf "%.16g" f)
×
18
    | `String s -> buffer_add_json_string b s
626✔
19
    | `Array els -> (
89✔
20
        match els with
21
        | [] -> Buffer.add_string b "[]"
12✔
22
        | el :: els ->
77✔
23
            let add_sep_el b e =
24
              Buffer.add_char b ',';
62✔
25
              buffer_add_json b e
62✔
26
            in
27
            Buffer.add_char b '[';
28
            buffer_add_json b el;
77✔
29
            List.iter (add_sep_el b) els;
77✔
30
            Buffer.add_char b ']')
77✔
31
    | `Object mems -> (
339✔
32
        match mems with
33
        | [] -> Buffer.add_string b "{}"
×
34
        | mem :: mems ->
339✔
35
            let add_mem b (k, v) =
36
              buffer_add_json_string b k;
867✔
37
              Buffer.add_char b ':';
867✔
38
              buffer_add_json b v
867✔
39
            in
40
            let add_sep_mem b mem =
41
              Buffer.add_char b ',';
528✔
42
              add_mem b mem
528✔
43
            in
44
            Buffer.add_char b '{';
45
            add_mem b mem;
339✔
46
            List.iter (add_sep_mem b) mems;
339✔
47
            Buffer.add_char b '}')
339✔
48

49
  and buffer_add_json_string b s =
50
    let is_control = function
1,493✔
51
      | '\x00' .. '\x1F' | '\x7F' -> true
×
52
      | _ -> false
22,621✔
53
    in
54
    let len = String.length s in
55
    let max_idx = len - 1 in
1,493✔
56
    let flush b start i =
57
      if start < len then Buffer.add_substring b s start (i - start)
2,297✔
58
    in
59
    let rec loop start i =
60
      match i > max_idx with
24,953✔
61
      | true -> flush b start i
1,493✔
62
      | false -> (
23,460✔
63
          let next = i + 1 in
64
          match String.get s i with
65
          | '"' ->
822✔
66
              flush b start i;
67
              Buffer.add_string b "\\\"";
822✔
68
              loop next next
822✔
69
          | '\\' ->
×
70
              flush b start i;
71
              Buffer.add_string b "\\\\";
×
72
              loop next next
×
73
          | c when is_control c ->
22,638✔
74
              flush b start i;
17✔
75
              Buffer.add_string b (Printf.sprintf "\\u%04X" (Char.code c));
17✔
76
              loop next next
17✔
77
          | _c -> loop start next)
22,621✔
78
    in
79
    Buffer.add_char b '"';
80
    loop 0 0;
1,493✔
81
    Buffer.add_char b '"'
1,493✔
82

83
  let to_string json =
84
    let b = Buffer.create 1024 in
74✔
85
    buffer_add_json b json;
74✔
86
    Buffer.contents b
74✔
87
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc