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