Debian Codegraph @

vendor/re/src/color_map.ml

raw
1 (* In reality, this can really be represented as a bool array.
2
3 The representation is best thought of as a list of all chars along with a
4 flag:
5
6 (a, 0), (b, 1), (c, 0), (d, 0), ...
7
8 characters belonging to the same color are represented by sequnces of
9 characters with the flag set to 0.
10 *)
11
12 type t = Bytes.t
13
14 module Repr = struct
15 type t = string
16
17 let repr t color = t.[Cset.to_int color]
18 let length = String.length
19 end
20
21 module Table = struct
22 type t = string
23
24 let get_char t c = t.[Cset.to_int c]
25 let get t c = Cset.of_char (String.unsafe_get t (Char.code c))
26
27 let translate_colors (cm : t) cset =
28 Cset.fold_right cset ~init:Cset.empty ~f:(fun i j l ->
29 let start = get_char cm i in
30 let stop = get_char cm j in
31 Cset.union (Cset.cseq start stop) l)
32 ;;
33 end
34
35 let make () = Bytes.make 257 '\000'
36
37 let flatten cm =
38 let c = Bytes.create 256 in
39 let color_repr = Bytes.create 256 in
40 let v = ref 0 in
41 Bytes.set c 0 '\000';
42 Bytes.set color_repr 0 '\000';
43 for i = 1 to 255 do
44 if Bytes.get cm i <> '\000' then incr v;
45 Bytes.set c i (Char.chr !v);
46 Bytes.set color_repr !v (Char.chr i)
47 done;
48 Bytes.unsafe_to_string c, Bytes.sub_string color_repr 0 (!v + 1)
49 ;;
50
51 (* mark all the endpoints of the intervals of the char set with the 1 byte *)
52 let split t set =
53 Cset.iter set ~f:(fun i j ->
54 Bytes.set t (Cset.to_int i) '\001';
55 Bytes.set t (Cset.to_int j + 1) '\001')
56 ;;