@willbooster/tree-sitter-c 42.0.0 → 44.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,431 @@
1
+ // The characters of a directive segment, read from the lexer as the line-state pass of `emptyDirectives.h` needs them.
2
+ #ifndef TREE_SITTER_C_DIRECTIVE_BUFFER_H_
3
+ #define TREE_SITTER_C_DIRECTIVE_BUFFER_H_
4
+
5
+ #include "directiveScan.h"
6
+ #include <stdlib.h>
7
+
8
+ #define DIRECTIVE_PAGE_SHIFT 12
9
+ #define DIRECTIVE_PAGE_SIZE (1 << DIRECTIVE_PAGE_SHIFT)
10
+ #define DIRECTIVE_SEGMENT_UNITS (DIRECTIVE_PAGE_SIZE * 16)
11
+ #define DIRECTIVE_FILL_UNITS 256
12
+
13
+ typedef struct {
14
+ uint32_t index, byte, width;
15
+ } DirectivePosition;
16
+
17
+ // The characters of one directive segment, read from the lexer as the line-state pass needs them.
18
+ typedef struct {
19
+ uint16_t **characters;
20
+ DirectivePosition *positions;
21
+ TSLexer *lexer;
22
+ uint32_t length, page_count, page_capacity, position_count, position_capacity;
23
+ // The lexer's byte position, which follows the last stored character.
24
+ uint32_t fill_byte;
25
+ uint32_t conditional_depth;
26
+ // The characters stored last, as `DirectiveScan` keeps those it reads.
27
+ uint64_t recent;
28
+ // Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries, and
29
+ // whether the characters stored or scanned hold `pragma_word`.
30
+ bool utf16, encoding_known, failed, carries, pragma;
31
+ uint8_t classes[UNIT_CLASS_COUNT];
32
+ } DirectiveBuffer;
33
+
34
+ // A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
35
+ typedef struct {
36
+ DirectiveBuffer *buffer;
37
+ uint32_t index, byte, position_index, previous_byte, before_previous_byte;
38
+ // Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
39
+ bool spaced_splice;
40
+ } DirectiveInput;
41
+
42
+ typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
43
+
44
+ static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
45
+ static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
46
+ static int32_t directive_next(DirectiveInput *input);
47
+ static int32_t directive_physical_next(DirectiveInput *input);
48
+ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
49
+ static bool directive_skip_splice(DirectiveInput *input);
50
+ static uint32_t directive_splice_end(const DirectiveInput *input);
51
+ static int32_t directive_peek(const DirectiveInput *input);
52
+ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
53
+ static void directive_advance_plain(DirectiveInput *input, uint32_t length);
54
+ static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
55
+ static bool directive_run_continues(DirectiveRun run, uint16_t c);
56
+ static void directive_skip_plain(DirectiveInput *input);
57
+ static int32_t directive_plain(const DirectiveInput *input);
58
+ static uint32_t directive_byte(const DirectiveInput *input);
59
+ static inline bool directive_has(const DirectiveInput *input, uint32_t index);
60
+ static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
61
+ static uint32_t directive_fill_units(DirectiveBuffer *buffer, uint32_t limit);
62
+ static bool directive_append(DirectiveBuffer *buffer);
63
+ static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
64
+ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
65
+ static inline void directive_note(DirectiveBuffer *buffer, uint32_t c);
66
+ static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
67
+ static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
68
+ static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
69
+ static void directive_store(DirectiveBuffer *buffer, uint16_t c);
70
+ static void directive_release(DirectiveBuffer *buffer);
71
+ static unsigned directive_utf8_width(uint32_t c);
72
+
73
+ // Drops the characters before `start`, which `DirectiveScan` has consumed.
74
+ static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
75
+ if (!start) return;
76
+ uint32_t length = buffer->length - start;
77
+ for (uint32_t i = 0; i < length; i++) {
78
+ uint32_t from = start + i;
79
+ buffer->characters[i >> DIRECTIVE_PAGE_SHIFT][i & (DIRECTIVE_PAGE_SIZE - 1)] =
80
+ buffer->characters[from >> DIRECTIVE_PAGE_SHIFT][from & (DIRECTIVE_PAGE_SIZE - 1)];
81
+ }
82
+ buffer->length = length;
83
+ uint32_t count = 0;
84
+ for (uint32_t i = 0; i < buffer->position_count; i++) {
85
+ DirectivePosition position = buffer->positions[i];
86
+ if (position.index < start) continue;
87
+ position.index -= start;
88
+ buffer->positions[count++] = position;
89
+ }
90
+ buffer->position_count = count;
91
+ }
92
+
93
+ // Moves back to the character before `*index`, reading a surrogate pair as one character, and decodes it into `*c` and
94
+ // its byte width into `*width`; false when its width exceeds the bytes left.
95
+ static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width) {
96
+ const DirectiveBuffer *buffer = input->buffer;
97
+ uint32_t i = --*index;
98
+ if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
99
+ directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) i = --*index;
100
+ *width = directive_expected_width(input, i, c);
101
+ if (*position && buffer->positions[*position - 1].index == i) {
102
+ DirectivePosition recorded = buffer->positions[--*position];
103
+ *byte = recorded.byte;
104
+ *width = recorded.width;
105
+ return true;
106
+ }
107
+ if (*width > *byte) return false;
108
+ *byte -= *width;
109
+ return true;
110
+ }
111
+
112
+ static int32_t directive_next(DirectiveInput *input) {
113
+ int32_t plain = directive_plain(input);
114
+ if (plain >= 0) {
115
+ directive_skip_plain(input);
116
+ return plain;
117
+ }
118
+ while (directive_has(input, input->index)) {
119
+ int32_t c = directive_physical_next(input);
120
+ if (c == '\\' && directive_skip_splice(input)) continue;
121
+ if (directive_newline(c)) {
122
+ if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
123
+ directive_character(input, input->index) != c) directive_physical_next(input);
124
+ return '\n';
125
+ }
126
+ return c;
127
+ }
128
+ return -1;
129
+ }
130
+
131
+ static int32_t directive_physical_next(DirectiveInput *input) {
132
+ const DirectiveBuffer *buffer = input->buffer;
133
+ uint32_t index = input->index;
134
+ int32_t c;
135
+ uint32_t width = directive_expected_width(input, index, &c);
136
+ input->index += c > 0xffff ? 2 : 1;
137
+ input->before_previous_byte = input->previous_byte;
138
+ input->previous_byte = input->byte;
139
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index == index) {
140
+ DirectivePosition position = buffer->positions[input->position_index++];
141
+ input->previous_byte = position.byte;
142
+ input->byte = position.byte + position.width;
143
+ } else input->byte += width;
144
+ return c;
145
+ }
146
+
147
+ // Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
148
+ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
149
+ uint32_t unit = directive_character(input, index);
150
+ if (unit >= 0xd800 && unit <= 0xdbff && directive_has(input, index + 1)) {
151
+ uint32_t low = directive_character(input, index + 1);
152
+ if (low >= 0xdc00 && low <= 0xdfff) {
153
+ *c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
154
+ return 4;
155
+ }
156
+ }
157
+ *c = (int32_t)unit;
158
+ return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
159
+ }
160
+
161
+ // Consumes the rest of a line splice after its `\\`; false when none follows.
162
+ static bool directive_skip_splice(DirectiveInput *input) {
163
+ uint32_t after = directive_splice_end(input);
164
+ if (after == input->index) return false;
165
+ if (directive_horizontal(directive_character(input, input->index))) input->spaced_splice = true;
166
+ while (input->index < after) directive_physical_next(input);
167
+ return true;
168
+ }
169
+
170
+ static uint32_t directive_splice_end(const DirectiveInput *input) {
171
+ uint32_t after = input->index;
172
+ while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
173
+ if (!directive_has(input, after) || !directive_newline(directive_character(input, after))) return input->index;
174
+ int32_t newline = directive_character(input, after++);
175
+ if (directive_has(input, after) && directive_newline(directive_character(input, after)) &&
176
+ directive_character(input, after) != newline) after++;
177
+ return after;
178
+ }
179
+
180
+ static int32_t directive_peek(const DirectiveInput *input) {
181
+ int32_t plain = directive_plain(input);
182
+ if (plain >= 0) return plain;
183
+ DirectiveInput copy = *input;
184
+ return directive_next(&copy);
185
+ }
186
+
187
+ // Consumes the longest run of plain characters that `run` accepts and returns its length.
188
+ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
189
+ const DirectiveBuffer *buffer = input->buffer;
190
+ uint32_t index = input->index;
191
+ // Scans the stored characters before the next recorded position and reads more only at their end, because reading
192
+ // may store a character with a recorded position, which ends the run.
193
+ while (directive_has(input, index)) {
194
+ uint32_t end = buffer->length;
195
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
196
+ end = buffer->positions[input->position_index].index;
197
+ }
198
+ uint32_t start = index;
199
+ index = directive_run_end(buffer, run, index, end);
200
+ if (index < end || index == start || end < buffer->length) break;
201
+ }
202
+ uint32_t length = index - input->index;
203
+ if (length) directive_advance_plain(input, length);
204
+ return length;
205
+ }
206
+
207
+ // Moves `input` over `length` plain characters, at least one.
208
+ static void directive_advance_plain(DirectiveInput *input, uint32_t length) {
209
+ uint32_t width = input->buffer->utf16 ? 2 : 1;
210
+ input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
211
+ input->previous_byte = input->byte + (length - 1) * width;
212
+ input->byte += length * width;
213
+ input->index += length;
214
+ }
215
+
216
+ // The index of the first stored character from `index` to `end` that ends the run, or `end`.
217
+ static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end) {
218
+ while (index < end) {
219
+ const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
220
+ uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
221
+ if (page_end > end) page_end = end;
222
+ for (; index < page_end; index++) {
223
+ uint16_t c = page[index & (DIRECTIVE_PAGE_SIZE - 1)];
224
+ if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) return index;
225
+ }
226
+ }
227
+ return end;
228
+ }
229
+
230
+ // `c` is ASCII.
231
+ static bool directive_run_continues(DirectiveRun run, uint16_t c) {
232
+ bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
233
+ switch (run) {
234
+ case DIRECTIVE_RUN_WORD:
235
+ return identifier;
236
+ case DIRECTIVE_RUN_SPACE:
237
+ return directive_horizontal(c);
238
+ case DIRECTIVE_RUN_LINE_COMMENT:
239
+ return true;
240
+ case DIRECTIVE_RUN_BLOCK_COMMENT:
241
+ return c != '*';
242
+ }
243
+ return false;
244
+ }
245
+
246
+ static void directive_skip_plain(DirectiveInput *input) {
247
+ input->index++;
248
+ input->before_previous_byte = input->previous_byte;
249
+ input->previous_byte = input->byte;
250
+ input->byte += input->buffer->utf16 ? 2 : 1;
251
+ }
252
+
253
+ // The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
254
+ static int32_t directive_plain(const DirectiveInput *input) {
255
+ if (!directive_has(input, input->index)) return -1;
256
+ uint16_t c = directive_character(input, input->index);
257
+ if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
258
+ const DirectiveBuffer *buffer = input->buffer;
259
+ bool positioned = input->position_index < buffer->position_count &&
260
+ buffer->positions[input->position_index].index == input->index;
261
+ return positioned ? -1 : c;
262
+ }
263
+
264
+ static uint32_t directive_byte(const DirectiveInput *input) {
265
+ return input->byte;
266
+ }
267
+
268
+ // Reads characters from the lexer until `index` is stored; false at the end of the input.
269
+ static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
270
+ return index < input->buffer->length || directive_fill(input->buffer, index);
271
+ }
272
+
273
+ // Reads ahead to the end of the line or `DIRECTIVE_FILL_UNITS` characters past `index`, since the pass reads each
274
+ // stored character several times and the characters it leaves behind go back to `DirectiveScan`.
275
+ static bool directive_fill(DirectiveBuffer *buffer, uint32_t index) {
276
+ uint32_t limit = index + DIRECTIVE_FILL_UNITS;
277
+ while (buffer->length < limit && !buffer->failed && !pragma_eof(buffer->lexer)) {
278
+ int32_t c = buffer->lexer->lookahead;
279
+ if (directive_fill_units(buffer, limit)) c = directive_character(&(DirectiveInput){.buffer = buffer}, buffer->length - 1);
280
+ else if (!directive_append(buffer)) buffer->failed = true;
281
+ if (!buffer->failed && directive_newline(c) && buffer->length > index) break;
282
+ }
283
+ return index < buffer->length;
284
+ }
285
+
286
+ // Stores the ASCII characters that the lexer hands out as units, up to the first newline, the end of the page or
287
+ // `limit` characters, as `directive_append` stores them one at a time, and returns how many. It stores none where a
288
+ // character may take another width than its encoding's (before the encoding is known, and at the start of an included
289
+ // range, whose first character follows bytes that are left out) or may complete a `pragma_word` stored before.
290
+ // code-gauge-allow functionCognitiveComplexity -- Each bound on the characters stored at once is a condition of its own.
291
+ // code-gauge-allow functionNcss -- Each bound on the characters stored at once is a condition of its own.
292
+ static uint32_t directive_fill_units(DirectiveBuffer *buffer, uint32_t limit) {
293
+ TSLexer *lexer = buffer->lexer;
294
+ uint32_t index = buffer->length, count, size;
295
+ if (!buffer->encoding_known || (index >> DIRECTIVE_PAGE_SHIFT) >= buffer->page_count || directive_pragma_pending(buffer->recent)) return 0;
296
+ const char *units = lexer->peek_units(lexer, &count, &size);
297
+ if (!count || size != (buffer->utf16 ? 2u : 1u) || lexer->is_at_included_range_start(lexer)) return 0;
298
+ uint32_t room = DIRECTIVE_PAGE_SIZE - (index & (DIRECTIVE_PAGE_SIZE - 1));
299
+ if (count > room) count = room;
300
+ if (count > limit - index) count = limit - index;
301
+ uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT] + (index & (DIRECTIVE_PAGE_SIZE - 1));
302
+ uint32_t length = 0, c = 0;
303
+ uint8_t seen = 0;
304
+ while (length < count && (c = directive_unit(units, size, length)) < UNIT_CLASS_COUNT) {
305
+ page[length++] = (uint16_t)c;
306
+ seen |= buffer->classes[c];
307
+ if (directive_newline((int32_t)c)) break;
308
+ }
309
+ if (!length) return 0;
310
+ if ((seen & UNIT_PRAGMA) == UNIT_PRAGMA && directive_units_pragma(units, size, 0, length)) buffer->pragma = true;
311
+ for (uint32_t i = length > 8 ? length - 8 : 0; i < length; i++) buffer->recent = buffer->recent << 8 | page[i];
312
+ buffer->length = index + length;
313
+ buffer->fill_byte += length * size;
314
+ bool line_feed = page[length - 1] == '\n';
315
+ lexer->advance_units(lexer, length, line_feed, line_feed ? 0 : length);
316
+ return length;
317
+ }
318
+
319
+ static bool directive_append(DirectiveBuffer *buffer) {
320
+ TSLexer *lexer = buffer->lexer;
321
+ uint32_t c = lexer->lookahead, index = buffer->length, start = buffer->fill_byte;
322
+ if (c < 0x80 && buffer->encoding_known && (index >> DIRECTIVE_PAGE_SHIFT) < buffer->page_count) {
323
+ return directive_append_ascii(buffer, (uint16_t)c);
324
+ }
325
+ unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
326
+ if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
327
+ directive_encode(buffer, c);
328
+ directive_note(buffer, c);
329
+ TSLexerPosition end;
330
+ lexer->advance(lexer, false);
331
+ lexer->get_position(lexer, &end);
332
+ buffer->fill_byte = end.bytes;
333
+ uint32_t measured = end.bytes - start;
334
+ // A character stored before the encoding is known keeps its measured width, which a later choice cannot change.
335
+ bool known = buffer->encoding_known;
336
+ if (!known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
337
+ buffer->utf16 = measured == utf16_width;
338
+ buffer->encoding_known = true;
339
+ }
340
+ if (!known || c > 0x10ffff || measured != (buffer->utf16 ? utf16_width : width)) {
341
+ return directive_position(buffer, index, start, measured);
342
+ }
343
+ return true;
344
+ }
345
+
346
+ static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width) {
347
+ if (buffer->position_count == buffer->position_capacity) {
348
+ uint32_t capacity = buffer->position_capacity ? buffer->position_capacity * 2 : 16;
349
+ if (capacity < buffer->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*buffer->positions)) return false;
350
+ void *positions = realloc(buffer->positions, (size_t)capacity * sizeof(*buffer->positions));
351
+ if (!positions) return false;
352
+ buffer->positions = positions;
353
+ buffer->position_capacity = capacity;
354
+ }
355
+ buffer->positions[buffer->position_count++] = (DirectivePosition){index, byte, width};
356
+ return true;
357
+ }
358
+
359
+ // An ASCII character on a stored page, once the encoding is known, takes the expected width unless the lexer skipped to
360
+ // another included range after it.
361
+ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
362
+ TSLexer *lexer = buffer->lexer;
363
+ uint32_t index = buffer->length, start = buffer->fill_byte;
364
+ buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
365
+ buffer->length = index + 1;
366
+ directive_note(buffer, c);
367
+ TSLexerPosition end;
368
+ lexer->advance(lexer, false);
369
+ lexer->get_position(lexer, &end);
370
+ buffer->fill_byte = end.bytes;
371
+ return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
372
+ }
373
+
374
+ static inline void directive_note(DirectiveBuffer *buffer, uint32_t c) {
375
+ buffer->recent = buffer->recent << 8 | (c < 0x80 ? c : 0);
376
+ if (directive_pragma_word(buffer->recent)) buffer->pragma = true;
377
+ }
378
+
379
+ static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
380
+ if (buffer->length > UINT32_MAX - width) return false;
381
+ uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
382
+ if (last >= buffer->page_capacity) {
383
+ uint32_t next = buffer->page_capacity ? buffer->page_capacity * 2 : 16;
384
+ if (next < buffer->page_capacity || (size_t)next > SIZE_MAX / sizeof(*buffer->characters)) return false;
385
+ void *characters = realloc(buffer->characters, (size_t)next * sizeof(*buffer->characters));
386
+ if (!characters) return false;
387
+ buffer->characters = characters;
388
+ buffer->page_capacity = next;
389
+ }
390
+ while (buffer->page_count <= last) {
391
+ uint16_t *page = malloc((size_t)DIRECTIVE_PAGE_SIZE * sizeof(*page));
392
+ if (!page) return false;
393
+ buffer->characters[buffer->page_count++] = page;
394
+ }
395
+ return true;
396
+ }
397
+
398
+ // Only the two units of a character above U+FFFF form a surrogate pair. A decoding error is stored as U+FFFD with the
399
+ // recorded position `directive_append` gives it, and a lone surrogate as U+E000, which `preproc_word` and `pragma_space`
400
+ // classify alike, so that neither joins a neighbouring lone surrogate into a character the lexer did not read.
401
+ static void directive_encode(DirectiveBuffer *buffer, uint32_t c) {
402
+ if (c > 0x10ffff) directive_store(buffer, 0xfffd);
403
+ else if (c >= 0xd800 && c <= 0xdfff) directive_store(buffer, 0xe000);
404
+ else if (c <= 0xffff) directive_store(buffer, c);
405
+ else {
406
+ c -= 0x10000;
407
+ directive_store(buffer, 0xd800 | (c >> 10));
408
+ directive_store(buffer, 0xdc00 | (c & 0x3ff));
409
+ }
410
+ }
411
+
412
+ static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
413
+ return input->buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
414
+ }
415
+
416
+ static void directive_store(DirectiveBuffer *buffer, uint16_t c) {
417
+ buffer->characters[buffer->length >> DIRECTIVE_PAGE_SHIFT][buffer->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
418
+ buffer->length++;
419
+ }
420
+
421
+ static void directive_release(DirectiveBuffer *buffer) {
422
+ for (uint32_t i = 0; i < buffer->page_count; i++) free(buffer->characters[i]);
423
+ free(buffer->characters);
424
+ free(buffer->positions);
425
+ }
426
+
427
+ static unsigned directive_utf8_width(uint32_t c) {
428
+ return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
429
+ }
430
+
431
+ #endif