mirror of https://github.com/python/cpython.git
Make ssize_t-clean.
This commit is contained in:
parent
21dd1afde7
commit
5c97c798d2
|
@ -1,5 +1,6 @@
|
||||||
/* strop module */
|
/* strop module */
|
||||||
|
|
||||||
|
#define PY_SSIZE_T_CLEAN
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
@ -26,10 +27,11 @@ PyDoc_STRVAR(strop_module__doc__,
|
||||||
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
split_whitespace(char *s, int len, int maxsplit)
|
split_whitespace(char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
||||||
{
|
{
|
||||||
int i = 0, j, err;
|
Py_ssize_t i = 0, j;
|
||||||
int countsplit = 0;
|
int err;
|
||||||
|
Py_ssize_t countsplit = 0;
|
||||||
PyObject* item;
|
PyObject* item;
|
||||||
PyObject *list = PyList_New(0);
|
PyObject *list = PyList_New(0);
|
||||||
|
|
||||||
|
@ -45,7 +47,7 @@ split_whitespace(char *s, int len, int maxsplit)
|
||||||
i = i+1;
|
i = i+1;
|
||||||
}
|
}
|
||||||
if (j < i) {
|
if (j < i) {
|
||||||
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
item = PyString_FromStringAndSize(s+j, i-j);
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
goto finally;
|
goto finally;
|
||||||
|
|
||||||
|
@ -60,7 +62,7 @@ split_whitespace(char *s, int len, int maxsplit)
|
||||||
}
|
}
|
||||||
if (maxsplit && (countsplit >= maxsplit) && i < len) {
|
if (maxsplit && (countsplit >= maxsplit) && i < len) {
|
||||||
item = PyString_FromStringAndSize(
|
item = PyString_FromStringAndSize(
|
||||||
s+i, (int)(len - i));
|
s+i, len - i);
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
goto finally;
|
goto finally;
|
||||||
|
|
||||||
|
@ -94,8 +96,8 @@ PyDoc_STRVAR(splitfields__doc__,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
strop_splitfields(PyObject *self, PyObject *args)
|
strop_splitfields(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
int len, n, i, j, err;
|
Py_ssize_t len, n, i, j, err;
|
||||||
int splitcount, maxsplit;
|
Py_ssize_t splitcount, maxsplit;
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
PyObject *list, *item;
|
PyObject *list, *item;
|
||||||
|
|
||||||
|
@ -104,7 +106,7 @@ strop_splitfields(PyObject *self, PyObject *args)
|
||||||
n = 0;
|
n = 0;
|
||||||
splitcount = 0;
|
splitcount = 0;
|
||||||
maxsplit = 0;
|
maxsplit = 0;
|
||||||
if (!PyArg_ParseTuple(args, "t#|z#i:split", &s, &len, &sub, &n, &maxsplit))
|
if (!PyArg_ParseTuple(args, "t#|z#n:split", &s, &len, &sub, &n, &maxsplit))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (sub == NULL)
|
if (sub == NULL)
|
||||||
return split_whitespace(s, len, maxsplit);
|
return split_whitespace(s, len, maxsplit);
|
||||||
|
@ -120,7 +122,7 @@ strop_splitfields(PyObject *self, PyObject *args)
|
||||||
i = j = 0;
|
i = j = 0;
|
||||||
while (i+n <= len) {
|
while (i+n <= len) {
|
||||||
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
|
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
|
||||||
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
item = PyString_FromStringAndSize(s+j, i-j);
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
err = PyList_Append(list, item);
|
err = PyList_Append(list, item);
|
||||||
|
@ -135,7 +137,7 @@ strop_splitfields(PyObject *self, PyObject *args)
|
||||||
else
|
else
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
item = PyString_FromStringAndSize(s+j, (int)(len-j));
|
item = PyString_FromStringAndSize(s+j, len-j);
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
err = PyList_Append(list, item);
|
err = PyList_Append(list, item);
|
||||||
|
@ -287,10 +289,10 @@ static PyObject *
|
||||||
strop_find(PyObject *self, PyObject *args)
|
strop_find(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
int len, n, i = 0, last = INT_MAX;
|
Py_ssize_t len, n, i = 0, last = PY_SSIZE_T_MAX;
|
||||||
|
|
||||||
WARN;
|
WARN;
|
||||||
if (!PyArg_ParseTuple(args, "t#t#|ii:find", &s, &len, &sub, &n, &i, &last))
|
if (!PyArg_ParseTuple(args, "t#t#|nn:find", &s, &len, &sub, &n, &i, &last))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (last > len)
|
if (last > len)
|
||||||
|
@ -330,11 +332,11 @@ static PyObject *
|
||||||
strop_rfind(PyObject *self, PyObject *args)
|
strop_rfind(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
int len, n, j;
|
Py_ssize_t len, n, j;
|
||||||
int i = 0, last = INT_MAX;
|
Py_ssize_t i = 0, last = INT_MAX;
|
||||||
|
|
||||||
WARN;
|
WARN;
|
||||||
if (!PyArg_ParseTuple(args, "t#t#|ii:rfind", &s, &len, &sub, &n, &i, &last))
|
if (!PyArg_ParseTuple(args, "t#t#|nn:rfind", &s, &len, &sub, &n, &i, &last))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (last > len)
|
if (last > len)
|
||||||
|
@ -576,10 +578,10 @@ strop_expandtabs(PyObject *self, PyObject *args)
|
||||||
char* e;
|
char* e;
|
||||||
char* p;
|
char* p;
|
||||||
char* q;
|
char* q;
|
||||||
int i, j;
|
Py_ssize_t i, j;
|
||||||
PyObject* out;
|
PyObject* out;
|
||||||
char* string;
|
char* string;
|
||||||
int stringlen;
|
Py_ssize_t stringlen;
|
||||||
int tabsize = 8;
|
int tabsize = 8;
|
||||||
|
|
||||||
WARN;
|
WARN;
|
||||||
|
@ -644,12 +646,12 @@ static PyObject *
|
||||||
strop_count(PyObject *self, PyObject *args)
|
strop_count(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
int len, n;
|
Py_ssize_t len, n;
|
||||||
int i = 0, last = INT_MAX;
|
Py_ssize_t i = 0, last = INT_MAX;
|
||||||
int m, r;
|
Py_ssize_t m, r;
|
||||||
|
|
||||||
WARN;
|
WARN;
|
||||||
if (!PyArg_ParseTuple(args, "t#t#|ii:count", &s, &len, &sub, &n, &i, &last))
|
if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (last > len)
|
if (last > len)
|
||||||
last = len;
|
last = len;
|
||||||
|
@ -884,7 +886,7 @@ static PyObject *
|
||||||
strop_maketrans(PyObject *self, PyObject *args)
|
strop_maketrans(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
unsigned char *c, *from=NULL, *to=NULL;
|
unsigned char *c, *from=NULL, *to=NULL;
|
||||||
int i, fromlen=0, tolen=0;
|
Py_ssize_t i, fromlen=0, tolen=0;
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
|
if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
|
||||||
|
@ -998,10 +1000,10 @@ strop_translate(PyObject *self, PyObject *args)
|
||||||
found, or -1 if not found. If len of PAT is greater than length of
|
found, or -1 if not found. If len of PAT is greater than length of
|
||||||
MEM, the function returns -1.
|
MEM, the function returns -1.
|
||||||
*/
|
*/
|
||||||
static int
|
static Py_ssize_t
|
||||||
mymemfind(const char *mem, int len, const char *pat, int pat_len)
|
mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
|
||||||
{
|
{
|
||||||
register int ii;
|
register Py_ssize_t ii;
|
||||||
|
|
||||||
/* pattern can not occur in the last pat_len-1 chars */
|
/* pattern can not occur in the last pat_len-1 chars */
|
||||||
len -= pat_len;
|
len -= pat_len;
|
||||||
|
@ -1023,11 +1025,11 @@ mymemfind(const char *mem, int len, const char *pat, int pat_len)
|
||||||
meaning mem=1111 and pat==11 returns 2.
|
meaning mem=1111 and pat==11 returns 2.
|
||||||
mem=11111 and pat==11 also return 2.
|
mem=11111 and pat==11 also return 2.
|
||||||
*/
|
*/
|
||||||
static int
|
static Py_ssize_t
|
||||||
mymemcnt(const char *mem, int len, const char *pat, int pat_len)
|
mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
|
||||||
{
|
{
|
||||||
register int offset = 0;
|
register Py_ssize_t offset = 0;
|
||||||
int nfound = 0;
|
Py_ssize_t nfound = 0;
|
||||||
|
|
||||||
while (len >= 0) {
|
while (len >= 0) {
|
||||||
offset = mymemfind(mem, len, pat, pat_len);
|
offset = mymemfind(mem, len, pat, pat_len);
|
||||||
|
@ -1060,15 +1062,15 @@ mymemcnt(const char *mem, int len, const char *pat, int pat_len)
|
||||||
NULL if an error occurred.
|
NULL if an error occurred.
|
||||||
*/
|
*/
|
||||||
static char *
|
static char *
|
||||||
mymemreplace(const char *str, int len, /* input string */
|
mymemreplace(const char *str, Py_ssize_t len, /* input string */
|
||||||
const char *pat, int pat_len, /* pattern string to find */
|
const char *pat, Py_ssize_t pat_len, /* pattern string to find */
|
||||||
const char *sub, int sub_len, /* substitution string */
|
const char *sub, Py_ssize_t sub_len, /* substitution string */
|
||||||
int count, /* number of replacements */
|
Py_ssize_t count, /* number of replacements */
|
||||||
int *out_len)
|
Py_ssize_t *out_len)
|
||||||
{
|
{
|
||||||
char *out_s;
|
char *out_s;
|
||||||
char *new_s;
|
char *new_s;
|
||||||
int nfound, offset, new_len;
|
Py_ssize_t nfound, offset, new_len;
|
||||||
|
|
||||||
if (len == 0 || pat_len > len)
|
if (len == 0 || pat_len > len)
|
||||||
goto return_same;
|
goto return_same;
|
||||||
|
@ -1137,12 +1139,12 @@ static PyObject *
|
||||||
strop_replace(PyObject *self, PyObject *args)
|
strop_replace(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
char *str, *pat,*sub,*new_s;
|
char *str, *pat,*sub,*new_s;
|
||||||
int len,pat_len,sub_len,out_len;
|
Py_ssize_t len,pat_len,sub_len,out_len;
|
||||||
int count = -1;
|
Py_ssize_t count = -1;
|
||||||
PyObject *new;
|
PyObject *new;
|
||||||
|
|
||||||
WARN;
|
WARN;
|
||||||
if (!PyArg_ParseTuple(args, "t#t#t#|i:replace",
|
if (!PyArg_ParseTuple(args, "t#t#t#|n:replace",
|
||||||
&str, &len, &pat, &pat_len, &sub, &sub_len,
|
&str, &len, &pat, &pat_len, &sub, &sub_len,
|
||||||
&count))
|
&count))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue