mirror of
https://github.com/python/cpython.git
synced 2025-07-15 23:35:23 +00:00
Reuse the stringlib in findchar(), and make its signature more convenient
This commit is contained in:
parent
c198d0599b
commit
f0b934b01a
1 changed files with 41 additions and 39 deletions
|
@ -519,36 +519,45 @@ make_bloom_mask(int kind, void* ptr, Py_ssize_t len)
|
||||||
#include "stringlib/localeutil.h"
|
#include "stringlib/localeutil.h"
|
||||||
#include "stringlib/undef.h"
|
#include "stringlib/undef.h"
|
||||||
|
|
||||||
|
#include "stringlib/unicodedefs.h"
|
||||||
|
#include "stringlib/fastsearch.h"
|
||||||
|
#include "stringlib/count.h"
|
||||||
|
#include "stringlib/find.h"
|
||||||
|
|
||||||
/* --- Unicode Object ----------------------------------------------------- */
|
/* --- Unicode Object ----------------------------------------------------- */
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s));
|
fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s));
|
||||||
|
|
||||||
Py_LOCAL_INLINE(char *) findchar(void *s, int kind,
|
Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind,
|
||||||
Py_ssize_t size, Py_UCS4 ch,
|
Py_ssize_t size, Py_UCS4 ch,
|
||||||
int direction)
|
int direction)
|
||||||
{
|
{
|
||||||
/* like wcschr, but doesn't stop at NULL characters */
|
int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH;
|
||||||
Py_ssize_t i;
|
|
||||||
if (kind == 1) {
|
switch (kind) {
|
||||||
if (direction == 1)
|
case PyUnicode_1BYTE_KIND:
|
||||||
return memchr(s, ch, size);
|
{
|
||||||
#ifdef HAVE_MEMRCHR
|
Py_UCS1 ch1 = (Py_UCS1) ch;
|
||||||
else
|
if (ch1 == ch)
|
||||||
return memrchr(s, ch, size);
|
return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode);
|
||||||
#endif
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
case PyUnicode_2BYTE_KIND:
|
||||||
|
{
|
||||||
|
Py_UCS2 ch2 = (Py_UCS2) ch;
|
||||||
|
if (ch2 == ch)
|
||||||
|
return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode);
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
case PyUnicode_4BYTE_KIND:
|
||||||
|
return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode);
|
||||||
|
default:
|
||||||
|
assert(0);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
if (direction == 1) {
|
|
||||||
for(i = 0; i < size; i++)
|
|
||||||
if (PyUnicode_READ(kind, s, i) == ch)
|
|
||||||
return (char*)s + kind * i;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for(i = size-1; i >= 0; i--)
|
|
||||||
if (PyUnicode_READ(kind, s, i) == ch)
|
|
||||||
return (char*)s + kind * i;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
|
@ -3311,7 +3320,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
|
if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
|
||||||
PyUnicode_GET_LENGTH(output), 0, 1)) {
|
PyUnicode_GET_LENGTH(output), 0, 1) >= 0) {
|
||||||
PyErr_SetString(PyExc_TypeError, "embedded NUL character");
|
PyErr_SetString(PyExc_TypeError, "embedded NUL character");
|
||||||
Py_DECREF(output);
|
Py_DECREF(output);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -8638,12 +8647,6 @@ _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#include "stringlib/unicodedefs.h"
|
|
||||||
#include "stringlib/fastsearch.h"
|
|
||||||
|
|
||||||
#include "stringlib/count.h"
|
|
||||||
#include "stringlib/find.h"
|
|
||||||
|
|
||||||
/* helper macro to fixup start/end slice values */
|
/* helper macro to fixup start/end slice values */
|
||||||
#define ADJUST_INDICES(start, end, len) \
|
#define ADJUST_INDICES(start, end, len) \
|
||||||
if (end > len) \
|
if (end > len) \
|
||||||
|
@ -8779,8 +8782,8 @@ PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
|
||||||
Py_ssize_t start, Py_ssize_t end,
|
Py_ssize_t start, Py_ssize_t end,
|
||||||
int direction)
|
int direction)
|
||||||
{
|
{
|
||||||
char *result;
|
|
||||||
int kind;
|
int kind;
|
||||||
|
Py_ssize_t result;
|
||||||
if (PyUnicode_READY(str) == -1)
|
if (PyUnicode_READY(str) == -1)
|
||||||
return -2;
|
return -2;
|
||||||
if (start < 0 || end < 0) {
|
if (start < 0 || end < 0) {
|
||||||
|
@ -8790,13 +8793,12 @@ PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
|
||||||
if (end > PyUnicode_GET_LENGTH(str))
|
if (end > PyUnicode_GET_LENGTH(str))
|
||||||
end = PyUnicode_GET_LENGTH(str);
|
end = PyUnicode_GET_LENGTH(str);
|
||||||
kind = PyUnicode_KIND(str);
|
kind = PyUnicode_KIND(str);
|
||||||
result = findchar(PyUnicode_1BYTE_DATA(str)
|
result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start,
|
||||||
+ kind*start,
|
kind, end-start, ch, direction);
|
||||||
kind,
|
if (result == -1)
|
||||||
end-start, ch, direction);
|
|
||||||
if (!result)
|
|
||||||
return -1;
|
return -1;
|
||||||
return (result-(char*)PyUnicode_DATA(str)) >> (kind-1);
|
else
|
||||||
|
return start + result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -9707,8 +9709,8 @@ replace(PyObject *self, PyObject *str1,
|
||||||
Py_UCS4 u1, u2;
|
Py_UCS4 u1, u2;
|
||||||
int rkind;
|
int rkind;
|
||||||
u1 = PyUnicode_READ_CHAR(str1, 0);
|
u1 = PyUnicode_READ_CHAR(str1, 0);
|
||||||
if (!findchar(sbuf, PyUnicode_KIND(self),
|
if (findchar(sbuf, PyUnicode_KIND(self),
|
||||||
slen, u1, 1))
|
slen, u1, 1) < 0)
|
||||||
goto nothing;
|
goto nothing;
|
||||||
u2 = PyUnicode_READ_CHAR(str2, 0);
|
u2 = PyUnicode_READ_CHAR(str2, 0);
|
||||||
u = PyUnicode_New(slen, maxchar);
|
u = PyUnicode_New(slen, maxchar);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue