cinap_lenrekadd hg and python4bb9e2215 years agoHistory
1248 lines · 27.8 KB · C
RawDownload
  MEM, the function returns -1.*/static Py_ssize_t mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len){	register Py_ssize_t ii; 	/* pattern can not occur in the last pat_len-1 chars */	len -= pat_len; 	for (ii = 0; ii <= len; ii++) {		if (mem[ii] == pat[0] &&		    (pat_len == 1 ||		     memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {			return ii;		}	}	return -1;} /*  mymemcnt    Return the number of distinct times PAT is found in MEM.   meaning mem=1111 and pat==11 returns 2.           mem=11111 and pat==11 also return 2. */static Py_ssize_t mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len){	register Py_ssize_t offset = 0;	Py_ssize_t nfound = 0; 	while (len >= 0) {		offset = mymemfind(mem, len, pat, pat_len);		if (offset == -1)			break;		mem += offset + pat_len;		len -= offset + pat_len;		nfound++;	}	return nfound;} /*   mymemreplace    Return a string in which all occurrences of PAT in memory STR are   replaced with SUB.    If length of PAT is less than length of STR or there are no occurrences   of PAT in STR, then the original string is returned. Otherwise, a new   string is allocated here and returned.    on return, out_len is:       the length of output string, or       -1 if the input string is returned, or       unchanged if an error occurs (no memory).    return value is:       the new string allocated locally, or       NULL if an error occurred.*/static char *mymemreplace(const char *str, Py_ssize_t len,		/* input string */             const char *pat, Py_ssize_t pat_len,	/* pattern string to find */             const char *sub, Py_ssize_t sub_len,	/* substitution string */             Py_ssize_t count,				/* number of replacements */	     Py_ssize_t *out_len){	char *out_s;	char *new_s;	Py_ssize_t nfound, offset, new_len; 	if (len == 0 || pat_len > len)		goto return_same; 	/* find length of output string */	nfound = mymemcnt(str, len, pat, pat_len);	if (count < 0)		count = PY_SSIZE_T_MAX;	else if (nfound > count)		nfound = count;	if (nfound == 0)		goto return_same; 	new_len = len + nfound*(sub_len - pat_len);	if (new_len == 0) {		/* Have to allocate something for the caller to free(). */		out_s = (char *)PyMem_MALLOC(1);		if (out_s == NULL)			return NULL;		out_s[0] = '\0';	}	else {		assert(new_len > 0);		new_s = (char *)PyMem_MALLOC(new_len);		if (new_s == NULL)			return NULL;		out_s = new_s; 		for (; count > 0 && len > 0; --count) {			/* find index of next instance of pattern */			offset = mymemfind(str, len, pat, pat_len);			if (offset == -1)				break; 			/* copy non matching part of input string */			memcpy(new_s, str, offset);			str += offset + pat_len;			len -= offset + pat_len; 			/* copy substitute into the output string */			new_s += offset;			memcpy(new_s, sub, sub_len);			new_s += sub_len;		}		/* copy any remaining values into output string */		if (len > 0)			memcpy(new_s, str, len);	}	*out_len = new_len;	return out_s;   return_same:	*out_len = -1;	return (char *)str; /* cast away const */}  PyDoc_STRVAR(replace__doc__,"replace (str, old, new[, maxsplit]) -> string\n""\n""Return a copy of string str 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 *strop_replace(PyObject *self, PyObject *args){	char *str, *pat,*sub,*new_s;	Py_ssize_t len,pat_len,sub_len,out_len;	Py_ssize_t count = -1;	PyObject *newstr; 	WARN;	if (!PyArg_ParseTuple(args, "t#t#t#|n:replace",			      &str, &len, &pat, &pat_len, &sub, &sub_len,			      &count))		return NULL;	if (pat_len <= 0) {		PyErr_SetString(PyExc_ValueError, "empty pattern string");		return NULL;	}	/* CAUTION:  strop treats a replace count of 0 as infinity, unlke	 * current (2.1) string.py and string methods.  Preserve this for	 * ... well, hard to say for what <wink>.	 */	if (count == 0)		count = -1;	new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);	if (new_s == NULL) {		PyErr_NoMemory();		return NULL;	}	if (out_len == -1) {		/* we're returning another reference to the input string */		newstr = PyTuple_GetItem(args, 0);		Py_XINCREF(newstr);	}	else {		newstr = PyString_FromStringAndSize(new_s, out_len);		PyMem_FREE(new_s);	}	return newstr;}  /* List of functions defined in the module */ static PyMethodDefstrop_methods[] = {	{"atof",	strop_atof,	   METH_VARARGS, atof__doc__},	{"atoi",	strop_atoi,	   METH_VARARGS, atoi__doc__},	{"atol",	strop_atol,	   METH_VARARGS, atol__doc__},	{"capitalize",	strop_capitalize,  METH_O,       capitalize__doc__},	{"count",	strop_count,	   METH_VARARGS, count__doc__},	{"expandtabs",	strop_expandtabs,  METH_VARARGS, expandtabs__doc__},	{"find",	strop_find,	   METH_VARARGS, find__doc__},	{"join",	strop_joinfields,  METH_VARARGS, joinfields__doc__},	{"joinfields",	strop_joinfields,  METH_VARARGS, joinfields__doc__},	{"lstrip",	strop_lstrip,	   METH_O,       lstrip__doc__},	{"lower",	strop_lower,	   METH_O,       lower__doc__},	{"maketrans",	strop_maketrans,   METH_VARARGS, maketrans__doc__},	{"replace",	strop_replace,	   METH_VARARGS, replace__doc__},	{"rfind",	strop_rfind,	   METH_VARARGS, rfind__doc__},	{"rstrip",	strop_rstrip,	   METH_O,       rstrip__doc__},	{"split",	strop_splitfields, METH_VARARGS, splitfields__doc__},	{"splitfields",	strop_splitfields, METH_VARARGS, splitfields__doc__},	{"strip",	strop_strip,	   METH_O,       strip__doc__},	{"swapcase",	strop_swapcase,    METH_O,       swapcase__doc__},	{"translate",	strop_translate,   METH_VARARGS, translate__doc__},	{"upper",	strop_upper,	   METH_O,       upper__doc__},	{NULL,		NULL}	/* sentinel */};  PyMODINIT_FUNCinitstrop(void){	PyObject *m, *s;	char buf[256];	int c, n;	m = Py_InitModule4("strop", strop_methods, strop_module__doc__,			   (PyObject*)NULL, PYTHON_API_VERSION);	if (m == NULL)		return; 	/* Create 'whitespace' object */	n = 0;	for (c = 0; c < 256; c++) {		if (isspace(c))			buf[n++] = c;	}	s = PyString_FromStringAndSize(buf, n);	if (s)		PyModule_AddObject(m, "whitespace", s); 	/* Create 'lowercase' object */	n = 0;	for (c = 0; c < 256; c++) {		if (islower(c))			buf[n++] = c;	}	s = PyString_FromStringAndSize(buf, n);	if (s)		PyModule_AddObject(m, "lowercase", s); 	/* Create 'uppercase' object */	n = 0;	for (c = 0; c < 256; c++) {		if (isupper(c))			buf[n++] = c;	}	s = PyString_FromStringAndSize(buf, n);	if (s)		PyModule_AddObject(m, "uppercase", s);}

Lines 1001–1248 of 1248. Previous lines