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