bpo-47070: Add _PyBytes_Repeat() (GH-31999)

Use it where appropriate: the repeat functions of `array.array`, `bytes`, `bytearray`, and `str`.
This commit is contained in:
Pieter Eendebak 2022-03-28 10:43:45 +02:00 committed by GitHub
parent 86384cf83f
commit 850687df47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 103 deletions

View file

@ -42,6 +42,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_atomic_funcs.h" // _Py_atomic_size_get()
#include "pycore_bytesobject.h" // _PyBytes_Repeat()
#include "pycore_bytes_methods.h" // _Py_bytes_lower()
#include "pycore_format.h" // F_LJUST
#include "pycore_initconfig.h" // _PyStatus_OK()
@ -12782,17 +12783,10 @@ unicode_repeat(PyObject *str, Py_ssize_t len)
}
}
else {
/* number of characters copied this far */
Py_ssize_t done = PyUnicode_GET_LENGTH(str);
Py_ssize_t char_size = PyUnicode_KIND(str);
char *to = (char *) PyUnicode_DATA(u);
memcpy(to, PyUnicode_DATA(str),
PyUnicode_GET_LENGTH(str) * char_size);
while (done < nchars) {
n = (done <= nchars-done) ? done : nchars-done;
memcpy(to + (done * char_size), to, n * char_size);
done += n;
}
_PyBytes_Repeat(to, nchars * char_size, PyUnicode_DATA(str),
PyUnicode_GET_LENGTH(str) * char_size);
}
assert(_PyUnicode_CheckConsistency(u, 1));