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