diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-08-23 16:24:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-23 16:24:42 (GMT) |
commit | a6427cb2a279c4125dc26d468bcbb3374cdc5f41 (patch) | |
tree | 42d91048474cdef61df318428aab3c9be44629e3 /Python | |
parent | fe64ba611b587cf6e609679d40f0cdb8b7c93ca0 (diff) | |
download | cpython-a6427cb2a279c4125dc26d468bcbb3374cdc5f41.zip cpython-a6427cb2a279c4125dc26d468bcbb3374cdc5f41.tar.gz cpython-a6427cb2a279c4125dc26d468bcbb3374cdc5f41.tar.bz2 |
bpo-36763: Implement PyWideStringList_Insert() of PEP 587 (GH-15423)
(cherry picked from commit 3842f2997fbd4dc840986aad2bb94656815e243b)
Co-authored-by: Victor Stinner <vstinner@redhat.com>
Diffstat (limited to 'Python')
-rw-r--r-- | Python/initconfig.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c index dd8dd7a..8a6ad7c 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -297,26 +297,40 @@ _PyWideStringList_Copy(PyWideStringList *list, const PyWideStringList *list2) PyStatus -PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) +PyWideStringList_Insert(PyWideStringList *list, + Py_ssize_t index, const wchar_t *item) { - if (list->length == PY_SSIZE_T_MAX) { + Py_ssize_t len = list->length; + if (len == PY_SSIZE_T_MAX) { /* length+1 would overflow */ return _PyStatus_NO_MEMORY(); } + if (index < 0) { + return _PyStatus_ERR("PyWideStringList_Insert index must be >= 0"); + } + if (index > len) { + index = len; + } wchar_t *item2 = _PyMem_RawWcsdup(item); if (item2 == NULL) { return _PyStatus_NO_MEMORY(); } - size_t size = (list->length + 1) * sizeof(list->items[0]); + size_t size = (len + 1) * sizeof(list->items[0]); wchar_t **items2 = (wchar_t **)PyMem_RawRealloc(list->items, size); if (items2 == NULL) { PyMem_RawFree(item2); return _PyStatus_NO_MEMORY(); } - items2[list->length] = item2; + if (index < len) { + memmove(&items2[index + 1], + &items2[index], + (len - index) * sizeof(items2[0])); + } + + items2[index] = item2; list->items = items2; list->length++; return _PyStatus_OK(); @@ -324,6 +338,13 @@ PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) PyStatus +PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) +{ + return PyWideStringList_Insert(list, list->length, item); +} + + +PyStatus _PyWideStringList_Extend(PyWideStringList *list, const PyWideStringList *list2) { for (Py_ssize_t i = 0; i < list2->length; i++) { |