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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
(** *)
let utf8_nb_bytes_of_char c =
let n = Char.code c in
if n < 0b10000000 then
1
else if n < 0b11100000 then
2
else if n < 0b11110000 then
3
else
4
let utf8_index_of_char s c =
let cpt = ref 0 in
let current = ref 0 in
let len = String.length s in
while !current < c && !cpt < len do
cpt := !cpt + utf8_nb_bytes_of_char (String.get s !cpt) ;
incr current;
done;
if !current = c then
!cpt
else
raise Not_found
let utf8_char_of_index s i =
let len = String.length s in
if i >= len || i < 0 then
invalid_arg "utf8_char_from_index"
else
begin
let char_count = ref (-1) in
let pos = ref 0 in
while !pos <= i && !pos < len do
incr char_count;
pos := !pos + utf8_nb_bytes_of_char s.[!pos]
done;
!char_count
end
let utf8_length s =
let len = String.length s in
let rec iter acc n =
if n >= len then
acc
else
iter (acc+1) (n + (utf8_nb_bytes_of_char s.[n]))
in
iter 0 0
let utf8_substr s pos l =
let inv () = invalid_arg "Utf8.utf8_substr" in
let len = String.length s in
if pos < 0 || l >= len then inv ();
let start = ref (-1) in
let stop = ref (-1) in
let rec iter i n =
if n = pos then start := i ;
if n = pos + l then
stop := i
else
(
if n >= len then inv () ;
let size = utf8_nb_bytes_of_char s.[i] in
iter (i+size) (n+1)
)
in
iter 0 0 ;
String.sub s !start (!stop - !start)
;;
let utf8_is_prefix s1 s2 =
let len1 = utf8_length s1 in
let len2 = utf8_length s2 in
(len1 >= len2) && (String.sub s1 0 len2) = s2
;;
let utf8_is_suffix s1 s2 =
let len1 = utf8_length s1 in
let len2 = utf8_length s2 in
if len1 = len2 then
s1 = s2
else
(len1 >= len2) && (String.sub s1 (len1 - len2) len2) = s2
;;
let utf8_substr_pos s1 s2 =
let ulen1 = utf8_length s1 in
let ulen2 = utf8_length s2 in
let len2 = String.length s2 in
let rec iter i n =
if ulen1 - n >= ulen2 then
(
if String.sub s1 i len2 = s2 then
Some i
else
(
let size = utf8_nb_bytes_of_char s1.[i] in
iter (i+size) (n+1)
)
)
else
None
in
iter 0 0
;;
let utf8_contains s1 s2 = utf8_substr_pos s1 s2 <> None;;
let utf8_strbefore s1 = function
"" -> s1
| s2 ->
match utf8_substr_pos s1 s2 with
None
| Some 0 -> ""
| Some n -> String.sub s1 0 n
;;
let utf8_strafter s1 = function
"" -> s1
| s2 ->
match utf8_substr_pos s1 s2 with
None -> ""
| Some 0 -> s1
| Some n ->
let start = n + String.length s2 in
String.sub s1 start (String.length s1 - start)
;;
(** conversions algorithm from [http://en.wikipedia.org/wiki/UTF-8]. *)
let utf8_char_of_code n =
let n_int = Int32.to_int n in
if Int32.compare n (Int32.of_int 128) < 0 then
String.make 1 (Char.chr n_int)
else
let z_mask = Int32.of_int 0b00111111 in
let z_part = Int32.logand n z_mask in
let z = Int32.logor (Int32.of_int 0b10000000) z_part in
if Int32.compare n (Int32.of_int 0x0007FF) <= 0 then
(
let y_mask = Int32.of_int 0b0000011111000000 in
let y_part = Int32.shift_right_logical (Int32.logand n y_mask) 6 in
let y = Int32.logor (Int32.of_int 0b11000000) y_part in
let s = Bytes.of_string "12" in
Bytes.set s 0 (Char.chr (Int32.to_int y)) ;
Bytes.set s 1 (Char.chr (Int32.to_int z)) ;
Bytes.to_string s
)
else
let y_mask = Int32.of_int 0b111111000000 in
let y_part = Int32.shift_right_logical (Int32.logand n y_mask) 6 in
let y = Int32.logor (Int32.of_int 0b10000000) y_part in
if Int32.compare n (Int32.of_int 0x00FFFF) <= 0 then
(
let x_mask = Int32.of_int (0b1111 lsl 12) in
let x_part = Int32.shift_right_logical (Int32.logand n x_mask) 12 in
let x = Int32.logor (Int32.of_int 0b11100000) x_part in
let s = Bytes.of_string "123" in
Bytes.set s 0 (Char.chr (Int32.to_int x)) ;
Bytes.set s 1 (Char.chr (Int32.to_int y)) ;
Bytes.set s 2 (Char.chr (Int32.to_int z)) ;
Bytes.to_string s
)
else
if Int32.compare n (Int32.of_int 0x10FFFF) <= 0 then
(
let x_mask = Int32.of_int (0b111111 lsl 12) in
let x_part = Int32.shift_right_logical (Int32.logand n x_mask) 12 in
let x = Int32.logor (Int32.of_int 0b10000000) x_part in
let w_mask = Int32.of_int (0b111 lsl 18) in
let w_part = Int32.shift_right_logical (Int32.logand n w_mask) 18 in
let w = Int32.logor (Int32.of_int 0b11110000) w_part in
let s = Bytes.of_string "1234" in
Bytes.set s 0 (Char.chr (Int32.to_int w)) ;
Bytes.set s 1 (Char.chr (Int32.to_int x)) ;
Bytes.set s 2 (Char.chr (Int32.to_int y)) ;
Bytes.set s 3 (Char.chr (Int32.to_int z)) ;
Bytes.to_string s
)
else
failwith ("UTF-8 code out of range: "^ (Int32.to_string n))
;;
let utf8_get_bol =
let rec iter acc len s line char i =
if i >= len then
List.rev acc
else
(
let size = utf8_nb_bytes_of_char s.[i] in
match s.[i] with
'\n' -> iter ((line+1,char)::acc) len s (line+1) (char+1) (i+size)
| _ -> iter acc len s line (char+1) (i+size)
)
in
fun s -> iter [] (String.length s) s 1 0 0
;;
let utf8_count_nl =
let rec iter len s n i =
if i >= len then n
else
(let size = utf8_nb_bytes_of_char s.[i] in
match s.[i] with
'\n' -> iter len s (n+1) (i+size)
| _ -> iter len s n (i+size)
)
in
fun s -> iter (String.length s) s 0 0
;;
let utf8_char_escape_string dest str =
let len = String.length str in
Buffer.add_string dest "\\";
Buffer.add_char dest (if len <= 2 then 'u' else 'U');
String.iter
(fun c -> Printf.bprintf dest "%02X" (Char.code c))
str
let utf8_escape =
let f b i = function
| `Malformed str -> Buffer.add_string b str; b
| `Uchar cp ->
match Uchar.to_int cp with
| 0x08 -> Buffer.add_string b "\\b"; b
| 0x09 -> Buffer.add_string b "\\t"; b
| 0x0A -> Buffer.add_string b "\\n"; b
| 0x0C -> Buffer.add_string b "\\f"; b
| 0x0D -> Buffer.add_string b "\\r"; b
| 0x22 -> Buffer.add_string b "\\\""; b
| 0x5C -> Buffer.add_string b "\\\\"; b
| n when n < 128 -> Uutf.Buffer.add_utf_8 b cp; b
| n ->
let str = Printf.sprintf "%x" n in
if String.length str > 4 then
Printf.bprintf b "\\U%08X" n
else
Printf.bprintf b "\\u%04X" n;
b
in
fun s ->
let b = Buffer.create (String.length s) in
ignore(Uutf.String.fold_utf_8 f b s) ;
Buffer.contents b
;;
let int32_of_chars s start len =
let s2 = "0x"^(String.sub s start len) in
try Int32.of_string s2
with _ -> failwith ("Invalid UTF8 code: "^s)
let utf8_unescape =
let escaped_chars = List.fold_left
(fun map (c1, c2) -> Types.CMap.add c1 c2 map)
Types.CMap.empty
[ 'b', '\b' ;
'f', Char.chr 0x0c ;
'n', '\n' ;
'r', '\r' ;
't', '\t' ;
'"', '"' ;
'\'', '\'' ;
'\\', '\\' ;
]
in
let rec iter b len s i =
if i >= len then
()
else
begin
let size = utf8_nb_bytes_of_char s.[i] in
let next =
match size, s.[i] with
| 1, '\\' ->
if i + size < len then
try
let c = Types.CMap.find s.[i+size] escaped_chars in
Buffer.add_char b c;
i+size+1
with
| Not_found ->
match s.[i+size] with
| 'u' when i+size+4 < len ->
let code = int32_of_chars s (i+size+1) 4 in
Buffer.add_string b (utf8_char_of_code code);
i+size+5
| 'U' when i+size+8 < len ->
let code = int32_of_chars s (i+size+1) 8 in
Buffer.add_string b (utf8_char_of_code code);
i+size+9
| c ->
Buffer.add_char b s.[i];
i + size
else
(
Buffer.add_char b s.[i] ;
i + size
)
| _ ->
Buffer.add_string b (String.sub s i size);
i + size
in
iter b len s next
end
in
fun s ->
let len = String.length s in
let b = Buffer.create len in
iter b len s 0 ;
Buffer.contents b ;;
let utf8_unescape_to_percent =
let rec iter b len s i =
if i >= len then
()
else
begin
let size = utf8_nb_bytes_of_char s.[i] in
let next =
match size, s.[i] with
| 1, '\\' ->
if i + 1 < len then
match s.[i+1] with
| 'u' when i+size+1+4 < len ->
let code = int32_of_chars s (i+size+1) 4 in
let n = Int32.to_int code in
let () =
if (n = 58 )
|| (n = 64 )
|| (n >= 48 && n <= 57 )
|| (n >= 65 && n <= 90 )
|| (n >= 97 && n <= 122 )
|| (n = 95 )
|| (n = 45 )
|| (n = 46 )
|| (n = 126 )
|| (n = 33 )
|| (n = 36 )
|| (n >= 38 && n <= 46)
|| (n = 61 )
|| (n = 59 )
then
Buffer.add_char b (Char.chr n)
else
if (n >= 0xA0 && n <= 0xD7FF)
|| (n >= 0xF900 && n <= 0xFDCF)
|| (n >= 0xFDF0 && n <= 0xFFEF)
then
Buffer.add_string b (utf8_char_of_code code)
else
let h1 = String.sub s (i+2) 2 in
let h2 = String.sub s (i+4) 2 in
if n < 256 then
Printf.bprintf b "%%%s" h2
else
Printf.bprintf b "%%%s%%%s" h1 h2
in
i+size+5
| 'U' when i+size+8 < len ->
let code = int32_of_chars s (i+size+1) 8 in
Buffer.add_string b (utf8_char_of_code code);
i+size+9
| c ->
Buffer.add_char b s.[i];
i + size
else
(
Buffer.add_char b s.[i] ;
i + size
)
| _ ->
Buffer.add_string b (String.sub s i size);
i + size
in
iter b len s next
end
in
fun s ->
let len = String.length s in
let b = Buffer.create len in
iter b len s 0 ;
let res = Buffer.contents b in
res
;;
let cmap_utf_8 cmap s =
let b = Buffer.create (String.length s * 2) in
let add_map _ _ u =
let u = match u with `Malformed _ -> Uutf.u_rep | `Uchar u -> u in
match cmap u with
| `Self -> Uutf.Buffer.add_utf_8 b u
| `Uchars us -> List.iter (Uutf.Buffer.add_utf_8 b) us
in
Uutf.String.fold_utf_8 add_map () s; Buffer.contents b
let utf8_lowercase s = cmap_utf_8 Uucp.Case.Map.to_lower s
let utf8_uppercase s = cmap_utf_8 Uucp.Case.Map.to_upper s
let quote_char = Uchar.of_char '"'
let utf8_backslash_quotes s =
let b = Buffer.create 64 in
let f () _i = function
| `Malformed str -> Buffer.add_string b str
| `Uchar cp ->
if Uchar.equal cp quote_char then
Buffer.add_char b '\\' ;
Uutf.Buffer.add_utf_8 b cp
in
Uutf.String.fold_utf_8 f () s;
Buffer.contents b