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
let high_surrogate_start = 0xD800
let high_surrogate_end = 0xDBFF
let low_surrogate_start = 0xDC00
let low_surrogate_end = 0xDFFF
let other_planes_start = 0x10000
module type TWO_BYTE =
sig
val add: int -> int -> int
val byte1: int -> int
val byte2: int -> int
end
module Be2 =
struct
let add (i1: int) (i2: int): int =
(i1 lsl 8) + i2
let byte1 (i: int): int =
i lsr 8
let byte2 (i: int): int =
i land 0xFF
end
module Le2 =
struct
let add (i1: int) (i2: int): int =
i1 + (i2 lsl 8)
let byte1 (i: int): int =
i land 0xFF
let byte2 (i: int): int =
i lsr 8
end
module Encoder (Byte: TWO_BYTE) =
struct
type t = Uchar.t
let to_string (scalar: int): string =
assert (scalar <= 0x10FFFF);
let open Printf in
let open Char in
let open Byte in
if scalar < other_planes_start then begin
assert (
scalar < high_surrogate_start
||
low_surrogate_end < scalar
);
sprintf "%c%c" (byte1 scalar |> chr) (byte2 scalar |> chr)
end
else
let i = scalar - other_planes_start in
let hi = high_surrogate_start + i lsr 10
and lo = low_surrogate_start + i land (1 lsl 10 - 1)
in
sprintf "%c%c%c%c"
(byte1 hi |> chr) (byte2 hi |> chr)
(byte1 lo |> chr) (byte2 lo |> chr)
let to_internal (uc: t): string =
Utf8.Encoder.to_internal uc
let to_external (uc: t): string =
to_string Uchar.(to_int uc)
end
module Decoder (Byte: TWO_BYTE) =
struct
type t = {
value: int;
w1: int;
w2: int;
missing: int;
length: int;
}
let is_complete (u: t): bool =
u.missing = 0
let has_error (u: t): bool =
u.value = -1
let uchar (u: t): Uchar.t =
if Uchar.is_valid u.value then
Uchar.of_int u.value
else
Uchar.rep
let scalar (u: t): int =
if Uchar.is_valid u.value then
u.value
else
Uchar.(to_int rep)
let byte_width (u: t): int =
u.length - u.missing
let width (u: t): int =
let i = scalar u in
if i = Char.code '\t' then
4
else if i < Char.code ' ' then
0
else
match i with
| 0x200B
| 0xFEFF
| 0x200C
| 0x200D
-> 0
| _
-> 1
let is_newline (u: t): bool =
scalar u = 0x0A
let init: t =
{
value = 0;
w1 = 0;
w2 = 0;
missing = 0;
length = 0;
}
let error (length: int): t =
{
value = -1;
w1 = 0;
w2 = 0;
missing = 0;
length;
}
let put (c: char) (u: t): t =
let c = Char.code c
in
if u.missing = 0 then
{
value = c;
w1 = c;
w2 = c;
missing = 1;
length = 2;
}
else if u.missing = 1 && u.length = 2 then
let w1 = Byte.add u.w1 c
in
if w1 < high_surrogate_start || low_surrogate_end < w1 then
{
u with
w1;
value = w1;
missing = 0;
}
else if high_surrogate_start <= w1 && w1 <= high_surrogate_end then
{
u with
w1;
missing = 2;
length = 4;
}
else
error 2
else if u.missing = 2 && u.length = 4 then
{
u with
w2 = c;
missing = 1;
}
else begin
assert (u.missing = 1);
assert (u.length = 4);
let w2 = Byte.add u.w2 c
in
if low_surrogate_start <= w2 && w2 <= low_surrogate_end then
{
u with
w2;
value =
(u.w1 - high_surrogate_start) lsl 10
+
(w2 - low_surrogate_start)
+
other_planes_start;
missing = 0;
}
else
error 4
end
let run_on_string (str: string): t =
let len = String.length str in
let rec run i u =
if i = len then
u
else
run (i + 1) (put str.[i] u)
in
run 0 init
let round_trip (str: string): string =
let module Enc = Encoder (Byte) in
let u = run_on_string str in
Enc.to_external (uchar u)
end
module Be =
struct
module Encoder = Encoder (Be2)
module Decoder = Decoder (Be2)
end
module Le =
struct
module Encoder = Encoder (Le2)
module Decoder = Decoder (Le2)
end
let ocaml_encode_be (uc: Uchar.t): string =
let module B = Stdlib.Buffer in
let buf = B.create 4 in
B.add_utf_16be_uchar buf uc;
B.contents buf
let ocaml_encode_le (uc: Uchar.t): string =
let module B = Stdlib.Buffer in
let buf = B.create 4 in
B.add_utf_16le_uchar buf uc;
B.contents buf
let round_trip_le (i: int) (len: int): bool =
let uc = Uchar.of_int i in
let str = ocaml_encode_le uc in
assert (String.length str = len);
str
=
Le.Decoder.round_trip str
let round_trip_be (i: int) (len: int): bool =
let uc = Uchar.of_int i in
let str = ocaml_encode_be uc in
assert (String.length str = len);
let res = Be.Decoder.round_trip str
in
str
=
res
let%test _ =
round_trip_le 0x0 2
let%test _ =
round_trip_le 0x10000 4
let%test _ =
round_trip_be 0x0 2
let%test _ =
round_trip_be 0x10000 4