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