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 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* strop module */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | #include "modsupport.h" |
| 29 | |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 31 | /* XXX This file assumes that the <ctype.h> is*() functions |
| 32 | XXX are defined for all 8-bit characters! */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 36 | |
| 37 | static object * |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 38 | split_whitespace(s, len) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 39 | char *s; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 40 | int len; |
| 41 | { |
| 42 | int i, j, err; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 43 | object *list, *item; |
| 44 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 45 | list = newlistobject(0); |
| 46 | if (list == NULL) |
| 47 | return NULL; |
| 48 | |
| 49 | i = 0; |
| 50 | while (i < len) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 51 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 52 | i = i+1; |
| 53 | } |
| 54 | j = i; |
Guido van Rossum | ee1813d | 1995-02-14 00:58:59 +0000 | [diff] [blame] | 55 | while (i < len && !isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 56 | i = i+1; |
| 57 | } |
| 58 | if (j < i) { |
| 59 | item = newsizedstringobject(s+j, (int)(i-j)); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 60 | if (item == NULL) { |
| 61 | DECREF(list); |
| 62 | return NULL; |
| 63 | } |
| 64 | err = addlistitem(list, item); |
| 65 | DECREF(item); |
| 66 | if (err < 0) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 67 | DECREF(list); |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return list; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static object * |
| 78 | strop_splitfields(self, args) |
| 79 | object *self; /* Not used */ |
| 80 | object *args; |
| 81 | { |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 82 | int len, n, i, j, err; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 83 | char *s, *sub; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 84 | object *list, *item; |
| 85 | |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 86 | sub = NULL; |
| 87 | n = 0; |
| 88 | if (!newgetargs(args, "s#|z#", &s, &len, &sub, &n)) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 89 | return NULL; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 90 | if (sub == NULL) |
| 91 | return split_whitespace(s, len); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 92 | if (n == 0) { |
| 93 | err_setstr(ValueError, "empty separator"); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | list = newlistobject(0); |
| 98 | if (list == NULL) |
| 99 | return NULL; |
| 100 | |
| 101 | i = j = 0; |
| 102 | while (i+n <= len) { |
| 103 | if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) { |
| 104 | item = newsizedstringobject(s+j, (int)(i-j)); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 105 | if (item == NULL) |
| 106 | goto fail; |
| 107 | err = addlistitem(list, item); |
| 108 | DECREF(item); |
| 109 | if (err < 0) |
| 110 | goto fail; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 111 | i = j = i + n; |
| 112 | } |
| 113 | else |
| 114 | i++; |
| 115 | } |
| 116 | item = newsizedstringobject(s+j, (int)(len-j)); |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 117 | if (item == NULL) |
| 118 | goto fail; |
| 119 | err = addlistitem(list, item); |
| 120 | DECREF(item); |
| 121 | if (err < 0) |
| 122 | goto fail; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 123 | |
| 124 | return list; |
Guido van Rossum | 572d2d9 | 1993-11-05 10:14:49 +0000 | [diff] [blame] | 125 | |
| 126 | fail: |
| 127 | DECREF(list); |
| 128 | return NULL; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | static object * |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 133 | strop_joinfields(self, args) |
| 134 | object *self; /* Not used */ |
| 135 | object *args; |
| 136 | { |
| 137 | object *seq, *item, *res; |
| 138 | object * (*getitem) FPROTO((object *, int)); |
| 139 | char *sep, *p; |
| 140 | int seplen, seqlen, reslen, itemlen, i; |
| 141 | |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 142 | sep = NULL; |
| 143 | seplen = 0; |
| 144 | if (!newgetargs(args, "O|s#", &seq, &sep, &seplen)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 145 | return NULL; |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 146 | if (sep == NULL) { |
| 147 | sep = " "; |
| 148 | seplen = 1; |
| 149 | } |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 150 | if (is_listobject(seq)) { |
| 151 | getitem = getlistitem; |
| 152 | seqlen = getlistsize(seq); |
| 153 | } |
| 154 | else if (is_tupleobject(seq)) { |
| 155 | getitem = gettupleitem; |
| 156 | seqlen = gettuplesize(seq); |
| 157 | } |
| 158 | else { |
| 159 | err_setstr(TypeError, "first argument must be list/tuple"); |
| 160 | return NULL; |
| 161 | } |
| 162 | reslen = 0; |
| 163 | for (i = 0; i < seqlen; i++) { |
| 164 | item = getitem(seq, i); |
| 165 | if (!is_stringobject(item)) { |
| 166 | err_setstr(TypeError, |
| 167 | "first argument must be list/tuple of strings"); |
| 168 | return NULL; |
| 169 | } |
| 170 | if (i > 0) |
| 171 | reslen = reslen + seplen; |
| 172 | reslen = reslen + getstringsize(item); |
| 173 | } |
| 174 | if (seqlen == 1) { |
| 175 | /* Optimization if there's only one item */ |
| 176 | item = getitem(seq, 0); |
| 177 | INCREF(item); |
| 178 | return item; |
| 179 | } |
| 180 | res = newsizedstringobject((char *)NULL, reslen); |
| 181 | if (res == NULL) |
| 182 | return NULL; |
| 183 | p = getstringvalue(res); |
| 184 | for (i = 0; i < seqlen; i++) { |
| 185 | item = getitem(seq, i); |
| 186 | if (i > 0) { |
| 187 | memcpy(p, sep, seplen); |
| 188 | p += seplen; |
| 189 | } |
| 190 | itemlen = getstringsize(item); |
| 191 | memcpy(p, getstringvalue(item), itemlen); |
| 192 | p += itemlen; |
| 193 | } |
| 194 | if (p != getstringvalue(res) + reslen) { |
| 195 | err_setstr(SystemError, "strop.joinfields: assertion failed"); |
| 196 | return NULL; |
| 197 | } |
| 198 | return res; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static object * |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 203 | strop_find(self, args) |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 204 | object *self; /* Not used */ |
| 205 | object *args; |
| 206 | { |
| 207 | char *s, *sub; |
| 208 | int len, n, i; |
| 209 | |
| 210 | if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) { |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 211 | if (i < 0) |
| 212 | i += len; |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 213 | if (i < 0) |
| 214 | i = 0; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 215 | } |
| 216 | else { |
| 217 | err_clear(); |
| 218 | if (!getargs(args, "(s#s#)", &s, &len, &sub, &n)) |
| 219 | return NULL; |
| 220 | i = 0; |
| 221 | } |
| 222 | |
| 223 | if (n == 0) |
| 224 | return newintobject((long)i); |
| 225 | |
| 226 | len -= n; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 227 | for (; i <= len; ++i) |
| 228 | if (s[i] == sub[0] && |
| 229 | (n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0)) |
| 230 | return newintobject((long)i); |
| 231 | |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 232 | return newintobject(-1L); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | static object * |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 237 | strop_rfind(self, args) |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 238 | object *self; /* Not used */ |
| 239 | object *args; |
| 240 | { |
| 241 | char *s, *sub; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | int len, n, i, j; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 243 | |
| 244 | if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) { |
| 245 | if (i < 0) |
| 246 | i += len; |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 247 | if (i < 0) |
| 248 | i = 0; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 249 | } |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 250 | else { |
| 251 | err_clear(); |
| 252 | if (!getargs(args, "(s#s#)", &s, &len, &sub, &n)) |
| 253 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | i = 0; |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | if (n == 0) |
Guido van Rossum | c65a525 | 1994-08-05 13:44:50 +0000 | [diff] [blame] | 258 | return newintobject((long)len); |
Guido van Rossum | ee9012f | 1993-10-26 15:23:55 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | for (j = len-n; j >= i; --j) |
| 261 | if (s[j] == sub[0] && |
| 262 | (n == 1 || strncmp(&s[j+1], &sub[1], n-1) == 0)) |
| 263 | return newintobject((long)j); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 265 | return newintobject(-1L); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | static object * |
| 270 | strop_strip(self, args) |
| 271 | object *self; /* Not used */ |
| 272 | object *args; |
| 273 | { |
| 274 | char *s; |
| 275 | int len, i, j; |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 276 | |
| 277 | if (!getargs(args, "s#", &s, &len)) |
| 278 | return NULL; |
| 279 | |
| 280 | i = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 281 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 282 | i++; |
| 283 | } |
| 284 | |
| 285 | j = len; |
| 286 | do { |
| 287 | j--; |
Guido van Rossum | ee1813d | 1995-02-14 00:58:59 +0000 | [diff] [blame] | 288 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 289 | j++; |
| 290 | |
| 291 | if (i == 0 && j == len) { |
| 292 | INCREF(args); |
| 293 | return args; |
| 294 | } |
| 295 | else |
| 296 | return newsizedstringobject(s+i, j-i); |
| 297 | } |
| 298 | |
| 299 | |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 300 | static object * |
| 301 | strop_lower(self, args) |
| 302 | object *self; /* Not used */ |
| 303 | object *args; |
| 304 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 305 | char *s, *s_new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 306 | int i, n; |
| 307 | object *new; |
| 308 | int changed; |
| 309 | |
| 310 | if (!getargs(args, "s#", &s, &n)) |
| 311 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 312 | new = newsizedstringobject(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 313 | if (new == NULL) |
| 314 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 315 | s_new = getstringvalue(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 316 | changed = 0; |
| 317 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 318 | int c = Py_CHARMASK(*s++); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 319 | if (isupper(c)) { |
| 320 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 321 | *s_new = tolower(c); |
| 322 | } else |
| 323 | *s_new = c; |
| 324 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 325 | } |
| 326 | if (!changed) { |
| 327 | DECREF(new); |
| 328 | INCREF(args); |
| 329 | return args; |
| 330 | } |
| 331 | return new; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | static object * |
| 336 | strop_upper(self, args) |
| 337 | object *self; /* Not used */ |
| 338 | object *args; |
| 339 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 340 | char *s, *s_new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 341 | int i, n; |
| 342 | object *new; |
| 343 | int changed; |
| 344 | |
| 345 | if (!getargs(args, "s#", &s, &n)) |
| 346 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 347 | new = newsizedstringobject(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 348 | if (new == NULL) |
| 349 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 350 | s_new = getstringvalue(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 351 | changed = 0; |
| 352 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 353 | int c = Py_CHARMASK(*s++); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 354 | if (islower(c)) { |
| 355 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 356 | *s_new = toupper(c); |
| 357 | } else |
| 358 | *s_new = c; |
| 359 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 360 | } |
| 361 | if (!changed) { |
| 362 | DECREF(new); |
| 363 | INCREF(args); |
| 364 | return args; |
| 365 | } |
| 366 | return new; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | static object * |
| 371 | strop_swapcase(self, args) |
| 372 | object *self; /* Not used */ |
| 373 | object *args; |
| 374 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 375 | char *s, *s_new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 376 | int i, n; |
| 377 | object *new; |
| 378 | int changed; |
| 379 | |
| 380 | if (!getargs(args, "s#", &s, &n)) |
| 381 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 382 | new = newsizedstringobject(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 383 | if (new == NULL) |
| 384 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 385 | s_new = getstringvalue(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 386 | changed = 0; |
| 387 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 388 | int c = Py_CHARMASK(*s++); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 389 | if (islower(c)) { |
| 390 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 391 | *s_new = toupper(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 392 | } |
| 393 | else if (isupper(c)) { |
| 394 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 395 | *s_new = tolower(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 396 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 397 | else |
| 398 | *s_new = c; |
| 399 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 400 | } |
| 401 | if (!changed) { |
| 402 | DECREF(new); |
| 403 | INCREF(args); |
| 404 | return args; |
| 405 | } |
| 406 | return new; |
| 407 | } |
| 408 | |
| 409 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 410 | static object * |
| 411 | strop_atoi(self, args) |
| 412 | object *self; /* Not used */ |
| 413 | object *args; |
| 414 | { |
| 415 | extern long mystrtol PROTO((const char *, char **, int)); |
| 416 | extern unsigned long mystrtoul PROTO((const char *, char **, int)); |
| 417 | char *s, *end; |
| 418 | int base = 10; |
| 419 | long x; |
| 420 | |
| 421 | if (args != NULL && is_tupleobject(args)) { |
| 422 | if (!getargs(args, "(si)", &s, &base)) |
| 423 | return NULL; |
| 424 | if (base != 0 && base < 2 || base > 36) { |
| 425 | err_setstr(ValueError, "invalid base for atoi()"); |
| 426 | return NULL; |
| 427 | } |
| 428 | } |
| 429 | else if (!getargs(args, "s", &s)) |
| 430 | return NULL; |
| 431 | errno = 0; |
| 432 | if (base == 0 && s[0] == '0') |
| 433 | x = (long) mystrtoul(s, &end, base); |
| 434 | else |
| 435 | x = mystrtol(s, &end, base); |
| 436 | if (*end != '\0') { |
| 437 | err_setstr(ValueError, "invalid literal for atoi()"); |
| 438 | return NULL; |
| 439 | } |
| 440 | else if (errno != 0) { |
| 441 | err_setstr(OverflowError, "atoi() literal too large"); |
| 442 | return NULL; |
| 443 | } |
| 444 | return newintobject(x); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | static object * |
| 449 | strop_atol(self, args) |
| 450 | object *self; /* Not used */ |
| 451 | object *args; |
| 452 | { |
| 453 | char *s, *end; |
| 454 | int base = 10; |
| 455 | object *x; |
| 456 | |
| 457 | if (args != NULL && is_tupleobject(args)) { |
| 458 | if (!getargs(args, "(si)", &s, &base)) |
| 459 | return NULL; |
| 460 | if (base != 0 && base < 2 || base > 36) { |
| 461 | err_setstr(ValueError, "invalid base for atol()"); |
| 462 | return NULL; |
| 463 | } |
| 464 | } |
| 465 | else if (!getargs(args, "s", &s)) |
| 466 | return NULL; |
| 467 | x = long_escan(s, &end, base); |
| 468 | if (x == NULL) |
| 469 | return NULL; |
| 470 | if (base == 0 && (*end == 'l' || *end == 'L')) |
| 471 | end++; |
| 472 | if (*end != '\0') { |
| 473 | err_setstr(ValueError, "invalid literal for atol()"); |
| 474 | DECREF(x); |
| 475 | return NULL; |
| 476 | } |
| 477 | return x; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | static object * |
| 482 | strop_atof(self, args) |
| 483 | object *self; /* Not used */ |
| 484 | object *args; |
| 485 | { |
| 486 | extern double strtod PROTO((const char *, char **)); |
| 487 | char *s, *end; |
| 488 | double x; |
| 489 | |
| 490 | if (!getargs(args, "s", &s)) |
| 491 | return NULL; |
| 492 | errno = 0; |
| 493 | x = strtod(s, &end); |
| 494 | if (*end != '\0') { |
| 495 | err_setstr(ValueError, "invalid literal for atof()"); |
| 496 | return NULL; |
| 497 | } |
| 498 | else if (errno != 0) { |
| 499 | err_setstr(OverflowError, "atof() literal too large"); |
| 500 | return NULL; |
| 501 | } |
| 502 | return newfloatobject(x); |
| 503 | } |
| 504 | |
| 505 | |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 506 | static object * |
| 507 | strop_translate(self, args) |
| 508 | object *self; |
| 509 | object *args; |
| 510 | { |
| 511 | char *input, *table, *output; |
| 512 | int inlen, tablen; |
| 513 | object *result; |
| 514 | int i; |
| 515 | |
| 516 | if (!newgetargs(args, "s#s#", &input, &inlen, &table, &tablen)) |
| 517 | return NULL; |
| 518 | if (tablen != 256) { |
| 519 | err_setstr(ValueError, |
| 520 | "translation table must be 256 characters long"); |
| 521 | return NULL; |
| 522 | } |
| 523 | result = newsizedstringobject((char *)NULL, inlen); |
| 524 | if (result == NULL) |
| 525 | return NULL; |
| 526 | output = getstringvalue(result); |
| 527 | for (i = 0; i < inlen; i++) { |
| 528 | int c = Py_CHARMASK(*input++); |
| 529 | *output++ = table[c]; |
| 530 | } |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 535 | /* List of functions defined in the module */ |
| 536 | |
| 537 | static struct methodlist strop_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 538 | {"atof", strop_atof}, |
| 539 | {"atoi", strop_atoi}, |
| 540 | {"atol", strop_atol}, |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 541 | {"find", strop_find}, |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 542 | {"join", strop_joinfields, 1}, |
| 543 | {"joinfields", strop_joinfields, 1}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 544 | {"lower", strop_lower}, |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 545 | {"rfind", strop_rfind}, |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 546 | {"split", strop_splitfields, 1}, |
| 547 | {"splitfields", strop_splitfields, 1}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 548 | {"strip", strop_strip}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 549 | {"swapcase", strop_swapcase}, |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 550 | {"translate", strop_translate, 1}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 551 | {"upper", strop_upper}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 552 | {NULL, NULL} /* sentinel */ |
| 553 | }; |
| 554 | |
| 555 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 556 | void |
| 557 | initstrop() |
| 558 | { |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 559 | object *m, *d, *s; |
| 560 | char buf[256]; |
| 561 | int c, n; |
| 562 | m = initmodule("strop", strop_methods); |
| 563 | d = getmoduledict(m); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 564 | |
| 565 | /* Create 'whitespace' object */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 566 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 567 | for (c = 0; c < 256; c++) { |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 568 | if (isspace(c)) |
| 569 | buf[n++] = c; |
| 570 | } |
Sjoerd Mullender | d96ec44 | 1993-09-14 08:37:39 +0000 | [diff] [blame] | 571 | s = newsizedstringobject(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 572 | if (s) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 573 | dictinsert(d, "whitespace", s); |
| 574 | DECREF(s); |
| 575 | } |
| 576 | /* Create 'lowercase' object */ |
| 577 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 578 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 579 | if (islower(c)) |
| 580 | buf[n++] = c; |
| 581 | } |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 582 | s = newsizedstringobject(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 583 | if (s) { |
| 584 | dictinsert(d, "lowercase", s); |
| 585 | DECREF(s); |
| 586 | } |
| 587 | |
| 588 | /* Create 'uppercase' object */ |
| 589 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 590 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 591 | if (isupper(c)) |
| 592 | buf[n++] = c; |
| 593 | } |
| 594 | s = newsizedstringobject(buf, n); |
| 595 | if (s) { |
| 596 | dictinsert(d, "uppercase", s); |
| 597 | DECREF(s); |
| 598 | } |
| 599 | |
| 600 | if (err_occurred()) |
| 601 | fatal("can't initialize module strop"); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 602 | } |