cinap_lenrekadd hg and python4bb9e2215 years agoHistory
8067 lines · 210.4 KB · C
RawDownload
		PyUnicodeObject *substring,		Py_ssize_t maxcount){    PyObject *list;     if (maxcount < 0)        maxcount = PY_SSIZE_T_MAX;     list = PyList_New(0);    if (!list)        return NULL;     if (substring == NULL)	return split_whitespace(self,list,maxcount);     else if (substring->length == 1)	return split_char(self,list,substring->str[0],maxcount);     else if (substring->length == 0) {	Py_DECREF(list);	PyErr_SetString(PyExc_ValueError, "empty separator");	return NULL;    }    else	return split_substring(self,list,substring,maxcount);} staticPyObject *rsplit(PyUnicodeObject *self,		 PyUnicodeObject *substring,		 Py_ssize_t maxcount){    PyObject *list;     if (maxcount < 0)        maxcount = PY_SSIZE_T_MAX;     list = PyList_New(0);    if (!list)        return NULL;     if (substring == NULL)	return rsplit_whitespace(self,list,maxcount);     else if (substring->length == 1)	return rsplit_char(self,list,substring->str[0],maxcount);     else if (substring->length == 0) {	Py_DECREF(list);	PyErr_SetString(PyExc_ValueError, "empty separator");	return NULL;    }    else	return rsplit_substring(self,list,substring,maxcount);} staticPyObject *replace(PyUnicodeObject *self,		  PyUnicodeObject *str1,		  PyUnicodeObject *str2,		  Py_ssize_t maxcount){    PyUnicodeObject *u;     if (maxcount < 0)	maxcount = PY_SSIZE_T_MAX;     if (str1->length == str2->length) {        /* same length */        Py_ssize_t i;        if (str1->length == 1) {            /* replace characters */            Py_UNICODE u1, u2;            if (!findchar(self->str, self->length, str1->str[0]))                goto nothing;            u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);            if (!u)                return NULL;            Py_UNICODE_COPY(u->str, self->str, self->length);            u1 = str1->str[0];            u2 = str2->str[0];            for (i = 0; i < u->length; i++)                if (u->str[i] == u1) {                    if (--maxcount < 0)                        break;                    u->str[i] = u2;                }        } else {            i = fastsearch(                self->str, self->length, str1->str, str1->length, FAST_SEARCH                );            if (i < 0)                goto nothing;            u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);            if (!u)                return NULL;            Py_UNICODE_COPY(u->str, self->str, self->length);            while (i <= self->length - str1->length)                if (Py_UNICODE_MATCH(self, i, str1)) {                    if (--maxcount < 0)                        break;                    Py_UNICODE_COPY(u->str+i, str2->str, str2->length);                    i += str1->length;                } else                    i++;        }    } else {         Py_ssize_t n, i, j, e;        Py_ssize_t product, new_size, delta;        Py_UNICODE *p;         /* replace strings */        n = stringlib_count(self->str, self->length, str1->str, str1->length);        if (n > maxcount)            n = maxcount;        if (n == 0)            goto nothing;        /* new_size = self->length + n * (str2->length - str1->length)); */        delta = (str2->length - str1->length);        if (delta == 0) {            new_size = self->length;        } else {            product = n * (str2->length - str1->length);            if ((product / (str2->length - str1->length)) != n) {                PyErr_SetString(PyExc_OverflowError,                                "replace string is too long");                return NULL;            }            new_size = self->length + product;            if (new_size < 0) {                PyErr_SetString(PyExc_OverflowError,                                "replace string is too long");                return NULL;            }        }        u = _PyUnicode_New(new_size);        if (!u)            return NULL;        i = 0;        p = u->str;        e = self->length - str1->length;        if (str1->length > 0) {            while (n-- > 0) {                /* look for next match */                j = i;                while (j <= e) {                    if (Py_UNICODE_MATCH(self, j, str1))                        break;                    j++;                }		if (j > i) {                    if (j > e)                        break;                    /* copy unchanged part [i:j] */                    Py_UNICODE_COPY(p, self->str+i, j-i);                    p += j - i;                }                /* copy substitution string */                if (str2->length > 0) {                    Py_UNICODE_COPY(p, str2->str, str2->length);                    p += str2->length;                }                i = j + str1->length;            }            if (i < self->length)                /* copy tail [i:] */                Py_UNICODE_COPY(p, self->str+i, self->length-i);        } else {            /* interleave */            while (n > 0) {                Py_UNICODE_COPY(p, str2->str, str2->length);                p += str2->length;                if (--n <= 0)                    break;                *p++ = self->str[i++];            }            Py_UNICODE_COPY(p, self->str+i, self->length-i);        }    }    return (PyObject *) u; nothing:    /* nothing to replace; return original string (when possible) */    if (PyUnicode_CheckExact(self)) {        Py_INCREF(self);        return (PyObject *) self;    }    return PyUnicode_FromUnicode(self->str, self->length);} /* --- Unicode Object Methods --------------------------------------------- */ PyDoc_STRVAR(title__doc__,"S.title() -> unicode\n\\n\Return a titlecased version of S, i.e. words start with title case\n\characters, all remaining cased characters have lower case."); static PyObject*unicode_title(PyUnicodeObject *self){    return fixup(self, fixtitle);} PyDoc_STRVAR(capitalize__doc__,"S.capitalize() -> unicode\n\\n\Return a capitalized version of S, i.e. make the first character\n\have upper case."); static PyObject*unicode_capitalize(PyUnicodeObject *self){    return fixup(self, fixcapitalize);} #if 0PyDoc_STRVAR(capwords__doc__,"S.capwords() -> unicode\n\\n\Apply .capitalize() to all words in S and return the result with\n\normalized whitespace (all whitespace strings are replaced by ' ')."); static PyObject*unicode_capwords(PyUnicodeObject *self){    PyObject *list;    PyObject *item;    Py_ssize_t i;     /* Split into words */    list = split(self, NULL, -1);    if (!list)        return NULL;     /* Capitalize each word */    for (i = 0; i < PyList_GET_SIZE(list); i++) {        item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i),		     fixcapitalize);        if (item == NULL)            goto onError;        Py_DECREF(PyList_GET_ITEM(list, i));        PyList_SET_ITEM(list, i, item);    }     /* Join the words to form a new string */    item = PyUnicode_Join(NULL, list); onError:    Py_DECREF(list);    return (PyObject *)item;}#endif /* Argument converter.  Coerces to a single unicode character */ static intconvert_uc(PyObject *obj, void *addr){	Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;	PyObject *uniobj;	Py_UNICODE *unistr; 	uniobj = PyUnicode_FromObject(obj);	if (uniobj == NULL) {		PyErr_SetString(PyExc_TypeError,			"The fill character cannot be converted to Unicode");		return 0;	}	if (PyUnicode_GET_SIZE(uniobj) != 1) {		PyErr_SetString(PyExc_TypeError,			"The fill character must be exactly one character long");		Py_DECREF(uniobj);		return 0;	}	unistr = PyUnicode_AS_UNICODE(uniobj);	*fillcharloc = unistr[0];	Py_DECREF(uniobj);	return 1;} PyDoc_STRVAR(center__doc__,"S.center(width[, fillchar]) -> unicode\n\\n\Return S centered in a Unicode string of length width. Padding is\n\done using the specified fill character (default is a space)"); static PyObject *unicode_center(PyUnicodeObject *self, PyObject *args){    Py_ssize_t marg, left;    Py_ssize_t width;    Py_UNICODE fillchar = ' ';     if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar))        return NULL;     if (self->length >= width && PyUnicode_CheckExact(self)) {        Py_INCREF(self);        return (PyObject*) self;    }     marg = width - self->length;    left = marg / 2 + (marg & width & 1);     return (PyObject*) pad(self, left, marg - left, fillchar);} #if 0 /* This code should go into some future Unicode collation support   module. The basic comparison should compare ordinals on a naive   basis (this is what Java does and thus JPython too). */ /* speedy UTF-16 code point order comparison *//* gleaned from: *//* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ static short utf16Fixup[32] ={    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800}; static intunicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2){    Py_ssize_t len1, len2;     Py_UNICODE *s1 = str1->str;    Py_UNICODE *s2 = str2->str;     len1 = str1->length;    len2 = str2->length;     while (len1 > 0 && len2 > 0) {        Py_UNICODE c1, c2;         c1 = *s1++;        c2 = *s2++; 	if (c1 > (1<<11) * 26)	    c1 += utf16Fixup[c1>>11];	if (c2 > (1<<11) * 26)            c2 += utf16Fixup[c2>>11];        /* now c1 and c2 are in UTF-32-compatible order */         if (c1 != c2)            return (c1 < c2) ? -1 : 1;         len1--; len2--;    }     return (len1 < len2) ? -1 : (len1 != len2);} #else static intunicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2){    register Py_ssize_t len1, len2;     Py_UNICODE *s1 = str1->str;    Py_UNICODE *s2 = str2->str;     len1 = str1->length;    len2 = str2->length;     while (len1 > 0 && len2 > 0) {        Py_UNICODE c1, c2;         c1 = *s1++;        c2 = *s2++;         if (c1 != c2)            return (c1 < c2) ? -1 : 1;         len1--; len2--;    }     return (len1 < len2) ? -1 : (len1 != len2);} #endif int PyUnicode_Compare(PyObject *left,		      PyObject *right){    PyUnicodeObject *u = NULL, *v = NULL;    int result;     /* Coerce the two arguments */    u = (PyUnicodeObject *)PyUnicode_FromObject(left);    if (u == NULL)	goto onError;    v = (PyUnicodeObject *)PyUnicode_FromObject(right);    if (v == NULL)	goto onError;     /* Shortcut for empty or interned objects */    if (v == u) {	Py_DECREF(u);	Py_DECREF(v);	return 0;    }     result = unicode_compare(u, v);     Py_DECREF(u);    Py_DECREF(v);    return result; onError:    Py_XDECREF(u);    Py_XDECREF(v);    return -1;} PyObject *PyUnicode_RichCompare(PyObject *left,                                PyObject *right,                                int op){    int result;     result = PyUnicode_Compare(left, right);    if (result == -1 && PyErr_Occurred())        goto onError;     /* Convert the return value to a Boolean */    switch (op) {    case Py_EQ:        result = (result == 0);        break;    case Py_NE:        result = (result != 0);        break;    case Py_LE:        result = (result <= 0);        break;    case Py_GE:        result = (result >= 0);        break;    case Py_LT:        result = (result == -1);        break;    case Py_GT:        result = (result == 1);        break;    }    return PyBool_FromLong(result);  onError:     /* Standard case        Type errors mean that PyUnicode_FromObject() could not convert       one of the arguments (usually the right hand side) to Unicode,       ie. we can't handle the comparison request. However, it is       possible that the other object knows a comparison method, which       is why we return Py_NotImplemented to give the other object a       chance.     */    if (PyErr_ExceptionMatches(PyExc_TypeError)) {        PyErr_Clear();        Py_INCREF(Py_NotImplemented);        return Py_NotImplemented;    }    if (op != Py_EQ && op != Py_NE)        return NULL;     /* Equality comparison.        This is a special case: we silence any PyExc_UnicodeDecodeError       and instead turn it into a PyErr_UnicodeWarning.     */    if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError))        return NULL;    PyErr_Clear();    if (PyErr_Warn(PyExc_UnicodeWarning,                    (op == Py_EQ) ?                    "Unicode equal comparison "                   "failed to convert both arguments to Unicode - "                   "interpreting them as being unequal" :                   "Unicode unequal comparison "                   "failed to convert both arguments to Unicode - "                   "interpreting them as being unequal"                   ) < 0)        return NULL;    result = (op == Py_NE);    return PyBool_FromLong(result);} int PyUnicode_Contains(PyObject *container,		       PyObject *element)

Lines 5001–5500 of 8067. Previous lines