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
(** This module provides facilities for translating language-based locations to
protocol-based locations.
After a long discussion (thanks Léo !), we have decided that the best is to
have `Lang.Point` to store columns offset in the values that are native to
the protocol under consideration, set by the upper layers.
This scheme kind of follows what we have done since the start with coq-lsp. *)
module Encoding = struct
type t =
| Utf8
| Utf16
| Utf32
end
type utf8_string = string
type char = int
type utf8_index = int
type utf16_index = int
let rec search_head s i =
if i >= String.length s then i
else
let n = Char.code (String.unsafe_get s i) in
if n < 0x80 || n >= 0xc2 then i else search_head s (i + 1)
let next s i =
let n = Char.code s.[i] in
if n < 0x80 then i + 1
else if n < 0xc0 then search_head s (i + 1)
else if n <= 0xdf then i + 2
else if n <= 0xef then i + 3
else if n <= 0xf7 then i + 4
else if n <= 0xfb then i + 5
else if n <= 0xfd then i + 6
else invalid_arg "UTF8.next"
let rec length_aux s c i =
if i >= String.length s then c
else
let n = Char.code (String.unsafe_get s i) in
let k =
if n < 0x80 then 1
else if n < 0xc0 then invalid_arg "UTF8.length"
else if n < 0xe0 then 2
else if n < 0xf0 then 3
else if n < 0xf8 then 4
else if n < 0xfc then 5
else if n < 0xfe then 6
else invalid_arg "UTF8.length"
in
length_aux s (c + 1) (i + k)
let length s = length_aux s 0 0
let rec nth_aux s i n = if n = 0 then i else nth_aux s (next s i) (n - 1)
let nth s n = nth_aux s 0 n
let length_utf16 line =
let byte_idx = ref 0 in
let utf16_len = ref 0 in
let len = String.length line in
while !byte_idx < len do
let ch = Compat.OCaml4_14.String.get_utf_8_uchar line !byte_idx in
let next_idx = next line !byte_idx in
byte_idx := next_idx;
let l =
Compat.OCaml4_14.Uchar.(utf_16_byte_length (utf_decode_uchar ch)) / 2
in
utf16_len := !utf16_len + l
done;
!utf16_len
let utf8_offset_of_utf16_offset ~line ~(offset : utf16_index) =
let byte_idx = ref 0 in
let utf16_char_count = ref 0 in
let len = String.length line in
(try
while !utf16_char_count < offset do
let ch = Compat.OCaml4_14.String.get_utf_8_uchar line !byte_idx in
let next_idx = next line !byte_idx in
if next_idx >= len then raise Not_found else byte_idx := next_idx;
let code_unit_count =
Compat.OCaml4_14.Uchar.(utf_16_byte_length (utf_decode_uchar ch)) / 2
in
utf16_char_count := !utf16_char_count + code_unit_count;
()
done
with _ -> ());
!byte_idx
let utf16_offset_of_utf8_offset ~line ~(offset : utf8_index) =
let byte_idx = ref 0 in
let utf16_char_count = ref 0 in
let len = String.length line in
(try
while !byte_idx < offset do
let ch = Compat.OCaml4_14.String.get_utf_8_uchar line !byte_idx in
let next_idx = next line !byte_idx in
if next_idx > len then raise Not_found else byte_idx := next_idx;
let code_unit_count =
Compat.OCaml4_14.Uchar.(utf_16_byte_length (utf_decode_uchar ch)) / 2
in
utf16_char_count := !utf16_char_count + code_unit_count;
()
done
with _ -> ());
!utf16_char_count
(** Not used anywhere, remove? *)
let char_of_utf16_offset ~line ~(offset : utf16_index) =
let byte_idx = ref 0 in
let count = ref 0 in
let utf16_char_count = ref 0 in
let len = String.length line in
(try
while !utf16_char_count < offset do
let ch = Compat.OCaml4_14.String.get_utf_8_uchar line !byte_idx in
let next_idx = next line !byte_idx in
if next_idx >= len then raise Not_found else byte_idx := next_idx;
let code_unit_count =
Compat.OCaml4_14.Uchar.(utf_16_byte_length (utf_decode_uchar ch)) / 2
in
utf16_char_count := !utf16_char_count + code_unit_count;
count := !count + 1;
()
done
with _ -> ());
!count
let utf16_offset_of_char ~line ~(char : char) =
let offset16 = ref 0 in
let idx = ref 0 in
for _ = 0 to char - 1 do
let ch = Compat.OCaml4_14.String.get_utf_8_uchar line !idx in
let byte_len =
Compat.OCaml4_14.Uchar.(utf_16_byte_length (utf_decode_uchar ch))
in
offset16 := !offset16 + (byte_len / 2);
idx := next line !idx
done;
!offset16
let utf8_offset_of_char ~line ~char =
if char < length line then Some (nth line char) else None
let find_char line byte =
let rec f index n_chars =
let next_index = next line index in
if next_index > byte then n_chars else f next_index (n_chars + 1)
in
if byte < String.length line then Some (f 0 0) else None
let char_of_utf8_offset ~line ~offset =
let char = find_char line offset in
char