-
Notifications
You must be signed in to change notification settings - Fork 0
/
rp.ml
30 lines (26 loc) · 774 Bytes
/
rp.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
include Rope.Make_array (struct
include Uchar
let uchar_to_utf_8 =
let buf = Buffer.create 16 in
fun uchar ->
Uutf.Buffer.add_utf_8 buf uchar;
let res = Buffer.contents buf in
Buffer.clear buf;
res
let print =
Fmt.if_utf_8
Fmt.(using uchar_to_utf_8 string)
Fmt.(using Uchar.to_int (any "U+04X"))
end)
let to_utf_8_string rope =
let len = length rope in
let buf = Buffer.create len in
iter_range (Uutf.Buffer.add_utf_8 buf) rope 0 len;
Buffer.contents buf
let of_utf_8_string str =
Uutf.String.fold_utf_8
(fun (rope, upos) _bpos -> function
| `Malformed _ -> (insert_char rope upos Uutf.u_rep, succ upos)
| `Uchar uchr -> (insert_char rope upos uchr, succ upos))
(empty, 0) str
|> fst