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 * |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 371 | strop_capitalize(self, args) |
| 372 | object *self; /* Not used */ |
| 373 | object *args; |
| 374 | { |
| 375 | char *s, *s_new; |
| 376 | int i, n; |
| 377 | object *new; |
| 378 | int changed; |
| 379 | |
| 380 | if (!getargs(args, "s#", &s, &n)) |
| 381 | return NULL; |
| 382 | new = newsizedstringobject(NULL, n); |
| 383 | if (new == NULL) |
| 384 | return NULL; |
| 385 | s_new = getstringvalue(new); |
| 386 | changed = 0; |
Guido van Rossum | 529c963 | 1996-06-17 16:59:33 +0000 | [diff] [blame] | 387 | if (0 < n) { |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 388 | int c = Py_CHARMASK(*s++); |
| 389 | if (islower(c)) { |
| 390 | changed = 1; |
| 391 | *s_new = toupper(c); |
| 392 | } else |
| 393 | *s_new = c; |
| 394 | s_new++; |
| 395 | } |
| 396 | for (i = 1; i < n; i++) { |
| 397 | int c = Py_CHARMASK(*s++); |
| 398 | if (isupper(c)) { |
| 399 | changed = 1; |
| 400 | *s_new = tolower(c); |
| 401 | } else |
| 402 | *s_new = c; |
| 403 | s_new++; |
| 404 | } |
| 405 | if (!changed) { |
| 406 | DECREF(new); |
| 407 | INCREF(args); |
| 408 | return args; |
| 409 | } |
| 410 | return new; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | static object * |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 415 | strop_swapcase(self, args) |
| 416 | object *self; /* Not used */ |
| 417 | object *args; |
| 418 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 419 | char *s, *s_new; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 420 | int i, n; |
| 421 | object *new; |
| 422 | int changed; |
| 423 | |
| 424 | if (!getargs(args, "s#", &s, &n)) |
| 425 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 426 | new = newsizedstringobject(NULL, n); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 427 | if (new == NULL) |
| 428 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 429 | s_new = getstringvalue(new); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 430 | changed = 0; |
| 431 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 432 | int c = Py_CHARMASK(*s++); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 433 | if (islower(c)) { |
| 434 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 435 | *s_new = toupper(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 436 | } |
| 437 | else if (isupper(c)) { |
| 438 | changed = 1; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 439 | *s_new = tolower(c); |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 440 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 441 | else |
| 442 | *s_new = c; |
| 443 | s_new++; |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 444 | } |
| 445 | if (!changed) { |
| 446 | DECREF(new); |
| 447 | INCREF(args); |
| 448 | return args; |
| 449 | } |
| 450 | return new; |
| 451 | } |
| 452 | |
| 453 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 454 | static object * |
| 455 | strop_atoi(self, args) |
| 456 | object *self; /* Not used */ |
| 457 | object *args; |
| 458 | { |
| 459 | extern long mystrtol PROTO((const char *, char **, int)); |
| 460 | extern unsigned long mystrtoul PROTO((const char *, char **, int)); |
| 461 | char *s, *end; |
| 462 | int base = 10; |
| 463 | long x; |
| 464 | |
| 465 | if (args != NULL && is_tupleobject(args)) { |
| 466 | if (!getargs(args, "(si)", &s, &base)) |
| 467 | return NULL; |
| 468 | if (base != 0 && base < 2 || base > 36) { |
| 469 | err_setstr(ValueError, "invalid base for atoi()"); |
| 470 | return NULL; |
| 471 | } |
| 472 | } |
| 473 | else if (!getargs(args, "s", &s)) |
| 474 | return NULL; |
| 475 | errno = 0; |
| 476 | if (base == 0 && s[0] == '0') |
| 477 | x = (long) mystrtoul(s, &end, base); |
| 478 | else |
| 479 | x = mystrtol(s, &end, base); |
| 480 | if (*end != '\0') { |
| 481 | err_setstr(ValueError, "invalid literal for atoi()"); |
| 482 | return NULL; |
| 483 | } |
| 484 | else if (errno != 0) { |
| 485 | err_setstr(OverflowError, "atoi() literal too large"); |
| 486 | return NULL; |
| 487 | } |
| 488 | return newintobject(x); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | static object * |
| 493 | strop_atol(self, args) |
| 494 | object *self; /* Not used */ |
| 495 | object *args; |
| 496 | { |
| 497 | char *s, *end; |
| 498 | int base = 10; |
| 499 | object *x; |
| 500 | |
| 501 | if (args != NULL && is_tupleobject(args)) { |
| 502 | if (!getargs(args, "(si)", &s, &base)) |
| 503 | return NULL; |
| 504 | if (base != 0 && base < 2 || base > 36) { |
| 505 | err_setstr(ValueError, "invalid base for atol()"); |
| 506 | return NULL; |
| 507 | } |
| 508 | } |
| 509 | else if (!getargs(args, "s", &s)) |
| 510 | return NULL; |
| 511 | x = long_escan(s, &end, base); |
| 512 | if (x == NULL) |
| 513 | return NULL; |
| 514 | if (base == 0 && (*end == 'l' || *end == 'L')) |
| 515 | end++; |
| 516 | if (*end != '\0') { |
| 517 | err_setstr(ValueError, "invalid literal for atol()"); |
| 518 | DECREF(x); |
| 519 | return NULL; |
| 520 | } |
| 521 | return x; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | static object * |
| 526 | strop_atof(self, args) |
| 527 | object *self; /* Not used */ |
| 528 | object *args; |
| 529 | { |
| 530 | extern double strtod PROTO((const char *, char **)); |
| 531 | char *s, *end; |
| 532 | double x; |
| 533 | |
| 534 | if (!getargs(args, "s", &s)) |
| 535 | return NULL; |
| 536 | errno = 0; |
| 537 | x = strtod(s, &end); |
| 538 | if (*end != '\0') { |
| 539 | err_setstr(ValueError, "invalid literal for atof()"); |
| 540 | return NULL; |
| 541 | } |
| 542 | else if (errno != 0) { |
| 543 | err_setstr(OverflowError, "atof() literal too large"); |
| 544 | return NULL; |
| 545 | } |
| 546 | return newfloatobject(x); |
| 547 | } |
| 548 | |
| 549 | |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 550 | static PyObject * |
| 551 | strop_maketrans(self, args) |
| 552 | PyObject *self; /* Not used */ |
| 553 | PyObject *args; |
| 554 | { |
| 555 | unsigned char c[256], *from=NULL, *to=NULL; |
| 556 | int i, fromlen=0, tolen=0; |
| 557 | |
| 558 | if (PyTuple_Size(args)!=0) { |
| 559 | if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, |
| 560 | &to, &tolen)) |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | if (fromlen!=tolen) { |
| 565 | PyErr_SetString(ValueError, |
| 566 | "maketrans arguments must have same length"); |
| 567 | return NULL; |
| 568 | } |
| 569 | for(i=0; i<256; i++) |
| 570 | c[i]=(unsigned char)i; |
| 571 | for(i=0; i<fromlen; i++) { |
| 572 | c[from[i]]=to[i]; |
| 573 | } |
| 574 | return PyString_FromStringAndSize((char *)c, 256); |
| 575 | } |
| 576 | |
| 577 | |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 578 | static object * |
| 579 | strop_translate(self, args) |
| 580 | object *self; |
| 581 | object *args; |
| 582 | { |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 583 | char *input, *table, *output, *output_start, *delete=NULL; |
| 584 | int inlen, tablen, dellen; |
| 585 | PyObject *result; |
| 586 | int i, trans_table[256]; |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 588 | if (!PyArg_ParseTuple(args, "s#s#|s#", &input, &inlen, |
| 589 | &table, &tablen, &delete, &dellen)) |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 590 | return NULL; |
| 591 | if (tablen != 256) { |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 592 | PyErr_SetString(ValueError, |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 593 | "translation table must be 256 characters long"); |
| 594 | return NULL; |
| 595 | } |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 596 | for(i=0; i<256; i++) |
| 597 | trans_table[i]=Py_CHARMASK(table[i]); |
| 598 | if (delete!=NULL) { |
| 599 | for(i=0; i<dellen; i++) |
| 600 | trans_table[delete[i]]=-1; |
| 601 | } |
| 602 | |
| 603 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 604 | if (result == NULL) |
| 605 | return NULL; |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 606 | output_start = output = PyString_AsString(result); |
| 607 | if (delete!=NULL && dellen!=0) { |
| 608 | for (i = 0; i < inlen; i++) { |
| 609 | int c = Py_CHARMASK(*input++); |
| 610 | if (trans_table[c]!=-1) |
| 611 | *output++ = (char)trans_table[c]; |
| 612 | } |
| 613 | /* Fix the size of the resulting string */ |
| 614 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 615 | return NULL; |
| 616 | } else { |
| 617 | /* If no deletions are required, use a faster loop */ |
| 618 | for (i = 0; i < inlen; i++) { |
| 619 | int c = Py_CHARMASK(*input++); |
| 620 | *output++ = (char)trans_table[c]; |
| 621 | } |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 622 | } |
| 623 | return result; |
| 624 | } |
| 625 | |
| 626 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 627 | /* List of functions defined in the module */ |
| 628 | |
| 629 | static struct methodlist strop_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 630 | {"atof", strop_atof}, |
| 631 | {"atoi", strop_atoi}, |
| 632 | {"atol", strop_atol}, |
Guido van Rossum | 2745753 | 1996-06-12 04:24:52 +0000 | [diff] [blame] | 633 | {"capitalize", strop_capitalize}, |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 634 | {"find", strop_find}, |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 635 | {"join", strop_joinfields, 1}, |
| 636 | {"joinfields", strop_joinfields, 1}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 637 | {"lower", strop_lower}, |
Guido van Rossum | 5806a4f | 1994-08-17 13:15:46 +0000 | [diff] [blame] | 638 | {"rfind", strop_rfind}, |
Guido van Rossum | 009e79b | 1995-05-03 17:40:23 +0000 | [diff] [blame] | 639 | {"split", strop_splitfields, 1}, |
| 640 | {"splitfields", strop_splitfields, 1}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 641 | {"strip", strop_strip}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 642 | {"swapcase", strop_swapcase}, |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 643 | {"maketrans", strop_maketrans, 1}, |
Guido van Rossum | a3127e8 | 1995-09-13 17:39:06 +0000 | [diff] [blame] | 644 | {"translate", strop_translate, 1}, |
Guido van Rossum | 5c85062 | 1992-09-11 23:55:51 +0000 | [diff] [blame] | 645 | {"upper", strop_upper}, |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 646 | {NULL, NULL} /* sentinel */ |
| 647 | }; |
| 648 | |
| 649 | |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 650 | void |
| 651 | initstrop() |
| 652 | { |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 653 | object *m, *d, *s; |
| 654 | char buf[256]; |
| 655 | int c, n; |
| 656 | m = initmodule("strop", strop_methods); |
| 657 | d = getmoduledict(m); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 658 | |
| 659 | /* Create 'whitespace' object */ |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 660 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 661 | for (c = 0; c < 256; c++) { |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 662 | if (isspace(c)) |
| 663 | buf[n++] = c; |
| 664 | } |
Sjoerd Mullender | d96ec44 | 1993-09-14 08:37:39 +0000 | [diff] [blame] | 665 | s = newsizedstringobject(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 666 | if (s) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 667 | dictinsert(d, "whitespace", s); |
| 668 | DECREF(s); |
| 669 | } |
| 670 | /* Create 'lowercase' object */ |
| 671 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 672 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 673 | if (islower(c)) |
| 674 | buf[n++] = c; |
| 675 | } |
Guido van Rossum | d05eb8b | 1993-07-08 11:12:36 +0000 | [diff] [blame] | 676 | s = newsizedstringobject(buf, n); |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 677 | if (s) { |
| 678 | dictinsert(d, "lowercase", s); |
| 679 | DECREF(s); |
| 680 | } |
| 681 | |
| 682 | /* Create 'uppercase' object */ |
| 683 | n = 0; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 684 | for (c = 0; c < 256; c++) { |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 685 | if (isupper(c)) |
| 686 | buf[n++] = c; |
| 687 | } |
| 688 | s = newsizedstringobject(buf, n); |
| 689 | if (s) { |
| 690 | dictinsert(d, "uppercase", s); |
| 691 | DECREF(s); |
| 692 | } |
| 693 | |
| 694 | if (err_occurred()) |
| 695 | fatal("can't initialize module strop"); |
Guido van Rossum | e270b43 | 1992-09-03 20:21:07 +0000 | [diff] [blame] | 696 | } |