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