mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +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
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#include "Python.h"
|
||||
#include "pycore_abstract.h" // _PyIndex_Check()
|
||||
#include "pycore_bytesobject.h" // _PyBytes_Find()
|
||||
#include "pycore_bytesobject.h" // _PyBytes_Find(), _PyBytes_Repeat()
|
||||
#include "pycore_bytes_methods.h" // _Py_bytes_startswith()
|
||||
#include "pycore_call.h" // _PyObject_CallNoArgs()
|
||||
#include "pycore_format.h" // F_LJUST
|
||||
|
|
@ -1421,8 +1421,6 @@ bytes_concat(PyObject *a, PyObject *b)
|
|||
static PyObject *
|
||||
bytes_repeat(PyBytesObject *a, Py_ssize_t n)
|
||||
{
|
||||
Py_ssize_t i;
|
||||
Py_ssize_t j;
|
||||
Py_ssize_t size;
|
||||
PyBytesObject *op;
|
||||
size_t nbytes;
|
||||
|
|
@ -1457,20 +1455,9 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
|||
op->ob_shash = -1;
|
||||
_Py_COMP_DIAG_POP
|
||||
op->ob_sval[size] = '\0';
|
||||
if (Py_SIZE(a) == 1 && n > 0) {
|
||||
memset(op->ob_sval, a->ob_sval[0] , n);
|
||||
return (PyObject *) op;
|
||||
}
|
||||
i = 0;
|
||||
if (i < size) {
|
||||
memcpy(op->ob_sval, a->ob_sval, Py_SIZE(a));
|
||||
i = Py_SIZE(a);
|
||||
}
|
||||
while (i < size) {
|
||||
j = (i <= size-i) ? i : size-i;
|
||||
memcpy(op->ob_sval+i, op->ob_sval, j);
|
||||
i += j;
|
||||
}
|
||||
|
||||
_PyBytes_Repeat(op->ob_sval, size, a->ob_sval, Py_SIZE(a));
|
||||
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
|
|
@ -3528,3 +3515,28 @@ _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr,
|
|||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
_PyBytes_Repeat(char* dest, Py_ssize_t len_dest,
|
||||
const char* src, Py_ssize_t len_src)
|
||||
{
|
||||
if (len_dest == 0) {
|
||||
return;
|
||||
}
|
||||
if (len_src == 1) {
|
||||
memset(dest, src[0], len_dest);
|
||||
}
|
||||
else {
|
||||
if (src != dest) {
|
||||
memcpy(dest, src, len_src);
|
||||
}
|
||||
Py_ssize_t copied = len_src;
|
||||
while (copied < len_dest) {
|
||||
Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
|
||||
memcpy(dest + copied, dest, bytes_to_copy);
|
||||
copied += bytes_to_copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue