flexablelib 2.0.18__cp314-cp314-win_amd64.whl

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,6 @@
1
+ from .table import Table
2
+ from .style_compositions import TableComposition, SeparatorLine
3
+
4
+ # Names, not the objects themselves: `from flexablelib import *` walks this
5
+ # list and raises TypeError on anything that is not a string.
6
+ __all__ = ['Table', 'TableComposition', 'SeparatorLine']
@@ -0,0 +1,475 @@
1
+ /*
2
+ * C implementations of the hot paths in flexablelib.
3
+ *
4
+ * Rendering a table measures every cell several times: once per pass of the
5
+ * two-pass width computation, again while aligning, and again for each wrapped
6
+ * sub-row. On a table of any size, measurement dominates the runtime, and in
7
+ * pure Python it costs a function call and a dict lookup per character.
8
+ *
9
+ * This module reimplements the three hottest operations -- visible width,
10
+ * escape stripping, and padding -- over CPython's native string buffer, with
11
+ * no per-character allocation and a binary search over baked-in Unicode range
12
+ * tables instead of unicodedata lookups.
13
+ *
14
+ * The behaviour must match flexablelib/text_width.py exactly. That module is
15
+ * the reference and the fallback when this extension is unavailable; the two
16
+ * are exercised against each other by the test suite.
17
+ */
18
+
19
+ #define PY_SSIZE_T_CLEAN
20
+ #include <Python.h>
21
+
22
+ #include "_width_tables.h"
23
+
24
+ /* Codepoints that terminate or delimit escape sequences. */
25
+ #define ESC 0x1B
26
+ #define BEL 0x07
27
+
28
+ /*
29
+ * U+FE0F, the emoji presentation selector. It has no width of its own; what
30
+ * it does is make the character in front of it render as an emoji, which is
31
+ * two columns wide even where the bare character is one. `⚠` is one column,
32
+ * `⚠️` is two. See measured_clusters() in text_width.py -- the two
33
+ * implementations must agree on this or a status column drifts by a column
34
+ * per emoji depending on whether the extension was built.
35
+ */
36
+ #define VS16 0xFE0F
37
+
38
+ /* Py_NewRef arrived in 3.10; the package supports 3.8 upward. */
39
+ #if PY_VERSION_HEX < 0x030A0000
40
+ static inline PyObject *
41
+ Py_NewRef(PyObject *object)
42
+ {
43
+ Py_INCREF(object);
44
+ return object;
45
+ }
46
+ #endif
47
+
48
+
49
+ /*
50
+ * Is the codepoint inside one of the sorted, non-overlapping ranges?
51
+ *
52
+ * The tables are generated in ascending order by tools/generate_width_tables.py,
53
+ * which is what makes the binary search valid.
54
+ */
55
+ static int
56
+ in_ranges(const struct width_range *table, size_t count, Py_UCS4 codepoint)
57
+ {
58
+ size_t low = 0;
59
+ size_t high = count;
60
+
61
+ while (low < high) {
62
+ size_t middle = low + (high - low) / 2;
63
+ if (codepoint < table[middle].start) {
64
+ high = middle;
65
+ }
66
+ else if (codepoint > table[middle].end) {
67
+ low = middle + 1;
68
+ }
69
+ else {
70
+ return 1;
71
+ }
72
+ }
73
+ return 0;
74
+ }
75
+
76
+
77
+ /* Terminal columns occupied by one codepoint: 0, 1, or 2. */
78
+ static int
79
+ codepoint_width(Py_UCS4 codepoint)
80
+ {
81
+ /* Printable ASCII is the overwhelming majority of real table data and is
82
+ * always one column. Checking it first skips both binary searches. */
83
+ if (codepoint >= 0x20 && codepoint < 0x7F) {
84
+ return 1;
85
+ }
86
+ if (in_ranges(zero_width_ranges, ZERO_WIDTH_RANGES_COUNT, codepoint)) {
87
+ return 0;
88
+ }
89
+ if (in_ranges(wide_ranges, WIDE_RANGES_COUNT, codepoint)) {
90
+ return 2;
91
+ }
92
+ return 1;
93
+ }
94
+
95
+
96
+ /*
97
+ * Length of the escape sequence starting at `start`, or 0 if there is none.
98
+ *
99
+ * Recognises the same three families as the regex in text_width.py:
100
+ * CSI ESC [ params(0x30-0x3F)* intermediates(0x20-0x2F)* final(0x40-0x7E)
101
+ * OSC ESC ] ... terminated by BEL or by ESC backslash
102
+ * Fe ESC followed by one byte in 0x40-0x5F
103
+ *
104
+ * OSC is tested before the two-byte form because ']' (0x5D) falls inside the
105
+ * Fe range; matching it as a bare two-byte escape would leave the hyperlink
106
+ * payload to be counted as visible text.
107
+ *
108
+ * An unterminated sequence returns 0, so the lone ESC is measured as an
109
+ * ordinary character rather than swallowing the rest of the string.
110
+ */
111
+ static Py_ssize_t
112
+ ansi_sequence_length(int kind, const void *data, Py_ssize_t length,
113
+ Py_ssize_t start)
114
+ {
115
+ Py_ssize_t position;
116
+ Py_UCS4 next;
117
+
118
+ if (start + 1 >= length) {
119
+ return 0;
120
+ }
121
+
122
+ next = PyUnicode_READ(kind, data, start + 1);
123
+
124
+ if (next == ']') {
125
+ position = start + 2;
126
+ while (position < length) {
127
+ Py_UCS4 current = PyUnicode_READ(kind, data, position);
128
+ if (current == BEL) {
129
+ return position - start + 1;
130
+ }
131
+ if (current == ESC) {
132
+ if (position + 1 < length
133
+ && PyUnicode_READ(kind, data, position + 1) == '\\') {
134
+ return position - start + 2;
135
+ }
136
+ break;
137
+ }
138
+ position++;
139
+ }
140
+ /* Unterminated OSC. ']' is 0x5D, inside the two-byte escape range, so
141
+ * the reference regex falls through to that alternative and consumes
142
+ * just ESC ']'. Match it, or the two implementations disagree on how
143
+ * much of a malformed sequence is visible. */
144
+ return 2;
145
+ }
146
+
147
+ if (next == '[') {
148
+ position = start + 2;
149
+ while (position < length) {
150
+ Py_UCS4 current = PyUnicode_READ(kind, data, position);
151
+ if (current < 0x30 || current > 0x3F) {
152
+ break;
153
+ }
154
+ position++;
155
+ }
156
+ while (position < length) {
157
+ Py_UCS4 current = PyUnicode_READ(kind, data, position);
158
+ if (current < 0x20 || current > 0x2F) {
159
+ break;
160
+ }
161
+ position++;
162
+ }
163
+ if (position < length) {
164
+ Py_UCS4 final_byte = PyUnicode_READ(kind, data, position);
165
+ if (final_byte >= 0x40 && final_byte <= 0x7E) {
166
+ return position - start + 1;
167
+ }
168
+ }
169
+ return 0;
170
+ }
171
+
172
+ if (next >= 0x40 && next <= 0x5F) {
173
+ return 2;
174
+ }
175
+
176
+ return 0;
177
+ }
178
+
179
+
180
+ /* Shared prologue: validate the argument and expose its buffer. */
181
+ static int
182
+ unpack_string(PyObject *object, int *kind, const void **data,
183
+ Py_ssize_t *length)
184
+ {
185
+ if (!PyUnicode_Check(object)) {
186
+ PyErr_Format(PyExc_TypeError, "expected str, got %.100s",
187
+ Py_TYPE(object)->tp_name);
188
+ return -1;
189
+ }
190
+ #if PY_VERSION_HEX < 0x030C0000
191
+ if (PyUnicode_READY(object) < 0) {
192
+ return -1;
193
+ }
194
+ #endif
195
+ *kind = PyUnicode_KIND(object);
196
+ *data = PyUnicode_DATA(object);
197
+ *length = PyUnicode_GET_LENGTH(object);
198
+ return 0;
199
+ }
200
+
201
+
202
+ static Py_ssize_t
203
+ measure(int kind, const void *data, Py_ssize_t length)
204
+ {
205
+ Py_ssize_t total = 0;
206
+ Py_ssize_t position = 0;
207
+ /* Width of the last printable codepoint, so an emoji presentation
208
+ * selector can promote it. Zero means there is nothing to promote --
209
+ * either nothing has been seen yet, or it was already two columns. */
210
+ int previous_width = 0;
211
+
212
+ while (position < length) {
213
+ Py_UCS4 codepoint = PyUnicode_READ(kind, data, position);
214
+ int width;
215
+
216
+ if (codepoint == ESC) {
217
+ Py_ssize_t skip = ansi_sequence_length(kind, data, length, position);
218
+ if (skip > 0) {
219
+ position += skip;
220
+ continue;
221
+ }
222
+ }
223
+
224
+ if (codepoint == VS16) {
225
+ if (previous_width == 1) {
226
+ total += 1;
227
+ previous_width = 2;
228
+ }
229
+ position++;
230
+ continue;
231
+ }
232
+
233
+ width = codepoint_width(codepoint);
234
+ total += width;
235
+ previous_width = width;
236
+ position++;
237
+ }
238
+ return total;
239
+ }
240
+
241
+
242
+ PyDoc_STRVAR(visible_width_doc,
243
+ "visible_width(text, /)\n--\n\n"
244
+ "Terminal columns occupied by the string, ignoring ANSI escape sequences\n"
245
+ "and counting East Asian Wide characters as two columns.");
246
+
247
+ static PyObject *
248
+ py_visible_width(PyObject *Py_UNUSED(module), PyObject *argument)
249
+ {
250
+ int kind;
251
+ const void *data;
252
+ Py_ssize_t length;
253
+
254
+ if (unpack_string(argument, &kind, &data, &length) < 0) {
255
+ return NULL;
256
+ }
257
+ return PyLong_FromSsize_t(measure(kind, data, length));
258
+ }
259
+
260
+
261
+ PyDoc_STRVAR(strip_ansi_doc,
262
+ "strip_ansi(text, /)\n--\n\n"
263
+ "Return the string with every ANSI escape sequence removed.");
264
+
265
+ static PyObject *
266
+ py_strip_ansi(PyObject *Py_UNUSED(module), PyObject *argument)
267
+ {
268
+ int kind;
269
+ const void *data;
270
+ Py_ssize_t length;
271
+ Py_ssize_t position = 0;
272
+ Py_ssize_t written = 0;
273
+ Py_UCS4 *buffer;
274
+ PyObject *result;
275
+
276
+ if (unpack_string(argument, &kind, &data, &length) < 0) {
277
+ return NULL;
278
+ }
279
+
280
+ /* Nothing to strip is the common case; hand back the original object
281
+ * rather than allocating a copy. */
282
+ for (position = 0; position < length; position++) {
283
+ if (PyUnicode_READ(kind, data, position) == ESC) {
284
+ break;
285
+ }
286
+ }
287
+ if (position == length) {
288
+ return Py_NewRef(argument);
289
+ }
290
+ position = 0;
291
+
292
+ /* The output can only be shorter, so one allocation at input size is
293
+ * enough and no growth check is needed in the loop. */
294
+ buffer = PyMem_New(Py_UCS4, (size_t)length ? (size_t)length : 1);
295
+ if (buffer == NULL) {
296
+ return PyErr_NoMemory();
297
+ }
298
+
299
+ while (position < length) {
300
+ Py_UCS4 codepoint = PyUnicode_READ(kind, data, position);
301
+ if (codepoint == ESC) {
302
+ Py_ssize_t skip = ansi_sequence_length(kind, data, length, position);
303
+ if (skip > 0) {
304
+ position += skip;
305
+ continue;
306
+ }
307
+ }
308
+ buffer[written++] = codepoint;
309
+ position++;
310
+ }
311
+
312
+ result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, written);
313
+ PyMem_Free(buffer);
314
+ return result;
315
+ }
316
+
317
+
318
+ PyDoc_STRVAR(pad_to_width_doc,
319
+ "pad_to_width(text, width, align='l', fill=' ', /)\n--\n\n"
320
+ "Pad the string to an exact visible width. `align` is 'l', 'r', or 'c'.\n"
321
+ "A string already at or beyond the width is returned unchanged.");
322
+
323
+ static PyObject *
324
+ py_pad_to_width(PyObject *Py_UNUSED(module), PyObject *args)
325
+ {
326
+ PyObject *text;
327
+ PyObject *fill = NULL;
328
+ Py_ssize_t width;
329
+ const char *align = "l";
330
+ int kind;
331
+ const void *data;
332
+ Py_ssize_t length;
333
+ Py_ssize_t deficit;
334
+ PyObject *left_pad = NULL;
335
+ PyObject *right_pad = NULL;
336
+ PyObject *joined = NULL;
337
+ PyObject *result = NULL;
338
+
339
+ if (!PyArg_ParseTuple(args, "On|sU", &text, &width, &align, &fill)) {
340
+ return NULL;
341
+ }
342
+ if (unpack_string(text, &kind, &data, &length) < 0) {
343
+ return NULL;
344
+ }
345
+
346
+ deficit = width - measure(kind, data, length);
347
+ if (deficit <= 0) {
348
+ return Py_NewRef(text);
349
+ }
350
+
351
+ if (fill == NULL) {
352
+ fill = PyUnicode_FromString(" ");
353
+ if (fill == NULL) {
354
+ return NULL;
355
+ }
356
+ }
357
+ else {
358
+ Py_INCREF(fill);
359
+ }
360
+
361
+ if (align[0] == 'r') {
362
+ left_pad = PySequence_Repeat(fill, deficit);
363
+ if (left_pad == NULL) {
364
+ goto done;
365
+ }
366
+ result = PyUnicode_Concat(left_pad, text);
367
+ }
368
+ else if (align[0] == 'c') {
369
+ Py_ssize_t left_count = deficit / 2;
370
+ left_pad = PySequence_Repeat(fill, left_count);
371
+ right_pad = PySequence_Repeat(fill, deficit - left_count);
372
+ if (left_pad == NULL || right_pad == NULL) {
373
+ goto done;
374
+ }
375
+ joined = PyUnicode_Concat(left_pad, text);
376
+ if (joined == NULL) {
377
+ goto done;
378
+ }
379
+ result = PyUnicode_Concat(joined, right_pad);
380
+ }
381
+ else {
382
+ right_pad = PySequence_Repeat(fill, deficit);
383
+ if (right_pad == NULL) {
384
+ goto done;
385
+ }
386
+ result = PyUnicode_Concat(text, right_pad);
387
+ }
388
+
389
+ done:
390
+ Py_XDECREF(left_pad);
391
+ Py_XDECREF(right_pad);
392
+ Py_XDECREF(joined);
393
+ Py_DECREF(fill);
394
+ return result;
395
+ }
396
+
397
+
398
+ PyDoc_STRVAR(widths_of_doc,
399
+ "widths_of(strings, /)\n--\n\n"
400
+ "Visible width of every string in the sequence, as a list of ints.\n"
401
+ "Measuring a whole column in one call avoids the per-item interpreter\n"
402
+ "overhead that dominates when this is driven from a Python loop.");
403
+
404
+ static PyObject *
405
+ py_widths_of(PyObject *Py_UNUSED(module), PyObject *sequence)
406
+ {
407
+ PyObject *fast;
408
+ PyObject *result;
409
+ Py_ssize_t count;
410
+ Py_ssize_t index;
411
+
412
+ fast = PySequence_Fast(sequence, "expected an iterable of str");
413
+ if (fast == NULL) {
414
+ return NULL;
415
+ }
416
+ count = PySequence_Fast_GET_SIZE(fast);
417
+ result = PyList_New(count);
418
+ if (result == NULL) {
419
+ Py_DECREF(fast);
420
+ return NULL;
421
+ }
422
+
423
+ for (index = 0; index < count; index++) {
424
+ PyObject *item = PySequence_Fast_GET_ITEM(fast, index);
425
+ int kind;
426
+ const void *data;
427
+ Py_ssize_t length;
428
+ PyObject *value;
429
+
430
+ if (unpack_string(item, &kind, &data, &length) < 0) {
431
+ Py_DECREF(result);
432
+ Py_DECREF(fast);
433
+ return NULL;
434
+ }
435
+ value = PyLong_FromSsize_t(measure(kind, data, length));
436
+ if (value == NULL) {
437
+ Py_DECREF(result);
438
+ Py_DECREF(fast);
439
+ return NULL;
440
+ }
441
+ PyList_SET_ITEM(result, index, value);
442
+ }
443
+
444
+ Py_DECREF(fast);
445
+ return result;
446
+ }
447
+
448
+
449
+ static PyMethodDef module_methods[] = {
450
+ {"visible_width", py_visible_width, METH_O, visible_width_doc},
451
+ {"strip_ansi", py_strip_ansi, METH_O, strip_ansi_doc},
452
+ {"pad_to_width", py_pad_to_width, METH_VARARGS, pad_to_width_doc},
453
+ {"widths_of", py_widths_of, METH_O, widths_of_doc},
454
+ {NULL, NULL, 0, NULL}
455
+ };
456
+
457
+ PyDoc_STRVAR(module_doc,
458
+ "Accelerated text measurement for flexablelib.\n\n"
459
+ "Import flexablelib.fast rather than this module directly; it falls back to\n"
460
+ "the pure-Python implementation when the extension is not built.");
461
+
462
+ static struct PyModuleDef speedups_module = {
463
+ PyModuleDef_HEAD_INIT,
464
+ "flexablelib._speedups",
465
+ module_doc,
466
+ -1,
467
+ module_methods,
468
+ NULL, NULL, NULL, NULL
469
+ };
470
+
471
+ PyMODINIT_FUNC
472
+ PyInit__speedups(void)
473
+ {
474
+ return PyModule_Create(&speedups_module);
475
+ }