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