Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 1 | /* strop module */ |
| 2 | |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4 | #include "Python.h" |
| 5 | #include <ctype.h> |
| 6 | |
| 7 | PyDoc_STRVAR(strop_module__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 8 | "Common string manipulations, optimized for speed.\n" |
| 9 | "\n" |
| 10 | "Always use \"import string\" rather than referencing\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11 | "this module directly."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 13 | /* XXX This file assumes that the <ctype.h> is*() functions |
| 14 | XXX are defined for all 8-bit characters! */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 16 | #define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \ |
| 17 | "strop functions are obsolete; use string methods")) \ |
| 18 | return NULL |
| 19 | |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 20 | /* The lstrip(), rstrip() and strip() functions are implemented |
| 21 | in do_strip(), which uses an additional parameter to indicate what |
| 22 | type of strip should occur. */ |
| 23 | |
| 24 | #define LEFTSTRIP 0 |
| 25 | #define RIGHTSTRIP 1 |
| 26 | #define BOTHSTRIP 2 |
| 27 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 28 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 29 | static PyObject * |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 30 | split_whitespace(char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 31 | { |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 32 | Py_ssize_t i = 0, j; |
| 33 | int err; |
| 34 | Py_ssize_t countsplit = 0; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 35 | PyObject* item; |
| 36 | PyObject *list = PyList_New(0); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 38 | if (list == NULL) |
| 39 | return NULL; |
| 40 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 41 | while (i < len) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 42 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 43 | i = i+1; |
| 44 | } |
| 45 | j = i; |
Guido van Rossum | ee1813d | 1995-02-14 00:58:59 +0000 | [diff] [blame] | 46 | while (i < len && !isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 47 | i = i+1; |
| 48 | } |
| 49 | if (j < i) { |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 50 | item = PyString_FromStringAndSize(s+j, i-j); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 51 | if (item == NULL) |
| 52 | goto finally; |
| 53 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 54 | err = PyList_Append(list, item); |
| 55 | Py_DECREF(item); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 56 | if (err < 0) |
| 57 | goto finally; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 58 | |
| 59 | countsplit++; |
Barry Warsaw | 93be92d | 1997-12-02 00:29:30 +0000 | [diff] [blame] | 60 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 61 | i = i+1; |
| 62 | } |
| 63 | if (maxsplit && (countsplit >= maxsplit) && i < len) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 64 | item = PyString_FromStringAndSize( |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 65 | s+i, len - i); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 66 | if (item == NULL) |
| 67 | goto finally; |
| 68 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 69 | err = PyList_Append(list, item); |
| 70 | Py_DECREF(item); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 71 | if (err < 0) |
| 72 | goto finally; |
| 73 | |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 74 | i = len; |
| 75 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 78 | return list; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 79 | finally: |
| 80 | Py_DECREF(list); |
| 81 | return NULL; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 85 | PyDoc_STRVAR(splitfields__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 86 | "split(s [,sep [,maxsplit]]) -> list of strings\n" |
| 87 | "splitfields(s [,sep [,maxsplit]]) -> list of strings\n" |
| 88 | "\n" |
| 89 | "Return a list of the words in the string s, using sep as the\n" |
| 90 | "delimiter string. If maxsplit is nonzero, splits into at most\n" |
| 91 | "maxsplit words. If sep is not specified, any whitespace string\n" |
| 92 | "is a separator. Maxsplit defaults to 0.\n" |
| 93 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | "(split and splitfields are synonymous)"); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 96 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 97 | strop_splitfields(PyObject *self, PyObject *args) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 98 | { |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 99 | Py_ssize_t len, n, i, j, err; |
| 100 | Py_ssize_t splitcount, maxsplit; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 101 | char *s, *sub; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 102 | PyObject *list, *item; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 104 | WARN; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 105 | sub = NULL; |
| 106 | n = 0; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 107 | splitcount = 0; |
| 108 | maxsplit = 0; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 109 | if (!PyArg_ParseTuple(args, "t#|z#n:split", &s, &len, &sub, &n, &maxsplit)) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 110 | return NULL; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 111 | if (sub == NULL) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 112 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 113 | if (n == 0) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 114 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
| 117 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 118 | list = PyList_New(0); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 119 | if (list == NULL) |
| 120 | return NULL; |
| 121 | |
| 122 | i = j = 0; |
| 123 | while (i+n <= len) { |
Guido van Rossum | a0ca4c4 | 1996-10-04 13:39:37 +0000 | [diff] [blame] | 124 | if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) { |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 125 | item = PyString_FromStringAndSize(s+j, i-j); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 126 | if (item == NULL) |
| 127 | goto fail; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 128 | err = PyList_Append(list, item); |
| 129 | Py_DECREF(item); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 130 | if (err < 0) |
| 131 | goto fail; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 132 | i = j = i + n; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 133 | splitcount++; |
| 134 | if (maxsplit && (splitcount >= maxsplit)) |
| 135 | break; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | i++; |
| 139 | } |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 140 | item = PyString_FromStringAndSize(s+j, len-j); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 141 | if (item == NULL) |
| 142 | goto fail; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 143 | err = PyList_Append(list, item); |
| 144 | Py_DECREF(item); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 145 | if (err < 0) |
| 146 | goto fail; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 147 | |
| 148 | return list; |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 149 | |
| 150 | fail: |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 151 | Py_DECREF(list); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 152 | return NULL; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 156 | PyDoc_STRVAR(joinfields__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 157 | "join(list [,sep]) -> string\n" |
| 158 | "joinfields(list [,sep]) -> string\n" |
| 159 | "\n" |
| 160 | "Return a string composed of the words in list, with\n" |
| 161 | "intervening occurrences of sep. Sep defaults to a single\n" |
| 162 | "space.\n" |
| 163 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 164 | "(join and joinfields are synonymous)"); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 165 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 166 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 167 | strop_joinfields(PyObject *self, PyObject *args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 168 | { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 169 | PyObject *seq; |
| 170 | char *sep = NULL; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 171 | Py_ssize_t seqlen, seplen = 0; |
| 172 | Py_ssize_t i, reslen = 0, slen = 0, sz = 100; |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 173 | PyObject *res = NULL; |
| 174 | char* p = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 175 | ssizeargfunc getitemfunc; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 177 | WARN; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 178 | if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 179 | return NULL; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 180 | if (sep == NULL) { |
| 181 | sep = " "; |
| 182 | seplen = 1; |
| 183 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 184 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 185 | seqlen = PySequence_Size(seq); |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 186 | if (seqlen < 0 && PyErr_Occurred()) |
| 187 | return NULL; |
| 188 | |
| 189 | if (seqlen == 1) { |
| 190 | /* Optimization if there's only one item */ |
| 191 | PyObject *item = PySequence_GetItem(seq, 0); |
Guido van Rossum | 1ad1b3f | 1998-02-06 22:37:12 +0000 | [diff] [blame] | 192 | if (item && !PyString_Check(item)) { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 193 | PyErr_SetString(PyExc_TypeError, |
| 194 | "first argument must be sequence of strings"); |
Guido van Rossum | bf33830 | 1998-10-19 13:38:36 +0000 | [diff] [blame] | 195 | Py_DECREF(item); |
Guido van Rossum | 1ad1b3f | 1998-02-06 22:37:12 +0000 | [diff] [blame] | 196 | return NULL; |
| 197 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 198 | return item; |
| 199 | } |
| 200 | |
| 201 | if (!(res = PyString_FromStringAndSize((char*)NULL, sz))) |
| 202 | return NULL; |
| 203 | p = PyString_AsString(res); |
| 204 | |
| 205 | /* optimize for lists, since it's the most common case. all others |
| 206 | * (tuples and arbitrary sequences) just use the sequence abstract |
| 207 | * interface. |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 208 | */ |
| 209 | if (PyList_Check(seq)) { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 210 | for (i = 0; i < seqlen; i++) { |
| 211 | PyObject *item = PyList_GET_ITEM(seq, i); |
| 212 | if (!PyString_Check(item)) { |
| 213 | PyErr_SetString(PyExc_TypeError, |
| 214 | "first argument must be sequence of strings"); |
| 215 | Py_DECREF(res); |
| 216 | return NULL; |
| 217 | } |
| 218 | slen = PyString_GET_SIZE(item); |
| 219 | while (reslen + slen + seplen >= sz) { |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 220 | if (_PyString_Resize(&res, sz * 2) < 0) |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 221 | return NULL; |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 222 | sz *= 2; |
| 223 | p = PyString_AsString(res) + reslen; |
| 224 | } |
| 225 | if (i > 0) { |
| 226 | memcpy(p, sep, seplen); |
| 227 | p += seplen; |
| 228 | reslen += seplen; |
| 229 | } |
| 230 | memcpy(p, PyString_AS_STRING(item), slen); |
| 231 | p += slen; |
| 232 | reslen += slen; |
| 233 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 234 | _PyString_Resize(&res, reslen); |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 235 | return res; |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 236 | } |
Guido van Rossum | 7df115d | 1998-05-22 00:53:47 +0000 | [diff] [blame] | 237 | |
| 238 | if (seq->ob_type->tp_as_sequence == NULL || |
| 239 | (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL) |
| 240 | { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 241 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 242 | "first argument must be a sequence"); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
Guido van Rossum | 7df115d | 1998-05-22 00:53:47 +0000 | [diff] [blame] | 245 | /* This is now type safe */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 246 | for (i = 0; i < seqlen; i++) { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 247 | PyObject *item = getitemfunc(seq, i); |
| 248 | if (!item || !PyString_Check(item)) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 249 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 250 | "first argument must be sequence of strings"); |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 251 | Py_DECREF(res); |
| 252 | Py_XDECREF(item); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 253 | return NULL; |
| 254 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 255 | slen = PyString_GET_SIZE(item); |
| 256 | while (reslen + slen + seplen >= sz) { |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 257 | if (_PyString_Resize(&res, sz * 2) < 0) { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 258 | Py_DECREF(item); |
| 259 | return NULL; |
| 260 | } |
| 261 | sz *= 2; |
| 262 | p = PyString_AsString(res) + reslen; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 263 | } |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 264 | if (i > 0) { |
| 265 | memcpy(p, sep, seplen); |
| 266 | p += seplen; |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 267 | reslen += seplen; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 268 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 269 | memcpy(p, PyString_AS_STRING(item), slen); |
| 270 | p += slen; |
| 271 | reslen += slen; |
| 272 | Py_DECREF(item); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 273 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 274 | _PyString_Resize(&res, reslen); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 275 | return res; |
| 276 | } |
| 277 | |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 279 | PyDoc_STRVAR(find__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 280 | "find(s, sub [,start [,end]]) -> in\n" |
| 281 | "\n" |
| 282 | "Return the lowest index in s where substring sub is found,\n" |
| 283 | "such that sub is contained within s[start,end]. Optional\n" |
| 284 | "arguments start and end are interpreted as in slice notation.\n" |
| 285 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 286 | "Return -1 on failure."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 287 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 288 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 289 | strop_find(PyObject *self, PyObject *args) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 290 | { |
| 291 | char *s, *sub; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 292 | Py_ssize_t len, n, i = 0, last = PY_SSIZE_T_MAX; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 294 | WARN; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 295 | if (!PyArg_ParseTuple(args, "t#t#|nn:find", &s, &len, &sub, &n, &i, &last)) |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 296 | return NULL; |
| 297 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 298 | if (last > len) |
| 299 | last = len; |
| 300 | if (last < 0) |
| 301 | last += len; |
| 302 | if (last < 0) |
| 303 | last = 0; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 304 | if (i < 0) |
| 305 | i += len; |
| 306 | if (i < 0) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 307 | i = 0; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 308 | |
Guido van Rossum | 031c631 | 1998-03-24 04:19:22 +0000 | [diff] [blame] | 309 | if (n == 0 && i <= last) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 310 | return PyInt_FromLong((long)i); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 312 | last -= n; |
| 313 | for (; i <= last; ++i) |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 314 | if (s[i] == sub[0] && |
Guido van Rossum | a0ca4c4 | 1996-10-04 13:39:37 +0000 | [diff] [blame] | 315 | (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0)) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 316 | return PyInt_FromLong((long)i); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 317 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 318 | return PyInt_FromLong(-1L); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 322 | PyDoc_STRVAR(rfind__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 323 | "rfind(s, sub [,start [,end]]) -> int\n" |
| 324 | "\n" |
| 325 | "Return the highest index in s where substring sub is found,\n" |
| 326 | "such that sub is contained within s[start,end]. Optional\n" |
| 327 | "arguments start and end are interpreted as in slice notation.\n" |
| 328 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 329 | "Return -1 on failure."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 330 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 331 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 332 | strop_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 333 | { |
| 334 | char *s, *sub; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 335 | Py_ssize_t len, n, j; |
| 336 | Py_ssize_t i = 0, last = INT_MAX; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 338 | WARN; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 339 | if (!PyArg_ParseTuple(args, "t#t#|nn:rfind", &s, &len, &sub, &n, &i, &last)) |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 342 | if (last > len) |
| 343 | last = len; |
| 344 | if (last < 0) |
| 345 | last += len; |
| 346 | if (last < 0) |
| 347 | last = 0; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 348 | if (i < 0) |
| 349 | i += len; |
| 350 | if (i < 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 351 | i = 0; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | 031c631 | 1998-03-24 04:19:22 +0000 | [diff] [blame] | 353 | if (n == 0 && i <= last) |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 354 | return PyInt_FromLong((long)last); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 356 | for (j = last-n; j >= i; --j) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 357 | if (s[j] == sub[0] && |
Guido van Rossum | a0ca4c4 | 1996-10-04 13:39:37 +0000 | [diff] [blame] | 358 | (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0)) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 359 | return PyInt_FromLong((long)j); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 360 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 361 | return PyInt_FromLong(-1L); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 364 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 365 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 366 | do_strip(PyObject *args, int striptype) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 367 | { |
| 368 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 369 | Py_ssize_t len, i, j; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 370 | |
| 371 | |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 372 | if (PyString_AsStringAndSize(args, &s, &len)) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 373 | return NULL; |
| 374 | |
| 375 | i = 0; |
| 376 | if (striptype != RIGHTSTRIP) { |
| 377 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 378 | i++; |
| 379 | } |
| 380 | } |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 381 | |
| 382 | j = len; |
| 383 | if (striptype != LEFTSTRIP) { |
| 384 | do { |
| 385 | j--; |
| 386 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 387 | j++; |
| 388 | } |
| 389 | |
| 390 | if (i == 0 && j == len) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 391 | Py_INCREF(args); |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 392 | return args; |
| 393 | } |
| 394 | else |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 395 | return PyString_FromStringAndSize(s+i, j-i); |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 398 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 399 | PyDoc_STRVAR(strip__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 400 | "strip(s) -> string\n" |
| 401 | "\n" |
| 402 | "Return a copy of the string s with leading and trailing\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 403 | "whitespace removed."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 404 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 405 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 406 | strop_strip(PyObject *self, PyObject *args) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 407 | { |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 408 | WARN; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 409 | return do_strip(args, BOTHSTRIP); |
| 410 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 413 | PyDoc_STRVAR(lstrip__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 414 | "lstrip(s) -> string\n" |
| 415 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 416 | "Return a copy of the string s with leading whitespace removed."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 417 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 418 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 419 | strop_lstrip(PyObject *self, PyObject *args) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 420 | { |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 421 | WARN; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 422 | return do_strip(args, LEFTSTRIP); |
| 423 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 425 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 426 | PyDoc_STRVAR(rstrip__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 427 | "rstrip(s) -> string\n" |
| 428 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 429 | "Return a copy of the string s with trailing whitespace removed."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 430 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 431 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 432 | strop_rstrip(PyObject *self, PyObject *args) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 433 | { |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 434 | WARN; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 435 | return do_strip(args, RIGHTSTRIP); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 439 | PyDoc_STRVAR(lower__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 440 | "lower(s) -> string\n" |
| 441 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 442 | "Return a copy of the string s converted to lowercase."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 443 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 444 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 445 | strop_lower(PyObject *self, PyObject *args) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 446 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 447 | char *s, *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 448 | Py_ssize_t i, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 449 | PyObject *new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 450 | int changed; |
| 451 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 452 | WARN; |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 453 | if (PyString_AsStringAndSize(args, &s, &n)) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 454 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 455 | new = PyString_FromStringAndSize(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 456 | if (new == NULL) |
| 457 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 458 | s_new = PyString_AsString(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 459 | changed = 0; |
| 460 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 461 | int c = Py_CHARMASK(*s++); |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 462 | if (isupper(c)) { |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 463 | changed = 1; |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 464 | *s_new = tolower(c); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 465 | } else |
| 466 | *s_new = c; |
| 467 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 468 | } |
| 469 | if (!changed) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 470 | Py_DECREF(new); |
| 471 | Py_INCREF(args); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 472 | return args; |
| 473 | } |
| 474 | return new; |
| 475 | } |
| 476 | |
| 477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 478 | PyDoc_STRVAR(upper__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 479 | "upper(s) -> string\n" |
| 480 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 481 | "Return a copy of the string s converted to uppercase."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 482 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 483 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 484 | strop_upper(PyObject *self, PyObject *args) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 485 | { |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 486 | char *s, *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 487 | Py_ssize_t i, n; |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 488 | PyObject *new; |
| 489 | int changed; |
| 490 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 491 | WARN; |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 492 | if (PyString_AsStringAndSize(args, &s, &n)) |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 493 | return NULL; |
| 494 | new = PyString_FromStringAndSize(NULL, n); |
| 495 | if (new == NULL) |
| 496 | return NULL; |
| 497 | s_new = PyString_AsString(new); |
| 498 | changed = 0; |
| 499 | for (i = 0; i < n; i++) { |
| 500 | int c = Py_CHARMASK(*s++); |
| 501 | if (islower(c)) { |
| 502 | changed = 1; |
| 503 | *s_new = toupper(c); |
| 504 | } else |
| 505 | *s_new = c; |
| 506 | s_new++; |
| 507 | } |
| 508 | if (!changed) { |
| 509 | Py_DECREF(new); |
| 510 | Py_INCREF(args); |
| 511 | return args; |
| 512 | } |
| 513 | return new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 517 | PyDoc_STRVAR(capitalize__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 518 | "capitalize(s) -> string\n" |
| 519 | "\n" |
| 520 | "Return a copy of the string s with only its first character\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 521 | "capitalized."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 522 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 523 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 524 | strop_capitalize(PyObject *self, PyObject *args) |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 525 | { |
| 526 | char *s, *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 527 | Py_ssize_t i, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 528 | PyObject *new; |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 529 | int changed; |
| 530 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 531 | WARN; |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 532 | if (PyString_AsStringAndSize(args, &s, &n)) |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 533 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 534 | new = PyString_FromStringAndSize(NULL, n); |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 535 | if (new == NULL) |
| 536 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 537 | s_new = PyString_AsString(new); |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 538 | changed = 0; |
Guido van Rossum | 529c963 | 1996-06-17 16:59:33 +0000 | [diff] [blame] | 539 | if (0 < n) { |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 540 | int c = Py_CHARMASK(*s++); |
| 541 | if (islower(c)) { |
| 542 | changed = 1; |
| 543 | *s_new = toupper(c); |
| 544 | } else |
| 545 | *s_new = c; |
| 546 | s_new++; |
| 547 | } |
| 548 | for (i = 1; i < n; i++) { |
| 549 | int c = Py_CHARMASK(*s++); |
| 550 | if (isupper(c)) { |
| 551 | changed = 1; |
| 552 | *s_new = tolower(c); |
| 553 | } else |
| 554 | *s_new = c; |
| 555 | s_new++; |
| 556 | } |
| 557 | if (!changed) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 558 | Py_DECREF(new); |
| 559 | Py_INCREF(args); |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 560 | return args; |
| 561 | } |
| 562 | return new; |
| 563 | } |
| 564 | |
| 565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 566 | PyDoc_STRVAR(expandtabs__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 567 | "expandtabs(string, [tabsize]) -> string\n" |
| 568 | "\n" |
| 569 | "Expand tabs in a string, i.e. replace them by one or more spaces,\n" |
| 570 | "depending on the current column and the given tab size (default 8).\n" |
| 571 | "The column number is reset to zero after each newline occurring in the\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 572 | "string. This doesn't understand other non-printing characters."); |
Guido van Rossum | 54ec288 | 1999-01-25 22:36:24 +0000 | [diff] [blame] | 573 | |
| 574 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 575 | strop_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | 54ec288 | 1999-01-25 22:36:24 +0000 | [diff] [blame] | 576 | { |
| 577 | /* Original by Fredrik Lundh */ |
| 578 | char* e; |
| 579 | char* p; |
| 580 | char* q; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 581 | Py_ssize_t i, j; |
Guido van Rossum | 54ec288 | 1999-01-25 22:36:24 +0000 | [diff] [blame] | 582 | PyObject* out; |
| 583 | char* string; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 584 | Py_ssize_t stringlen; |
Guido van Rossum | 54ec288 | 1999-01-25 22:36:24 +0000 | [diff] [blame] | 585 | int tabsize = 8; |
| 586 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 587 | WARN; |
Guido van Rossum | 54ec288 | 1999-01-25 22:36:24 +0000 | [diff] [blame] | 588 | /* Get arguments */ |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 589 | if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize)) |
Guido van Rossum | 54ec288 | 1999-01-25 22:36:24 +0000 | [diff] [blame] | 590 | return NULL; |
| 591 | if (tabsize < 1) { |
| 592 | PyErr_SetString(PyExc_ValueError, |
| 593 | "tabsize must be at least 1"); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | /* First pass: determine size of output string */ |
| 598 | i = j = 0; /* j: current column; i: total of previous lines */ |
| 599 | e = string + stringlen; |
| 600 | for (p = string; p < e; p++) { |
| 601 | if (*p == '\t') |
| 602 | j += tabsize - (j%tabsize); |
| 603 | else { |
| 604 | j++; |
| 605 | if (*p == '\n') { |
| 606 | i += j; |
| 607 | j = 0; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* Second pass: create output string and fill it */ |
| 613 | out = PyString_FromStringAndSize(NULL, i+j); |
| 614 | if (out == NULL) |
| 615 | return NULL; |
| 616 | |
| 617 | i = 0; |
| 618 | q = PyString_AS_STRING(out); |
| 619 | |
| 620 | for (p = string; p < e; p++) { |
| 621 | if (*p == '\t') { |
| 622 | j = tabsize - (i%tabsize); |
| 623 | i += j; |
| 624 | while (j-- > 0) |
| 625 | *q++ = ' '; |
| 626 | } else { |
| 627 | *q++ = *p; |
| 628 | i++; |
| 629 | if (*p == '\n') |
| 630 | i = 0; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return out; |
| 635 | } |
| 636 | |
| 637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 638 | PyDoc_STRVAR(count__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 639 | "count(s, sub[, start[, end]]) -> int\n" |
| 640 | "\n" |
| 641 | "Return the number of occurrences of substring sub in string\n" |
| 642 | "s[start:end]. Optional arguments start and end are\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 643 | "interpreted as in slice notation."); |
Guido van Rossum | d5bcf9a | 1998-10-06 19:43:14 +0000 | [diff] [blame] | 644 | |
| 645 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 646 | strop_count(PyObject *self, PyObject *args) |
Guido van Rossum | d5bcf9a | 1998-10-06 19:43:14 +0000 | [diff] [blame] | 647 | { |
| 648 | char *s, *sub; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 649 | Py_ssize_t len, n; |
| 650 | Py_ssize_t i = 0, last = INT_MAX; |
| 651 | Py_ssize_t m, r; |
Guido van Rossum | d5bcf9a | 1998-10-06 19:43:14 +0000 | [diff] [blame] | 652 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 653 | WARN; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 654 | if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last)) |
Guido van Rossum | d5bcf9a | 1998-10-06 19:43:14 +0000 | [diff] [blame] | 655 | return NULL; |
| 656 | if (last > len) |
| 657 | last = len; |
| 658 | if (last < 0) |
| 659 | last += len; |
| 660 | if (last < 0) |
| 661 | last = 0; |
| 662 | if (i < 0) |
| 663 | i += len; |
| 664 | if (i < 0) |
| 665 | i = 0; |
| 666 | m = last + 1 - n; |
| 667 | if (n == 0) |
| 668 | return PyInt_FromLong((long) (m-i)); |
| 669 | |
| 670 | r = 0; |
| 671 | while (i < m) { |
| 672 | if (!memcmp(s+i, sub, n)) { |
| 673 | r++; |
| 674 | i += n; |
| 675 | } else { |
| 676 | i++; |
| 677 | } |
| 678 | } |
| 679 | return PyInt_FromLong((long) r); |
| 680 | } |
| 681 | |
| 682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 683 | PyDoc_STRVAR(swapcase__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 684 | "swapcase(s) -> string\n" |
| 685 | "\n" |
| 686 | "Return a copy of the string s with upper case characters\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 687 | "converted to lowercase and vice versa."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 688 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 689 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 690 | strop_swapcase(PyObject *self, PyObject *args) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 691 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 692 | char *s, *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 693 | Py_ssize_t i, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 694 | PyObject *new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 695 | int changed; |
| 696 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 697 | WARN; |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 698 | if (PyString_AsStringAndSize(args, &s, &n)) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 699 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 700 | new = PyString_FromStringAndSize(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 701 | if (new == NULL) |
| 702 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 703 | s_new = PyString_AsString(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 704 | changed = 0; |
| 705 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 706 | int c = Py_CHARMASK(*s++); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 707 | if (islower(c)) { |
| 708 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 709 | *s_new = toupper(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 710 | } |
| 711 | else if (isupper(c)) { |
| 712 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 713 | *s_new = tolower(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 714 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 715 | else |
| 716 | *s_new = c; |
| 717 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 718 | } |
| 719 | if (!changed) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 720 | Py_DECREF(new); |
| 721 | Py_INCREF(args); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 722 | return args; |
| 723 | } |
| 724 | return new; |
| 725 | } |
| 726 | |
| 727 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 728 | PyDoc_STRVAR(atoi__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 729 | "atoi(s [,base]) -> int\n" |
| 730 | "\n" |
| 731 | "Return the integer represented by the string s in the given\n" |
| 732 | "base, which defaults to 10. The string s must consist of one\n" |
| 733 | "or more digits, possibly preceded by a sign. If base is 0, it\n" |
| 734 | "is chosen from the leading characters of s, 0 for octal, 0x or\n" |
| 735 | "0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 736 | "accepted."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 737 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 738 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 739 | strop_atoi(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 740 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 741 | char *s, *end; |
| 742 | int base = 10; |
| 743 | long x; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 744 | char buffer[256]; /* For errors */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 746 | WARN; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 747 | if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 748 | return NULL; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 749 | |
| 750 | if ((base != 0 && base < 2) || base > 36) { |
| 751 | PyErr_SetString(PyExc_ValueError, "invalid base for atoi()"); |
| 752 | return NULL; |
| 753 | } |
| 754 | |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 755 | while (*s && isspace(Py_CHARMASK(*s))) |
| 756 | s++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 757 | errno = 0; |
| 758 | if (base == 0 && s[0] == '0') |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 759 | x = (long) PyOS_strtoul(s, &end, base); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 760 | else |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 761 | x = PyOS_strtol(s, &end, base); |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 762 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
Guido van Rossum | 923fece5 | 1998-08-04 15:04:52 +0000 | [diff] [blame] | 763 | goto bad; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 764 | while (*end && isspace(Py_CHARMASK(*end))) |
| 765 | end++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 766 | if (*end != '\0') { |
Guido van Rossum | 923fece5 | 1998-08-04 15:04:52 +0000 | [diff] [blame] | 767 | bad: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 768 | PyOS_snprintf(buffer, sizeof(buffer), |
| 769 | "invalid literal for atoi(): %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 770 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 771 | return NULL; |
| 772 | } |
| 773 | else if (errno != 0) { |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 774 | PyOS_snprintf(buffer, sizeof(buffer), |
| 775 | "atoi() literal too large: %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 776 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 777 | return NULL; |
| 778 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 779 | return PyInt_FromLong(x); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 783 | PyDoc_STRVAR(atol__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 784 | "atol(s [,base]) -> long\n" |
| 785 | "\n" |
| 786 | "Return the long integer represented by the string s in the\n" |
| 787 | "given base, which defaults to 10. The string s must consist\n" |
| 788 | "of one or more digits, possibly preceded by a sign. If base\n" |
| 789 | "is 0, it is chosen from the leading characters of s, 0 for\n" |
| 790 | "octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n" |
| 791 | "0x or 0X is accepted. A trailing L or l is not accepted,\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 792 | "unless base is 0."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 793 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 794 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 795 | strop_atol(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 796 | { |
| 797 | char *s, *end; |
| 798 | int base = 10; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 799 | PyObject *x; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 800 | char buffer[256]; /* For errors */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 801 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 802 | WARN; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 803 | if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 804 | return NULL; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 805 | |
| 806 | if ((base != 0 && base < 2) || base > 36) { |
| 807 | PyErr_SetString(PyExc_ValueError, "invalid base for atol()"); |
| 808 | return NULL; |
| 809 | } |
| 810 | |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 811 | while (*s && isspace(Py_CHARMASK(*s))) |
| 812 | s++; |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 813 | if (s[0] == '\0') { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 814 | PyErr_SetString(PyExc_ValueError, "empty string for atol()"); |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 815 | return NULL; |
| 816 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 817 | x = PyLong_FromString(s, &end, base); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 818 | if (x == NULL) |
| 819 | return NULL; |
| 820 | if (base == 0 && (*end == 'l' || *end == 'L')) |
| 821 | end++; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 822 | while (*end && isspace(Py_CHARMASK(*end))) |
| 823 | end++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 824 | if (*end != '\0') { |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 825 | PyOS_snprintf(buffer, sizeof(buffer), |
| 826 | "invalid literal for atol(): %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 827 | PyErr_SetString(PyExc_ValueError, buffer); |
| 828 | Py_DECREF(x); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 829 | return NULL; |
| 830 | } |
| 831 | return x; |
| 832 | } |
| 833 | |
| 834 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 835 | PyDoc_STRVAR(atof__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 836 | "atof(s) -> float\n" |
| 837 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 838 | "Return the floating point number represented by the string s."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 839 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 840 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 841 | strop_atof(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 842 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 843 | char *s, *end; |
| 844 | double x; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 845 | char buffer[256]; /* For errors */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 847 | WARN; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 848 | if (!PyArg_ParseTuple(args, "s:atof", &s)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 849 | return NULL; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 850 | while (*s && isspace(Py_CHARMASK(*s))) |
| 851 | s++; |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 852 | if (s[0] == '\0') { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 853 | PyErr_SetString(PyExc_ValueError, "empty string for atof()"); |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 854 | return NULL; |
| 855 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 856 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 857 | PyFPE_START_PROTECT("strop_atof", return 0) |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 858 | x = PyOS_ascii_strtod(s, &end); |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 859 | PyFPE_END_PROTECT(x) |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 860 | while (*end && isspace(Py_CHARMASK(*end))) |
| 861 | end++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 862 | if (*end != '\0') { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 863 | PyOS_snprintf(buffer, sizeof(buffer), |
| 864 | "invalid literal for atof(): %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 865 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 866 | return NULL; |
| 867 | } |
| 868 | else if (errno != 0) { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 869 | PyOS_snprintf(buffer, sizeof(buffer), |
| 870 | "atof() literal too large: %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 871 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 872 | return NULL; |
| 873 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 874 | return PyFloat_FromDouble(x); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 878 | PyDoc_STRVAR(maketrans__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 879 | "maketrans(frm, to) -> string\n" |
| 880 | "\n" |
| 881 | "Return a translation table (a string of 256 bytes long)\n" |
| 882 | "suitable for use in string.translate. The strings frm and to\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 883 | "must be of the same length."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 884 | |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 885 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 886 | strop_maketrans(PyObject *self, PyObject *args) |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 887 | { |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 888 | unsigned char *c, *from=NULL, *to=NULL; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 889 | Py_ssize_t i, fromlen=0, tolen=0; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 890 | PyObject *result; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 891 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 892 | if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen)) |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 893 | return NULL; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 894 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 895 | if (fromlen != tolen) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 896 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 897 | "maketrans arguments must have same length"); |
| 898 | return NULL; |
| 899 | } |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 900 | |
| 901 | result = PyString_FromStringAndSize((char *)NULL, 256); |
| 902 | if (result == NULL) |
| 903 | return NULL; |
| 904 | c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 905 | for (i = 0; i < 256; i++) |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 906 | c[i]=(unsigned char)i; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 907 | for (i = 0; i < fromlen; i++) |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 908 | c[from[i]]=to[i]; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 909 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 910 | return result; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 914 | PyDoc_STRVAR(translate__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 915 | "translate(s,table [,deletechars]) -> string\n" |
| 916 | "\n" |
| 917 | "Return a copy of the string s, where all characters occurring\n" |
| 918 | "in the optional argument deletechars are removed, and the\n" |
| 919 | "remaining characters have been mapped through the given\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 920 | "translation table, which must be a string of length 256."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 921 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 922 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 923 | strop_translate(PyObject *self, PyObject *args) |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 924 | { |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 925 | register char *input, *table, *output; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 926 | Py_ssize_t i; |
| 927 | int c, changed = 0; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 928 | PyObject *input_obj; |
| 929 | char *table1, *output_start, *del_table=NULL; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 930 | Py_ssize_t inlen, tablen, dellen = 0; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 931 | PyObject *result; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 932 | int trans_table[256]; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 933 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 934 | WARN; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 935 | if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj, |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 936 | &table1, &tablen, &del_table, &dellen)) |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 937 | return NULL; |
| 938 | if (tablen != 256) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 939 | PyErr_SetString(PyExc_ValueError, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 940 | "translation table must be 256 characters long"); |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 941 | return NULL; |
| 942 | } |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 944 | table = table1; |
| 945 | inlen = PyString_Size(input_obj); |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 946 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 947 | if (result == NULL) |
| 948 | return NULL; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 949 | output_start = output = PyString_AsString(result); |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 950 | input = PyString_AsString(input_obj); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 951 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 952 | if (dellen == 0) { |
| 953 | /* If no deletions are required, use faster code */ |
| 954 | for (i = inlen; --i >= 0; ) { |
| 955 | c = Py_CHARMASK(*input++); |
| 956 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 957 | changed = 1; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 958 | } |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 959 | if (changed) |
| 960 | return result; |
| 961 | Py_DECREF(result); |
| 962 | Py_INCREF(input_obj); |
| 963 | return input_obj; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 964 | } |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 965 | |
| 966 | for (i = 0; i < 256; i++) |
| 967 | trans_table[i] = Py_CHARMASK(table[i]); |
| 968 | |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 969 | for (i = 0; i < dellen; i++) |
Guido van Rossum | 1ed5e57 | 1997-04-29 21:34:16 +0000 | [diff] [blame] | 970 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 971 | |
| 972 | for (i = inlen; --i >= 0; ) { |
| 973 | c = Py_CHARMASK(*input++); |
| 974 | if (trans_table[c] != -1) |
| 975 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 976 | continue; |
| 977 | changed = 1; |
| 978 | } |
| 979 | if (!changed) { |
| 980 | Py_DECREF(result); |
| 981 | Py_INCREF(input_obj); |
| 982 | return input_obj; |
| 983 | } |
| 984 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 985 | if (inlen > 0) |
| 986 | _PyString_Resize(&result, output - output_start); |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 987 | return result; |
| 988 | } |
| 989 | |
| 990 | |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 991 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 992 | |
| 993 | /* |
| 994 | mymemfind |
| 995 | |
| 996 | strstr replacement for arbitrary blocks of memory. |
| 997 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 998 | Locates the first occurrence in the memory pointed to by MEM of the |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 999 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 1000 | found, or -1 if not found. If len of PAT is greater than length of |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 1001 | MEM, the function returns -1. |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1002 | */ |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1003 | static Py_ssize_t |
| 1004 | mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1005 | { |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1006 | register Py_ssize_t ii; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1007 | |
| 1008 | /* pattern can not occur in the last pat_len-1 chars */ |
| 1009 | len -= pat_len; |
| 1010 | |
| 1011 | for (ii = 0; ii <= len; ii++) { |
| 1012 | if (mem[ii] == pat[0] && |
| 1013 | (pat_len == 1 || |
| 1014 | memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) { |
| 1015 | return ii; |
| 1016 | } |
| 1017 | } |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | mymemcnt |
| 1023 | |
| 1024 | Return the number of distinct times PAT is found in MEM. |
| 1025 | meaning mem=1111 and pat==11 returns 2. |
| 1026 | mem=11111 and pat==11 also return 2. |
| 1027 | */ |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1028 | static Py_ssize_t |
| 1029 | mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1030 | { |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1031 | register Py_ssize_t offset = 0; |
| 1032 | Py_ssize_t nfound = 0; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1033 | |
| 1034 | while (len >= 0) { |
| 1035 | offset = mymemfind(mem, len, pat, pat_len); |
| 1036 | if (offset == -1) |
| 1037 | break; |
| 1038 | mem += offset + pat_len; |
| 1039 | len -= offset + pat_len; |
| 1040 | nfound++; |
| 1041 | } |
| 1042 | return nfound; |
| 1043 | } |
| 1044 | |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 1045 | /* |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1046 | mymemreplace |
| 1047 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1048 | Return a string in which all occurrences of PAT in memory STR are |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 1049 | replaced with SUB. |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1050 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1051 | If length of PAT is less than length of STR or there are no occurrences |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1052 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 1053 | string is allocated here and returned. |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 1054 | |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1055 | on return, out_len is: |
| 1056 | the length of output string, or |
| 1057 | -1 if the input string is returned, or |
| 1058 | unchanged if an error occurs (no memory). |
| 1059 | |
| 1060 | return value is: |
| 1061 | the new string allocated locally, or |
| 1062 | NULL if an error occurred. |
| 1063 | */ |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1064 | static char * |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1065 | mymemreplace(const char *str, Py_ssize_t len, /* input string */ |
| 1066 | const char *pat, Py_ssize_t pat_len, /* pattern string to find */ |
| 1067 | const char *sub, Py_ssize_t sub_len, /* substitution string */ |
| 1068 | Py_ssize_t count, /* number of replacements */ |
| 1069 | Py_ssize_t *out_len) |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1070 | { |
| 1071 | char *out_s; |
| 1072 | char *new_s; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1073 | Py_ssize_t nfound, offset, new_len; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (len == 0 || pat_len > len) |
| 1076 | goto return_same; |
| 1077 | |
| 1078 | /* find length of output string */ |
| 1079 | nfound = mymemcnt(str, len, pat, pat_len); |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 1080 | if (count < 0) |
| 1081 | count = INT_MAX; |
| 1082 | else if (nfound > count) |
| 1083 | nfound = count; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1084 | if (nfound == 0) |
| 1085 | goto return_same; |
Tim Peters | 1a7b3ee | 2001-05-09 23:00:26 +0000 | [diff] [blame] | 1086 | |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1087 | new_len = len + nfound*(sub_len - pat_len); |
Tim Peters | 1a7b3ee | 2001-05-09 23:00:26 +0000 | [diff] [blame] | 1088 | if (new_len == 0) { |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1089 | /* Have to allocate something for the caller to free(). */ |
| 1090 | out_s = (char *)PyMem_MALLOC(1); |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 1091 | if (out_s == NULL) |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1092 | return NULL; |
| 1093 | out_s[0] = '\0'; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1094 | } |
Tim Peters | 1a7b3ee | 2001-05-09 23:00:26 +0000 | [diff] [blame] | 1095 | else { |
| 1096 | assert(new_len > 0); |
| 1097 | new_s = (char *)PyMem_MALLOC(new_len); |
| 1098 | if (new_s == NULL) |
| 1099 | return NULL; |
| 1100 | out_s = new_s; |
| 1101 | |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 1102 | for (; count > 0 && len > 0; --count) { |
Tim Peters | 1a7b3ee | 2001-05-09 23:00:26 +0000 | [diff] [blame] | 1103 | /* find index of next instance of pattern */ |
| 1104 | offset = mymemfind(str, len, pat, pat_len); |
| 1105 | if (offset == -1) |
| 1106 | break; |
| 1107 | |
| 1108 | /* copy non matching part of input string */ |
| 1109 | memcpy(new_s, str, offset); |
| 1110 | str += offset + pat_len; |
| 1111 | len -= offset + pat_len; |
| 1112 | |
| 1113 | /* copy substitute into the output string */ |
| 1114 | new_s += offset; |
| 1115 | memcpy(new_s, sub, sub_len); |
| 1116 | new_s += sub_len; |
Tim Peters | 1a7b3ee | 2001-05-09 23:00:26 +0000 | [diff] [blame] | 1117 | } |
| 1118 | /* copy any remaining values into output string */ |
| 1119 | if (len > 0) |
| 1120 | memcpy(new_s, str, len); |
| 1121 | } |
| 1122 | *out_len = new_len; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1123 | return out_s; |
| 1124 | |
| 1125 | return_same: |
| 1126 | *out_len = -1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1127 | return (char *)str; /* cast away const */ |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1131 | PyDoc_STRVAR(replace__doc__, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1132 | "replace (str, old, new[, maxsplit]) -> string\n" |
| 1133 | "\n" |
| 1134 | "Return a copy of string str with all occurrences of substring\n" |
| 1135 | "old replaced by new. If the optional argument maxsplit is\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1136 | "given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 1137 | |
| 1138 | static PyObject * |
Peter Schneider-Kamp | 8235f1c | 2000-07-10 09:43:24 +0000 | [diff] [blame] | 1139 | strop_replace(PyObject *self, PyObject *args) |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1140 | { |
| 1141 | char *str, *pat,*sub,*new_s; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1142 | Py_ssize_t len,pat_len,sub_len,out_len; |
| 1143 | Py_ssize_t count = -1; |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1144 | PyObject *new; |
| 1145 | |
Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 1146 | WARN; |
Martin v. Löwis | 5c97c79 | 2006-02-17 15:49:09 +0000 | [diff] [blame] | 1147 | if (!PyArg_ParseTuple(args, "t#t#t#|n:replace", |
Barry Warsaw | f577c08 | 1997-11-29 00:10:07 +0000 | [diff] [blame] | 1148 | &str, &len, &pat, &pat_len, &sub, &sub_len, |
| 1149 | &count)) |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1150 | return NULL; |
Guido van Rossum | 4ccda15 | 1998-05-14 02:36:29 +0000 | [diff] [blame] | 1151 | if (pat_len <= 0) { |
| 1152 | PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
| 1153 | return NULL; |
| 1154 | } |
Tim Peters | 1ee77d9 | 2001-05-10 01:23:39 +0000 | [diff] [blame] | 1155 | /* CAUTION: strop treats a replace count of 0 as infinity, unlke |
| 1156 | * current (2.1) string.py and string methods. Preserve this for |
| 1157 | * ... well, hard to say for what <wink>. |
| 1158 | */ |
| 1159 | if (count == 0) |
| 1160 | count = -1; |
Barry Warsaw | f577c08 | 1997-11-29 00:10:07 +0000 | [diff] [blame] | 1161 | new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len); |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1162 | if (new_s == NULL) { |
| 1163 | PyErr_NoMemory(); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | if (out_len == -1) { |
| 1167 | /* we're returning another reference to the input string */ |
| 1168 | new = PyTuple_GetItem(args, 0); |
| 1169 | Py_XINCREF(new); |
| 1170 | } |
| 1171 | else { |
| 1172 | new = PyString_FromStringAndSize(new_s, out_len); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1173 | PyMem_FREE(new_s); |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 1174 | } |
| 1175 | return new; |
| 1176 | } |
| 1177 | |
| 1178 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 1179 | /* List of functions defined in the module */ |
| 1180 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 1181 | static PyMethodDef |
| 1182 | strop_methods[] = { |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1183 | {"atof", strop_atof, METH_VARARGS, atof__doc__}, |
| 1184 | {"atoi", strop_atoi, METH_VARARGS, atoi__doc__}, |
| 1185 | {"atol", strop_atol, METH_VARARGS, atol__doc__}, |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 1186 | {"capitalize", strop_capitalize, METH_O, capitalize__doc__}, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1187 | {"count", strop_count, METH_VARARGS, count__doc__}, |
| 1188 | {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 1189 | {"find", strop_find, METH_VARARGS, find__doc__}, |
| 1190 | {"join", strop_joinfields, METH_VARARGS, joinfields__doc__}, |
| 1191 | {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__}, |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 1192 | {"lstrip", strop_lstrip, METH_O, lstrip__doc__}, |
| 1193 | {"lower", strop_lower, METH_O, lower__doc__}, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1194 | {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__}, |
| 1195 | {"replace", strop_replace, METH_VARARGS, replace__doc__}, |
| 1196 | {"rfind", strop_rfind, METH_VARARGS, rfind__doc__}, |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 1197 | {"rstrip", strop_rstrip, METH_O, rstrip__doc__}, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1198 | {"split", strop_splitfields, METH_VARARGS, splitfields__doc__}, |
| 1199 | {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__}, |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 1200 | {"strip", strop_strip, METH_O, strip__doc__}, |
| 1201 | {"swapcase", strop_swapcase, METH_O, swapcase__doc__}, |
Tim Peters | 0f8b494 | 2001-05-09 22:15:03 +0000 | [diff] [blame] | 1202 | {"translate", strop_translate, METH_VARARGS, translate__doc__}, |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 1203 | {"upper", strop_upper, METH_O, upper__doc__}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 1204 | {NULL, NULL} /* sentinel */ |
| 1205 | }; |
| 1206 | |
| 1207 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1208 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1209 | initstrop(void) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 1210 | { |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1211 | PyObject *m, *s; |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 1212 | char buf[256]; |
| 1213 | int c, n; |
Guido van Rossum | 983c930 | 1997-12-29 19:52:29 +0000 | [diff] [blame] | 1214 | m = Py_InitModule4("strop", strop_methods, strop_module__doc__, |
| 1215 | (PyObject*)NULL, PYTHON_API_VERSION); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1216 | if (m == NULL) |
| 1217 | return; |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1218 | |
| 1219 | /* Create 'whitespace' object */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 1220 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1221 | for (c = 0; c < 256; c++) { |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 1222 | if (isspace(c)) |
| 1223 | buf[n++] = c; |
| 1224 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1225 | s = PyString_FromStringAndSize(buf, n); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1226 | if (s) |
| 1227 | PyModule_AddObject(m, "whitespace", s); |
| 1228 | |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1229 | /* Create 'lowercase' object */ |
| 1230 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1231 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1232 | if (islower(c)) |
| 1233 | buf[n++] = c; |
| 1234 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1235 | s = PyString_FromStringAndSize(buf, n); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1236 | if (s) |
| 1237 | PyModule_AddObject(m, "lowercase", s); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1238 | |
| 1239 | /* Create 'uppercase' object */ |
| 1240 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1241 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1242 | if (isupper(c)) |
| 1243 | buf[n++] = c; |
| 1244 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1245 | s = PyString_FromStringAndSize(buf, n); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1246 | if (s) |
| 1247 | PyModule_AddObject(m, "uppercase", s); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 1248 | } |