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