-
Notifications
You must be signed in to change notification settings - Fork 5
/
interpolation.ml
54 lines (39 loc) · 1.1 KB
/
interpolation.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
open Core_kernel.Std
type 'a t = I of string * (int * int * 'a) list
let map ~f (I(str, xs)) =
I(str, List.map xs ~f:(fun (i,j,x) -> (i,j,f x)))
(* The ocaml standard library doesn't have a global-substitute
* function for regexes and I'd rather avoid requiring dependency
* just for this... *)
let of_string str =
let out = Queue.create () in
let isid x =
Char.is_alphanum x || x = '_'
in
let rec sA i j =
if j < String.length str && str.[j] <> '$' then
sA i (j+1)
else (
if j < String.length str then
sB j (j+1)
)
and sB i j =
if j < String.length str && isid str.[j] then
sB i (j+1)
else (
Queue.enqueue out (i, j, String.slice str (i+1) j);
if j < String.length str then
sA j j
)
in
sA 0 0;
I(str, Queue.to_list out)
let to_string (I(str, xs)) =
let out = Buffer.create (2*String.length str) in
let j = List.fold_left xs ~init:0 ~f:(fun i (j,k,s) ->
Buffer.add_substring out str i (j-i);
Buffer.add_string out s;
k
) in
Buffer.add_substring out str j (String.length str - j);
Buffer.contents out