cinap_lenrekadd hg and python4bb9e2215 years agoHistory
5009 lines · 120.0 KB · C
RawDownload
	size = a->ob_size * n;	if (n && size / n != a->ob_size) {		PyErr_SetString(PyExc_OverflowError,			"repeated string is too long");		return NULL;	}	if (size == a->ob_size && PyString_CheckExact(a)) {		Py_INCREF(a);		return (PyObject *)a;	}	nbytes = (size_t)size;	if (nbytes + sizeof(PyStringObject) <= nbytes) {		PyErr_SetString(PyExc_OverflowError,			"repeated string is too long");		return NULL;	}	op = (PyStringObject *)		PyObject_MALLOC(sizeof(PyStringObject) + nbytes);	if (op == NULL)		return PyErr_NoMemory();	PyObject_INIT_VAR(op, &PyString_Type, size);	op->ob_shash = -1;	op->ob_sstate = SSTATE_NOT_INTERNED;	op->ob_sval[size] = '\0';	if (a->ob_size == 1 && n > 0) {		memset(op->ob_sval, a->ob_sval[0] , n);		return (PyObject *) op;	}	i = 0;	if (i < size) {		Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size);		i = a->ob_size;	}	while (i < size) {		j = (i <= size-i)  ?  i  :  size-i;		Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);		i += j;	}	return (PyObject *) op;} /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ static PyObject *string_slice(register PyStringObject *a, register Py_ssize_t i,	     register Py_ssize_t j)     /* j -- may be negative! */{	if (i < 0)		i = 0;	if (j < 0)		j = 0; /* Avoid signed/unsigned bug in next line */	if (j > a->ob_size)		j = a->ob_size;	if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) {		/* It's the same as a */		Py_INCREF(a);		return (PyObject *)a;	}	if (j < i)		j = i;	return PyString_FromStringAndSize(a->ob_sval + i, j-i);} static intstring_contains(PyObject *str_obj, PyObject *sub_obj){	if (!PyString_CheckExact(sub_obj)) {#ifdef Py_USING_UNICODE		if (PyUnicode_Check(sub_obj))			return PyUnicode_Contains(str_obj, sub_obj);#endif		if (!PyString_Check(sub_obj)) {			PyErr_SetString(PyExc_TypeError,			    "'in <string>' requires string as left operand");			return -1;		}	} 	return stringlib_contains_obj(str_obj, sub_obj);} static PyObject *string_item(PyStringObject *a, register Py_ssize_t i){	char pchar;	PyObject *v;	if (i < 0 || i >= a->ob_size) {		PyErr_SetString(PyExc_IndexError, "string index out of range");		return NULL;	}	pchar = a->ob_sval[i];	v = (PyObject *)characters[pchar & UCHAR_MAX];	if (v == NULL)		v = PyString_FromStringAndSize(&pchar, 1);	else {#ifdef COUNT_ALLOCS		one_strings++;#endif		Py_INCREF(v);	}	return v;} static PyObject*string_richcompare(PyStringObject *a, PyStringObject *b, int op){	int c;	Py_ssize_t len_a, len_b;	Py_ssize_t min_len;	PyObject *result; 	/* Make sure both arguments are strings. */	if (!(PyString_Check(a) && PyString_Check(b))) {		result = Py_NotImplemented;		goto out;	}	if (a == b) {		switch (op) {		case Py_EQ:case Py_LE:case Py_GE:			result = Py_True;			goto out;		case Py_NE:case Py_LT:case Py_GT:			result = Py_False;			goto out;		}	}	if (op == Py_EQ) {		/* Supporting Py_NE here as well does not save		   much time, since Py_NE is rarely used.  */		if (a->ob_size == b->ob_size		    && (a->ob_sval[0] == b->ob_sval[0]			&& memcmp(a->ob_sval, b->ob_sval,				  a->ob_size) == 0)) {			result = Py_True;		} else {			result = Py_False;		}		goto out;	}	len_a = a->ob_size; len_b = b->ob_size;	min_len = (len_a < len_b) ? len_a : len_b;	if (min_len > 0) {		c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);		if (c==0)			c = memcmp(a->ob_sval, b->ob_sval, min_len);	}else		c = 0;	if (c == 0)		c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;	switch (op) {	case Py_LT: c = c <  0; break;	case Py_LE: c = c <= 0; break;	case Py_EQ: assert(0);  break; /* unreachable */	case Py_NE: c = c != 0; break;	case Py_GT: c = c >  0; break;	case Py_GE: c = c >= 0; break;	default:		result = Py_NotImplemented;		goto out;	}	result = c ? Py_True : Py_False;  out:	Py_INCREF(result);	return result;} int_PyString_Eq(PyObject *o1, PyObject *o2){	PyStringObject *a = (PyStringObject*) o1;	PyStringObject *b = (PyStringObject*) o2;        return a->ob_size == b->ob_size          && *a->ob_sval == *b->ob_sval          && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0;} static longstring_hash(PyStringObject *a){	register Py_ssize_t len;	register unsigned char *p;	register long x; 	if (a->ob_shash != -1)		return a->ob_shash;	len = a->ob_size;	p = (unsigned char *) a->ob_sval;	x = *p << 7;	while (--len >= 0)		x = (1000003*x) ^ *p++;	x ^= a->ob_size;	if (x == -1)		x = -2;	a->ob_shash = x;	return x;} static PyObject*string_subscript(PyStringObject* self, PyObject* item){	if (PyIndex_Check(item)) {		Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);		if (i == -1 && PyErr_Occurred())			return NULL;		if (i < 0)			i += PyString_GET_SIZE(self);		return string_item(self, i);	}	else if (PySlice_Check(item)) {		Py_ssize_t start, stop, step, slicelength, cur, i;		char* source_buf;		char* result_buf;		PyObject* result; 		if (PySlice_GetIndicesEx((PySliceObject*)item,				 PyString_GET_SIZE(self),				 &start, &stop, &step, &slicelength) < 0) {			return NULL;		} 		if (slicelength <= 0) {			return PyString_FromStringAndSize("", 0);		}		else {			source_buf = PyString_AsString((PyObject*)self);			result_buf = (char *)PyMem_Malloc(slicelength);			if (result_buf == NULL)				return PyErr_NoMemory(); 			for (cur = start, i = 0; i < slicelength;			     cur += step, i++) {				result_buf[i] = source_buf[cur];			} 			result = PyString_FromStringAndSize(result_buf,							    slicelength);			PyMem_Free(result_buf);			return result;		}	}	else {		PyErr_SetString(PyExc_TypeError,				"string indices must be integers");		return NULL;	}} static Py_ssize_tstring_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr){	if ( index != 0 ) {		PyErr_SetString(PyExc_SystemError,				"accessing non-existent string segment");		return -1;	}	*ptr = (void *)self->ob_sval;	return self->ob_size;} static Py_ssize_tstring_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr){	PyErr_SetString(PyExc_TypeError,			"Cannot use string as modifiable buffer");	return -1;} static Py_ssize_tstring_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp){	if ( lenp )		*lenp = self->ob_size;	return 1;} static Py_ssize_tstring_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr){	if ( index != 0 ) {		PyErr_SetString(PyExc_SystemError,				"accessing non-existent string segment");		return -1;	}	*ptr = self->ob_sval;	return self->ob_size;} static PySequenceMethods string_as_sequence = {	(lenfunc)string_length, /*sq_length*/	(binaryfunc)string_concat, /*sq_concat*/	(ssizeargfunc)string_repeat, /*sq_repeat*/	(ssizeargfunc)string_item, /*sq_item*/	(ssizessizeargfunc)string_slice, /*sq_slice*/	0,		/*sq_ass_item*/	0,		/*sq_ass_slice*/	(objobjproc)string_contains /*sq_contains*/}; static PyMappingMethods string_as_mapping = {	(lenfunc)string_length,	(binaryfunc)string_subscript,	0,}; static PyBufferProcs string_as_buffer = {	(readbufferproc)string_buffer_getreadbuf,	(writebufferproc)string_buffer_getwritebuf,	(segcountproc)string_buffer_getsegcount,	(charbufferproc)string_buffer_getcharbuf,};  #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)  /* Don't call if length < 2 */#define Py_STRING_MATCH(target, offset, pattern, length)	\  (target[offset] == pattern[0] &&				\   target[offset+length-1] == pattern[length-1] &&		\   !memcmp(target+offset+1, pattern+1, length-2) )  /* Overallocate the initial list to reduce the number of reallocs for small   split sizes.  Eg, "A A A A A A A A A A".split() (10 elements) has three   resizes, to sizes 4, 8, then 16.  Most observed string splits are for human   text (roughly 11 words per line) and field delimited data (usually 1-10   fields).  For large strings the split algorithms are bandwidth limited   so increasing the preallocation likely will not improve things.*/ #define MAX_PREALLOC 12 /* 5 splits gives 6 elements */#define PREALLOC_SIZE(maxsplit) \	(maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) #define SPLIT_APPEND(data, left, right)				\	str = PyString_FromStringAndSize((data) + (left),	\					 (right) - (left));	\	if (str == NULL)					\		goto onError;					\	if (PyList_Append(list, str)) {				\		Py_DECREF(str);					\		goto onError;					\	}							\	else							\		Py_DECREF(str); #define SPLIT_ADD(data, left, right) {				\	str = PyString_FromStringAndSize((data) + (left),	\					 (right) - (left));	\	if (str == NULL)					\		goto onError;					\	if (count < MAX_PREALLOC) {				\		PyList_SET_ITEM(list, count, str);		\	} else {						\		if (PyList_Append(list, str)) {			\			Py_DECREF(str);				\			goto onError;				\		}						\		else						\			Py_DECREF(str);				\	}							\	count++; } /* Always force the list to the expected size. */#define FIX_PREALLOC_SIZE(list) ((PyListObject *)list)->ob_size = count #define SKIP_SPACE(s, i, len)    { while (i<len &&  isspace(Py_CHARMASK(s[i]))) i++; }#define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; }#define RSKIP_SPACE(s, i)        { while (i>=0  &&  isspace(Py_CHARMASK(s[i]))) i--; }#define RSKIP_NONSPACE(s, i)     { while (i>=0  && !isspace(Py_CHARMASK(s[i]))) i--; } Py_LOCAL_INLINE(PyObject *)split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit){	Py_ssize_t i, j, count=0;	PyObject *str;	PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); 	if (list == NULL)		return NULL; 	i = j = 0; 	while (maxsplit-- > 0) {		SKIP_SPACE(s, i, len);		if (i==len) break;		j = i; i++;		SKIP_NONSPACE(s, i, len);		SPLIT_ADD(s, j, i);	} 	if (i < len) {		/* Only occurs when maxsplit was reached */		/* Skip any remaining whitespace and copy to end of string */		SKIP_SPACE(s, i, len);		if (i != len)			SPLIT_ADD(s, i, len);	}	FIX_PREALLOC_SIZE(list);	return list;  onError:	Py_DECREF(list);	return NULL;} Py_LOCAL_INLINE(PyObject *)split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount){	register Py_ssize_t i, j, count=0;	PyObject *str;	PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); 	if (list == NULL)		return NULL; 	i = j = 0;	while ((j < len) && (maxcount-- > 0)) {		for(; j<len; j++) {			/* I found that using memchr makes no difference */			if (s[j] == ch) {				SPLIT_ADD(s, i, j);				i = j = j + 1;				break;			}		}	}	if (i <= len) {		SPLIT_ADD(s, i, len);	}	FIX_PREALLOC_SIZE(list);	return list;   onError:	Py_DECREF(list);	return NULL;} PyDoc_STRVAR(split__doc__,"S.split([sep [,maxsplit]]) -> list of strings\n\\n\Return a list of the words in the string S, using sep as the\n\delimiter string.  If maxsplit is given, at most maxsplit\n\splits are done. If sep is not specified or is None, any\n\whitespace string is a separator."); static PyObject *string_split(PyStringObject *self, PyObject *args){	Py_ssize_t len = PyString_GET_SIZE(self), n, i, j;	Py_ssize_t maxsplit = -1, count=0;	const char *s = PyString_AS_STRING(self), *sub;	PyObject *list, *str, *subobj = Py_None;#ifdef USE_FAST	Py_ssize_t pos;#endif 	if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))		return NULL;	if (maxsplit < 0)		maxsplit = PY_SSIZE_T_MAX;	if (subobj == Py_None)		return split_whitespace(s, len, maxsplit);	if (PyString_Check(subobj)) {		sub = PyString_AS_STRING(subobj);		n = PyString_GET_SIZE(subobj);	}#ifdef Py_USING_UNICODE	else if (PyUnicode_Check(subobj))		return PyUnicode_Split((PyObject *)self, subobj, maxsplit);#endif	else if (PyObject_AsCharBuffer(subobj, &sub, &n))		return NULL; 	if (n == 0) {		PyErr_SetString(PyExc_ValueError, "empty separator");		return NULL;	}	else if (n == 1)		return split_char(s, len, sub[0], maxsplit); 	list = PyList_New(PREALLOC_SIZE(maxsplit));	if (list == NULL)		return NULL; #ifdef USE_FAST	i = j = 0;	while (maxsplit-- > 0) {		pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);		if (pos < 0)			break;

Lines 1001–1500 of 5009. Previous lines