mirror of
https://github.com/python/cpython.git
synced 2025-08-25 03:04:55 +00:00
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:
parent
86384cf83f
commit
850687df47
6 changed files with 81 additions and 103 deletions
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue