diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-08-23 15:57:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-23 15:57:54 (GMT) |
commit | 3842f2997fbd4dc840986aad2bb94656815e243b (patch) | |
tree | 7de805acc44dbbcbdf8364a14632d4fc6c2302a8 /Python | |
parent | 8e76c456226438f2e4931ce7baf05ac8faae34a1 (diff) | |
download | cpython-3842f2997fbd4dc840986aad2bb94656815e243b.zip cpython-3842f2997fbd4dc840986aad2bb94656815e243b.tar.gz cpython-3842f2997fbd4dc840986aad2bb94656815e243b.tar.bz2 |
bpo-36763: Implement PyWideStringList_Insert() of PEP 587 (GH-15423)
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 5bd7d4f..b706f4c 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++) { |