-
Notifications
You must be signed in to change notification settings - Fork 4
/
teng
336 lines (272 loc) · 6.95 KB
/
teng
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
/* Copyright 2023 Google LLC
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd */
/* Code in this file is first-party code (i.e. non-hterm) to implement the
* terminal engine. */
fn2(tmrealloc, oldo, newsz)
{
TMint newo = tmalloc(newsz), olsz = tmlen(oldo);
fldcpy( newo, 0,
oldo, 0,
olsz < newsz ?
olsz : newsz);
tmfree(oldo);
return newo;
}
fn0(deqmk)
{
/* deqcap: capacity, including the standard fields
*
* deqhd/deqtl: head/tail indices: if zero, they should both be zero,
* and it indicates the dequeue is empty. If non-zero, each field points
* to a valid slot.
*
* deqbasicflds: number of fields in the object that aren't used as
* dequeue slots.
*/
#define deqcap(d) fld(d, 0)
#define deqhd(d) fld(d, 1)
#define deqtl(d) fld(d, 2)
#define deqtlbytes(d) fld(d, 3) /* used by deqpushtlbyt */
#define deqbasicflds 4
TMint dcp = 16;
TMint deq = tmalloc(dcp);
deqcap(deq) = dcp;
return deq;
}
fn1(deqpophd, dq)
{
TMint res = fld(dq, deqhd(dq));
TMint last = deqhd(dq) == deqtl(dq);
if (!deqhd(dq))
sriously("dequeue %d is already empty; cannot pop", dq);
fld(dq, deqhd(dq)++) = 0;
if (last) deqhd(dq) = deqtl(dq) = 0;
if (deqhd(dq) == deqcap(dq)) deqhd(dq) = deqbasicflds;
return res;
}
/* Grows dequeue capacity with new space at tail end. */
fn1(deqgrowtl, dq)
{
TMint newdq, newcap;
newcap = deqcap(dq) * 3 / 2;
newdq = tmalloc(newcap);
deqcap(newdq) = newcap;
deqhd(newdq) = deqbasicflds;
deqtl(newdq) = deqbasicflds-1;
do {
fld(newdq, ++deqtl(newdq)) = deqpophd(dq);
}
while (deqhd(dq));
tmfree(dq);
return newdq;
}
fn2(deqpushtl, dq, val)
{
if (!deqhd(dq)) {
/* Push first element. We don't allow zero-capacity
* dequeues so it's safe to push. */
deqhd(dq) = deqbasicflds;
deqtl(dq) = deqbasicflds-1;
}
else if (deqtl(dq)+1 == deqhd(dq)) {
/* Dequeue is at capacity because tail is the slot right before
* head. */
dq = deqgrowtl(dq);
}
else if (deqtl(dq) != deqcap(dq) - 1) {
/* Dequeue is non at capacity yet, since we don't match the
* prior case [0] and tail is not at last slot. This means that
* the slot after tail is valid and unoccupied. */
}
else if (deqhd(dq) != deqbasicflds) {
/* Tail is last slot in buffer, but dequeue is not at capacity
* because there is still at least one slot before the head. */
deqtl(dq) = deqbasicflds-1;
}
else {
/* Tail is last slot in buffer and head is first slot, so the
* dequeue is at capapcity. */
dq = deqgrowtl(dq);
}
fld(dq, ++deqtl(dq)) = val;
return dq;
}
/* Pushes byte onto tail in little endian order. Cannot be used in conjuction
with other deqpush* deqpop* functions. */
fn2(deqpushbyt, dq, val)
{
TMint tlb = deqtlbytes(dq), shf;
val &= 0xff;
if (!tlb) {
dq = deqpushtl(dq, val);
tlb = 3;
}
else {
shf = (4 - tlb--) * 8;
fld(dq, deqtl(dq)) |= (val << shf);
}
/* For C, we need an extra 0 dword at the end of the string for safe use
of deqtostring */
if (!tlb && deqtl(dq) == deqcap(dq)-1) dq = deqgrowtl(dq);
deqtlbytes(dq) = tlb;
return dq;
}
/* Pushes the given codepoint as utf8 bytes, using deqpushbyt. */
fn2(deqpushcop, dq, cop)
{
if (cop < (1<<7)) {
dq = deqpushbyt(dq, cop);
} else if (cop < (1<<11)) {
dq = deqpushbyt(dq, 0xc0 | cop >> 6); cop &= 0x0003f;
dq = deqpushbyt(dq, 0x80 | cop);
} else if (cop < (1<<16)) {
dq = deqpushbyt(dq, 0xe0 | cop >> 12); cop &= 0x00fff;
dq = deqpushbyt(dq, 0x80 | cop >> 6); cop &= 0x0003f;
dq = deqpushbyt(dq, 0x80 | cop);
} else if (cop < 0x110000) {
dq = deqpushbyt(dq, 0xf0 | cop >> 18); cop &= 0x2ffff;
dq = deqpushbyt(dq, 0x80 | cop >> 12); cop &= 0x00fff;
dq = deqpushbyt(dq, 0x80 | cop >> 6); cop &= 0x0003f;
dq = deqpushbyt(dq, 0x80 | cop);
}
return dq;
}
fn1(deqsiz, dq)
{
TMint h = deqhd(dq), t;
if (!h) return 0;
t = deqtl(dq);
if (t >= h) return t - h + 1;
else return t - h + 1 + deqcap(dq) - deqbasicflds;
}
fn1(deqbytsiz, dq)
{
TMint h = deqhd(dq), qworsz;
if (!h) return 0;
qworsz = deqtl(dq) - h + 1;
if (qworsz < 0) sriously("deque not fit for byte ops: %d", qworsz);
return qworsz * 4 - deqtlbytes(dq);
}
/* return byte at given index, and replace with `val` if it is non-negative */
fn3(deqbytat, dq, i, val)
{
TMint max = deqbytsiz(dq), qwi, bi, oldqw, newqw;
if (i == max) return 0;
if (i < 0 || i > max) sriously("byte # out-of-range: %d, %d", i, max);
bi = 8 * (i % 4);
qwi = deqhd(dq) + ~~(i / 4);
oldqw = fld(dq, qwi);
if (val >= 0) {
val &= 0xff;
newqw = oldqw & ~(0xff << bi);
newqw |= val << bi;
fld(dq, qwi) = newqw;
}
return 0xff & (oldqw >> bi);
}
/* removes all elements without constructing a new deque. compatible with byte
operations. */
fn1(deqclear, dq)
{
deqtl(dq) = deqhd(dq) = 0;
deqtlbytes(dq) = 0;
return dq;
}
/* Parses integer in bytes of deq. byti is the starting byte index. fin is an
optional object id whose field 0 is set to the first non-digit character. */
fn3(deqatoi, deq, byti, fin)
{
TMint sign = 1, acc = 0, acn, c, digs = 0, er = 0;
switch (deqbytat(deq, byti, -1)) {
case ORD('-'): sign = -1;
case ORD('+'): byti++;
}
for (;;) {
c = deqbytat(deq, byti, -1);
c -= ORD('0');
if (c < 0 || c > 9) break;
digs++;
byti++;
acn = ~~(acc * 10);
if ( ~~(acn / 10) != acc) er = 1;
acc = ~~(acn + c);
if (acn > acc) er = 1;
}
if (fin) fld(fin, 0) = byti;
if (er || !digs) return 0x80000000;
return acc * sign;
}
fn2(deqcellat, deq, ci)
{
TMint fi = deqhd(deq) + ci, cap = deqcap(deq);
if (fi >= cap) fi += deqbasicflds - cap;
return fld(deq, fi);
}
fn2(deqcatbyt, deq, eeq)
{
TMint b, cnt = deqbytsiz(eeq);
for (b = 0; b < cnt; b++) deq = deqpushbyt(deq, deqbytat(eeq, b, -1));
return deq;
}
fn2(deqpushhex, deq, val)
{
val &= 0xf;
if (val <= 9) val += ORD('0');
else val += ORD('a');
return deqpushbyt(deq, val);
}
fn2(deqpshhex8, deq, by)
{
deq = deqpushhex(deq, by >> 4);
deq = deqpushhex(deq, by);
return deq;
}
fnx3(TMint, deqpshutf8, (TMint, deq), (TMutf8, str), (TMint, len))
{
TMint bi = 0;
TMutf8 u8 = tmutf8(str);
for (;;) {
if (bi == len) break;
if (len < 0 && !u8[bi]) break;
deq = deqpushbyt(deq, u8[bi++]);
}
return deq;
}
fnx3(TMint, deqsetutf8, (TMint, deq), (TMany, str), (TMint, len))
{
if (deq) deqclear(deq);
else deq = deqmk();
return deqpshutf8(deq, str, len);
}
fnx4(TMint, cmpdeqstr, (TMint, deq), (TMint, of), (TMint, sz), (TMany, str))
{
TMint so = 0, sb, db;
for (;;) {
sb = ORDAT(str, so++);
do db = deqbytat(deq, of++, -1);
while (db == ORD(' '));
if (db > sb) return 1;
if (db < sb) return -1;
if (!db) return 0;
}
}
#include "name2rgb"
fn2(deqpshitoa, deq, i)
{
TMint fac = 1000000000, dig;
if (i == 0x80000000) {
deq = deqpshutf8(deq, "-", -1);
deq = deqpshitoa(deq, -~~(i/10));
i = -(i%10);
}
if (i == 0) return deqpushhex(deq, 0);
do {
dig = ~~(i / fac);
if (dig) deq = deqpushhex(deq, dig % 10);
fac = ~~(fac / 10);
} while (fac);
return deq;
}