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 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 36 | #ifdef HAVE_LIMITS_H |
| 37 | #include <limits.h> |
| 38 | #else |
| 39 | #define INT_MAX 2147483647 |
| 40 | #endif |
| 41 | |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 42 | #include <ctype.h> |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 43 | /* XXX This file assumes that the <ctype.h> is*() functions |
| 44 | XXX are defined for all 8-bit characters! */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 46 | /* The lstrip(), rstrip() and strip() functions are implemented |
| 47 | in do_strip(), which uses an additional parameter to indicate what |
| 48 | type of strip should occur. */ |
| 49 | |
| 50 | #define LEFTSTRIP 0 |
| 51 | #define RIGHTSTRIP 1 |
| 52 | #define BOTHSTRIP 2 |
| 53 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 54 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 55 | static PyObject * |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 56 | split_whitespace(s, len, maxsplit) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 57 | char *s; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 58 | int len; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 59 | int maxsplit; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 60 | { |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 61 | int i = 0, j, err; |
| 62 | int countsplit = 0; |
| 63 | PyObject* item; |
| 64 | PyObject *list = PyList_New(0); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 66 | if (list == NULL) |
| 67 | return NULL; |
| 68 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 69 | while (i < len) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 70 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 71 | i = i+1; |
| 72 | } |
| 73 | j = i; |
Guido van Rossum | ee1813d | 1995-02-14 00:58:59 +0000 | [diff] [blame] | 74 | while (i < len && !isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 75 | i = i+1; |
| 76 | } |
| 77 | if (j < i) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 78 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
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; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 86 | |
| 87 | countsplit++; |
| 88 | if (maxsplit && (countsplit >= maxsplit)) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 89 | item = PyString_FromStringAndSize( |
| 90 | s+i, (int)(len - i)); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 91 | if (item == NULL) |
| 92 | goto finally; |
| 93 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 94 | err = PyList_Append(list, item); |
| 95 | Py_DECREF(item); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 96 | if (err < 0) |
| 97 | goto finally; |
| 98 | |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 99 | i = len; |
| 100 | } |
| 101 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 104 | return list; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 105 | finally: |
| 106 | Py_DECREF(list); |
| 107 | return NULL; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 111 | static PyObject * |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 112 | strop_splitfields(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 113 | PyObject *self; /* Not used */ |
| 114 | PyObject *args; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 115 | { |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 116 | int len, n, i, j, err; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 117 | int splitcount, maxsplit; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 118 | char *s, *sub; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 119 | PyObject *list, *item; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 121 | sub = NULL; |
| 122 | n = 0; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 123 | splitcount = 0; |
| 124 | maxsplit = 0; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 125 | 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] | 126 | return NULL; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 127 | if (sub == NULL) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 128 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 129 | if (n == 0) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 130 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 134 | list = PyList_New(0); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 135 | if (list == NULL) |
| 136 | return NULL; |
| 137 | |
| 138 | i = j = 0; |
| 139 | while (i+n <= len) { |
Guido van Rossum | a0ca4c4 | 1996-10-04 13:39:37 +0000 | [diff] [blame] | 140 | 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] | 141 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 142 | if (item == NULL) |
| 143 | goto fail; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 144 | err = PyList_Append(list, item); |
| 145 | Py_DECREF(item); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 146 | if (err < 0) |
| 147 | goto fail; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 148 | i = j = i + n; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 149 | splitcount++; |
| 150 | if (maxsplit && (splitcount >= maxsplit)) |
| 151 | break; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 152 | } |
| 153 | else |
| 154 | i++; |
| 155 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 156 | item = PyString_FromStringAndSize(s+j, (int)(len-j)); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 157 | if (item == NULL) |
| 158 | goto fail; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 159 | err = PyList_Append(list, item); |
| 160 | Py_DECREF(item); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 161 | if (err < 0) |
| 162 | goto fail; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 163 | |
| 164 | return list; |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 165 | |
| 166 | fail: |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 167 | Py_DECREF(list); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 168 | return NULL; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 172 | static PyObject * |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 173 | strop_joinfields(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 174 | PyObject *self; /* Not used */ |
| 175 | PyObject *args; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 176 | { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 177 | PyObject *seq; |
| 178 | char *sep = NULL; |
| 179 | int seqlen, seplen = 0; |
| 180 | int i, reslen = 0, slen = 0, sz = 100; |
| 181 | PyObject *res = NULL; |
| 182 | char* p = NULL; |
| 183 | intargfunc getitemfunc; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 184 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 185 | if (!PyArg_ParseTuple(args, "O|s#", &seq, &sep, &seplen)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 186 | return NULL; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 187 | if (sep == NULL) { |
| 188 | sep = " "; |
| 189 | seplen = 1; |
| 190 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 191 | |
| 192 | seqlen = PySequence_Length(seq); |
| 193 | if (seqlen < 0 && PyErr_Occurred()) |
| 194 | return NULL; |
| 195 | |
| 196 | if (seqlen == 1) { |
| 197 | /* Optimization if there's only one item */ |
| 198 | PyObject *item = PySequence_GetItem(seq, 0); |
| 199 | if (item && !PyString_Check(item)) |
| 200 | PyErr_SetString(PyExc_TypeError, |
| 201 | "first argument must be sequence of strings"); |
| 202 | return item; |
| 203 | } |
| 204 | |
| 205 | if (!(res = PyString_FromStringAndSize((char*)NULL, sz))) |
| 206 | return NULL; |
| 207 | p = PyString_AsString(res); |
| 208 | |
| 209 | /* optimize for lists, since it's the most common case. all others |
| 210 | * (tuples and arbitrary sequences) just use the sequence abstract |
| 211 | * interface. |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 212 | */ |
| 213 | if (PyList_Check(seq)) { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 214 | for (i = 0; i < seqlen; i++) { |
| 215 | PyObject *item = PyList_GET_ITEM(seq, i); |
| 216 | if (!PyString_Check(item)) { |
| 217 | PyErr_SetString(PyExc_TypeError, |
| 218 | "first argument must be sequence of strings"); |
| 219 | Py_DECREF(res); |
| 220 | return NULL; |
| 221 | } |
| 222 | slen = PyString_GET_SIZE(item); |
| 223 | while (reslen + slen + seplen >= sz) { |
| 224 | if (_PyString_Resize(&res, sz * 2)) { |
| 225 | Py_DECREF(res); |
| 226 | return NULL; |
| 227 | } |
| 228 | sz *= 2; |
| 229 | p = PyString_AsString(res) + reslen; |
| 230 | } |
| 231 | if (i > 0) { |
| 232 | memcpy(p, sep, seplen); |
| 233 | p += seplen; |
| 234 | reslen += seplen; |
| 235 | } |
| 236 | memcpy(p, PyString_AS_STRING(item), slen); |
| 237 | p += slen; |
| 238 | reslen += slen; |
| 239 | } |
| 240 | if (_PyString_Resize(&res, reslen)) { |
| 241 | Py_DECREF(res); |
| 242 | res = NULL; |
| 243 | } |
| 244 | return res; |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 245 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 246 | else if (!PySequence_Check(seq)) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 247 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 248 | "first argument must be a sequence"); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 249 | return NULL; |
| 250 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 251 | /* type safe */ |
| 252 | getitemfunc = seq->ob_type->tp_as_sequence->sq_item; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 253 | for (i = 0; i < seqlen; i++) { |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 254 | PyObject *item = getitemfunc(seq, i); |
| 255 | if (!item || !PyString_Check(item)) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 256 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 257 | "first argument must be sequence of strings"); |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 258 | Py_DECREF(res); |
| 259 | Py_XDECREF(item); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 260 | return NULL; |
| 261 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 262 | slen = PyString_GET_SIZE(item); |
| 263 | while (reslen + slen + seplen >= sz) { |
| 264 | if (_PyString_Resize(&res, sz * 2)) { |
| 265 | Py_DECREF(res); |
| 266 | Py_DECREF(item); |
| 267 | return NULL; |
| 268 | } |
| 269 | sz *= 2; |
| 270 | p = PyString_AsString(res) + reslen; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 271 | } |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 272 | if (i > 0) { |
| 273 | memcpy(p, sep, seplen); |
| 274 | p += seplen; |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 275 | reslen += seplen; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 276 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 277 | memcpy(p, PyString_AS_STRING(item), slen); |
| 278 | p += slen; |
| 279 | reslen += slen; |
| 280 | Py_DECREF(item); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 281 | } |
Barry Warsaw | d4ff1b9 | 1997-01-06 22:48:32 +0000 | [diff] [blame] | 282 | if (_PyString_Resize(&res, reslen)) { |
| 283 | Py_DECREF(res); |
| 284 | res = NULL; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 285 | } |
| 286 | return res; |
| 287 | } |
| 288 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 289 | static PyObject * |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 290 | strop_find(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 291 | PyObject *self; /* Not used */ |
| 292 | PyObject *args; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 293 | { |
| 294 | char *s, *sub; |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 295 | int len, n, i = 0, last = INT_MAX; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 297 | 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] | 298 | return NULL; |
| 299 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 300 | if (last > len) |
| 301 | last = len; |
| 302 | if (last < 0) |
| 303 | last += len; |
| 304 | if (last < 0) |
| 305 | last = 0; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 306 | if (i < 0) |
| 307 | i += len; |
| 308 | if (i < 0) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 309 | i = 0; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 310 | |
| 311 | if (n == 0) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 312 | return PyInt_FromLong((long)i); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 314 | last -= n; |
| 315 | for (; i <= last; ++i) |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 316 | if (s[i] == sub[0] && |
Guido van Rossum | a0ca4c4 | 1996-10-04 13:39:37 +0000 | [diff] [blame] | 317 | (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0)) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 318 | return PyInt_FromLong((long)i); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 319 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 320 | return PyInt_FromLong(-1L); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 324 | static PyObject * |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 325 | strop_rfind(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 326 | PyObject *self; /* Not used */ |
| 327 | PyObject *args; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 328 | { |
| 329 | char *s, *sub; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 330 | int len, n, j; |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 331 | int i = 0, last = INT_MAX; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 333 | 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] | 334 | return NULL; |
| 335 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 336 | if (last > len) |
| 337 | last = len; |
| 338 | if (last < 0) |
| 339 | last += len; |
| 340 | if (last < 0) |
| 341 | last = 0; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 342 | if (i < 0) |
| 343 | i += len; |
| 344 | if (i < 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 345 | i = 0; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 346 | |
| 347 | if (n == 0) |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 348 | return PyInt_FromLong((long)last); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 350 | for (j = last-n; j >= i; --j) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 351 | if (s[j] == sub[0] && |
Guido van Rossum | a0ca4c4 | 1996-10-04 13:39:37 +0000 | [diff] [blame] | 352 | (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0)) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 353 | return PyInt_FromLong((long)j); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 354 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 355 | return PyInt_FromLong(-1L); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 358 | static PyObject * |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 359 | do_strip(args, striptype) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 360 | PyObject *args; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 361 | int striptype; |
| 362 | { |
| 363 | char *s; |
| 364 | int len, i, j; |
| 365 | |
| 366 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 367 | if (!PyArg_Parse(args, "s#", &s, &len)) |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 368 | return NULL; |
| 369 | |
| 370 | i = 0; |
| 371 | if (striptype != RIGHTSTRIP) { |
| 372 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 373 | i++; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
| 378 | j = len; |
| 379 | if (striptype != LEFTSTRIP) { |
| 380 | do { |
| 381 | j--; |
| 382 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 383 | j++; |
| 384 | } |
| 385 | |
| 386 | if (i == 0 && j == len) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 387 | Py_INCREF(args); |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 388 | return args; |
| 389 | } |
| 390 | else |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 391 | return PyString_FromStringAndSize(s+i, j-i); |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 394 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 395 | static PyObject * |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 396 | strop_strip(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 397 | PyObject *self; /* Not used */ |
| 398 | PyObject *args; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 399 | { |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 400 | return do_strip(args, BOTHSTRIP); |
| 401 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 402 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 403 | static PyObject * |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 404 | strop_lstrip(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 405 | PyObject *self; /* Not used */ |
| 406 | PyObject *args; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 407 | { |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 408 | return do_strip(args, LEFTSTRIP); |
| 409 | } |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 410 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 411 | static PyObject * |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 412 | strop_rstrip(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 413 | PyObject *self; /* Not used */ |
| 414 | PyObject *args; |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 415 | { |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 416 | return do_strip(args, RIGHTSTRIP); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 420 | static PyObject * |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 421 | strop_lower(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 422 | PyObject *self; /* Not used */ |
| 423 | PyObject *args; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 424 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 425 | char *s, *s_new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 426 | int i, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 427 | PyObject *new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 428 | int changed; |
| 429 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 430 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 431 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 432 | new = PyString_FromStringAndSize(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 433 | if (new == NULL) |
| 434 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 435 | s_new = PyString_AsString(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 436 | changed = 0; |
| 437 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 438 | int c = Py_CHARMASK(*s++); |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 439 | if (isupper(c)) { |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 440 | changed = 1; |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 441 | *s_new = tolower(c); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 442 | } else |
| 443 | *s_new = c; |
| 444 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 445 | } |
| 446 | if (!changed) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 447 | Py_DECREF(new); |
| 448 | Py_INCREF(args); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 449 | return args; |
| 450 | } |
| 451 | return new; |
| 452 | } |
| 453 | |
| 454 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 455 | static PyObject * |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 456 | strop_upper(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 457 | PyObject *self; /* Not used */ |
| 458 | PyObject *args; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 459 | { |
Barry Warsaw | 04d2d15 | 1997-01-03 23:46:51 +0000 | [diff] [blame] | 460 | char *s, *s_new; |
| 461 | int i, n; |
| 462 | PyObject *new; |
| 463 | int changed; |
| 464 | |
| 465 | if (!PyArg_Parse(args, "s#", &s, &n)) |
| 466 | return NULL; |
| 467 | new = PyString_FromStringAndSize(NULL, n); |
| 468 | if (new == NULL) |
| 469 | return NULL; |
| 470 | s_new = PyString_AsString(new); |
| 471 | changed = 0; |
| 472 | for (i = 0; i < n; i++) { |
| 473 | int c = Py_CHARMASK(*s++); |
| 474 | if (islower(c)) { |
| 475 | changed = 1; |
| 476 | *s_new = toupper(c); |
| 477 | } else |
| 478 | *s_new = c; |
| 479 | s_new++; |
| 480 | } |
| 481 | if (!changed) { |
| 482 | Py_DECREF(new); |
| 483 | Py_INCREF(args); |
| 484 | return args; |
| 485 | } |
| 486 | return new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 490 | static PyObject * |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 491 | strop_capitalize(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 492 | PyObject *self; /* Not used */ |
| 493 | PyObject *args; |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 494 | { |
| 495 | char *s, *s_new; |
| 496 | int i, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 497 | PyObject *new; |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 498 | int changed; |
| 499 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 500 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 501 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 502 | new = PyString_FromStringAndSize(NULL, n); |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 503 | if (new == NULL) |
| 504 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 505 | s_new = PyString_AsString(new); |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 506 | changed = 0; |
Guido van Rossum | 529c963 | 1996-06-17 16:59:33 +0000 | [diff] [blame] | 507 | if (0 < n) { |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 508 | int c = Py_CHARMASK(*s++); |
| 509 | if (islower(c)) { |
| 510 | changed = 1; |
| 511 | *s_new = toupper(c); |
| 512 | } else |
| 513 | *s_new = c; |
| 514 | s_new++; |
| 515 | } |
| 516 | for (i = 1; i < n; i++) { |
| 517 | int c = Py_CHARMASK(*s++); |
| 518 | if (isupper(c)) { |
| 519 | changed = 1; |
| 520 | *s_new = tolower(c); |
| 521 | } else |
| 522 | *s_new = c; |
| 523 | s_new++; |
| 524 | } |
| 525 | if (!changed) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 526 | Py_DECREF(new); |
| 527 | Py_INCREF(args); |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 528 | return args; |
| 529 | } |
| 530 | return new; |
| 531 | } |
| 532 | |
| 533 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 534 | static PyObject * |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 535 | strop_swapcase(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 536 | PyObject *self; /* Not used */ |
| 537 | PyObject *args; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 538 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 539 | char *s, *s_new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 540 | int i, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 541 | PyObject *new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 542 | int changed; |
| 543 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 544 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 545 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 546 | new = PyString_FromStringAndSize(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 547 | if (new == NULL) |
| 548 | return NULL; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 549 | s_new = PyString_AsString(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 550 | changed = 0; |
| 551 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 552 | int c = Py_CHARMASK(*s++); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 553 | if (islower(c)) { |
| 554 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 555 | *s_new = toupper(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 556 | } |
| 557 | else if (isupper(c)) { |
| 558 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 559 | *s_new = tolower(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 560 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 561 | else |
| 562 | *s_new = c; |
| 563 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 564 | } |
| 565 | if (!changed) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 566 | Py_DECREF(new); |
| 567 | Py_INCREF(args); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 568 | return args; |
| 569 | } |
| 570 | return new; |
| 571 | } |
| 572 | |
| 573 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 574 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 575 | strop_atoi(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 | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 578 | { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 579 | extern long PyOS_strtol Py_PROTO((const char *, char **, int)); |
| 580 | extern unsigned long |
| 581 | PyOS_strtoul Py_PROTO((const char *, char **, int)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 582 | char *s, *end; |
| 583 | int base = 10; |
| 584 | long x; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 585 | char buffer[256]; /* For errors */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 586 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 587 | if (!PyArg_ParseTuple(args, "s|i", &s, &base)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 588 | return NULL; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 589 | |
| 590 | if ((base != 0 && base < 2) || base > 36) { |
| 591 | PyErr_SetString(PyExc_ValueError, "invalid base for atoi()"); |
| 592 | return NULL; |
| 593 | } |
| 594 | |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 595 | while (*s && isspace(Py_CHARMASK(*s))) |
| 596 | s++; |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 597 | if (s[0] == '\0') { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 598 | PyErr_SetString(PyExc_ValueError, "empty string for atoi()"); |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 599 | return NULL; |
| 600 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 601 | errno = 0; |
| 602 | if (base == 0 && s[0] == '0') |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 603 | x = (long) PyOS_strtoul(s, &end, base); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 604 | else |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 605 | x = PyOS_strtol(s, &end, base); |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 606 | while (*end && isspace(Py_CHARMASK(*end))) |
| 607 | end++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 608 | if (*end != '\0') { |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 609 | sprintf(buffer, "invalid literal for atoi(): %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 610 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 611 | return NULL; |
| 612 | } |
| 613 | else if (errno != 0) { |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 614 | sprintf(buffer, "atoi() literal too large: %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 615 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 616 | return NULL; |
| 617 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 618 | return PyInt_FromLong(x); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 622 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 623 | strop_atol(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 624 | PyObject *self; /* Not used */ |
| 625 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 626 | { |
| 627 | char *s, *end; |
| 628 | int base = 10; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 629 | PyObject *x; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 630 | char buffer[256]; /* For errors */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 631 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 632 | if (!PyArg_ParseTuple(args, "s|i", &s, &base)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 633 | return NULL; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 634 | |
| 635 | if ((base != 0 && base < 2) || base > 36) { |
| 636 | PyErr_SetString(PyExc_ValueError, "invalid base for atol()"); |
| 637 | return NULL; |
| 638 | } |
| 639 | |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 640 | while (*s && isspace(Py_CHARMASK(*s))) |
| 641 | s++; |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 642 | if (s[0] == '\0') { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 643 | PyErr_SetString(PyExc_ValueError, "empty string for atol()"); |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 644 | return NULL; |
| 645 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 646 | x = PyLong_FromString(s, &end, base); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 647 | if (x == NULL) |
| 648 | return NULL; |
| 649 | if (base == 0 && (*end == 'l' || *end == 'L')) |
| 650 | end++; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 651 | while (*end && isspace(Py_CHARMASK(*end))) |
| 652 | end++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 653 | if (*end != '\0') { |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 654 | sprintf(buffer, "invalid literal for atol(): %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 655 | PyErr_SetString(PyExc_ValueError, buffer); |
| 656 | Py_DECREF(x); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 657 | return NULL; |
| 658 | } |
| 659 | return x; |
| 660 | } |
| 661 | |
| 662 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 663 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 664 | strop_atof(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 665 | PyObject *self; /* Not used */ |
| 666 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 667 | { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 668 | extern double strtod Py_PROTO((const char *, char **)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 669 | char *s, *end; |
| 670 | double x; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 671 | char buffer[256]; /* For errors */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 672 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 673 | if (!PyArg_Parse(args, "s", &s)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 674 | return NULL; |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 675 | while (*s && isspace(Py_CHARMASK(*s))) |
| 676 | s++; |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 677 | if (s[0] == '\0') { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 678 | PyErr_SetString(PyExc_ValueError, "empty string for atof()"); |
Guido van Rossum | 171191e | 1996-08-21 20:02:25 +0000 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 681 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 682 | PyFPE_START_PROTECT("strop_atof", return 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 683 | x = strtod(s, &end); |
Guido van Rossum | 7b7c578 | 1997-03-14 04:13:56 +0000 | [diff] [blame] | 684 | PyFPE_END_PROTECT(x) |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 685 | while (*end && isspace(Py_CHARMASK(*end))) |
| 686 | end++; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 687 | if (*end != '\0') { |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 688 | sprintf(buffer, "invalid literal for atof(): %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 689 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 690 | return NULL; |
| 691 | } |
| 692 | else if (errno != 0) { |
Guido van Rossum | c35f933 | 1996-09-11 23:30:42 +0000 | [diff] [blame] | 693 | sprintf(buffer, "atof() literal too large: %.200s", s); |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 694 | PyErr_SetString(PyExc_ValueError, buffer); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 695 | return NULL; |
| 696 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 697 | return PyFloat_FromDouble(x); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 701 | static PyObject * |
| 702 | strop_maketrans(self, args) |
| 703 | PyObject *self; /* Not used */ |
| 704 | PyObject *args; |
| 705 | { |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 706 | unsigned char *c, *from=NULL, *to=NULL; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 707 | int i, fromlen=0, tolen=0; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 708 | PyObject *result; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 709 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 710 | if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, &to, &tolen)) |
| 711 | return NULL; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 712 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 713 | if (fromlen != tolen) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 714 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 715 | "maketrans arguments must have same length"); |
| 716 | return NULL; |
| 717 | } |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 718 | |
| 719 | result = PyString_FromStringAndSize((char *)NULL, 256); |
| 720 | if (result == NULL) |
| 721 | return NULL; |
| 722 | c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 723 | for (i = 0; i < 256; i++) |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 724 | c[i]=(unsigned char)i; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 725 | for (i = 0; i < fromlen; i++) |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 726 | c[from[i]]=to[i]; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 728 | return result; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 732 | static PyObject * |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 733 | strop_translate(self, args) |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 734 | PyObject *self; |
| 735 | PyObject *args; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 736 | { |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 737 | register char *input, *table, *output; |
| 738 | register int i, c, changed = 0; |
| 739 | PyObject *input_obj; |
| 740 | char *table1, *output_start, *del_table=NULL; |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 741 | int inlen, tablen, dellen = 0; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 742 | PyObject *result; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 743 | int trans_table[256]; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 744 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 745 | if (!PyArg_ParseTuple(args, "Ss#|s#", &input_obj, |
| 746 | &table1, &tablen, &del_table, &dellen)) |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 747 | return NULL; |
| 748 | if (tablen != 256) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 749 | PyErr_SetString(PyExc_ValueError, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 750 | "translation table must be 256 characters long"); |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 751 | return NULL; |
| 752 | } |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 753 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 754 | table = table1; |
| 755 | inlen = PyString_Size(input_obj); |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 756 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 757 | if (result == NULL) |
| 758 | return NULL; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 759 | output_start = output = PyString_AsString(result); |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 760 | input = PyString_AsString(input_obj); |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 761 | |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 762 | if (dellen == 0) { |
| 763 | /* If no deletions are required, use faster code */ |
| 764 | for (i = inlen; --i >= 0; ) { |
| 765 | c = Py_CHARMASK(*input++); |
| 766 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 767 | changed = 1; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 768 | } |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 769 | if (changed) |
| 770 | return result; |
| 771 | Py_DECREF(result); |
| 772 | Py_INCREF(input_obj); |
| 773 | return input_obj; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 774 | } |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 775 | |
| 776 | for (i = 0; i < 256; i++) |
| 777 | trans_table[i] = Py_CHARMASK(table[i]); |
| 778 | |
| 779 | for (i = 0; i < dellen; i++) |
Guido van Rossum | 1ed5e57 | 1997-04-29 21:34:16 +0000 | [diff] [blame] | 780 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
Guido van Rossum | e0548b8 | 1997-01-06 16:50:09 +0000 | [diff] [blame] | 781 | |
| 782 | for (i = inlen; --i >= 0; ) { |
| 783 | c = Py_CHARMASK(*input++); |
| 784 | if (trans_table[c] != -1) |
| 785 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 786 | continue; |
| 787 | changed = 1; |
| 788 | } |
| 789 | if (!changed) { |
| 790 | Py_DECREF(result); |
| 791 | Py_INCREF(input_obj); |
| 792 | return input_obj; |
| 793 | } |
| 794 | /* Fix the size of the resulting string */ |
| 795 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 796 | return NULL; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 797 | return result; |
| 798 | } |
| 799 | |
| 800 | |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 801 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 802 | |
| 803 | /* |
| 804 | mymemfind |
| 805 | |
| 806 | strstr replacement for arbitrary blocks of memory. |
| 807 | |
| 808 | Locates the first occurance in the memory pointed to by MEM of the |
| 809 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 810 | found, or -1 if not found. If len of PAT is greater than length of |
| 811 | MEM, the function returns -1. |
| 812 | */ |
| 813 | static int mymemfind(mem, len, pat, pat_len) |
| 814 | char *mem; |
| 815 | int len; |
| 816 | char *pat; |
| 817 | int pat_len; |
| 818 | { |
| 819 | register int ii; |
| 820 | |
| 821 | /* pattern can not occur in the last pat_len-1 chars */ |
| 822 | len -= pat_len; |
| 823 | |
| 824 | for (ii = 0; ii <= len; ii++) { |
| 825 | if (mem[ii] == pat[0] && |
| 826 | (pat_len == 1 || |
| 827 | memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) { |
| 828 | return ii; |
| 829 | } |
| 830 | } |
| 831 | return -1; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | mymemcnt |
| 836 | |
| 837 | Return the number of distinct times PAT is found in MEM. |
| 838 | meaning mem=1111 and pat==11 returns 2. |
| 839 | mem=11111 and pat==11 also return 2. |
| 840 | */ |
| 841 | static int mymemcnt(mem, len, pat, pat_len) |
| 842 | char *mem; |
| 843 | int len; |
| 844 | char *pat; |
| 845 | int pat_len; |
| 846 | { |
| 847 | register int offset = 0; |
| 848 | int nfound = 0; |
| 849 | |
| 850 | while (len >= 0) { |
| 851 | offset = mymemfind(mem, len, pat, pat_len); |
| 852 | if (offset == -1) |
| 853 | break; |
| 854 | mem += offset + pat_len; |
| 855 | len -= offset + pat_len; |
| 856 | nfound++; |
| 857 | } |
| 858 | return nfound; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | mymemreplace |
| 863 | |
| 864 | Return a string in which all occurences of PAT in memory STR are |
| 865 | replaced with SUB. |
| 866 | |
| 867 | If length of PAT is less than length of STR or there are no occurences |
| 868 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 869 | string is allocated here and returned. |
| 870 | |
| 871 | on return, out_len is: |
| 872 | the length of output string, or |
| 873 | -1 if the input string is returned, or |
| 874 | unchanged if an error occurs (no memory). |
| 875 | |
| 876 | return value is: |
| 877 | the new string allocated locally, or |
| 878 | NULL if an error occurred. |
| 879 | */ |
| 880 | static char *mymemreplace(str, len, pat, pat_len, sub, sub_len, out_len) |
| 881 | char *str; |
| 882 | int len; /* input string */ |
| 883 | char *pat; |
| 884 | int pat_len; /* pattern string to find */ |
| 885 | char *sub; |
| 886 | int sub_len; /* substitution string */ |
| 887 | int *out_len; |
| 888 | |
| 889 | { |
| 890 | char *out_s; |
| 891 | char *new_s; |
| 892 | int nfound, offset, new_len; |
| 893 | |
| 894 | if (len == 0 || pat_len > len) |
| 895 | goto return_same; |
| 896 | |
| 897 | /* find length of output string */ |
| 898 | nfound = mymemcnt(str, len, pat, pat_len); |
| 899 | if (nfound == 0) |
| 900 | goto return_same; |
| 901 | new_len = len + nfound*(sub_len - pat_len); |
| 902 | |
| 903 | new_s = (char *)malloc(new_len); |
| 904 | if (new_s == NULL) return NULL; |
| 905 | |
| 906 | *out_len = new_len; |
| 907 | out_s = new_s; |
| 908 | |
| 909 | while (len > 0) { |
| 910 | /* find index of next instance of pattern */ |
| 911 | offset = mymemfind(str, len, pat, pat_len); |
| 912 | /* if not found, break out of loop */ |
| 913 | if (offset == -1) break; |
| 914 | |
| 915 | /* copy non matching part of input string */ |
| 916 | memcpy(new_s, str, offset); /* copy part of str before pat */ |
| 917 | str += offset + pat_len; /* move str past pattern */ |
| 918 | len -= offset + pat_len; /* reduce length of str remaining */ |
| 919 | |
| 920 | /* copy substitute into the output string */ |
| 921 | new_s += offset; /* move new_s to dest for sub string */ |
| 922 | memcpy(new_s, sub, sub_len); /* copy substring into new_s */ |
| 923 | new_s += sub_len; /* offset new_s past sub string */ |
| 924 | } |
| 925 | /* copy any remaining values into output string */ |
| 926 | if (len > 0) |
| 927 | memcpy(new_s, str, len); |
| 928 | return out_s; |
| 929 | |
| 930 | return_same: |
| 931 | *out_len = -1; |
| 932 | return str; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | static PyObject* |
| 937 | strop_replace(self, args) |
| 938 | PyObject *self; /* Not used */ |
| 939 | PyObject *args; |
| 940 | { |
| 941 | char *str, *pat,*sub,*new_s; |
| 942 | int len,pat_len,sub_len,out_len; |
| 943 | PyObject *new; |
| 944 | |
| 945 | if (!PyArg_ParseTuple(args, "s#s#s#", |
| 946 | &str, &len, &pat, &pat_len, &sub, &sub_len)) |
| 947 | return NULL; |
| 948 | new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,&out_len); |
| 949 | if (new_s == NULL) { |
| 950 | PyErr_NoMemory(); |
| 951 | return NULL; |
| 952 | } |
| 953 | if (out_len == -1) { |
| 954 | /* we're returning another reference to the input string */ |
| 955 | new = PyTuple_GetItem(args, 0); |
| 956 | Py_XINCREF(new); |
| 957 | } |
| 958 | else { |
| 959 | new = PyString_FromStringAndSize(new_s, out_len); |
| 960 | free(new_s); |
| 961 | } |
| 962 | return new; |
| 963 | } |
| 964 | |
| 965 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 966 | /* List of functions defined in the module */ |
| 967 | |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 968 | static PyMethodDef |
| 969 | strop_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 970 | {"atof", strop_atof}, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 971 | {"atoi", strop_atoi, 1}, |
| 972 | {"atol", strop_atol, 1}, |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 973 | {"capitalize", strop_capitalize}, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 974 | {"find", strop_find, 1}, |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 975 | {"join", strop_joinfields, 1}, |
| 976 | {"joinfields", strop_joinfields, 1}, |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 977 | {"lstrip", strop_lstrip}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 978 | {"lower", strop_lower}, |
Guido van Rossum | 101923b | 1997-04-02 06:11:18 +0000 | [diff] [blame] | 979 | {"maketrans", strop_maketrans, 1}, |
| 980 | {"replace", strop_replace, 1}, |
Barry Warsaw | e8fc29c | 1997-01-03 22:45:34 +0000 | [diff] [blame] | 981 | {"rfind", strop_rfind, 1}, |
Guido van Rossum | 7999a5c | 1996-08-08 19:16:15 +0000 | [diff] [blame] | 982 | {"rstrip", strop_rstrip}, |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 983 | {"split", strop_splitfields, 1}, |
| 984 | {"splitfields", strop_splitfields, 1}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 985 | {"strip", strop_strip}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 986 | {"swapcase", strop_swapcase}, |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 987 | {"translate", strop_translate, 1}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 988 | {"upper", strop_upper}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 989 | {NULL, NULL} /* sentinel */ |
| 990 | }; |
| 991 | |
| 992 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 993 | void |
| 994 | initstrop() |
| 995 | { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 996 | PyObject *m, *d, *s; |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 997 | char buf[256]; |
| 998 | int c, n; |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 999 | m = Py_InitModule("strop", strop_methods); |
| 1000 | d = PyModule_GetDict(m); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1001 | |
| 1002 | /* Create 'whitespace' object */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 1003 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1004 | for (c = 0; c < 256; c++) { |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 1005 | if (isspace(c)) |
| 1006 | buf[n++] = c; |
| 1007 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1008 | s = PyString_FromStringAndSize(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1009 | if (s) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1010 | PyDict_SetItemString(d, "whitespace", s); |
| 1011 | Py_DECREF(s); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1012 | } |
| 1013 | /* Create 'lowercase' object */ |
| 1014 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1015 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1016 | if (islower(c)) |
| 1017 | buf[n++] = c; |
| 1018 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1019 | s = PyString_FromStringAndSize(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1020 | if (s) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1021 | PyDict_SetItemString(d, "lowercase", s); |
| 1022 | Py_DECREF(s); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* Create 'uppercase' object */ |
| 1026 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1027 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1028 | if (isupper(c)) |
| 1029 | buf[n++] = c; |
| 1030 | } |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1031 | s = PyString_FromStringAndSize(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1032 | if (s) { |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1033 | PyDict_SetItemString(d, "uppercase", s); |
| 1034 | Py_DECREF(s); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Barry Warsaw | f525601 | 1996-12-09 18:35:56 +0000 | [diff] [blame] | 1037 | if (PyErr_Occurred()) |
| 1038 | Py_FatalError("can't initialize module strop"); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 1039 | } |