|
7 | 7 | # include "handles.h" |
8 | 8 | #endif |
9 | 9 |
|
10 | | -#include <stdio.h> |
| 10 | +static const Py_ssize_t HPYUNICODEBUILDER_INITIAL_CAPACITY = 5; |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + Py_ssize_t capacity; // allocated handles |
| 14 | + Py_ssize_t length; // used handles |
| 15 | + PyObject *list; |
| 16 | +} _PyUnicodeBuilder_s; |
| 17 | + |
| 18 | +#ifdef HPY_UNIVERSAL_ABI |
| 19 | +static inline _PyUnicodeBuilder_s *_hb2pb(HPyUnicodeBuilder ht) { |
| 20 | + return (_PyUnicodeBuilder_s *) (ht)._i; |
| 21 | +} |
| 22 | +static inline HPyUnicodeBuilder _pb2hb(_PyUnicodeBuilder_s *bp) { |
| 23 | + return (HPyUnicodeBuilder) {(HPy_ssize_t) (bp)}; |
| 24 | +} |
| 25 | +#else |
| 26 | +static inline _PyUnicodeBuilder_s *_hb2pb(HPyUnicodeBuilder ht) { |
| 27 | + return (_PyUnicodeBuilder_s *) (ht)._o; |
| 28 | +} |
| 29 | +static inline HPyUnicodeBuilder _pb2hb(_PyUnicodeBuilder_s *bp) { |
| 30 | + return (HPyUnicodeBuilder) {(void *) (bp)}; |
| 31 | +} |
| 32 | +#endif |
11 | 33 |
|
12 | 34 | _HPy_HIDDEN HPyUnicodeBuilder |
13 | | -ctx_UnicodeBuilder_New(HPyContext *ctx) |
| 35 | +ctx_UnicodeBuilder_New(HPyContext *ctx, HPy_ssize_t capacity) |
14 | 36 | { |
15 | | - PyObject *lst = PyList_New(0); |
16 | | - if (lst == NULL) |
17 | | - PyErr_Clear(); /* delay the MemoryError */ |
18 | | - return (HPyUnicodeBuilder){(HPy_ssize_t)lst }; |
| 37 | + _PyUnicodeBuilder_s *bp; |
| 38 | + if (capacity == 0) { |
| 39 | + capacity = HPYUNICODEBUILDER_INITIAL_CAPACITY; |
| 40 | + } |
| 41 | + capacity++; // always reserve space for an extra handle, see the docs, analogue to HPyTracker |
| 42 | + |
| 43 | + bp = malloc(sizeof(_PyUnicodeBuilder_s)); |
| 44 | + if (bp == NULL) { |
| 45 | + HPyErr_NoMemory(ctx); |
| 46 | + return _pb2hb(0); |
| 47 | + } |
| 48 | + |
| 49 | + bp->list = PyList_New(capacity); |
| 50 | + if (bp->list == NULL) { |
| 51 | + free(bp); |
| 52 | + HPyErr_NoMemory(ctx); |
| 53 | + return _pb2hb(0); |
| 54 | + } |
| 55 | + bp->capacity = capacity; |
| 56 | + bp->length = 0; |
| 57 | + return _pb2hb(bp); |
19 | 58 | } |
20 | 59 |
|
21 | | -_HPy_HIDDEN void |
22 | | -ctx_UnicodeBuilder_Append(HPyContext *ctx, HPyUnicodeBuilder builder, HPy h_item) |
| 60 | +_HPy_HIDDEN int |
| 61 | +ctx_UnicodeBuilder_Add(HPyContext *ctx, HPyUnicodeBuilder builder, HPy h_item) |
23 | 62 | { |
24 | | - PyObject *lst = (PyObject *)builder._lst; |
25 | | - if (lst != NULL) { |
26 | | - PyObject *item = _h2py(h_item); |
27 | | - PyList_Append(lst, item); |
| 63 | + if(!HPyUnicode_Check(ctx, h_item)) { |
| 64 | + HPyErr_SetString(ctx, ctx->h_TypeError, "Argument must be of type HPyUnicode"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + _PyUnicodeBuilder_s *bp = _hb2pb(builder); |
| 69 | + PyObject *item = _h2py(h_item); |
| 70 | + |
| 71 | + // XXX: For the initial PoC we don't care about reallocation |
| 72 | + if (bp->capacity <= bp->length) { |
| 73 | + return -1; |
28 | 74 | } |
| 75 | + Py_INCREF(item); |
| 76 | + PyList_SET_ITEM(bp->list, bp->length++, item); |
| 77 | + return 0; |
29 | 78 | } |
30 | 79 |
|
31 | 80 | _HPy_HIDDEN HPy |
32 | 81 | ctx_UnicodeBuilder_Build(HPyContext *ctx, HPyUnicodeBuilder builder) |
33 | 82 | { |
34 | | - PyObject *lst = (PyObject *)builder._lst; |
35 | | - if (lst == NULL) { |
36 | | - PyErr_NoMemory(); |
37 | | - return HPy_NULL; |
38 | | - } |
39 | | - builder._lst = 0; |
| 83 | + _PyUnicodeBuilder_s *bp = _hb2pb(builder); |
| 84 | + PyObject *list = PyList_GetSlice(bp->list, 0, bp->length); |
| 85 | + |
40 | 86 | PyObject *sep = PyUnicode_FromString(""); |
41 | | - PyObject *str = PyUnicode_Join(sep, lst); |
| 87 | + PyObject *str = PyUnicode_Join(sep, list); |
42 | 88 | Py_XDECREF(sep); |
| 89 | + |
43 | 90 | if(str == NULL) { |
44 | 91 | PyErr_NoMemory(); |
45 | 92 | return HPy_NULL; |
46 | 93 | } |
47 | | - Py_XDECREF(lst); |
| 94 | + |
| 95 | + Py_XDECREF(bp->list); |
| 96 | + Py_XDECREF(list); |
| 97 | + free(bp); |
48 | 98 | return _py2h(str); |
49 | 99 | } |
50 | 100 |
|
51 | 101 | _HPy_HIDDEN void |
52 | 102 | ctx_UnicodeBuilder_Cancel(HPyContext *ctx, HPyUnicodeBuilder builder) |
53 | 103 | { |
54 | | - PyObject *lst = (PyObject *)builder._lst; |
55 | | - if (lst == NULL) { |
56 | | - // we don't report the memory error here: the builder |
57 | | - // is being cancelled (so the result of the builder is not being used) |
58 | | - // and likely it's being cancelled during the handling of another error |
59 | | - return; |
60 | | - } |
61 | | - builder._lst = 0; |
62 | | - Py_XDECREF(lst); |
| 104 | + _PyUnicodeBuilder_s *bp = _hb2pb(builder); |
| 105 | + Py_XDECREF(bp->list); |
| 106 | + free(bp); |
63 | 107 | } |
0 commit comments