cinap_lenrekadd hg and python4bb9e2215 years agoHistory
8067 lines · 210.4 KB · C
RawDownload
	status = 1;    }    s++;    while (--len > 0) {        if (Py_UNICODE_ISUPPER(*s)) {            *s = Py_UNICODE_TOLOWER(*s);            status = 1;        }        s++;    }    return status;} staticint fixtitle(PyUnicodeObject *self){    register Py_UNICODE *p = PyUnicode_AS_UNICODE(self);    register Py_UNICODE *e;    int previous_is_cased;     /* Shortcut for single character strings */    if (PyUnicode_GET_SIZE(self) == 1) {	Py_UNICODE ch = Py_UNICODE_TOTITLE(*p);	if (*p != ch) {	    *p = ch;	    return 1;	}	else	    return 0;    }     e = p + PyUnicode_GET_SIZE(self);    previous_is_cased = 0;    for (; p < e; p++) {	register const Py_UNICODE ch = *p; 	if (previous_is_cased)	    *p = Py_UNICODE_TOLOWER(ch);	else	    *p = Py_UNICODE_TOTITLE(ch); 	if (Py_UNICODE_ISLOWER(ch) ||	    Py_UNICODE_ISUPPER(ch) ||	    Py_UNICODE_ISTITLE(ch))	    previous_is_cased = 1;	else	    previous_is_cased = 0;    }    return 1;} PyObject *PyUnicode_Join(PyObject *separator, PyObject *seq){    PyObject *internal_separator = NULL;    const Py_UNICODE blank = ' ';    const Py_UNICODE *sep = &blank;    Py_ssize_t seplen = 1;    PyUnicodeObject *res = NULL; /* the result */    Py_ssize_t res_alloc = 100;  /* # allocated bytes for string in res */    Py_ssize_t res_used;         /* # used bytes */    Py_UNICODE *res_p;       /* pointer to free byte in res's string area */    PyObject *fseq;          /* PySequence_Fast(seq) */    Py_ssize_t seqlen;              /* len(fseq) -- number of items in sequence */    PyObject *item;    Py_ssize_t i;     fseq = PySequence_Fast(seq, "");    if (fseq == NULL) {    	return NULL;    }     /* Grrrr.  A codec may be invoked to convert str objects to     * Unicode, and so it's possible to call back into Python code     * during PyUnicode_FromObject(), and so it's possible for a sick     * codec to change the size of fseq (if seq is a list).  Therefore     * we have to keep refetching the size -- can't assume seqlen     * is invariant.     */    seqlen = PySequence_Fast_GET_SIZE(fseq);    /* If empty sequence, return u"". */    if (seqlen == 0) {    	res = _PyUnicode_New(0);  /* empty sequence; return u"" */    	goto Done;    }    /* If singleton sequence with an exact Unicode, return that. */    if (seqlen == 1) {	item = PySequence_Fast_GET_ITEM(fseq, 0);	if (PyUnicode_CheckExact(item)) {	    Py_INCREF(item);	    res = (PyUnicodeObject *)item;	    goto Done;	}    }     /* At least two items to join, or one that isn't exact Unicode. */    if (seqlen > 1) {        /* Set up sep and seplen -- they're needed. */    	if (separator == NULL) {	    sep = &blank;	    seplen = 1;        }    	else {	    internal_separator = PyUnicode_FromObject(separator);	    if (internal_separator == NULL)	        goto onError;	    sep = PyUnicode_AS_UNICODE(internal_separator);	    seplen = PyUnicode_GET_SIZE(internal_separator);	    /* In case PyUnicode_FromObject() mutated seq. */	    seqlen = PySequence_Fast_GET_SIZE(fseq);        }    }     /* Get space. */    res = _PyUnicode_New(res_alloc);    if (res == NULL)        goto onError;    res_p = PyUnicode_AS_UNICODE(res);    res_used = 0;     for (i = 0; i < seqlen; ++i) {	Py_ssize_t itemlen;	Py_ssize_t new_res_used; 	item = PySequence_Fast_GET_ITEM(fseq, i);	/* Convert item to Unicode. */	if (! PyUnicode_Check(item) && ! PyString_Check(item)) {	    PyErr_Format(PyExc_TypeError,			 "sequence item %zd: expected string or Unicode,"			 " %.80s found",			 i, item->ob_type->tp_name);	    goto onError;	}	item = PyUnicode_FromObject(item);	if (item == NULL)	    goto onError;	/* We own a reference to item from here on. */ 	/* In case PyUnicode_FromObject() mutated seq. */	seqlen = PySequence_Fast_GET_SIZE(fseq);         /* Make sure we have enough space for the separator and the item. */	itemlen = PyUnicode_GET_SIZE(item);	new_res_used = res_used + itemlen;	if (new_res_used < 0)	    goto Overflow;	if (i < seqlen - 1) {	    new_res_used += seplen;	    if (new_res_used < 0)		goto Overflow;	}	if (new_res_used > res_alloc) {	    /* double allocated size until it's big enough */	    do {	        res_alloc += res_alloc;	        if (res_alloc <= 0)	            goto Overflow;	    } while (new_res_used > res_alloc);	    if (_PyUnicode_Resize(&res, res_alloc) < 0) {		Py_DECREF(item);		goto onError;	    }            res_p = PyUnicode_AS_UNICODE(res) + res_used;	} 	/* Copy item, and maybe the separator. */	Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen);	res_p += itemlen;	if (i < seqlen - 1) {	    Py_UNICODE_COPY(res_p, sep, seplen);	    res_p += seplen;	}	Py_DECREF(item);	res_used = new_res_used;    }     /* Shrink res to match the used area; this probably can't fail,     * but it's cheap to check.     */    if (_PyUnicode_Resize(&res, res_used) < 0)	goto onError;  Done:    Py_XDECREF(internal_separator);    Py_DECREF(fseq);    return (PyObject *)res;  Overflow:    PyErr_SetString(PyExc_OverflowError,                    "join() result is too long for a Python string");    Py_DECREF(item);    /* fall through */  onError:    Py_XDECREF(internal_separator);    Py_DECREF(fseq);    Py_XDECREF(res);    return NULL;} staticPyUnicodeObject *pad(PyUnicodeObject *self,		     Py_ssize_t left,		     Py_ssize_t right,		     Py_UNICODE fill){    PyUnicodeObject *u;     if (left < 0)        left = 0;    if (right < 0)        right = 0;     if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) {        Py_INCREF(self);        return self;    }     u = _PyUnicode_New(left + self->length + right);    if (u) {        if (left)            Py_UNICODE_FILL(u->str, fill, left);        Py_UNICODE_COPY(u->str + left, self->str, self->length);        if (right)            Py_UNICODE_FILL(u->str + left + self->length, fill, right);    }     return u;} #define SPLIT_APPEND(data, left, right)					\	str = PyUnicode_FromUnicode((data) + (left), (right) - (left));	\	if (!str)							\	    goto onError;						\	if (PyList_Append(list, str)) {					\	    Py_DECREF(str);						\	    goto onError;						\	}								\        else								\            Py_DECREF(str); staticPyObject *split_whitespace(PyUnicodeObject *self,			   PyObject *list,			   Py_ssize_t maxcount){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len = self->length;    PyObject *str;     for (i = j = 0; i < len; ) {	/* find a token */	while (i < len && Py_UNICODE_ISSPACE(self->str[i]))	    i++;	j = i;	while (i < len && !Py_UNICODE_ISSPACE(self->str[i]))	    i++;	if (j < i) {	    if (maxcount-- <= 0)		break;	    SPLIT_APPEND(self->str, j, i);	    while (i < len && Py_UNICODE_ISSPACE(self->str[i]))		i++;	    j = i;	}    }    if (j < len) {	SPLIT_APPEND(self->str, j, len);    }    return list;  onError:    Py_DECREF(list);    return NULL;} PyObject *PyUnicode_Splitlines(PyObject *string,			       int keepends){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len;    PyObject *list;    PyObject *str;    Py_UNICODE *data;     string = PyUnicode_FromObject(string);    if (string == NULL)	return NULL;    data = PyUnicode_AS_UNICODE(string);    len = PyUnicode_GET_SIZE(string);     list = PyList_New(0);    if (!list)        goto onError;     for (i = j = 0; i < len; ) {	Py_ssize_t eol; 	/* Find a line and append it */	while (i < len && !BLOOM_LINEBREAK(data[i]))	    i++; 	/* Skip the line break reading CRLF as one line break */	eol = i;	if (i < len) {	    if (data[i] == '\r' && i + 1 < len &&		data[i+1] == '\n')		i += 2;	    else		i++;	    if (keepends)		eol = i;	}	SPLIT_APPEND(data, j, eol);	j = i;    }    if (j < len) {	SPLIT_APPEND(data, j, len);    }     Py_DECREF(string);    return list;  onError:    Py_XDECREF(list);    Py_DECREF(string);    return NULL;} staticPyObject *split_char(PyUnicodeObject *self,		     PyObject *list,		     Py_UNICODE ch,		     Py_ssize_t maxcount){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len = self->length;    PyObject *str;     for (i = j = 0; i < len; ) {	if (self->str[i] == ch) {	    if (maxcount-- <= 0)		break;	    SPLIT_APPEND(self->str, j, i);	    i = j = i + 1;	} else	    i++;    }    if (j <= len) {	SPLIT_APPEND(self->str, j, len);    }    return list;  onError:    Py_DECREF(list);    return NULL;} staticPyObject *split_substring(PyUnicodeObject *self,			  PyObject *list,			  PyUnicodeObject *substring,			  Py_ssize_t maxcount){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len = self->length;    Py_ssize_t sublen = substring->length;    PyObject *str;     for (i = j = 0; i <= len - sublen; ) {	if (Py_UNICODE_MATCH(self, i, substring)) {	    if (maxcount-- <= 0)		break;	    SPLIT_APPEND(self->str, j, i);	    i = j = i + sublen;	} else	    i++;    }    if (j <= len) {	SPLIT_APPEND(self->str, j, len);    }    return list;  onError:    Py_DECREF(list);    return NULL;} staticPyObject *rsplit_whitespace(PyUnicodeObject *self,			    PyObject *list,			    Py_ssize_t maxcount){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len = self->length;    PyObject *str;     for (i = j = len - 1; i >= 0; ) {	/* find a token */	while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i]))	    i--;	j = i;	while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i]))	    i--;	if (j > i) {	    if (maxcount-- <= 0)		break;	    SPLIT_APPEND(self->str, i + 1, j + 1);	    while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i]))		i--;	    j = i;	}    }    if (j >= 0) {	SPLIT_APPEND(self->str, 0, j + 1);    }    if (PyList_Reverse(list) < 0)        goto onError;    return list;  onError:    Py_DECREF(list);    return NULL;} static PyObject *rsplit_char(PyUnicodeObject *self,		      PyObject *list,		      Py_UNICODE ch,		      Py_ssize_t maxcount){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len = self->length;    PyObject *str;     for (i = j = len - 1; i >= 0; ) {	if (self->str[i] == ch) {	    if (maxcount-- <= 0)		break;	    SPLIT_APPEND(self->str, i + 1, j + 1);	    j = i = i - 1;	} else	    i--;    }    if (j >= -1) {	SPLIT_APPEND(self->str, 0, j + 1);    }    if (PyList_Reverse(list) < 0)        goto onError;    return list;  onError:    Py_DECREF(list);    return NULL;} static PyObject *rsplit_substring(PyUnicodeObject *self,			   PyObject *list,			   PyUnicodeObject *substring,			   Py_ssize_t maxcount){    register Py_ssize_t i;    register Py_ssize_t j;    Py_ssize_t len = self->length;    Py_ssize_t sublen = substring->length;    PyObject *str;     for (i = len - sublen, j = len; i >= 0; ) {	if (Py_UNICODE_MATCH(self, i, substring)) {	    if (maxcount-- <= 0)		break;	    SPLIT_APPEND(self->str, i + sublen, j);	    j = i;	    i -= sublen;	} else	    i--;    }    if (j >= 0) {	SPLIT_APPEND(self->str, 0, j);    }    if (PyList_Reverse(list) < 0)        goto onError;    return list;  onError:    Py_DECREF(list);    return NULL;} #undef SPLIT_APPEND staticPyObject *split(PyUnicodeObject *self,

Lines 4501–5000 of 8067. Previous lines