cinap_lenrekadd hg and python4bb9e2215 years agoHistory
8067 lines · 210.4 KB · C
RawDownload
{    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);    register const Py_UNICODE *e;     /* Shortcut for single character strings */    if (PyUnicode_GET_SIZE(self) == 1 &&	Py_UNICODE_ISALPHA(*p))	return PyBool_FromLong(1);     /* Special case for empty strings */    if (PyUnicode_GET_SIZE(self) == 0)	return PyBool_FromLong(0);     e = p + PyUnicode_GET_SIZE(self);    for (; p < e; p++) {	if (!Py_UNICODE_ISALPHA(*p))	    return PyBool_FromLong(0);    }    return PyBool_FromLong(1);} PyDoc_STRVAR(isalnum__doc__,"S.isalnum() -> bool\n\\n\Return True if all characters in S are alphanumeric\n\and there is at least one character in S, False otherwise."); static PyObject*unicode_isalnum(PyUnicodeObject *self){    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);    register const Py_UNICODE *e;     /* Shortcut for single character strings */    if (PyUnicode_GET_SIZE(self) == 1 &&	Py_UNICODE_ISALNUM(*p))	return PyBool_FromLong(1);     /* Special case for empty strings */    if (PyUnicode_GET_SIZE(self) == 0)	return PyBool_FromLong(0);     e = p + PyUnicode_GET_SIZE(self);    for (; p < e; p++) {	if (!Py_UNICODE_ISALNUM(*p))	    return PyBool_FromLong(0);    }    return PyBool_FromLong(1);} PyDoc_STRVAR(isdecimal__doc__,"S.isdecimal() -> bool\n\\n\Return True if there are only decimal characters in S,\n\False otherwise."); static PyObject*unicode_isdecimal(PyUnicodeObject *self){    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);    register const Py_UNICODE *e;     /* Shortcut for single character strings */    if (PyUnicode_GET_SIZE(self) == 1 &&	Py_UNICODE_ISDECIMAL(*p))	return PyBool_FromLong(1);     /* Special case for empty strings */    if (PyUnicode_GET_SIZE(self) == 0)	return PyBool_FromLong(0);     e = p + PyUnicode_GET_SIZE(self);    for (; p < e; p++) {	if (!Py_UNICODE_ISDECIMAL(*p))	    return PyBool_FromLong(0);    }    return PyBool_FromLong(1);} PyDoc_STRVAR(isdigit__doc__,"S.isdigit() -> bool\n\\n\Return True if all characters in S are digits\n\and there is at least one character in S, False otherwise."); static PyObject*unicode_isdigit(PyUnicodeObject *self){    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);    register const Py_UNICODE *e;     /* Shortcut for single character strings */    if (PyUnicode_GET_SIZE(self) == 1 &&	Py_UNICODE_ISDIGIT(*p))	return PyBool_FromLong(1);     /* Special case for empty strings */    if (PyUnicode_GET_SIZE(self) == 0)	return PyBool_FromLong(0);     e = p + PyUnicode_GET_SIZE(self);    for (; p < e; p++) {	if (!Py_UNICODE_ISDIGIT(*p))	    return PyBool_FromLong(0);    }    return PyBool_FromLong(1);} PyDoc_STRVAR(isnumeric__doc__,"S.isnumeric() -> bool\n\\n\Return True if there are only numeric characters in S,\n\False otherwise."); static PyObject*unicode_isnumeric(PyUnicodeObject *self){    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);    register const Py_UNICODE *e;     /* Shortcut for single character strings */    if (PyUnicode_GET_SIZE(self) == 1 &&	Py_UNICODE_ISNUMERIC(*p))	return PyBool_FromLong(1);     /* Special case for empty strings */    if (PyUnicode_GET_SIZE(self) == 0)	return PyBool_FromLong(0);     e = p + PyUnicode_GET_SIZE(self);    for (; p < e; p++) {	if (!Py_UNICODE_ISNUMERIC(*p))	    return PyBool_FromLong(0);    }    return PyBool_FromLong(1);} PyDoc_STRVAR(join__doc__,"S.join(sequence) -> unicode\n\\n\Return a string which is the concatenation of the strings in the\n\sequence.  The separator between elements is S."); static PyObject*unicode_join(PyObject *self, PyObject *data){    return PyUnicode_Join(self, data);} static Py_ssize_tunicode_length(PyUnicodeObject *self){    return self->length;} PyDoc_STRVAR(ljust__doc__,"S.ljust(width[, fillchar]) -> int\n\\n\Return S left justified in a Unicode string of length width. Padding is\n\done using the specified fill character (default is a space)."); static PyObject *unicode_ljust(PyUnicodeObject *self, PyObject *args){    Py_ssize_t width;    Py_UNICODE fillchar = ' ';     if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar))        return NULL;     if (self->length >= width && PyUnicode_CheckExact(self)) {        Py_INCREF(self);        return (PyObject*) self;    }     return (PyObject*) pad(self, 0, width - self->length, fillchar);} PyDoc_STRVAR(lower__doc__,"S.lower() -> unicode\n\\n\Return a copy of the string S converted to lowercase."); static PyObject*unicode_lower(PyUnicodeObject *self){    return fixup(self, fixlower);} #define LEFTSTRIP 0#define RIGHTSTRIP 1#define BOTHSTRIP 2 /* Arrays indexed by above */static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; #define STRIPNAME(i) (stripformat[i]+3) /* externally visible for str.strip(unicode) */PyObject *_PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj){	Py_UNICODE *s = PyUnicode_AS_UNICODE(self);	Py_ssize_t len = PyUnicode_GET_SIZE(self);	Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj);	Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj);	Py_ssize_t i, j;         BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); 	i = 0;	if (striptype != RIGHTSTRIP) {            while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) {                i++;            }	} 	j = len;	if (striptype != LEFTSTRIP) {            do {                j--;            } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen));            j++;	} 	if (i == 0 && j == len && PyUnicode_CheckExact(self)) {            Py_INCREF(self);            return (PyObject*)self;	}	else            return PyUnicode_FromUnicode(s+i, j-i);}  static PyObject *do_strip(PyUnicodeObject *self, int striptype){	Py_UNICODE *s = PyUnicode_AS_UNICODE(self);	Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; 	i = 0;	if (striptype != RIGHTSTRIP) {		while (i < len && Py_UNICODE_ISSPACE(s[i])) {			i++;		}	} 	j = len;	if (striptype != LEFTSTRIP) {		do {			j--;		} while (j >= i && Py_UNICODE_ISSPACE(s[j]));		j++;	} 	if (i == 0 && j == len && PyUnicode_CheckExact(self)) {		Py_INCREF(self);		return (PyObject*)self;	}	else		return PyUnicode_FromUnicode(s+i, j-i);}  static PyObject *do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args){	PyObject *sep = NULL; 	if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))		return NULL; 	if (sep != NULL && sep != Py_None) {		if (PyUnicode_Check(sep))			return _PyUnicode_XStrip(self, striptype, sep);		else if (PyString_Check(sep)) {			PyObject *res;			sep = PyUnicode_FromObject(sep);			if (sep==NULL)				return NULL;			res = _PyUnicode_XStrip(self, striptype, sep);			Py_DECREF(sep);			return res;		}		else {			PyErr_Format(PyExc_TypeError,				     "%s arg must be None, unicode or str",				     STRIPNAME(striptype));			return NULL;		}	} 	return do_strip(self, striptype);}  PyDoc_STRVAR(strip__doc__,"S.strip([chars]) -> unicode\n\\n\Return a copy of the string S with leading and trailing\n\whitespace removed.\n\If chars is given and not None, remove characters in chars instead.\n\If chars is a str, it will be converted to unicode before stripping"); static PyObject *unicode_strip(PyUnicodeObject *self, PyObject *args){	if (PyTuple_GET_SIZE(args) == 0)		return do_strip(self, BOTHSTRIP); /* Common case */	else		return do_argstrip(self, BOTHSTRIP, args);}  PyDoc_STRVAR(lstrip__doc__,"S.lstrip([chars]) -> unicode\n\\n\Return a copy of the string S with leading whitespace removed.\n\If chars is given and not None, remove characters in chars instead.\n\If chars is a str, it will be converted to unicode before stripping"); static PyObject *unicode_lstrip(PyUnicodeObject *self, PyObject *args){	if (PyTuple_GET_SIZE(args) == 0)		return do_strip(self, LEFTSTRIP); /* Common case */	else		return do_argstrip(self, LEFTSTRIP, args);}  PyDoc_STRVAR(rstrip__doc__,"S.rstrip([chars]) -> unicode\n\\n\Return a copy of the string S with trailing whitespace removed.\n\If chars is given and not None, remove characters in chars instead.\n\If chars is a str, it will be converted to unicode before stripping"); static PyObject *unicode_rstrip(PyUnicodeObject *self, PyObject *args){	if (PyTuple_GET_SIZE(args) == 0)		return do_strip(self, RIGHTSTRIP); /* Common case */	else		return do_argstrip(self, RIGHTSTRIP, args);}  static PyObject*unicode_repeat(PyUnicodeObject *str, Py_ssize_t len){    PyUnicodeObject *u;    Py_UNICODE *p;    Py_ssize_t nchars;    size_t nbytes;     if (len < 0)        len = 0;     if (len == 1 && PyUnicode_CheckExact(str)) {        /* no repeat, return original string */        Py_INCREF(str);        return (PyObject*) str;    }     /* ensure # of chars needed doesn't overflow int and # of bytes     * needed doesn't overflow size_t     */    nchars = len * str->length;    if (len && nchars / len != str->length) {        PyErr_SetString(PyExc_OverflowError,                        "repeated string is too long");        return NULL;    }    nbytes = (nchars + 1) * sizeof(Py_UNICODE);    if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) {        PyErr_SetString(PyExc_OverflowError,                        "repeated string is too long");        return NULL;    }    u = _PyUnicode_New(nchars);    if (!u)        return NULL;     p = u->str;     if (str->length == 1 && len > 0) {        Py_UNICODE_FILL(p, str->str[0], len);    } else {	Py_ssize_t done = 0; /* number of characters copied this far */	if (done < nchars) {            Py_UNICODE_COPY(p, str->str, str->length);            done = str->length;	}	while (done < nchars) {            int n = (done <= nchars-done) ? done : nchars-done;            Py_UNICODE_COPY(p+done, p, n);            done += n;	}    }     return (PyObject*) u;} PyObject *PyUnicode_Replace(PyObject *obj,			    PyObject *subobj,			    PyObject *replobj,			    Py_ssize_t maxcount){    PyObject *self;    PyObject *str1;    PyObject *str2;    PyObject *result;     self = PyUnicode_FromObject(obj);    if (self == NULL)	return NULL;    str1 = PyUnicode_FromObject(subobj);    if (str1 == NULL) {	Py_DECREF(self);	return NULL;    }    str2 = PyUnicode_FromObject(replobj);    if (str2 == NULL) {	Py_DECREF(self);	Py_DECREF(str1);	return NULL;    }    result = replace((PyUnicodeObject *)self,		     (PyUnicodeObject *)str1,		     (PyUnicodeObject *)str2,		     maxcount);    Py_DECREF(self);    Py_DECREF(str1);    Py_DECREF(str2);    return result;} PyDoc_STRVAR(replace__doc__,"S.replace (old, new[, maxsplit]) -> unicode\n\\n\Return a copy of S with all occurrences of substring\n\old replaced by new.  If the optional argument maxsplit is\n\given, only the first maxsplit occurrences are replaced."); static PyObject*unicode_replace(PyUnicodeObject *self, PyObject *args){    PyUnicodeObject *str1;    PyUnicodeObject *str2;    Py_ssize_t maxcount = -1;    PyObject *result;     if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))        return NULL;    str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1);    if (str1 == NULL)	return NULL;    str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2);    if (str2 == NULL) {	Py_DECREF(str1);	return NULL;    }     result = replace(self, str1, str2, maxcount);     Py_DECREF(str1);    Py_DECREF(str2);    return result;} staticPyObject *unicode_repr(PyObject *unicode){    return unicodeescape_string(PyUnicode_AS_UNICODE(unicode),				PyUnicode_GET_SIZE(unicode),				1);} PyDoc_STRVAR(rfind__doc__,"S.rfind(sub [,start [,end]]) -> int\n\\n\Return the highest index in S where substring sub is found,\n\such that sub is contained within s[start,end].  Optional\n\arguments start and end are interpreted as in slice notation.\n\\n\Return -1 on failure."); static PyObject *unicode_rfind(PyUnicodeObject *self, PyObject *args){    PyObject *substring;    Py_ssize_t start = 0;    Py_ssize_t end = PY_SSIZE_T_MAX;    Py_ssize_t result;     if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring,		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))        return NULL;    substring = PyUnicode_FromObject(substring);

Lines 6001–6500 of 8067. Previous lines