Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1 | /* stringlib: bytes joining implementation */ |
| 2 | |
| 3 | #if STRINGLIB_SIZEOF_CHAR != 1 |
| 4 | #error join.h only compatible with byte-wise strings |
| 5 | #endif |
| 6 | |
| 7 | Py_LOCAL_INLINE(PyObject *) |
| 8 | STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable) |
| 9 | { |
| 10 | char *sepstr = STRINGLIB_STR(sep); |
| 11 | const Py_ssize_t seplen = STRINGLIB_LEN(sep); |
| 12 | PyObject *res = NULL; |
| 13 | char *p; |
| 14 | Py_ssize_t seqlen = 0; |
| 15 | Py_ssize_t sz = 0; |
| 16 | Py_ssize_t i, nbufs; |
| 17 | PyObject *seq, *item; |
| 18 | Py_buffer *buffers = NULL; |
| 19 | #define NB_STATIC_BUFFERS 10 |
| 20 | Py_buffer static_buffers[NB_STATIC_BUFFERS]; |
| 21 | |
| 22 | seq = PySequence_Fast(iterable, "can only join an iterable"); |
| 23 | if (seq == NULL) { |
| 24 | return NULL; |
| 25 | } |
| 26 | |
| 27 | seqlen = PySequence_Fast_GET_SIZE(seq); |
| 28 | if (seqlen == 0) { |
| 29 | Py_DECREF(seq); |
| 30 | return STRINGLIB_NEW(NULL, 0); |
| 31 | } |
| 32 | #ifndef STRINGLIB_MUTABLE |
| 33 | if (seqlen == 1) { |
| 34 | item = PySequence_Fast_GET_ITEM(seq, 0); |
| 35 | if (STRINGLIB_CHECK_EXACT(item)) { |
| 36 | Py_INCREF(item); |
| 37 | Py_DECREF(seq); |
| 38 | return item; |
| 39 | } |
| 40 | } |
| 41 | #endif |
| 42 | if (seqlen > NB_STATIC_BUFFERS) { |
| 43 | buffers = PyMem_NEW(Py_buffer, seqlen); |
| 44 | if (buffers == NULL) { |
| 45 | Py_DECREF(seq); |
Christian Heimes | 5f7e8da | 2012-12-02 07:56:42 +0100 | [diff] [blame] | 46 | PyErr_NoMemory(); |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
| 49 | } |
| 50 | else { |
| 51 | buffers = static_buffers; |
| 52 | } |
| 53 | |
| 54 | /* Here is the general case. Do a pre-pass to figure out the total |
| 55 | * amount of space we'll need (sz), and see whether all arguments are |
| 56 | * buffer-compatible. |
| 57 | */ |
| 58 | for (i = 0, nbufs = 0; i < seqlen; i++) { |
| 59 | Py_ssize_t itemlen; |
| 60 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 61 | if (_getbuffer(item, &buffers[i]) < 0) { |
| 62 | PyErr_Format(PyExc_TypeError, |
| 63 | "sequence item %zd: expected bytes, bytearray, " |
| 64 | "or an object with the buffer interface, %.80s found", |
| 65 | i, Py_TYPE(item)->tp_name); |
| 66 | goto error; |
| 67 | } |
| 68 | nbufs = i + 1; /* for error cleanup */ |
| 69 | itemlen = buffers[i].len; |
| 70 | if (itemlen > PY_SSIZE_T_MAX - sz) { |
| 71 | PyErr_SetString(PyExc_OverflowError, |
| 72 | "join() result is too long"); |
| 73 | goto error; |
| 74 | } |
| 75 | sz += itemlen; |
| 76 | if (i != 0) { |
| 77 | if (seplen > PY_SSIZE_T_MAX - sz) { |
| 78 | PyErr_SetString(PyExc_OverflowError, |
| 79 | "join() result is too long"); |
| 80 | goto error; |
| 81 | } |
| 82 | sz += seplen; |
| 83 | } |
| 84 | if (seqlen != PySequence_Fast_GET_SIZE(seq)) { |
| 85 | PyErr_SetString(PyExc_RuntimeError, |
| 86 | "sequence changed size during iteration"); |
| 87 | goto error; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Allocate result space. */ |
| 92 | res = STRINGLIB_NEW(NULL, sz); |
| 93 | if (res == NULL) |
| 94 | goto error; |
| 95 | |
| 96 | /* Catenate everything. */ |
| 97 | p = STRINGLIB_STR(res); |
Antoine Pitrou | 6f7b0da | 2012-10-20 23:08:34 +0200 | [diff] [blame] | 98 | if (!seplen) { |
| 99 | /* fast path */ |
| 100 | for (i = 0; i < nbufs; i++) { |
| 101 | Py_ssize_t n = buffers[i].len; |
| 102 | char *q = buffers[i].buf; |
| 103 | Py_MEMCPY(p, q, n); |
| 104 | p += n; |
| 105 | } |
| 106 | goto done; |
| 107 | } |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 108 | for (i = 0; i < nbufs; i++) { |
| 109 | Py_ssize_t n; |
| 110 | char *q; |
| 111 | if (i) { |
| 112 | Py_MEMCPY(p, sepstr, seplen); |
| 113 | p += seplen; |
| 114 | } |
| 115 | n = buffers[i].len; |
| 116 | q = buffers[i].buf; |
| 117 | Py_MEMCPY(p, q, n); |
| 118 | p += n; |
| 119 | } |
| 120 | goto done; |
| 121 | |
| 122 | error: |
| 123 | res = NULL; |
| 124 | done: |
| 125 | Py_DECREF(seq); |
| 126 | for (i = 0; i < nbufs; i++) |
| 127 | PyBuffer_Release(&buffers[i]); |
| 128 | if (buffers != static_buffers) |
| 129 | PyMem_FREE(buffers); |
| 130 | return res; |
| 131 | } |
| 132 | |
| 133 | #undef NB_STATIC_BUFFERS |