-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathbuffer.res
307 lines (281 loc) · 8.61 KB
/
buffer.res
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
/* ************************************************************************ */
/* */
/* OCaml */
/* */
/* Pierre Weis and Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1999 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/* ************************************************************************ */
/* Extensible buffers */
type t = {
mutable buffer: bytes,
mutable position: int,
mutable length: int,
initial_buffer: bytes,
}
let create = n => {
let n = if n < 1 {
1
} else {
n
}
let s = Bytes.create(n)
{buffer: s, position: 0, length: n, initial_buffer: s}
}
let contents = b => Bytes.sub_string(b.buffer, 0, b.position)
let to_bytes = b => Bytes.sub(b.buffer, 0, b.position)
let sub = (b, ofs, len) =>
if ofs < 0 || (len < 0 || ofs > b.position - len) {
invalid_arg("Buffer.sub")
} else {
Bytes.sub_string(b.buffer, ofs, len)
}
let blit = (src, srcoff, dst, dstoff, len) =>
if (
len < 0 ||
(srcoff < 0 ||
(srcoff > src.position - len || (dstoff < 0 || dstoff > Bytes.length(dst) - len)))
) {
invalid_arg("Buffer.blit")
} else {
Bytes.blit(src.buffer, srcoff, dst, dstoff, len)
}
let nth = (b, ofs) =>
if ofs < 0 || ofs >= b.position {
invalid_arg("Buffer.nth")
} else {
Bytes.unsafe_get(b.buffer, ofs)
}
let length = b => b.position
let clear = b => b.position = 0
let reset = b => {
b.position = 0
b.buffer = b.initial_buffer
b.length = Bytes.length(b.buffer)
}
let resize = (b, more) => {
let len = b.length
let new_len = ref(len)
while b.position + more > new_len.contents {
new_len := 2 * new_len.contents
}
let new_buffer = Bytes.create(new_len.contents)
/* PR#6148: let's keep using [blit] rather than [unsafe_blit] in
this tricky function that is slow anyway. */
Bytes.blit(b.buffer, 0, new_buffer, 0, b.position)
b.buffer = new_buffer
b.length = new_len.contents
}
let add_char = (b, c) => {
let pos = b.position
if pos >= b.length {
resize(b, 1)
}
Bytes.unsafe_set(b.buffer, pos, c)
b.position = pos + 1
}
let add_utf_8_uchar = (b, u) =>
switch Uchar.to_int(u) {
| u if u < 0 => assert(false)
| u if u <= 0x007F => add_char(b, Char.unsafe_chr(u))
| u if u <= 0x07FF =>
let pos = b.position
if pos + 2 > b.length {
resize(b, 2)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(lor(0xC0, lsr(u, 6))))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(lor(0x80, land(u, 0x3F))))
b.position = pos + 2
| u if u <= 0xFFFF =>
let pos = b.position
if pos + 3 > b.length {
resize(b, 3)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(lor(0xE0, lsr(u, 12))))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(lor(0x80, land(lsr(u, 6), 0x3F))))
Bytes.unsafe_set(b.buffer, pos + 2, Char.unsafe_chr(lor(0x80, land(u, 0x3F))))
b.position = pos + 3
| u if u <= 0x10FFFF =>
let pos = b.position
if pos + 4 > b.length {
resize(b, 4)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(lor(0xF0, lsr(u, 18))))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(lor(0x80, land(lsr(u, 12), 0x3F))))
Bytes.unsafe_set(b.buffer, pos + 2, Char.unsafe_chr(lor(0x80, land(lsr(u, 6), 0x3F))))
Bytes.unsafe_set(b.buffer, pos + 3, Char.unsafe_chr(lor(0x80, land(u, 0x3F))))
b.position = pos + 4
| _ => assert(false)
}
let add_utf_16be_uchar = (b, u) =>
switch Uchar.to_int(u) {
| u if u < 0 => assert(false)
| u if u <= 0xFFFF =>
let pos = b.position
if pos + 2 > b.length {
resize(b, 2)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(lsr(u, 8)))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(land(u, 0xFF)))
b.position = pos + 2
| u if u <= 0x10FFFF =>
let u' = u - 0x10000
let hi = lor(0xD800, lsr(u', 10))
let lo = lor(0xDC00, land(u', 0x3FF))
let pos = b.position
if pos + 4 > b.length {
resize(b, 4)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(lsr(hi, 8)))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(land(hi, 0xFF)))
Bytes.unsafe_set(b.buffer, pos + 2, Char.unsafe_chr(lsr(lo, 8)))
Bytes.unsafe_set(b.buffer, pos + 3, Char.unsafe_chr(land(lo, 0xFF)))
b.position = pos + 4
| _ => assert(false)
}
let add_utf_16le_uchar = (b, u) =>
switch Uchar.to_int(u) {
| u if u < 0 => assert(false)
| u if u <= 0xFFFF =>
let pos = b.position
if pos + 2 > b.length {
resize(b, 2)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(land(u, 0xFF)))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(lsr(u, 8)))
b.position = pos + 2
| u if u <= 0x10FFFF =>
let u' = u - 0x10000
let hi = lor(0xD800, lsr(u', 10))
let lo = lor(0xDC00, land(u', 0x3FF))
let pos = b.position
if pos + 4 > b.length {
resize(b, 4)
}
Bytes.unsafe_set(b.buffer, pos, Char.unsafe_chr(land(hi, 0xFF)))
Bytes.unsafe_set(b.buffer, pos + 1, Char.unsafe_chr(lsr(hi, 8)))
Bytes.unsafe_set(b.buffer, pos + 2, Char.unsafe_chr(land(lo, 0xFF)))
Bytes.unsafe_set(b.buffer, pos + 3, Char.unsafe_chr(lsr(lo, 8)))
b.position = pos + 4
| _ => assert(false)
}
let add_substring = (b, s, offset, len) => {
if offset < 0 || (len < 0 || offset > String.length(s) - len) {
invalid_arg("Buffer.add_substring/add_subbytes")
}
let new_position = b.position + len
if new_position > b.length {
resize(b, len)
}
Bytes.blit_string(s, offset, b.buffer, b.position, len)
b.position = new_position
}
let add_subbytes = (b, s, offset, len) => add_substring(b, Bytes.unsafe_to_string(s), offset, len)
let add_string = (b, s) => {
let len = String.length(s)
let new_position = b.position + len
if new_position > b.length {
resize(b, len)
}
Bytes.blit_string(s, 0, b.buffer, b.position, len)
b.position = new_position
}
let add_bytes = (b, s) => add_string(b, Bytes.unsafe_to_string(s))
let add_buffer = (b, bs) => add_subbytes(b, bs.buffer, 0, bs.position)
let closing = param =>
switch param {
| '(' => ')'
| '{' => '}'
| _ => assert(false)
}
/* opening and closing: open and close characters, typically ( and )
k: balance of opening and closing chars
s: the string where we are searching
start: the index where we start the search. */
let advance_to_closing = (opening, closing, k, s, start) => {
let rec advance = (k, i, lim) =>
if i >= lim {
raise(Not_found)
} else if String.get(s, i) == opening {
advance(k + 1, i + 1, lim)
} else if String.get(s, i) == closing {
if k == 0 {
i
} else {
advance(k - 1, i + 1, lim)
}
} else {
advance(k, i + 1, lim)
}
advance(k, start, String.length(s))
}
let advance_to_non_alpha = (s, start) => {
let rec advance = (i, lim) =>
if i >= lim {
lim
} else {
switch String.get(s, i) {
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' => advance(i + 1, lim)
| _ => i
}
}
advance(start, String.length(s))
}
/* We are just at the beginning of an ident in s, starting at start. */
let find_ident = (s, start, lim) =>
if start >= lim {
raise(Not_found)
} else {
switch String.get(s, start) {
/* Parenthesized ident ? */
| ('(' | '{') as c =>
let new_start = start + 1
let stop = advance_to_closing(c, closing(c), 0, s, new_start)
(String.sub(s, new_start, stop - start - 1), stop + 1)
/* Regular ident */
| _ =>
let stop = advance_to_non_alpha(s, start + 1)
(String.sub(s, start, stop - start), stop)
}
}
/* Substitute $ident, $(ident), or ${ident} in s,
according to the function mapping f. */
let add_substitute = (b, f, s) => {
let lim = String.length(s)
let rec subst = (previous, i) =>
if i < lim {
switch String.get(s, i) {
| '$' as current if previous == '\\' =>
add_char(b, current)
subst(' ', i + 1)
| '$' =>
let j = i + 1
let (ident, next_i) = find_ident(s, j, lim)
add_string(b, f(ident))
subst(' ', next_i)
| current if previous === '\\' =>
add_char(b, '\\')
add_char(b, current)
subst(' ', i + 1)
| '\\' as current => subst(current, i + 1)
| current =>
add_char(b, current)
subst(current, i + 1)
}
} else if previous == '\\' {
add_char(b, previous)
}
subst(' ', 0)
}
let truncate = (b, len) =>
if len < 0 || len > length(b) {
invalid_arg("Buffer.truncate")
} else {
b.position = len
}