cinap_lenrekadd hg and python4bb9e2215 years agoHistory
5009 lines · 120.0 KB · C
RawDownload
		return return_self(self);	} 	if (to_len == 0) {		/* delete all occurances of 'from' string */		if (from_len == 1) {			return replace_delete_single_character(				self, from_s[0], maxcount);		} else {			return replace_delete_substring(self, from_s, from_len, maxcount);		}	} 	/* Handle special case where both strings have the same length */ 	if (from_len == to_len) {		if (from_len == 1) {			return replace_single_character_in_place(				self,				from_s[0],				to_s[0],				maxcount);		} else {			return replace_substring_in_place(				self, from_s, from_len, to_s, to_len, maxcount);		}	} 	/* Otherwise use the more generic algorithms */	if (from_len == 1) {		return replace_single_character(self, from_s[0],						to_s, to_len, maxcount);	} else {		/* len('from')>=2, len('to')>=1 */		return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);	}} PyDoc_STRVAR(replace__doc__,"S.replace (old, new[, count]) -> string\n\\n\Return a copy of string S with all occurrences of substring\n\old replaced by new.  If the optional argument count is\n\given, only the first count occurrences are replaced."); static PyObject *string_replace(PyStringObject *self, PyObject *args){	Py_ssize_t count = -1;	PyObject *from, *to;	const char *from_s, *to_s;	Py_ssize_t from_len, to_len; 	if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))		return NULL; 	if (PyString_Check(from)) {		from_s = PyString_AS_STRING(from);		from_len = PyString_GET_SIZE(from);	}#ifdef Py_USING_UNICODE	if (PyUnicode_Check(from))		return PyUnicode_Replace((PyObject *)self,					 from, to, count);#endif	else if (PyObject_AsCharBuffer(from, &from_s, &from_len))		return NULL; 	if (PyString_Check(to)) {		to_s = PyString_AS_STRING(to);		to_len = PyString_GET_SIZE(to);	}#ifdef Py_USING_UNICODE	else if (PyUnicode_Check(to))		return PyUnicode_Replace((PyObject *)self,					 from, to, count);#endif	else if (PyObject_AsCharBuffer(to, &to_s, &to_len))		return NULL; 	return (PyObject *)replace((PyStringObject *) self,				   from_s, from_len,				   to_s, to_len, count);} /** End DALKE **/ /* Matches the end (direction >= 0) or start (direction < 0) of self * against substr, using the start and end arguments. Returns * -1 on error, 0 if not found and 1 if found. */Py_LOCAL(int)_string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start,		  Py_ssize_t end, int direction){	Py_ssize_t len = PyString_GET_SIZE(self);	Py_ssize_t slen;	const char* sub;	const char* str; 	if (PyString_Check(substr)) {		sub = PyString_AS_STRING(substr);		slen = PyString_GET_SIZE(substr);	}#ifdef Py_USING_UNICODE	else if (PyUnicode_Check(substr))		return PyUnicode_Tailmatch((PyObject *)self,					   substr, start, end, direction);#endif	else if (PyObject_AsCharBuffer(substr, &sub, &slen))		return -1;	str = PyString_AS_STRING(self); 	string_adjust_indices(&start, &end, len); 	if (direction < 0) {		/* startswith */		if (start+slen > len)			return 0;	} else {		/* endswith */		if (end-start < slen || start > len)			return 0; 		if (end-slen > start)			start = end - slen;	}	if (end-start >= slen)		return ! memcmp(str+start, sub, slen);	return 0;}  PyDoc_STRVAR(startswith__doc__,"S.startswith(prefix[, start[, end]]) -> bool\n\\n\Return True if S starts with the specified prefix, False otherwise.\n\With optional start, test S beginning at that position.\n\With optional end, stop comparing S at that position.\n\prefix can also be a tuple of strings to try."); static PyObject *string_startswith(PyStringObject *self, PyObject *args){	Py_ssize_t start = 0;	Py_ssize_t end = PY_SSIZE_T_MAX;	PyObject *subobj;	int result; 	if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))		return NULL;	if (PyTuple_Check(subobj)) {		Py_ssize_t i;		for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {			result = _string_tailmatch(self,					PyTuple_GET_ITEM(subobj, i),					start, end, -1);			if (result == -1)				return NULL;			else if (result) {				Py_RETURN_TRUE;			}		}		Py_RETURN_FALSE;	}	result = _string_tailmatch(self, subobj, start, end, -1);	if (result == -1)		return NULL;	else		return PyBool_FromLong(result);}  PyDoc_STRVAR(endswith__doc__,"S.endswith(suffix[, start[, end]]) -> bool\n\\n\Return True if S ends with the specified suffix, False otherwise.\n\With optional start, test S beginning at that position.\n\With optional end, stop comparing S at that position.\n\suffix can also be a tuple of strings to try."); static PyObject *string_endswith(PyStringObject *self, PyObject *args){	Py_ssize_t start = 0;	Py_ssize_t end = PY_SSIZE_T_MAX;	PyObject *subobj;	int result; 	if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))		return NULL;	if (PyTuple_Check(subobj)) {		Py_ssize_t i;		for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {			result = _string_tailmatch(self,					PyTuple_GET_ITEM(subobj, i),					start, end, +1);			if (result == -1)				return NULL;			else if (result) {				Py_RETURN_TRUE;			}		}		Py_RETURN_FALSE;	}	result = _string_tailmatch(self, subobj, start, end, +1);	if (result == -1)		return NULL;	else		return PyBool_FromLong(result);}  PyDoc_STRVAR(encode__doc__,"S.encode([encoding[,errors]]) -> object\n\\n\Encodes S using the codec registered for encoding. encoding defaults\n\to the default encoding. errors may be given to set a different error\n\handling scheme. Default is 'strict' meaning that encoding errors raise\n\a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\'xmlcharrefreplace' as well as any other name registered with\n\codecs.register_error that is able to handle UnicodeEncodeErrors."); static PyObject *string_encode(PyStringObject *self, PyObject *args){    char *encoding = NULL;    char *errors = NULL;    PyObject *v;     if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))        return NULL;    v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);    if (v == NULL)        goto onError;    if (!PyString_Check(v) && !PyUnicode_Check(v)) {        PyErr_Format(PyExc_TypeError,                     "encoder did not return a string/unicode object "                     "(type=%.400s)",                     v->ob_type->tp_name);        Py_DECREF(v);        return NULL;    }    return v;  onError:    return NULL;}  PyDoc_STRVAR(decode__doc__,"S.decode([encoding[,errors]]) -> object\n\\n\Decodes S using the codec registered for encoding. encoding defaults\n\to the default encoding. errors may be given to set a different error\n\handling scheme. Default is 'strict' meaning that encoding errors raise\n\a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\as well as any other name registerd with codecs.register_error that is\n\able to handle UnicodeDecodeErrors."); static PyObject *string_decode(PyStringObject *self, PyObject *args){    char *encoding = NULL;    char *errors = NULL;    PyObject *v;     if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))        return NULL;    v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);    if (v == NULL)        goto onError;    if (!PyString_Check(v) && !PyUnicode_Check(v)) {        PyErr_Format(PyExc_TypeError,                     "decoder did not return a string/unicode object "                     "(type=%.400s)",                     v->ob_type->tp_name);        Py_DECREF(v);        return NULL;    }    return v;  onError:    return NULL;}  PyDoc_STRVAR(expandtabs__doc__,"S.expandtabs([tabsize]) -> string\n\\n\Return a copy of S where all tab characters are expanded using spaces.\n\If tabsize is not given, a tab size of 8 characters is assumed."); static PyObject*string_expandtabs(PyStringObject *self, PyObject *args){    const char *e, *p;    char *q;    Py_ssize_t i, j;    PyObject *u;    int tabsize = 8;     if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))	return NULL;     /* First pass: determine size of output string */    i = j = 0;    e = PyString_AS_STRING(self) + PyString_GET_SIZE(self);    for (p = PyString_AS_STRING(self); p < e; p++)        if (*p == '\t') {	    if (tabsize > 0)		j += tabsize - (j % tabsize);	}        else {            j++;            if (*p == '\n' || *p == '\r') {                i += j;                j = 0;            }        }     /* Second pass: create output string and fill it */    u = PyString_FromStringAndSize(NULL, i + j);    if (!u)        return NULL;     j = 0;    q = PyString_AS_STRING(u);     for (p = PyString_AS_STRING(self); p < e; p++)        if (*p == '\t') {	    if (tabsize > 0) {		i = tabsize - (j % tabsize);		j += i;		while (i--)		    *q++ = ' ';	    }	}	else {            j++;	    *q++ = *p;            if (*p == '\n' || *p == '\r')                j = 0;        }     return u;} Py_LOCAL_INLINE(PyObject *)pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill){    PyObject *u;     if (left < 0)        left = 0;    if (right < 0)        right = 0;     if (left == 0 && right == 0 && PyString_CheckExact(self)) {        Py_INCREF(self);        return (PyObject *)self;    }     u = PyString_FromStringAndSize(NULL,				   left + PyString_GET_SIZE(self) + right);    if (u) {        if (left)            memset(PyString_AS_STRING(u), fill, left);        Py_MEMCPY(PyString_AS_STRING(u) + left,	       PyString_AS_STRING(self),	       PyString_GET_SIZE(self));        if (right)            memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self),		   fill, right);    }     return u;} PyDoc_STRVAR(ljust__doc__,"S.ljust(width[, fillchar]) -> string\n""\n""Return S left justified in a string of length width. Padding is\n""done using the specified fill character (default is a space)."); static PyObject *string_ljust(PyStringObject *self, PyObject *args){    Py_ssize_t width;    char fillchar = ' ';     if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar))        return NULL;     if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {        Py_INCREF(self);        return (PyObject*) self;    }     return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);}  PyDoc_STRVAR(rjust__doc__,"S.rjust(width[, fillchar]) -> string\n""\n""Return S right justified in a string of length width. Padding is\n""done using the specified fill character (default is a space)"); static PyObject *string_rjust(PyStringObject *self, PyObject *args){    Py_ssize_t width;    char fillchar = ' ';     if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar))        return NULL;     if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {        Py_INCREF(self);        return (PyObject*) self;    }     return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);}  PyDoc_STRVAR(center__doc__,"S.center(width[, fillchar]) -> string\n""\n""Return S centered in a string of length width. Padding is\n""done using the specified fill character (default is a space)"); static PyObject *string_center(PyStringObject *self, PyObject *args){    Py_ssize_t marg, left;    Py_ssize_t width;    char fillchar = ' ';     if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar))        return NULL;     if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {        Py_INCREF(self);        return (PyObject*) self;    }     marg = width - PyString_GET_SIZE(self);    left = marg / 2 + (marg & width & 1);     return pad(self, left, marg - left, fillchar);} PyDoc_STRVAR(zfill__doc__,"S.zfill(width) -> string\n""\n""Pad a numeric string S with zeros on the left, to fill a field\n""of the specified width.  The string S is never truncated."); static PyObject *string_zfill(PyStringObject *self, PyObject *args){    Py_ssize_t fill;    PyObject *s;    char *p;    Py_ssize_t width;     if (!PyArg_ParseTuple(args, "n:zfill", &width))        return NULL;     if (PyString_GET_SIZE(self) >= width) {        if (PyString_CheckExact(self)) {            Py_INCREF(self);            return (PyObject*) self;        }        else            return PyString_FromStringAndSize(                PyString_AS_STRING(self),                PyString_GET_SIZE(self)            );    }     fill = width - PyString_GET_SIZE(self);     s = pad(self, fill, 0, '0');     if (s == NULL)        return NULL;     p = PyString_AS_STRING(s);    if (p[fill] == '+' || p[fill] == '-') {        /* move sign to beginning of string */        p[0] = p[fill];        p[fill] = '0';    }     return (PyObject*) s;

Lines 3001–3500 of 5009. Previous lines