if (islower(c)) { changed = 1; *s_new = toupper(c); } else *s_new = c; s_new++; } if (!changed) { Py_DECREF(newstr); Py_INCREF(args); return args; } return newstr;} PyDoc_STRVAR(capitalize__doc__,"capitalize(s) -> string\n""\n""Return a copy of the string s with only its first character\n""capitalized."); static PyObject *strop_capitalize(PyObject *self, PyObject *args){ char *s, *s_new; Py_ssize_t i, n; PyObject *newstr; int changed; WARN; if (PyString_AsStringAndSize(args, &s, &n)) return NULL; newstr = PyString_FromStringAndSize(NULL, n); if (newstr == NULL) return NULL; s_new = PyString_AsString(newstr); changed = 0; if (0 < n) { int c = Py_CHARMASK(*s++); if (islower(c)) { changed = 1; *s_new = toupper(c); } else *s_new = c; s_new++; } for (i = 1; i < n; i++) { int c = Py_CHARMASK(*s++); if (isupper(c)) { changed = 1; *s_new = tolower(c); } else *s_new = c; s_new++; } if (!changed) { Py_DECREF(newstr); Py_INCREF(args); return args; } return newstr;} PyDoc_STRVAR(expandtabs__doc__,"expandtabs(string, [tabsize]) -> string\n""\n""Expand tabs in a string, i.e. replace them by one or more spaces,\n""depending on the current column and the given tab size (default 8).\n""The column number is reset to zero after each newline occurring in the\n""string. This doesn't understand other non-printing characters."); static PyObject *strop_expandtabs(PyObject *self, PyObject *args){ /* Original by Fredrik Lundh */ char* e; char* p; char* q; Py_ssize_t i, j; PyObject* out; char* string; Py_ssize_t stringlen; int tabsize = 8; WARN; /* Get arguments */ if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize)) return NULL; if (tabsize < 1) { PyErr_SetString(PyExc_ValueError, "tabsize must be at least 1"); return NULL; } /* First pass: determine size of output string */ i = j = 0; /* j: current column; i: total of previous lines */ e = string + stringlen; for (p = string; p < e; p++) { if (*p == '\t') j += tabsize - (j%tabsize); else { j++; if (*p == '\n') { i += j; j = 0; } } } /* Second pass: create output string and fill it */ out = PyString_FromStringAndSize(NULL, i+j); if (out == NULL) return NULL; i = 0; q = PyString_AS_STRING(out); for (p = string; p < e; p++) { if (*p == '\t') { j = tabsize - (i%tabsize); i += j; while (j-- > 0) *q++ = ' '; } else { *q++ = *p; i++; if (*p == '\n') i = 0; } } return out;} PyDoc_STRVAR(count__doc__,"count(s, sub[, start[, end]]) -> int\n""\n""Return the number of occurrences of substring sub in string\n""s[start:end]. Optional arguments start and end are\n""interpreted as in slice notation."); static PyObject *strop_count(PyObject *self, PyObject *args){ char *s, *sub; Py_ssize_t len, n; Py_ssize_t i = 0, last = PY_SSIZE_T_MAX; Py_ssize_t m, r; WARN; if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last)) return NULL; if (last > len) last = len; if (last < 0) last += len; if (last < 0) last = 0; if (i < 0) i += len; if (i < 0) i = 0; m = last + 1 - n; if (n == 0) return PyInt_FromLong((long) (m-i)); r = 0; while (i < m) { if (!memcmp(s+i, sub, n)) { r++; i += n; } else { i++; } } return PyInt_FromLong((long) r);} PyDoc_STRVAR(swapcase__doc__,"swapcase(s) -> string\n""\n""Return a copy of the string s with upper case characters\n""converted to lowercase and vice versa."); static PyObject *strop_swapcase(PyObject *self, PyObject *args){ char *s, *s_new; Py_ssize_t i, n; PyObject *newstr; int changed; WARN; if (PyString_AsStringAndSize(args, &s, &n)) return NULL; newstr = PyString_FromStringAndSize(NULL, n); if (newstr == NULL) return NULL; s_new = PyString_AsString(newstr); changed = 0; for (i = 0; i < n; i++) { int c = Py_CHARMASK(*s++); if (islower(c)) { changed = 1; *s_new = toupper(c); } else if (isupper(c)) { changed = 1; *s_new = tolower(c); } else *s_new = c; s_new++; } if (!changed) { Py_DECREF(newstr); Py_INCREF(args); return args; } return newstr;} PyDoc_STRVAR(atoi__doc__,"atoi(s [,base]) -> int\n""\n""Return the integer represented by the string s in the given\n""base, which defaults to 10. The string s must consist of one\n""or more digits, possibly preceded by a sign. If base is 0, it\n""is chosen from the leading characters of s, 0 for octal, 0x or\n""0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n""accepted."); static PyObject *strop_atoi(PyObject *self, PyObject *args){ char *s, *end; int base = 10; long x; char buffer[256]; /* For errors */ WARN; if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base)) return NULL; if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, "invalid base for atoi()"); return NULL; } while (*s && isspace(Py_CHARMASK(*s))) s++; errno = 0; if (base == 0 && s[0] == '0') x = (long) PyOS_strtoul(s, &end, base); else x = PyOS_strtol(s, &end, base); if (end == s || !isalnum(Py_CHARMASK(end[-1]))) goto bad; while (*end && isspace(Py_CHARMASK(*end))) end++; if (*end != '\0') { bad: PyOS_snprintf(buffer, sizeof(buffer), "invalid literal for atoi(): %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } else if (errno != 0) { PyOS_snprintf(buffer, sizeof(buffer), "atoi() literal too large: %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } return PyInt_FromLong(x);} PyDoc_STRVAR(atol__doc__,"atol(s [,base]) -> long\n""\n""Return the long integer represented by the string s in the\n""given base, which defaults to 10. The string s must consist\n""of one or more digits, possibly preceded by a sign. If base\n""is 0, it is chosen from the leading characters of s, 0 for\n""octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n""0x or 0X is accepted. A trailing L or l is not accepted,\n""unless base is 0."); static PyObject *strop_atol(PyObject *self, PyObject *args){ char *s, *end; int base = 10; PyObject *x; char buffer[256]; /* For errors */ WARN; if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base)) return NULL; if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, "invalid base for atol()"); return NULL; } while (*s && isspace(Py_CHARMASK(*s))) s++; if (s[0] == '\0') { PyErr_SetString(PyExc_ValueError, "empty string for atol()"); return NULL; } x = PyLong_FromString(s, &end, base); if (x == NULL) return NULL; if (base == 0 && (*end == 'l' || *end == 'L')) end++; while (*end && isspace(Py_CHARMASK(*end))) end++; if (*end != '\0') { PyOS_snprintf(buffer, sizeof(buffer), "invalid literal for atol(): %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); Py_DECREF(x); return NULL; } return x;} PyDoc_STRVAR(atof__doc__,"atof(s) -> float\n""\n""Return the floating point number represented by the string s."); static PyObject *strop_atof(PyObject *self, PyObject *args){ char *s, *end; double x; char buffer[256]; /* For errors */ WARN; if (!PyArg_ParseTuple(args, "s:atof", &s)) return NULL; while (*s && isspace(Py_CHARMASK(*s))) s++; if (s[0] == '\0') { PyErr_SetString(PyExc_ValueError, "empty string for atof()"); return NULL; } errno = 0; PyFPE_START_PROTECT("strop_atof", return 0) x = PyOS_ascii_strtod(s, &end); PyFPE_END_PROTECT(x) while (*end && isspace(Py_CHARMASK(*end))) end++; if (*end != '\0') { PyOS_snprintf(buffer, sizeof(buffer), "invalid literal for atof(): %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } else if (errno != 0) { PyOS_snprintf(buffer, sizeof(buffer), "atof() literal too large: %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } return PyFloat_FromDouble(x);} PyDoc_STRVAR(maketrans__doc__,"maketrans(frm, to) -> string\n""\n""Return a translation table (a string of 256 bytes long)\n""suitable for use in string.translate. The strings frm and to\n""must be of the same length."); static PyObject *strop_maketrans(PyObject *self, PyObject *args){ unsigned char *c, *from=NULL, *to=NULL; Py_ssize_t i, fromlen=0, tolen=0; PyObject *result; if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen)) return NULL; if (fromlen != tolen) { PyErr_SetString(PyExc_ValueError, "maketrans arguments must have same length"); return NULL; } result = PyString_FromStringAndSize((char *)NULL, 256); if (result == NULL) return NULL; c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result); for (i = 0; i < 256; i++) c[i]=(unsigned char)i; for (i = 0; i < fromlen; i++) c[from[i]]=to[i]; return result;} PyDoc_STRVAR(translate__doc__,"translate(s,table [,deletechars]) -> string\n""\n""Return a copy of the string s, where all characters occurring\n""in the optional argument deletechars are removed, and the\n""remaining characters have been mapped through the given\n""translation table, which must be a string of length 256."); static PyObject *strop_translate(PyObject *self, PyObject *args){ register char *input, *table, *output; Py_ssize_t i; int c, changed = 0; PyObject *input_obj; char *table1, *output_start, *del_table=NULL; Py_ssize_t inlen, tablen, dellen = 0; PyObject *result; int trans_table[256]; WARN; if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj, &table1, &tablen, &del_table, &dellen)) return NULL; if (tablen != 256) { PyErr_SetString(PyExc_ValueError, "translation table must be 256 characters long"); return NULL; } table = table1; inlen = PyString_GET_SIZE(input_obj); result = PyString_FromStringAndSize((char *)NULL, inlen); if (result == NULL) return NULL; output_start = output = PyString_AsString(result); input = PyString_AsString(input_obj); if (dellen == 0) { /* If no deletions are required, use faster code */ for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); if (Py_CHARMASK((*output++ = table[c])) != c) changed = 1; } if (changed) return result; Py_DECREF(result); Py_INCREF(input_obj); return input_obj; } for (i = 0; i < 256; i++) trans_table[i] = Py_CHARMASK(table[i]); for (i = 0; i < dellen; i++) trans_table[(int) Py_CHARMASK(del_table[i])] = -1; for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); if (trans_table[c] != -1) if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) continue; changed = 1; } if (!changed) { Py_DECREF(result); Py_INCREF(input_obj); return input_obj; } /* Fix the size of the resulting string */ if (inlen > 0) _PyString_Resize(&result, output - output_start); return result;} /* What follows is used for implementing replace(). Perry Stoll. */ /* mymemfind strstr replacement for arbitrary blocks of memory. Locates the first occurrence in the memory pointed to by MEM of the contents of memory pointed to by PAT. Returns the index into MEM if found, or -1 if not found. If len of PAT is greater than length ofLines 501–1000 of 1248. Previous lines Load more
if (islower(c)) { changed = 1; *s_new = toupper(c); } else *s_new = c; s_new++; } if (!changed) { Py_DECREF(newstr); Py_INCREF(args); return args; } return newstr; } PyDoc_STRVAR(capitalize__doc__, "capitalize(s) -> string\n" "\n" "Return a copy of the string s with only its first character\n" "capitalized."); static PyObject * strop_capitalize(PyObject *self, PyObject *args) { char *s, *s_new; Py_ssize_t i, n; PyObject *newstr; int changed; WARN; if (PyString_AsStringAndSize(args, &s, &n)) return NULL; newstr = PyString_FromStringAndSize(NULL, n); if (newstr == NULL) return NULL; s_new = PyString_AsString(newstr); changed = 0; if (0 < n) { int c = Py_CHARMASK(*s++); if (islower(c)) { changed = 1; *s_new = toupper(c); } else *s_new = c; s_new++; } for (i = 1; i < n; i++) { int c = Py_CHARMASK(*s++); if (isupper(c)) { changed = 1; *s_new = tolower(c); } else *s_new = c; s_new++; } if (!changed) { Py_DECREF(newstr); Py_INCREF(args); return args; } return newstr; } PyDoc_STRVAR(expandtabs__doc__, "expandtabs(string, [tabsize]) -> string\n" "\n" "Expand tabs in a string, i.e. replace them by one or more spaces,\n" "depending on the current column and the given tab size (default 8).\n" "The column number is reset to zero after each newline occurring in the\n" "string. This doesn't understand other non-printing characters."); static PyObject * strop_expandtabs(PyObject *self, PyObject *args) { /* Original by Fredrik Lundh */ char* e; char* p; char* q; Py_ssize_t i, j; PyObject* out; char* string; Py_ssize_t stringlen; int tabsize = 8; WARN; /* Get arguments */ if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize)) return NULL; if (tabsize < 1) { PyErr_SetString(PyExc_ValueError, "tabsize must be at least 1"); return NULL; } /* First pass: determine size of output string */ i = j = 0; /* j: current column; i: total of previous lines */ e = string + stringlen; for (p = string; p < e; p++) { if (*p == '\t') j += tabsize - (j%tabsize); else { j++; if (*p == '\n') { i += j; j = 0; } } } /* Second pass: create output string and fill it */ out = PyString_FromStringAndSize(NULL, i+j); if (out == NULL) return NULL; i = 0; q = PyString_AS_STRING(out); for (p = string; p < e; p++) { if (*p == '\t') { j = tabsize - (i%tabsize); i += j; while (j-- > 0) *q++ = ' '; } else { *q++ = *p; i++; if (*p == '\n') i = 0; } } return out; } PyDoc_STRVAR(count__doc__, "count(s, sub[, start[, end]]) -> int\n" "\n" "Return the number of occurrences of substring sub in string\n" "s[start:end]. Optional arguments start and end are\n" "interpreted as in slice notation."); static PyObject * strop_count(PyObject *self, PyObject *args) { char *s, *sub; Py_ssize_t len, n; Py_ssize_t i = 0, last = PY_SSIZE_T_MAX; Py_ssize_t m, r; WARN; if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last)) return NULL; if (last > len) last = len; if (last < 0) last += len; if (last < 0) last = 0; if (i < 0) i += len; if (i < 0) i = 0; m = last + 1 - n; if (n == 0) return PyInt_FromLong((long) (m-i)); r = 0; while (i < m) { if (!memcmp(s+i, sub, n)) { r++; i += n; } else { i++; } } return PyInt_FromLong((long) r); } PyDoc_STRVAR(swapcase__doc__, "swapcase(s) -> string\n" "\n" "Return a copy of the string s with upper case characters\n" "converted to lowercase and vice versa."); static PyObject * strop_swapcase(PyObject *self, PyObject *args) { char *s, *s_new; Py_ssize_t i, n; PyObject *newstr; int changed; WARN; if (PyString_AsStringAndSize(args, &s, &n)) return NULL; newstr = PyString_FromStringAndSize(NULL, n); if (newstr == NULL) return NULL; s_new = PyString_AsString(newstr); changed = 0; for (i = 0; i < n; i++) { int c = Py_CHARMASK(*s++); if (islower(c)) { changed = 1; *s_new = toupper(c); } else if (isupper(c)) { changed = 1; *s_new = tolower(c); } else *s_new = c; s_new++; } if (!changed) { Py_DECREF(newstr); Py_INCREF(args); return args; } return newstr; } PyDoc_STRVAR(atoi__doc__, "atoi(s [,base]) -> int\n" "\n" "Return the integer represented by the string s in the given\n" "base, which defaults to 10. The string s must consist of one\n" "or more digits, possibly preceded by a sign. If base is 0, it\n" "is chosen from the leading characters of s, 0 for octal, 0x or\n" "0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n" "accepted."); static PyObject * strop_atoi(PyObject *self, PyObject *args) { char *s, *end; int base = 10; long x; char buffer[256]; /* For errors */ WARN; if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base)) return NULL; if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, "invalid base for atoi()"); return NULL; } while (*s && isspace(Py_CHARMASK(*s))) s++; errno = 0; if (base == 0 && s[0] == '0') x = (long) PyOS_strtoul(s, &end, base); else x = PyOS_strtol(s, &end, base); if (end == s || !isalnum(Py_CHARMASK(end[-1]))) goto bad; while (*end && isspace(Py_CHARMASK(*end))) end++; if (*end != '\0') { bad: PyOS_snprintf(buffer, sizeof(buffer), "invalid literal for atoi(): %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } else if (errno != 0) { PyOS_snprintf(buffer, sizeof(buffer), "atoi() literal too large: %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } return PyInt_FromLong(x); } PyDoc_STRVAR(atol__doc__, "atol(s [,base]) -> long\n" "\n" "Return the long integer represented by the string s in the\n" "given base, which defaults to 10. The string s must consist\n" "of one or more digits, possibly preceded by a sign. If base\n" "is 0, it is chosen from the leading characters of s, 0 for\n" "octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n" "0x or 0X is accepted. A trailing L or l is not accepted,\n" "unless base is 0."); static PyObject * strop_atol(PyObject *self, PyObject *args) { char *s, *end; int base = 10; PyObject *x; char buffer[256]; /* For errors */ WARN; if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base)) return NULL; if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, "invalid base for atol()"); return NULL; } while (*s && isspace(Py_CHARMASK(*s))) s++; if (s[0] == '\0') { PyErr_SetString(PyExc_ValueError, "empty string for atol()"); return NULL; } x = PyLong_FromString(s, &end, base); if (x == NULL) return NULL; if (base == 0 && (*end == 'l' || *end == 'L')) end++; while (*end && isspace(Py_CHARMASK(*end))) end++; if (*end != '\0') { PyOS_snprintf(buffer, sizeof(buffer), "invalid literal for atol(): %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); Py_DECREF(x); return NULL; } return x; } PyDoc_STRVAR(atof__doc__, "atof(s) -> float\n" "\n" "Return the floating point number represented by the string s."); static PyObject * strop_atof(PyObject *self, PyObject *args) { char *s, *end; double x; char buffer[256]; /* For errors */ WARN; if (!PyArg_ParseTuple(args, "s:atof", &s)) return NULL; while (*s && isspace(Py_CHARMASK(*s))) s++; if (s[0] == '\0') { PyErr_SetString(PyExc_ValueError, "empty string for atof()"); return NULL; } errno = 0; PyFPE_START_PROTECT("strop_atof", return 0) x = PyOS_ascii_strtod(s, &end); PyFPE_END_PROTECT(x) while (*end && isspace(Py_CHARMASK(*end))) end++; if (*end != '\0') { PyOS_snprintf(buffer, sizeof(buffer), "invalid literal for atof(): %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } else if (errno != 0) { PyOS_snprintf(buffer, sizeof(buffer), "atof() literal too large: %.200s", s); PyErr_SetString(PyExc_ValueError, buffer); return NULL; } return PyFloat_FromDouble(x); } PyDoc_STRVAR(maketrans__doc__, "maketrans(frm, to) -> string\n" "\n" "Return a translation table (a string of 256 bytes long)\n" "suitable for use in string.translate. The strings frm and to\n" "must be of the same length."); static PyObject * strop_maketrans(PyObject *self, PyObject *args) { unsigned char *c, *from=NULL, *to=NULL; Py_ssize_t i, fromlen=0, tolen=0; PyObject *result; if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen)) return NULL; if (fromlen != tolen) { PyErr_SetString(PyExc_ValueError, "maketrans arguments must have same length"); return NULL; } result = PyString_FromStringAndSize((char *)NULL, 256); if (result == NULL) return NULL; c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result); for (i = 0; i < 256; i++) c[i]=(unsigned char)i; for (i = 0; i < fromlen; i++) c[from[i]]=to[i]; return result; } PyDoc_STRVAR(translate__doc__, "translate(s,table [,deletechars]) -> string\n" "\n" "Return a copy of the string s, where all characters occurring\n" "in the optional argument deletechars are removed, and the\n" "remaining characters have been mapped through the given\n" "translation table, which must be a string of length 256."); static PyObject * strop_translate(PyObject *self, PyObject *args) { register char *input, *table, *output; Py_ssize_t i; int c, changed = 0; PyObject *input_obj; char *table1, *output_start, *del_table=NULL; Py_ssize_t inlen, tablen, dellen = 0; PyObject *result; int trans_table[256]; WARN; if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj, &table1, &tablen, &del_table, &dellen)) return NULL; if (tablen != 256) { PyErr_SetString(PyExc_ValueError, "translation table must be 256 characters long"); return NULL; } table = table1; inlen = PyString_GET_SIZE(input_obj); result = PyString_FromStringAndSize((char *)NULL, inlen); if (result == NULL) return NULL; output_start = output = PyString_AsString(result); input = PyString_AsString(input_obj); if (dellen == 0) { /* If no deletions are required, use faster code */ for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); if (Py_CHARMASK((*output++ = table[c])) != c) changed = 1; } if (changed) return result; Py_DECREF(result); Py_INCREF(input_obj); return input_obj; } for (i = 0; i < 256; i++) trans_table[i] = Py_CHARMASK(table[i]); for (i = 0; i < dellen; i++) trans_table[(int) Py_CHARMASK(del_table[i])] = -1; for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); if (trans_table[c] != -1) if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) continue; changed = 1; } if (!changed) { Py_DECREF(result); Py_INCREF(input_obj); return input_obj; } /* Fix the size of the resulting string */ if (inlen > 0) _PyString_Resize(&result, output - output_start); return result; } /* What follows is used for implementing replace(). Perry Stoll. */ /* mymemfind strstr replacement for arbitrary blocks of memory. Locates the first occurrence in the memory pointed to by MEM of the contents of memory pointed to by PAT. Returns the index into MEM if found, or -1 if not found. If len of PAT is greater than length of