Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* String object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
| 7 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 8 | #ifdef COUNT_ALLOCS |
| 9 | int null_strings, one_strings; |
| 10 | #endif |
| 11 | |
Fred Drake | d5fadf7 | 2000-09-26 05:46:01 +0000 | [diff] [blame] | 12 | #if !defined(HAVE_LIMITS_H) && !defined(UCHAR_MAX) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 13 | #define UCHAR_MAX 255 |
| 14 | #endif |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 16 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 17 | #ifndef DONT_SHARE_SHORT_STRINGS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 18 | static PyStringObject *nullstring; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 19 | #endif |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 22 | PyString_FromStringAndSize() and PyString_FromString() try in certain cases |
| 23 | to share string objects. When the size of the string is zero, these |
| 24 | routines always return a pointer to the same string object; when the size |
| 25 | is one, they return a pointer to an already existing object if the contents |
| 26 | of the string is known. For PyString_FromString() this is always the case, |
| 27 | for PyString_FromStringAndSize() this is the case when the first argument |
| 28 | in not NULL. |
| 29 | |
| 30 | A common practice of allocating a string and then filling it in or changing |
| 31 | it must be done carefully. It is only allowed to change the contents of |
| 32 | the string if the object was gotten from PyString_FromStringAndSize() with |
| 33 | a NULL first argument, because in the future these routines may try to do |
| 34 | even more sharing of objects. |
| 35 | |
| 36 | The parameter `size' denotes number of characters to allocate, not counting |
| 37 | the null terminating character. If the `str' argument is not NULL, then it |
| 38 | must point to a null-terminated string of length `size'. |
| 39 | |
| 40 | The member `op->ob_size' denotes the number of bytes of data in the string, |
| 41 | not counting the null terminating character, and is therefore equal to the |
| 42 | `size' parameter. |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 43 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 44 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 45 | PyString_FromStringAndSize(const char *str, int size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 47 | register PyStringObject *op; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 48 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 49 | if (size == 0 && (op = nullstring) != NULL) { |
| 50 | #ifdef COUNT_ALLOCS |
| 51 | null_strings++; |
| 52 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 53 | Py_INCREF(op); |
| 54 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 55 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 56 | if (size == 1 && str != NULL && |
| 57 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 58 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 59 | #ifdef COUNT_ALLOCS |
| 60 | one_strings++; |
| 61 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | Py_INCREF(op); |
| 63 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 64 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 65 | #endif /* DONT_SHARE_SHORT_STRINGS */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 66 | |
| 67 | /* PyObject_NewVar is inlined */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 68 | op = (PyStringObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 69 | PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 70 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 71 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 72 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 73 | #ifdef CACHE_HASH |
| 74 | op->ob_shash = -1; |
| 75 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 76 | #ifdef INTERN_STRINGS |
| 77 | op->ob_sinterned = NULL; |
| 78 | #endif |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 79 | if (str != NULL) |
| 80 | memcpy(op->ob_sval, str, size); |
| 81 | op->ob_sval[size] = '\0'; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 82 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 83 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 84 | PyObject *t = (PyObject *)op; |
| 85 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 86 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 87 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 89 | } else if (size == 1 && str != NULL) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 90 | PyObject *t = (PyObject *)op; |
| 91 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 92 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 93 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 94 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 95 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 96 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 97 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 101 | PyString_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 103 | register size_t size = strlen(str); |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 104 | register PyStringObject *op; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 105 | if (size > INT_MAX) { |
| 106 | PyErr_SetString(PyExc_OverflowError, |
| 107 | "string is too long for a Python string"); |
| 108 | return NULL; |
| 109 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 110 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 111 | if (size == 0 && (op = nullstring) != NULL) { |
| 112 | #ifdef COUNT_ALLOCS |
| 113 | null_strings++; |
| 114 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | Py_INCREF(op); |
| 116 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 117 | } |
| 118 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 119 | #ifdef COUNT_ALLOCS |
| 120 | one_strings++; |
| 121 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | Py_INCREF(op); |
| 123 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 124 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 125 | #endif /* DONT_SHARE_SHORT_STRINGS */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 126 | |
| 127 | /* PyObject_NewVar is inlined */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | op = (PyStringObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 129 | PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 130 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 131 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 132 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 133 | #ifdef CACHE_HASH |
| 134 | op->ob_shash = -1; |
| 135 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 136 | #ifdef INTERN_STRINGS |
| 137 | op->ob_sinterned = NULL; |
| 138 | #endif |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 139 | strcpy(op->ob_sval, str); |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 140 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 141 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 142 | PyObject *t = (PyObject *)op; |
| 143 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 144 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 145 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 147 | } else if (size == 1) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 148 | PyObject *t = (PyObject *)op; |
| 149 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 150 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 151 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 153 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 155 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 158 | PyObject * |
| 159 | PyString_FromFormatV(const char *format, va_list vargs) |
| 160 | { |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 161 | va_list count; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 162 | int n = 0; |
| 163 | const char* f; |
| 164 | char *s; |
| 165 | PyObject* string; |
| 166 | |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 167 | #ifdef VA_LIST_IS_ARRAY |
| 168 | memcpy(count, vargs, sizeof(va_list)); |
| 169 | #else |
| 170 | count = vargs; |
| 171 | #endif |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 172 | /* step 1: figure out how large a buffer we need */ |
| 173 | for (f = format; *f; f++) { |
| 174 | if (*f == '%') { |
| 175 | const char* p = f; |
| 176 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 177 | ; |
| 178 | |
| 179 | /* skip the 'l' in %ld, since it doesn't change the |
| 180 | width. although only %d is supported (see |
| 181 | "expand" section below), others can be easily |
| 182 | add */ |
| 183 | if (*f == 'l' && *(f+1) == 'd') |
| 184 | ++f; |
| 185 | |
| 186 | switch (*f) { |
| 187 | case 'c': |
| 188 | (void)va_arg(count, int); |
| 189 | /* fall through... */ |
| 190 | case '%': |
| 191 | n++; |
| 192 | break; |
| 193 | case 'd': case 'i': case 'x': |
| 194 | (void) va_arg(count, int); |
| 195 | /* 20 bytes should be enough to hold a 64-bit |
| 196 | integer */ |
| 197 | n += 20; |
| 198 | break; |
| 199 | case 's': |
| 200 | s = va_arg(count, char*); |
| 201 | n += strlen(s); |
| 202 | break; |
| 203 | case 'p': |
| 204 | (void) va_arg(count, int); |
| 205 | /* maximum 64-bit pointer representation: |
| 206 | * 0xffffffffffffffff |
| 207 | * so 19 characters is enough. |
| 208 | */ |
| 209 | n += 19; |
| 210 | break; |
| 211 | default: |
| 212 | /* if we stumble upon an unknown |
| 213 | formatting code, copy the rest of |
| 214 | the format string to the output |
| 215 | string. (we cannot just skip the |
| 216 | code, since there's no way to know |
| 217 | what's in the argument list) */ |
| 218 | n += strlen(p); |
| 219 | goto expand; |
| 220 | } |
| 221 | } else |
| 222 | n++; |
| 223 | } |
| 224 | expand: |
| 225 | /* step 2: fill the buffer */ |
| 226 | string = PyString_FromStringAndSize(NULL, n); |
| 227 | if (!string) |
| 228 | return NULL; |
| 229 | |
| 230 | s = PyString_AsString(string); |
| 231 | |
| 232 | for (f = format; *f; f++) { |
| 233 | if (*f == '%') { |
| 234 | const char* p = f++; |
| 235 | int i, longflag = 0; |
| 236 | /* parse the width.precision part (we're only |
| 237 | interested in the precision value, if any) */ |
| 238 | n = 0; |
| 239 | while (isdigit(Py_CHARMASK(*f))) |
| 240 | n = (n*10) + *f++ - '0'; |
| 241 | if (*f == '.') { |
| 242 | f++; |
| 243 | n = 0; |
| 244 | while (isdigit(Py_CHARMASK(*f))) |
| 245 | n = (n*10) + *f++ - '0'; |
| 246 | } |
| 247 | while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 248 | f++; |
| 249 | /* handle the long flag, but only for %ld. others |
| 250 | can be added when necessary. */ |
| 251 | if (*f == 'l' && *(f+1) == 'd') { |
| 252 | longflag = 1; |
| 253 | ++f; |
| 254 | } |
| 255 | |
| 256 | switch (*f) { |
| 257 | case 'c': |
| 258 | *s++ = va_arg(vargs, int); |
| 259 | break; |
| 260 | case 'd': |
| 261 | if (longflag) |
| 262 | sprintf(s, "%ld", va_arg(vargs, long)); |
| 263 | else |
| 264 | sprintf(s, "%d", va_arg(vargs, int)); |
| 265 | s += strlen(s); |
| 266 | break; |
| 267 | case 'i': |
| 268 | sprintf(s, "%i", va_arg(vargs, int)); |
| 269 | s += strlen(s); |
| 270 | break; |
| 271 | case 'x': |
| 272 | sprintf(s, "%x", va_arg(vargs, int)); |
| 273 | s += strlen(s); |
| 274 | break; |
| 275 | case 's': |
| 276 | p = va_arg(vargs, char*); |
| 277 | i = strlen(p); |
| 278 | if (n > 0 && i > n) |
| 279 | i = n; |
| 280 | memcpy(s, p, i); |
| 281 | s += i; |
| 282 | break; |
| 283 | case 'p': |
| 284 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 285 | /* %p is ill-defined: ensure leading 0x. */ |
| 286 | if (s[1] == 'X') |
| 287 | s[1] = 'x'; |
| 288 | else if (s[1] != 'x') { |
| 289 | memmove(s+2, s, strlen(s)+1); |
| 290 | s[0] = '0'; |
| 291 | s[1] = 'x'; |
| 292 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 293 | s += strlen(s); |
| 294 | break; |
| 295 | case '%': |
| 296 | *s++ = '%'; |
| 297 | break; |
| 298 | default: |
| 299 | strcpy(s, p); |
| 300 | s += strlen(s); |
| 301 | goto end; |
| 302 | } |
| 303 | } else |
| 304 | *s++ = *f; |
| 305 | } |
| 306 | |
| 307 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 308 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 309 | return string; |
| 310 | } |
| 311 | |
| 312 | PyObject * |
| 313 | PyString_FromFormat(const char *format, ...) |
| 314 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 315 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 316 | va_list vargs; |
| 317 | |
| 318 | #ifdef HAVE_STDARG_PROTOTYPES |
| 319 | va_start(vargs, format); |
| 320 | #else |
| 321 | va_start(vargs); |
| 322 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 323 | ret = PyString_FromFormatV(format, vargs); |
| 324 | va_end(vargs); |
| 325 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 329 | PyObject *PyString_Decode(const char *s, |
| 330 | int size, |
| 331 | const char *encoding, |
| 332 | const char *errors) |
| 333 | { |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 334 | PyObject *v, *str; |
| 335 | |
| 336 | str = PyString_FromStringAndSize(s, size); |
| 337 | if (str == NULL) |
| 338 | return NULL; |
| 339 | v = PyString_AsDecodedString(str, encoding, errors); |
| 340 | Py_DECREF(str); |
| 341 | return v; |
| 342 | } |
| 343 | |
| 344 | PyObject *PyString_AsDecodedObject(PyObject *str, |
| 345 | const char *encoding, |
| 346 | const char *errors) |
| 347 | { |
| 348 | PyObject *v; |
| 349 | |
| 350 | if (!PyString_Check(str)) { |
| 351 | PyErr_BadArgument(); |
| 352 | goto onError; |
| 353 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 354 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 355 | if (encoding == NULL) { |
| 356 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 357 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 358 | #else |
| 359 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 360 | goto onError; |
| 361 | #endif |
| 362 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 363 | |
| 364 | /* Decode via the codec registry */ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 365 | v = PyCodec_Decode(str, encoding, errors); |
| 366 | if (v == NULL) |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 367 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 368 | |
| 369 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 370 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 371 | onError: |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | PyObject *PyString_AsDecodedString(PyObject *str, |
| 376 | const char *encoding, |
| 377 | const char *errors) |
| 378 | { |
| 379 | PyObject *v; |
| 380 | |
| 381 | v = PyString_AsDecodedObject(str, encoding, errors); |
| 382 | if (v == NULL) |
| 383 | goto onError; |
| 384 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 385 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 386 | /* Convert Unicode to a string using the default encoding */ |
| 387 | if (PyUnicode_Check(v)) { |
| 388 | PyObject *temp = v; |
| 389 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 390 | Py_DECREF(temp); |
| 391 | if (v == NULL) |
| 392 | goto onError; |
| 393 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 394 | #endif |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 395 | if (!PyString_Check(v)) { |
| 396 | PyErr_Format(PyExc_TypeError, |
| 397 | "decoder did not return a string object (type=%.400s)", |
| 398 | v->ob_type->tp_name); |
| 399 | Py_DECREF(v); |
| 400 | goto onError; |
| 401 | } |
| 402 | |
| 403 | return v; |
| 404 | |
| 405 | onError: |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | PyObject *PyString_Encode(const char *s, |
| 410 | int size, |
| 411 | const char *encoding, |
| 412 | const char *errors) |
| 413 | { |
| 414 | PyObject *v, *str; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 415 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 416 | str = PyString_FromStringAndSize(s, size); |
| 417 | if (str == NULL) |
| 418 | return NULL; |
| 419 | v = PyString_AsEncodedString(str, encoding, errors); |
| 420 | Py_DECREF(str); |
| 421 | return v; |
| 422 | } |
| 423 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 424 | PyObject *PyString_AsEncodedObject(PyObject *str, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 425 | const char *encoding, |
| 426 | const char *errors) |
| 427 | { |
| 428 | PyObject *v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 429 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 430 | if (!PyString_Check(str)) { |
| 431 | PyErr_BadArgument(); |
| 432 | goto onError; |
| 433 | } |
| 434 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 435 | if (encoding == NULL) { |
| 436 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 437 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 438 | #else |
| 439 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 440 | goto onError; |
| 441 | #endif |
| 442 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 443 | |
| 444 | /* Encode via the codec registry */ |
| 445 | v = PyCodec_Encode(str, encoding, errors); |
| 446 | if (v == NULL) |
| 447 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 448 | |
| 449 | return v; |
| 450 | |
| 451 | onError: |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | PyObject *PyString_AsEncodedString(PyObject *str, |
| 456 | const char *encoding, |
| 457 | const char *errors) |
| 458 | { |
| 459 | PyObject *v; |
| 460 | |
Marc-André Lemburg | 8c2133d | 2001-06-12 13:14:10 +0000 | [diff] [blame] | 461 | v = PyString_AsEncodedObject(str, encoding, errors); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 462 | if (v == NULL) |
| 463 | goto onError; |
| 464 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 465 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 466 | /* Convert Unicode to a string using the default encoding */ |
| 467 | if (PyUnicode_Check(v)) { |
| 468 | PyObject *temp = v; |
| 469 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 470 | Py_DECREF(temp); |
| 471 | if (v == NULL) |
| 472 | goto onError; |
| 473 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 474 | #endif |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 475 | if (!PyString_Check(v)) { |
| 476 | PyErr_Format(PyExc_TypeError, |
| 477 | "encoder did not return a string object (type=%.400s)", |
| 478 | v->ob_type->tp_name); |
| 479 | Py_DECREF(v); |
| 480 | goto onError; |
| 481 | } |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 482 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 483 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 484 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 485 | onError: |
| 486 | return NULL; |
| 487 | } |
| 488 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 489 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 490 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 491 | { |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 492 | op->ob_type->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 495 | static int |
| 496 | string_getsize(register PyObject *op) |
| 497 | { |
| 498 | char *s; |
| 499 | int len; |
| 500 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 501 | return -1; |
| 502 | return len; |
| 503 | } |
| 504 | |
| 505 | static /*const*/ char * |
| 506 | string_getbuffer(register PyObject *op) |
| 507 | { |
| 508 | char *s; |
| 509 | int len; |
| 510 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 511 | return NULL; |
| 512 | return s; |
| 513 | } |
| 514 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 515 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 516 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 517 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 518 | if (!PyString_Check(op)) |
| 519 | return string_getsize(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 520 | return ((PyStringObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 524 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 526 | if (!PyString_Check(op)) |
| 527 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 528 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 531 | int |
| 532 | PyString_AsStringAndSize(register PyObject *obj, |
| 533 | register char **s, |
| 534 | register int *len) |
| 535 | { |
| 536 | if (s == NULL) { |
| 537 | PyErr_BadInternalCall(); |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | if (!PyString_Check(obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 542 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 543 | if (PyUnicode_Check(obj)) { |
| 544 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 545 | if (obj == NULL) |
| 546 | return -1; |
| 547 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 548 | else |
| 549 | #endif |
| 550 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 551 | PyErr_Format(PyExc_TypeError, |
| 552 | "expected string or Unicode object, " |
| 553 | "%.200s found", obj->ob_type->tp_name); |
| 554 | return -1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | *s = PyString_AS_STRING(obj); |
| 559 | if (len != NULL) |
| 560 | *len = PyString_GET_SIZE(obj); |
| 561 | else if ((int)strlen(*s) != PyString_GET_SIZE(obj)) { |
| 562 | PyErr_SetString(PyExc_TypeError, |
| 563 | "expected string without null bytes"); |
| 564 | return -1; |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | /* Methods */ |
| 570 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 571 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 572 | string_print(PyStringObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 573 | { |
| 574 | int i; |
| 575 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 576 | int quote; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 578 | /* XXX Ought to check for interrupts when writing long strings */ |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 579 | if (! PyString_CheckExact(op)) { |
| 580 | int ret; |
| 581 | /* A str subclass may have its own __str__ method. */ |
| 582 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
| 583 | if (op == NULL) |
| 584 | return -1; |
| 585 | ret = string_print(op, fp, flags); |
| 586 | Py_DECREF(op); |
| 587 | return ret; |
| 588 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 589 | if (flags & Py_PRINT_RAW) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 590 | fwrite(op->ob_sval, 1, (int) op->ob_size, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 591 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 592 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 593 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 594 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 595 | quote = '\''; |
| 596 | if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) |
| 597 | quote = '"'; |
| 598 | |
| 599 | fputc(quote, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 600 | for (i = 0; i < op->ob_size; i++) { |
| 601 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 602 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | fprintf(fp, "\\%c", c); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 604 | else if (c == '\t') |
| 605 | fprintf(fp, "\\t"); |
| 606 | else if (c == '\n') |
| 607 | fprintf(fp, "\\n"); |
| 608 | else if (c == '\r') |
| 609 | fprintf(fp, "\\r"); |
| 610 | else if (c < ' ' || c >= 0x7f) |
| 611 | fprintf(fp, "\\x%02x", c & 0xff); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | else |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 613 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 615 | fputc(quote, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 616 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 619 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 620 | string_repr(register PyStringObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 621 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 622 | size_t newsize = 2 + 4 * op->ob_size * sizeof(char); |
| 623 | PyObject *v; |
| 624 | if (newsize > INT_MAX) { |
| 625 | PyErr_SetString(PyExc_OverflowError, |
| 626 | "string is too large to make repr"); |
| 627 | } |
| 628 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 629 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 630 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 631 | } |
| 632 | else { |
| 633 | register int i; |
| 634 | register char c; |
| 635 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 636 | int quote; |
| 637 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 638 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 639 | quote = '\''; |
| 640 | if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) |
| 641 | quote = '"'; |
| 642 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 643 | p = ((PyStringObject *)v)->ob_sval; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 644 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 645 | for (i = 0; i < op->ob_size; i++) { |
| 646 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 647 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 648 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 649 | else if (c == '\t') |
| 650 | *p++ = '\\', *p++ = 't'; |
| 651 | else if (c == '\n') |
| 652 | *p++ = '\\', *p++ = 'n'; |
| 653 | else if (c == '\r') |
| 654 | *p++ = '\\', *p++ = 'r'; |
| 655 | else if (c < ' ' || c >= 0x7f) { |
| 656 | sprintf(p, "\\x%02x", c & 0xff); |
| 657 | p += 4; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | } |
| 659 | else |
| 660 | *p++ = c; |
| 661 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 662 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 663 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 664 | _PyString_Resize( |
| 665 | &v, (int) (p - ((PyStringObject *)v)->ob_sval)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 666 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 667 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 670 | static PyObject * |
| 671 | string_str(PyObject *s) |
| 672 | { |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 673 | assert(PyString_Check(s)); |
| 674 | if (PyString_CheckExact(s)) { |
| 675 | Py_INCREF(s); |
| 676 | return s; |
| 677 | } |
| 678 | else { |
| 679 | /* Subtype -- return genuine string with the same value. */ |
| 680 | PyStringObject *t = (PyStringObject *) s; |
| 681 | return PyString_FromStringAndSize(t->ob_sval, t->ob_size); |
| 682 | } |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 685 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 686 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 687 | { |
| 688 | return a->ob_size; |
| 689 | } |
| 690 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 691 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 692 | string_concat(register PyStringObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 693 | { |
| 694 | register unsigned int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 695 | register PyStringObject *op; |
| 696 | if (!PyString_Check(bb)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 697 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 698 | if (PyUnicode_Check(bb)) |
| 699 | return PyUnicode_Concat((PyObject *)a, bb); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 700 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 701 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 702 | "cannot concatenate 'str' and '%.200s' objects", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 703 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 706 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 707 | /* Optimize cases with empty left or right operand */ |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 708 | if ((a->ob_size == 0 || b->ob_size == 0) && |
| 709 | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
| 710 | if (a->ob_size == 0) { |
| 711 | Py_INCREF(bb); |
| 712 | return bb; |
| 713 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 714 | Py_INCREF(a); |
| 715 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 716 | } |
| 717 | size = a->ob_size + b->ob_size; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 718 | /* PyObject_NewVar is inlined */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 719 | op = (PyStringObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 720 | PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 721 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 722 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 723 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 724 | #ifdef CACHE_HASH |
| 725 | op->ob_shash = -1; |
| 726 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 727 | #ifdef INTERN_STRINGS |
| 728 | op->ob_sinterned = NULL; |
| 729 | #endif |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 730 | memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); |
| 731 | memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); |
| 732 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 733 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 734 | #undef b |
| 735 | } |
| 736 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 737 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 738 | string_repeat(register PyStringObject *a, register int n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 739 | { |
| 740 | register int i; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 741 | register int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 742 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 743 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 744 | if (n < 0) |
| 745 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 746 | /* watch out for overflows: the size can overflow int, |
| 747 | * and the # of bytes needed can overflow size_t |
| 748 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 749 | size = a->ob_size * n; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 750 | if (n && size / n != a->ob_size) { |
| 751 | PyErr_SetString(PyExc_OverflowError, |
| 752 | "repeated string is too long"); |
| 753 | return NULL; |
| 754 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 755 | if (size == a->ob_size && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 756 | Py_INCREF(a); |
| 757 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 758 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 759 | nbytes = size * sizeof(char); |
| 760 | if (nbytes / sizeof(char) != (size_t)size || |
| 761 | nbytes + sizeof(PyStringObject) <= nbytes) { |
| 762 | PyErr_SetString(PyExc_OverflowError, |
| 763 | "repeated string is too long"); |
| 764 | return NULL; |
| 765 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 766 | op = (PyStringObject *) |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 767 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 768 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 769 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 770 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 771 | #ifdef CACHE_HASH |
| 772 | op->ob_shash = -1; |
| 773 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 774 | #ifdef INTERN_STRINGS |
| 775 | op->ob_sinterned = NULL; |
| 776 | #endif |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 777 | for (i = 0; i < size; i += a->ob_size) |
| 778 | memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); |
| 779 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 780 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 784 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 785 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 786 | string_slice(register PyStringObject *a, register int i, register int j) |
| 787 | /* j -- may be negative! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 788 | { |
| 789 | if (i < 0) |
| 790 | i = 0; |
| 791 | if (j < 0) |
| 792 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 793 | if (j > a->ob_size) |
| 794 | j = a->ob_size; |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 795 | if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) { |
| 796 | /* It's the same as a */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 797 | Py_INCREF(a); |
| 798 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 799 | } |
| 800 | if (j < i) |
| 801 | j = i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 802 | return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 805 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 806 | string_contains(PyObject *a, PyObject *el) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 807 | { |
| 808 | register char *s, *end; |
| 809 | register char c; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 810 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 811 | if (PyUnicode_Check(el)) |
Guido van Rossum | 96a45ad | 2000-03-13 15:56:08 +0000 | [diff] [blame] | 812 | return PyUnicode_Contains(a, el); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 813 | #endif |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 814 | if (!PyString_Check(el) || PyString_Size(el) != 1) { |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 815 | PyErr_SetString(PyExc_TypeError, |
Andrew M. Kuchling | cb95a14 | 2000-06-09 14:04:53 +0000 | [diff] [blame] | 816 | "'in <string>' requires character as left operand"); |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 817 | return -1; |
| 818 | } |
| 819 | c = PyString_AsString(el)[0]; |
| 820 | s = PyString_AsString(a); |
| 821 | end = s + PyString_Size(a); |
| 822 | while (s < end) { |
| 823 | if (c == *s++) |
| 824 | return 1; |
| 825 | } |
| 826 | return 0; |
| 827 | } |
| 828 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 829 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 830 | string_item(PyStringObject *a, register int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 831 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 832 | PyObject *v; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 833 | char *pchar; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 834 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 835 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 836 | return NULL; |
| 837 | } |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 838 | pchar = a->ob_sval + i; |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 839 | v = (PyObject *)characters[*pchar & UCHAR_MAX]; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 840 | if (v == NULL) |
| 841 | v = PyString_FromStringAndSize(pchar, 1); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 842 | else { |
| 843 | #ifdef COUNT_ALLOCS |
| 844 | one_strings++; |
| 845 | #endif |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 846 | Py_INCREF(v); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 847 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 848 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 851 | static PyObject* |
| 852 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 853 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 854 | int c; |
| 855 | int len_a, len_b; |
| 856 | int min_len; |
| 857 | PyObject *result; |
| 858 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 859 | /* Make sure both arguments are strings. */ |
| 860 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 861 | result = Py_NotImplemented; |
| 862 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 863 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 864 | if (a == b) { |
| 865 | switch (op) { |
| 866 | case Py_EQ:case Py_LE:case Py_GE: |
| 867 | result = Py_True; |
| 868 | goto out; |
| 869 | case Py_NE:case Py_LT:case Py_GT: |
| 870 | result = Py_False; |
| 871 | goto out; |
| 872 | } |
| 873 | } |
| 874 | if (op == Py_EQ) { |
| 875 | /* Supporting Py_NE here as well does not save |
| 876 | much time, since Py_NE is rarely used. */ |
| 877 | if (a->ob_size == b->ob_size |
| 878 | && (a->ob_sval[0] == b->ob_sval[0] |
| 879 | && memcmp(a->ob_sval, b->ob_sval, |
| 880 | a->ob_size) == 0)) { |
| 881 | result = Py_True; |
| 882 | } else { |
| 883 | result = Py_False; |
| 884 | } |
| 885 | goto out; |
| 886 | } |
| 887 | len_a = a->ob_size; len_b = b->ob_size; |
| 888 | min_len = (len_a < len_b) ? len_a : len_b; |
| 889 | if (min_len > 0) { |
| 890 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 891 | if (c==0) |
| 892 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
| 893 | }else |
| 894 | c = 0; |
| 895 | if (c == 0) |
| 896 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 897 | switch (op) { |
| 898 | case Py_LT: c = c < 0; break; |
| 899 | case Py_LE: c = c <= 0; break; |
| 900 | case Py_EQ: assert(0); break; /* unreachable */ |
| 901 | case Py_NE: c = c != 0; break; |
| 902 | case Py_GT: c = c > 0; break; |
| 903 | case Py_GE: c = c >= 0; break; |
| 904 | default: |
| 905 | result = Py_NotImplemented; |
| 906 | goto out; |
| 907 | } |
| 908 | result = c ? Py_True : Py_False; |
| 909 | out: |
| 910 | Py_INCREF(result); |
| 911 | return result; |
| 912 | } |
| 913 | |
| 914 | int |
| 915 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 916 | { |
| 917 | PyStringObject *a, *b; |
| 918 | a = (PyStringObject*)o1; |
| 919 | b = (PyStringObject*)o2; |
| 920 | return a->ob_size == b->ob_size |
| 921 | && *a->ob_sval == *b->ob_sval |
| 922 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 925 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 926 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 927 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 928 | register int len; |
| 929 | register unsigned char *p; |
| 930 | register long x; |
| 931 | |
| 932 | #ifdef CACHE_HASH |
| 933 | if (a->ob_shash != -1) |
| 934 | return a->ob_shash; |
Guido van Rossum | 36b9f79 | 1997-02-14 16:29:22 +0000 | [diff] [blame] | 935 | #ifdef INTERN_STRINGS |
| 936 | if (a->ob_sinterned != NULL) |
| 937 | return (a->ob_shash = |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 938 | ((PyStringObject *)(a->ob_sinterned))->ob_shash); |
Guido van Rossum | 36b9f79 | 1997-02-14 16:29:22 +0000 | [diff] [blame] | 939 | #endif |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 940 | #endif |
| 941 | len = a->ob_size; |
| 942 | p = (unsigned char *) a->ob_sval; |
| 943 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 944 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 945 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 946 | x ^= a->ob_size; |
| 947 | if (x == -1) |
| 948 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 949 | #ifdef CACHE_HASH |
| 950 | a->ob_shash = x; |
| 951 | #endif |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 952 | return x; |
| 953 | } |
| 954 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 955 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 956 | string_buffer_getreadbuf(PyStringObject *self, int index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 957 | { |
| 958 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 959 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 960 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 961 | return -1; |
| 962 | } |
| 963 | *ptr = (void *)self->ob_sval; |
| 964 | return self->ob_size; |
| 965 | } |
| 966 | |
| 967 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 968 | string_buffer_getwritebuf(PyStringObject *self, int index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 969 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 970 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 971 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 976 | string_buffer_getsegcount(PyStringObject *self, int *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 977 | { |
| 978 | if ( lenp ) |
| 979 | *lenp = self->ob_size; |
| 980 | return 1; |
| 981 | } |
| 982 | |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 983 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 984 | string_buffer_getcharbuf(PyStringObject *self, int index, const char **ptr) |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 985 | { |
| 986 | if ( index != 0 ) { |
| 987 | PyErr_SetString(PyExc_SystemError, |
| 988 | "accessing non-existent string segment"); |
| 989 | return -1; |
| 990 | } |
| 991 | *ptr = self->ob_sval; |
| 992 | return self->ob_size; |
| 993 | } |
| 994 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 995 | static PySequenceMethods string_as_sequence = { |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 996 | (inquiry)string_length, /*sq_length*/ |
| 997 | (binaryfunc)string_concat, /*sq_concat*/ |
| 998 | (intargfunc)string_repeat, /*sq_repeat*/ |
| 999 | (intargfunc)string_item, /*sq_item*/ |
| 1000 | (intintargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1001 | 0, /*sq_ass_item*/ |
| 1002 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1003 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1004 | }; |
| 1005 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1006 | static PyBufferProcs string_as_buffer = { |
| 1007 | (getreadbufferproc)string_buffer_getreadbuf, |
| 1008 | (getwritebufferproc)string_buffer_getwritebuf, |
| 1009 | (getsegcountproc)string_buffer_getsegcount, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1010 | (getcharbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1011 | }; |
| 1012 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1013 | |
| 1014 | |
| 1015 | #define LEFTSTRIP 0 |
| 1016 | #define RIGHTSTRIP 1 |
| 1017 | #define BOTHSTRIP 2 |
| 1018 | |
| 1019 | |
| 1020 | static PyObject * |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1021 | split_whitespace(const char *s, int len, int maxsplit) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1022 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1023 | int i, j, err; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1024 | PyObject* item; |
| 1025 | PyObject *list = PyList_New(0); |
| 1026 | |
| 1027 | if (list == NULL) |
| 1028 | return NULL; |
| 1029 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1030 | for (i = j = 0; i < len; ) { |
| 1031 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 1032 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1033 | j = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1034 | while (i < len && !isspace(Py_CHARMASK(s[i]))) |
| 1035 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1036 | if (j < i) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1037 | if (maxsplit-- <= 0) |
| 1038 | break; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1039 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 1040 | if (item == NULL) |
| 1041 | goto finally; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1042 | err = PyList_Append(list, item); |
| 1043 | Py_DECREF(item); |
| 1044 | if (err < 0) |
| 1045 | goto finally; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1046 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 1047 | i++; |
| 1048 | j = i; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1051 | if (j < len) { |
| 1052 | item = PyString_FromStringAndSize(s+j, (int)(len - j)); |
| 1053 | if (item == NULL) |
| 1054 | goto finally; |
| 1055 | err = PyList_Append(list, item); |
| 1056 | Py_DECREF(item); |
| 1057 | if (err < 0) |
| 1058 | goto finally; |
| 1059 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1060 | return list; |
| 1061 | finally: |
| 1062 | Py_DECREF(list); |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | static char split__doc__[] = |
| 1068 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 1069 | \n\ |
| 1070 | Return a list of the words in the string S, using sep as the\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1071 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
| 1072 | splits are done. If sep is not specified, any whitespace string\n\ |
| 1073 | is a separator."; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1074 | |
| 1075 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1076 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1077 | { |
| 1078 | int len = PyString_GET_SIZE(self), n, i, j, err; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1079 | int maxsplit = -1; |
| 1080 | const char *s = PyString_AS_STRING(self), *sub; |
| 1081 | PyObject *list, *item, *subobj = Py_None; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1082 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1083 | if (!PyArg_ParseTuple(args, "|Oi:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1084 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1085 | if (maxsplit < 0) |
| 1086 | maxsplit = INT_MAX; |
| 1087 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1088 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1089 | if (PyString_Check(subobj)) { |
| 1090 | sub = PyString_AS_STRING(subobj); |
| 1091 | n = PyString_GET_SIZE(subobj); |
| 1092 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1093 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1094 | else if (PyUnicode_Check(subobj)) |
| 1095 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1096 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1097 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1098 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1099 | if (n == 0) { |
| 1100 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
| 1104 | list = PyList_New(0); |
| 1105 | if (list == NULL) |
| 1106 | return NULL; |
| 1107 | |
| 1108 | i = j = 0; |
| 1109 | while (i+n <= len) { |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1110 | if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1111 | if (maxsplit-- <= 0) |
| 1112 | break; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1113 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 1114 | if (item == NULL) |
| 1115 | goto fail; |
| 1116 | err = PyList_Append(list, item); |
| 1117 | Py_DECREF(item); |
| 1118 | if (err < 0) |
| 1119 | goto fail; |
| 1120 | i = j = i + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1121 | } |
| 1122 | else |
| 1123 | i++; |
| 1124 | } |
| 1125 | item = PyString_FromStringAndSize(s+j, (int)(len-j)); |
| 1126 | if (item == NULL) |
| 1127 | goto fail; |
| 1128 | err = PyList_Append(list, item); |
| 1129 | Py_DECREF(item); |
| 1130 | if (err < 0) |
| 1131 | goto fail; |
| 1132 | |
| 1133 | return list; |
| 1134 | |
| 1135 | fail: |
| 1136 | Py_DECREF(list); |
| 1137 | return NULL; |
| 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | static char join__doc__[] = |
| 1142 | "S.join(sequence) -> string\n\ |
| 1143 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1144 | Return a string which is the concatenation of the strings in the\n\ |
| 1145 | sequence. The separator between elements is S."; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1146 | |
| 1147 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1148 | string_join(PyStringObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1149 | { |
| 1150 | char *sep = PyString_AS_STRING(self); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1151 | const int seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1152 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1153 | char *p; |
| 1154 | int seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1155 | size_t sz = 0; |
| 1156 | int i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1157 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1158 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1159 | seq = PySequence_Fast(orig, ""); |
| 1160 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1161 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1162 | PyErr_Format(PyExc_TypeError, |
| 1163 | "sequence expected, %.80s found", |
| 1164 | orig->ob_type->tp_name); |
| 1165 | return NULL; |
| 1166 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1167 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1168 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1169 | if (seqlen == 0) { |
| 1170 | Py_DECREF(seq); |
| 1171 | return PyString_FromString(""); |
| 1172 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1173 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1174 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1175 | if (!PyString_Check(item) && !PyUnicode_Check(item)) { |
| 1176 | PyErr_Format(PyExc_TypeError, |
| 1177 | "sequence item 0: expected string," |
| 1178 | " %.80s found", |
| 1179 | item->ob_type->tp_name); |
| 1180 | Py_DECREF(seq); |
| 1181 | return NULL; |
| 1182 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1183 | Py_INCREF(item); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1184 | Py_DECREF(seq); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1185 | return item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1186 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1187 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1188 | /* There are at least two things to join. Do a pre-pass to figure out |
| 1189 | * the total amount of space we'll need (sz), see whether any argument |
| 1190 | * is absurd, and defer to the Unicode join if appropriate. |
| 1191 | */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1192 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1193 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1194 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1195 | if (!PyString_Check(item)){ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1196 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1197 | if (PyUnicode_Check(item)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1198 | /* Defer to Unicode join. |
| 1199 | * CAUTION: There's no gurantee that the |
| 1200 | * original sequence can be iterated over |
| 1201 | * again, so we must pass seq here. |
| 1202 | */ |
| 1203 | PyObject *result; |
| 1204 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1205 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1206 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1207 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1208 | #endif |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1209 | PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1210 | "sequence item %i: expected string," |
| 1211 | " %.80s found", |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1212 | i, item->ob_type->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1213 | Py_DECREF(seq); |
| 1214 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1215 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1216 | sz += PyString_GET_SIZE(item); |
| 1217 | if (i != 0) |
| 1218 | sz += seplen; |
| 1219 | if (sz < old_sz || sz > INT_MAX) { |
| 1220 | PyErr_SetString(PyExc_OverflowError, |
| 1221 | "join() is too long for a Python string"); |
| 1222 | Py_DECREF(seq); |
| 1223 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1224 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | /* Allocate result space. */ |
| 1228 | res = PyString_FromStringAndSize((char*)NULL, (int)sz); |
| 1229 | if (res == NULL) { |
| 1230 | Py_DECREF(seq); |
| 1231 | return NULL; |
| 1232 | } |
| 1233 | |
| 1234 | /* Catenate everything. */ |
| 1235 | p = PyString_AS_STRING(res); |
| 1236 | for (i = 0; i < seqlen; ++i) { |
| 1237 | size_t n; |
| 1238 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1239 | n = PyString_GET_SIZE(item); |
| 1240 | memcpy(p, PyString_AS_STRING(item), n); |
| 1241 | p += n; |
| 1242 | if (i < seqlen - 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1243 | memcpy(p, sep, seplen); |
| 1244 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1245 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1246 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1247 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1248 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1249 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1252 | PyObject * |
| 1253 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1254 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1255 | assert(sep != NULL && PyString_Check(sep)); |
| 1256 | assert(x != NULL); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1257 | return string_join((PyStringObject *)sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1260 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1261 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1262 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1263 | const char *s = PyString_AS_STRING(self), *sub; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1264 | int len = PyString_GET_SIZE(self); |
| 1265 | int n, i = 0, last = INT_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1266 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1267 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1268 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1269 | &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1270 | return -2; |
| 1271 | if (PyString_Check(subobj)) { |
| 1272 | sub = PyString_AS_STRING(subobj); |
| 1273 | n = PyString_GET_SIZE(subobj); |
| 1274 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1275 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1276 | else if (PyUnicode_Check(subobj)) |
| 1277 | return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1278 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1279 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1280 | return -2; |
| 1281 | |
| 1282 | if (last > len) |
| 1283 | last = len; |
| 1284 | if (last < 0) |
| 1285 | last += len; |
| 1286 | if (last < 0) |
| 1287 | last = 0; |
| 1288 | if (i < 0) |
| 1289 | i += len; |
| 1290 | if (i < 0) |
| 1291 | i = 0; |
| 1292 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1293 | if (dir > 0) { |
| 1294 | if (n == 0 && i <= last) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1295 | return (long)i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1296 | last -= n; |
| 1297 | for (; i <= last; ++i) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1298 | if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1299 | return (long)i; |
| 1300 | } |
| 1301 | else { |
| 1302 | int j; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1303 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1304 | if (n == 0 && i <= last) |
| 1305 | return (long)last; |
| 1306 | for (j = last-n; j >= i; --j) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1307 | if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1308 | return (long)j; |
| 1309 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1310 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | static char find__doc__[] = |
| 1316 | "S.find(sub [,start [,end]]) -> int\n\ |
| 1317 | \n\ |
| 1318 | Return the lowest index in S where substring sub is found,\n\ |
| 1319 | such that sub is contained within s[start,end]. Optional\n\ |
| 1320 | arguments start and end are interpreted as in slice notation.\n\ |
| 1321 | \n\ |
| 1322 | Return -1 on failure."; |
| 1323 | |
| 1324 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1325 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1326 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1327 | long result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1328 | if (result == -2) |
| 1329 | return NULL; |
| 1330 | return PyInt_FromLong(result); |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | static char index__doc__[] = |
| 1335 | "S.index(sub [,start [,end]]) -> int\n\ |
| 1336 | \n\ |
| 1337 | Like S.find() but raise ValueError when the substring is not found."; |
| 1338 | |
| 1339 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1340 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1341 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1342 | long result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1343 | if (result == -2) |
| 1344 | return NULL; |
| 1345 | if (result == -1) { |
| 1346 | PyErr_SetString(PyExc_ValueError, |
| 1347 | "substring not found in string.index"); |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | return PyInt_FromLong(result); |
| 1351 | } |
| 1352 | |
| 1353 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1354 | static char rfind__doc__[] = |
| 1355 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 1356 | \n\ |
| 1357 | Return the highest index in S where substring sub is found,\n\ |
| 1358 | such that sub is contained within s[start,end]. Optional\n\ |
| 1359 | arguments start and end are interpreted as in slice notation.\n\ |
| 1360 | \n\ |
| 1361 | Return -1 on failure."; |
| 1362 | |
| 1363 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1364 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1365 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1366 | long result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1367 | if (result == -2) |
| 1368 | return NULL; |
| 1369 | return PyInt_FromLong(result); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | static char rindex__doc__[] = |
| 1374 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 1375 | \n\ |
| 1376 | Like S.rfind() but raise ValueError when the substring is not found."; |
| 1377 | |
| 1378 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1379 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1380 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1381 | long result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1382 | if (result == -2) |
| 1383 | return NULL; |
| 1384 | if (result == -1) { |
| 1385 | PyErr_SetString(PyExc_ValueError, |
| 1386 | "substring not found in string.rindex"); |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | return PyInt_FromLong(result); |
| 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1394 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1395 | { |
| 1396 | char *s = PyString_AS_STRING(self); |
| 1397 | int len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1398 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1399 | i = 0; |
| 1400 | if (striptype != RIGHTSTRIP) { |
| 1401 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 1402 | i++; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | j = len; |
| 1407 | if (striptype != LEFTSTRIP) { |
| 1408 | do { |
| 1409 | j--; |
| 1410 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 1411 | j++; |
| 1412 | } |
| 1413 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1414 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1415 | Py_INCREF(self); |
| 1416 | return (PyObject*)self; |
| 1417 | } |
| 1418 | else |
| 1419 | return PyString_FromStringAndSize(s+i, j-i); |
| 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | static char strip__doc__[] = |
| 1424 | "S.strip() -> string\n\ |
| 1425 | \n\ |
| 1426 | Return a copy of the string S with leading and trailing\n\ |
| 1427 | whitespace removed."; |
| 1428 | |
| 1429 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1430 | string_strip(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1431 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1432 | return do_strip(self, BOTHSTRIP); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | static char lstrip__doc__[] = |
| 1437 | "S.lstrip() -> string\n\ |
| 1438 | \n\ |
| 1439 | Return a copy of the string S with leading whitespace removed."; |
| 1440 | |
| 1441 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1442 | string_lstrip(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1443 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1444 | return do_strip(self, LEFTSTRIP); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | static char rstrip__doc__[] = |
| 1449 | "S.rstrip() -> string\n\ |
| 1450 | \n\ |
| 1451 | Return a copy of the string S with trailing whitespace removed."; |
| 1452 | |
| 1453 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1454 | string_rstrip(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1455 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1456 | return do_strip(self, RIGHTSTRIP); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | static char lower__doc__[] = |
| 1461 | "S.lower() -> string\n\ |
| 1462 | \n\ |
| 1463 | Return a copy of the string S converted to lowercase."; |
| 1464 | |
| 1465 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1466 | string_lower(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1467 | { |
| 1468 | char *s = PyString_AS_STRING(self), *s_new; |
| 1469 | int i, n = PyString_GET_SIZE(self); |
| 1470 | PyObject *new; |
| 1471 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1472 | new = PyString_FromStringAndSize(NULL, n); |
| 1473 | if (new == NULL) |
| 1474 | return NULL; |
| 1475 | s_new = PyString_AsString(new); |
| 1476 | for (i = 0; i < n; i++) { |
| 1477 | int c = Py_CHARMASK(*s++); |
| 1478 | if (isupper(c)) { |
| 1479 | *s_new = tolower(c); |
| 1480 | } else |
| 1481 | *s_new = c; |
| 1482 | s_new++; |
| 1483 | } |
| 1484 | return new; |
| 1485 | } |
| 1486 | |
| 1487 | |
| 1488 | static char upper__doc__[] = |
| 1489 | "S.upper() -> string\n\ |
| 1490 | \n\ |
| 1491 | Return a copy of the string S converted to uppercase."; |
| 1492 | |
| 1493 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1494 | string_upper(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1495 | { |
| 1496 | char *s = PyString_AS_STRING(self), *s_new; |
| 1497 | int i, n = PyString_GET_SIZE(self); |
| 1498 | PyObject *new; |
| 1499 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1500 | new = PyString_FromStringAndSize(NULL, n); |
| 1501 | if (new == NULL) |
| 1502 | return NULL; |
| 1503 | s_new = PyString_AsString(new); |
| 1504 | for (i = 0; i < n; i++) { |
| 1505 | int c = Py_CHARMASK(*s++); |
| 1506 | if (islower(c)) { |
| 1507 | *s_new = toupper(c); |
| 1508 | } else |
| 1509 | *s_new = c; |
| 1510 | s_new++; |
| 1511 | } |
| 1512 | return new; |
| 1513 | } |
| 1514 | |
| 1515 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1516 | static char title__doc__[] = |
| 1517 | "S.title() -> string\n\ |
| 1518 | \n\ |
| 1519 | Return a titlecased version of S, i.e. words start with uppercase\n\ |
| 1520 | characters, all remaining cased characters have lowercase."; |
| 1521 | |
| 1522 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1523 | string_title(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1524 | { |
| 1525 | char *s = PyString_AS_STRING(self), *s_new; |
| 1526 | int i, n = PyString_GET_SIZE(self); |
| 1527 | int previous_is_cased = 0; |
| 1528 | PyObject *new; |
| 1529 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1530 | new = PyString_FromStringAndSize(NULL, n); |
| 1531 | if (new == NULL) |
| 1532 | return NULL; |
| 1533 | s_new = PyString_AsString(new); |
| 1534 | for (i = 0; i < n; i++) { |
| 1535 | int c = Py_CHARMASK(*s++); |
| 1536 | if (islower(c)) { |
| 1537 | if (!previous_is_cased) |
| 1538 | c = toupper(c); |
| 1539 | previous_is_cased = 1; |
| 1540 | } else if (isupper(c)) { |
| 1541 | if (previous_is_cased) |
| 1542 | c = tolower(c); |
| 1543 | previous_is_cased = 1; |
| 1544 | } else |
| 1545 | previous_is_cased = 0; |
| 1546 | *s_new++ = c; |
| 1547 | } |
| 1548 | return new; |
| 1549 | } |
| 1550 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1551 | static char capitalize__doc__[] = |
| 1552 | "S.capitalize() -> string\n\ |
| 1553 | \n\ |
| 1554 | Return a copy of the string S with only its first character\n\ |
| 1555 | capitalized."; |
| 1556 | |
| 1557 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1558 | string_capitalize(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1559 | { |
| 1560 | char *s = PyString_AS_STRING(self), *s_new; |
| 1561 | int i, n = PyString_GET_SIZE(self); |
| 1562 | PyObject *new; |
| 1563 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1564 | new = PyString_FromStringAndSize(NULL, n); |
| 1565 | if (new == NULL) |
| 1566 | return NULL; |
| 1567 | s_new = PyString_AsString(new); |
| 1568 | if (0 < n) { |
| 1569 | int c = Py_CHARMASK(*s++); |
| 1570 | if (islower(c)) |
| 1571 | *s_new = toupper(c); |
| 1572 | else |
| 1573 | *s_new = c; |
| 1574 | s_new++; |
| 1575 | } |
| 1576 | for (i = 1; i < n; i++) { |
| 1577 | int c = Py_CHARMASK(*s++); |
| 1578 | if (isupper(c)) |
| 1579 | *s_new = tolower(c); |
| 1580 | else |
| 1581 | *s_new = c; |
| 1582 | s_new++; |
| 1583 | } |
| 1584 | return new; |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | static char count__doc__[] = |
| 1589 | "S.count(sub[, start[, end]]) -> int\n\ |
| 1590 | \n\ |
| 1591 | Return the number of occurrences of substring sub in string\n\ |
| 1592 | S[start:end]. Optional arguments start and end are\n\ |
| 1593 | interpreted as in slice notation."; |
| 1594 | |
| 1595 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1596 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1597 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1598 | const char *s = PyString_AS_STRING(self), *sub; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1599 | int len = PyString_GET_SIZE(self), n; |
| 1600 | int i = 0, last = INT_MAX; |
| 1601 | int m, r; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1602 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1603 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1604 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, |
| 1605 | _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1606 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1607 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1608 | if (PyString_Check(subobj)) { |
| 1609 | sub = PyString_AS_STRING(subobj); |
| 1610 | n = PyString_GET_SIZE(subobj); |
| 1611 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1612 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1613 | else if (PyUnicode_Check(subobj)) { |
| 1614 | int count; |
| 1615 | count = PyUnicode_Count((PyObject *)self, subobj, i, last); |
| 1616 | if (count == -1) |
| 1617 | return NULL; |
| 1618 | else |
| 1619 | return PyInt_FromLong((long) count); |
| 1620 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1621 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1622 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1623 | return NULL; |
| 1624 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1625 | if (last > len) |
| 1626 | last = len; |
| 1627 | if (last < 0) |
| 1628 | last += len; |
| 1629 | if (last < 0) |
| 1630 | last = 0; |
| 1631 | if (i < 0) |
| 1632 | i += len; |
| 1633 | if (i < 0) |
| 1634 | i = 0; |
| 1635 | m = last + 1 - n; |
| 1636 | if (n == 0) |
| 1637 | return PyInt_FromLong((long) (m-i)); |
| 1638 | |
| 1639 | r = 0; |
| 1640 | while (i < m) { |
| 1641 | if (!memcmp(s+i, sub, n)) { |
| 1642 | r++; |
| 1643 | i += n; |
| 1644 | } else { |
| 1645 | i++; |
| 1646 | } |
| 1647 | } |
| 1648 | return PyInt_FromLong((long) r); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | static char swapcase__doc__[] = |
| 1653 | "S.swapcase() -> string\n\ |
| 1654 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1655 | Return a copy of the string S with uppercase characters\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1656 | converted to lowercase and vice versa."; |
| 1657 | |
| 1658 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1659 | string_swapcase(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1660 | { |
| 1661 | char *s = PyString_AS_STRING(self), *s_new; |
| 1662 | int i, n = PyString_GET_SIZE(self); |
| 1663 | PyObject *new; |
| 1664 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1665 | new = PyString_FromStringAndSize(NULL, n); |
| 1666 | if (new == NULL) |
| 1667 | return NULL; |
| 1668 | s_new = PyString_AsString(new); |
| 1669 | for (i = 0; i < n; i++) { |
| 1670 | int c = Py_CHARMASK(*s++); |
| 1671 | if (islower(c)) { |
| 1672 | *s_new = toupper(c); |
| 1673 | } |
| 1674 | else if (isupper(c)) { |
| 1675 | *s_new = tolower(c); |
| 1676 | } |
| 1677 | else |
| 1678 | *s_new = c; |
| 1679 | s_new++; |
| 1680 | } |
| 1681 | return new; |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | static char translate__doc__[] = |
| 1686 | "S.translate(table [,deletechars]) -> string\n\ |
| 1687 | \n\ |
| 1688 | Return a copy of the string S, where all characters occurring\n\ |
| 1689 | in the optional argument deletechars are removed, and the\n\ |
| 1690 | remaining characters have been mapped through the given\n\ |
| 1691 | translation table, which must be a string of length 256."; |
| 1692 | |
| 1693 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1694 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1695 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1696 | register char *input, *output; |
| 1697 | register const char *table; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1698 | register int i, c, changed = 0; |
| 1699 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1700 | const char *table1, *output_start, *del_table=NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1701 | int inlen, tablen, dellen = 0; |
| 1702 | PyObject *result; |
| 1703 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1704 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1705 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1706 | if (!PyArg_ParseTuple(args, "O|O:translate", |
| 1707 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1708 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1709 | |
| 1710 | if (PyString_Check(tableobj)) { |
| 1711 | table1 = PyString_AS_STRING(tableobj); |
| 1712 | tablen = PyString_GET_SIZE(tableobj); |
| 1713 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1714 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1715 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1716 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1717 | parameter; instead a mapping to None will cause characters |
| 1718 | to be deleted. */ |
| 1719 | if (delobj != NULL) { |
| 1720 | PyErr_SetString(PyExc_TypeError, |
| 1721 | "deletions are implemented differently for unicode"); |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 1725 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1726 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1727 | else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1728 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1729 | |
| 1730 | if (delobj != NULL) { |
| 1731 | if (PyString_Check(delobj)) { |
| 1732 | del_table = PyString_AS_STRING(delobj); |
| 1733 | dellen = PyString_GET_SIZE(delobj); |
| 1734 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1735 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1736 | else if (PyUnicode_Check(delobj)) { |
| 1737 | PyErr_SetString(PyExc_TypeError, |
| 1738 | "deletions are implemented differently for unicode"); |
| 1739 | return NULL; |
| 1740 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1741 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1742 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 1743 | return NULL; |
| 1744 | |
| 1745 | if (tablen != 256) { |
| 1746 | PyErr_SetString(PyExc_ValueError, |
| 1747 | "translation table must be 256 characters long"); |
| 1748 | return NULL; |
| 1749 | } |
| 1750 | } |
| 1751 | else { |
| 1752 | del_table = NULL; |
| 1753 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | table = table1; |
| 1757 | inlen = PyString_Size(input_obj); |
| 1758 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 1759 | if (result == NULL) |
| 1760 | return NULL; |
| 1761 | output_start = output = PyString_AsString(result); |
| 1762 | input = PyString_AsString(input_obj); |
| 1763 | |
| 1764 | if (dellen == 0) { |
| 1765 | /* If no deletions are required, use faster code */ |
| 1766 | for (i = inlen; --i >= 0; ) { |
| 1767 | c = Py_CHARMASK(*input++); |
| 1768 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1769 | changed = 1; |
| 1770 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1771 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1772 | return result; |
| 1773 | Py_DECREF(result); |
| 1774 | Py_INCREF(input_obj); |
| 1775 | return input_obj; |
| 1776 | } |
| 1777 | |
| 1778 | for (i = 0; i < 256; i++) |
| 1779 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1780 | |
| 1781 | for (i = 0; i < dellen; i++) |
| 1782 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 1783 | |
| 1784 | for (i = inlen; --i >= 0; ) { |
| 1785 | c = Py_CHARMASK(*input++); |
| 1786 | if (trans_table[c] != -1) |
| 1787 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1788 | continue; |
| 1789 | changed = 1; |
| 1790 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1791 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1792 | Py_DECREF(result); |
| 1793 | Py_INCREF(input_obj); |
| 1794 | return input_obj; |
| 1795 | } |
| 1796 | /* Fix the size of the resulting string */ |
| 1797 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 1798 | return NULL; |
| 1799 | return result; |
| 1800 | } |
| 1801 | |
| 1802 | |
| 1803 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 1804 | |
| 1805 | /* |
| 1806 | mymemfind |
| 1807 | |
| 1808 | strstr replacement for arbitrary blocks of memory. |
| 1809 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1810 | Locates the first occurrence in the memory pointed to by MEM of the |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1811 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 1812 | found, or -1 if not found. If len of PAT is greater than length of |
| 1813 | MEM, the function returns -1. |
| 1814 | */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1815 | static int |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1816 | mymemfind(const char *mem, int len, const char *pat, int pat_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1817 | { |
| 1818 | register int ii; |
| 1819 | |
| 1820 | /* pattern can not occur in the last pat_len-1 chars */ |
| 1821 | len -= pat_len; |
| 1822 | |
| 1823 | for (ii = 0; ii <= len; ii++) { |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1824 | if (mem[ii] == pat[0] && memcmp(&mem[ii], pat, pat_len) == 0) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1825 | return ii; |
| 1826 | } |
| 1827 | } |
| 1828 | return -1; |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | mymemcnt |
| 1833 | |
| 1834 | Return the number of distinct times PAT is found in MEM. |
| 1835 | meaning mem=1111 and pat==11 returns 2. |
| 1836 | mem=11111 and pat==11 also return 2. |
| 1837 | */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1838 | static int |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1839 | mymemcnt(const char *mem, int len, const char *pat, int pat_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1840 | { |
| 1841 | register int offset = 0; |
| 1842 | int nfound = 0; |
| 1843 | |
| 1844 | while (len >= 0) { |
| 1845 | offset = mymemfind(mem, len, pat, pat_len); |
| 1846 | if (offset == -1) |
| 1847 | break; |
| 1848 | mem += offset + pat_len; |
| 1849 | len -= offset + pat_len; |
| 1850 | nfound++; |
| 1851 | } |
| 1852 | return nfound; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | mymemreplace |
| 1857 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1858 | Return a string in which all occurrences of PAT in memory STR are |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1859 | replaced with SUB. |
| 1860 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1861 | If length of PAT is less than length of STR or there are no occurrences |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1862 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 1863 | string is allocated here and returned. |
| 1864 | |
| 1865 | on return, out_len is: |
| 1866 | the length of output string, or |
| 1867 | -1 if the input string is returned, or |
| 1868 | unchanged if an error occurs (no memory). |
| 1869 | |
| 1870 | return value is: |
| 1871 | the new string allocated locally, or |
| 1872 | NULL if an error occurred. |
| 1873 | */ |
| 1874 | static char * |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1875 | mymemreplace(const char *str, int len, /* input string */ |
| 1876 | const char *pat, int pat_len, /* pattern string to find */ |
| 1877 | const char *sub, int sub_len, /* substitution string */ |
| 1878 | int count, /* number of replacements */ |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1879 | int *out_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1880 | { |
| 1881 | char *out_s; |
| 1882 | char *new_s; |
| 1883 | int nfound, offset, new_len; |
| 1884 | |
| 1885 | if (len == 0 || pat_len > len) |
| 1886 | goto return_same; |
| 1887 | |
| 1888 | /* find length of output string */ |
| 1889 | nfound = mymemcnt(str, len, pat, pat_len); |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 1890 | if (count < 0) |
| 1891 | count = INT_MAX; |
| 1892 | else if (nfound > count) |
| 1893 | nfound = count; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1894 | if (nfound == 0) |
| 1895 | goto return_same; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1896 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1897 | new_len = len + nfound*(sub_len - pat_len); |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1898 | if (new_len == 0) { |
| 1899 | /* Have to allocate something for the caller to free(). */ |
| 1900 | out_s = (char *)PyMem_MALLOC(1); |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 1901 | if (out_s == NULL) |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1902 | return NULL; |
| 1903 | out_s[0] = '\0'; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1904 | } |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1905 | else { |
| 1906 | assert(new_len > 0); |
| 1907 | new_s = (char *)PyMem_MALLOC(new_len); |
| 1908 | if (new_s == NULL) |
| 1909 | return NULL; |
| 1910 | out_s = new_s; |
| 1911 | |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 1912 | for (; count > 0 && len > 0; --count) { |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1913 | /* find index of next instance of pattern */ |
| 1914 | offset = mymemfind(str, len, pat, pat_len); |
| 1915 | if (offset == -1) |
| 1916 | break; |
| 1917 | |
| 1918 | /* copy non matching part of input string */ |
| 1919 | memcpy(new_s, str, offset); |
| 1920 | str += offset + pat_len; |
| 1921 | len -= offset + pat_len; |
| 1922 | |
| 1923 | /* copy substitute into the output string */ |
| 1924 | new_s += offset; |
| 1925 | memcpy(new_s, sub, sub_len); |
| 1926 | new_s += sub_len; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1927 | } |
| 1928 | /* copy any remaining values into output string */ |
| 1929 | if (len > 0) |
| 1930 | memcpy(new_s, str, len); |
| 1931 | } |
| 1932 | *out_len = new_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1933 | return out_s; |
| 1934 | |
| 1935 | return_same: |
| 1936 | *out_len = -1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 1937 | return (char *)str; /* cast away const */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | |
| 1941 | static char replace__doc__[] = |
| 1942 | "S.replace (old, new[, maxsplit]) -> string\n\ |
| 1943 | \n\ |
| 1944 | Return a copy of string S with all occurrences of substring\n\ |
| 1945 | old replaced by new. If the optional argument maxsplit is\n\ |
| 1946 | given, only the first maxsplit occurrences are replaced."; |
| 1947 | |
| 1948 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1949 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1950 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1951 | const char *str = PyString_AS_STRING(self), *sub, *repl; |
| 1952 | char *new_s; |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1953 | const int len = PyString_GET_SIZE(self); |
| 1954 | int sub_len, repl_len, out_len; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1955 | int count = -1; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1956 | PyObject *new; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1957 | PyObject *subobj, *replobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1958 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1959 | if (!PyArg_ParseTuple(args, "OO|i:replace", |
| 1960 | &subobj, &replobj, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1961 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1962 | |
| 1963 | if (PyString_Check(subobj)) { |
| 1964 | sub = PyString_AS_STRING(subobj); |
| 1965 | sub_len = PyString_GET_SIZE(subobj); |
| 1966 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1967 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1968 | else if (PyUnicode_Check(subobj)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1969 | return PyUnicode_Replace((PyObject *)self, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1970 | subobj, replobj, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1971 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1972 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
| 1973 | return NULL; |
| 1974 | |
| 1975 | if (PyString_Check(replobj)) { |
| 1976 | repl = PyString_AS_STRING(replobj); |
| 1977 | repl_len = PyString_GET_SIZE(replobj); |
| 1978 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1979 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1980 | else if (PyUnicode_Check(replobj)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1981 | return PyUnicode_Replace((PyObject *)self, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1982 | subobj, replobj, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1983 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1984 | else if (PyObject_AsCharBuffer(replobj, &repl, &repl_len)) |
| 1985 | return NULL; |
| 1986 | |
Guido van Rossum | 96a45ad | 2000-03-13 15:56:08 +0000 | [diff] [blame] | 1987 | if (sub_len <= 0) { |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1988 | PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1989 | return NULL; |
| 1990 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1991 | new_s = mymemreplace(str,len,sub,sub_len,repl,repl_len,count,&out_len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1992 | if (new_s == NULL) { |
| 1993 | PyErr_NoMemory(); |
| 1994 | return NULL; |
| 1995 | } |
| 1996 | if (out_len == -1) { |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1997 | if (PyString_CheckExact(self)) { |
| 1998 | /* we're returning another reference to self */ |
| 1999 | new = (PyObject*)self; |
| 2000 | Py_INCREF(new); |
| 2001 | } |
| 2002 | else { |
| 2003 | new = PyString_FromStringAndSize(str, len); |
| 2004 | if (new == NULL) |
| 2005 | return NULL; |
| 2006 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2007 | } |
| 2008 | else { |
| 2009 | new = PyString_FromStringAndSize(new_s, out_len); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2010 | PyMem_FREE(new_s); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2011 | } |
| 2012 | return new; |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | static char startswith__doc__[] = |
| 2017 | "S.startswith(prefix[, start[, end]]) -> int\n\ |
| 2018 | \n\ |
| 2019 | Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ |
| 2020 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 2021 | comparing S at that position."; |
| 2022 | |
| 2023 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2024 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2025 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2026 | const char* str = PyString_AS_STRING(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2027 | int len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2028 | const char* prefix; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2029 | int plen; |
| 2030 | int start = 0; |
| 2031 | int end = -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2032 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2033 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2034 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 2035 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2036 | return NULL; |
| 2037 | if (PyString_Check(subobj)) { |
| 2038 | prefix = PyString_AS_STRING(subobj); |
| 2039 | plen = PyString_GET_SIZE(subobj); |
| 2040 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2041 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2042 | else if (PyUnicode_Check(subobj)) { |
| 2043 | int rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2044 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2045 | subobj, start, end, -1); |
| 2046 | if (rc == -1) |
| 2047 | return NULL; |
| 2048 | else |
| 2049 | return PyInt_FromLong((long) rc); |
| 2050 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2051 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2052 | else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2053 | return NULL; |
| 2054 | |
| 2055 | /* adopt Java semantics for index out of range. it is legal for |
| 2056 | * offset to be == plen, but this only returns true if prefix is |
| 2057 | * the empty string. |
| 2058 | */ |
| 2059 | if (start < 0 || start+plen > len) |
| 2060 | return PyInt_FromLong(0); |
| 2061 | |
| 2062 | if (!memcmp(str+start, prefix, plen)) { |
| 2063 | /* did the match end after the specified end? */ |
| 2064 | if (end < 0) |
| 2065 | return PyInt_FromLong(1); |
| 2066 | else if (end - start < plen) |
| 2067 | return PyInt_FromLong(0); |
| 2068 | else |
| 2069 | return PyInt_FromLong(1); |
| 2070 | } |
| 2071 | else return PyInt_FromLong(0); |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | static char endswith__doc__[] = |
| 2076 | "S.endswith(suffix[, start[, end]]) -> int\n\ |
| 2077 | \n\ |
| 2078 | Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ |
| 2079 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 2080 | comparing S at that position."; |
| 2081 | |
| 2082 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2083 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2084 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2085 | const char* str = PyString_AS_STRING(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2086 | int len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2087 | const char* suffix; |
| 2088 | int slen; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2089 | int start = 0; |
| 2090 | int end = -1; |
| 2091 | int lower, upper; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2092 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2093 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2094 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 2095 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2096 | return NULL; |
| 2097 | if (PyString_Check(subobj)) { |
| 2098 | suffix = PyString_AS_STRING(subobj); |
| 2099 | slen = PyString_GET_SIZE(subobj); |
| 2100 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2101 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2102 | else if (PyUnicode_Check(subobj)) { |
| 2103 | int rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2104 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2105 | subobj, start, end, +1); |
| 2106 | if (rc == -1) |
| 2107 | return NULL; |
| 2108 | else |
| 2109 | return PyInt_FromLong((long) rc); |
| 2110 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2111 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2112 | else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2113 | return NULL; |
| 2114 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2115 | if (start < 0 || start > len || slen > len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2116 | return PyInt_FromLong(0); |
| 2117 | |
| 2118 | upper = (end >= 0 && end <= len) ? end : len; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2119 | lower = (upper - slen) > start ? (upper - slen) : start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2120 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2121 | if (upper-lower >= slen && !memcmp(str+lower, suffix, slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2122 | return PyInt_FromLong(1); |
| 2123 | else return PyInt_FromLong(0); |
| 2124 | } |
| 2125 | |
| 2126 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2127 | static char encode__doc__[] = |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2128 | "S.encode([encoding[,errors]]) -> object\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2129 | \n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2130 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 2131 | to the default encoding. errors may be given to set a different error\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2132 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2133 | a ValueError. Other possible values are 'ignore' and 'replace'."; |
| 2134 | |
| 2135 | static PyObject * |
| 2136 | string_encode(PyStringObject *self, PyObject *args) |
| 2137 | { |
| 2138 | char *encoding = NULL; |
| 2139 | char *errors = NULL; |
| 2140 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 2141 | return NULL; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2142 | return PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
| 2143 | } |
| 2144 | |
| 2145 | |
| 2146 | static char decode__doc__[] = |
| 2147 | "S.decode([encoding[,errors]]) -> object\n\ |
| 2148 | \n\ |
| 2149 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 2150 | to the default encoding. errors may be given to set a different error\n\ |
| 2151 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2152 | a ValueError. Other possible values are 'ignore' and 'replace'."; |
| 2153 | |
| 2154 | static PyObject * |
| 2155 | string_decode(PyStringObject *self, PyObject *args) |
| 2156 | { |
| 2157 | char *encoding = NULL; |
| 2158 | char *errors = NULL; |
| 2159 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 2160 | return NULL; |
| 2161 | return PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2165 | static char expandtabs__doc__[] = |
| 2166 | "S.expandtabs([tabsize]) -> string\n\ |
| 2167 | \n\ |
| 2168 | Return a copy of S where all tab characters are expanded using spaces.\n\ |
| 2169 | If tabsize is not given, a tab size of 8 characters is assumed."; |
| 2170 | |
| 2171 | static PyObject* |
| 2172 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 2173 | { |
| 2174 | const char *e, *p; |
| 2175 | char *q; |
| 2176 | int i, j; |
| 2177 | PyObject *u; |
| 2178 | int tabsize = 8; |
| 2179 | |
| 2180 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 2181 | return NULL; |
| 2182 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 2183 | /* First pass: determine size of output string */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2184 | i = j = 0; |
| 2185 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 2186 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 2187 | if (*p == '\t') { |
| 2188 | if (tabsize > 0) |
| 2189 | j += tabsize - (j % tabsize); |
| 2190 | } |
| 2191 | else { |
| 2192 | j++; |
| 2193 | if (*p == '\n' || *p == '\r') { |
| 2194 | i += j; |
| 2195 | j = 0; |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | /* Second pass: create output string and fill it */ |
| 2200 | u = PyString_FromStringAndSize(NULL, i + j); |
| 2201 | if (!u) |
| 2202 | return NULL; |
| 2203 | |
| 2204 | j = 0; |
| 2205 | q = PyString_AS_STRING(u); |
| 2206 | |
| 2207 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 2208 | if (*p == '\t') { |
| 2209 | if (tabsize > 0) { |
| 2210 | i = tabsize - (j % tabsize); |
| 2211 | j += i; |
| 2212 | while (i--) |
| 2213 | *q++ = ' '; |
| 2214 | } |
| 2215 | } |
| 2216 | else { |
| 2217 | j++; |
| 2218 | *q++ = *p; |
| 2219 | if (*p == '\n' || *p == '\r') |
| 2220 | j = 0; |
| 2221 | } |
| 2222 | |
| 2223 | return u; |
| 2224 | } |
| 2225 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2226 | static PyObject * |
| 2227 | pad(PyStringObject *self, int left, int right, char fill) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2228 | { |
| 2229 | PyObject *u; |
| 2230 | |
| 2231 | if (left < 0) |
| 2232 | left = 0; |
| 2233 | if (right < 0) |
| 2234 | right = 0; |
| 2235 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2236 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2237 | Py_INCREF(self); |
| 2238 | return (PyObject *)self; |
| 2239 | } |
| 2240 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2241 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2242 | left + PyString_GET_SIZE(self) + right); |
| 2243 | if (u) { |
| 2244 | if (left) |
| 2245 | memset(PyString_AS_STRING(u), fill, left); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2246 | memcpy(PyString_AS_STRING(u) + left, |
| 2247 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2248 | PyString_GET_SIZE(self)); |
| 2249 | if (right) |
| 2250 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 2251 | fill, right); |
| 2252 | } |
| 2253 | |
| 2254 | return u; |
| 2255 | } |
| 2256 | |
| 2257 | static char ljust__doc__[] = |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2258 | "S.ljust(width) -> string\n" |
| 2259 | "\n" |
| 2260 | "Return S left justified in a string of length width. Padding is\n" |
| 2261 | "done using spaces."; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2262 | |
| 2263 | static PyObject * |
| 2264 | string_ljust(PyStringObject *self, PyObject *args) |
| 2265 | { |
| 2266 | int width; |
| 2267 | if (!PyArg_ParseTuple(args, "i:ljust", &width)) |
| 2268 | return NULL; |
| 2269 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2270 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2271 | Py_INCREF(self); |
| 2272 | return (PyObject*) self; |
| 2273 | } |
| 2274 | |
| 2275 | return pad(self, 0, width - PyString_GET_SIZE(self), ' '); |
| 2276 | } |
| 2277 | |
| 2278 | |
| 2279 | static char rjust__doc__[] = |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2280 | "S.rjust(width) -> string\n" |
| 2281 | "\n" |
| 2282 | "Return S right justified in a string of length width. Padding is\n" |
| 2283 | "done using spaces."; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2284 | |
| 2285 | static PyObject * |
| 2286 | string_rjust(PyStringObject *self, PyObject *args) |
| 2287 | { |
| 2288 | int width; |
| 2289 | if (!PyArg_ParseTuple(args, "i:rjust", &width)) |
| 2290 | return NULL; |
| 2291 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2292 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2293 | Py_INCREF(self); |
| 2294 | return (PyObject*) self; |
| 2295 | } |
| 2296 | |
| 2297 | return pad(self, width - PyString_GET_SIZE(self), 0, ' '); |
| 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | static char center__doc__[] = |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2302 | "S.center(width) -> string\n" |
| 2303 | "\n" |
| 2304 | "Return S centered in a string of length width. Padding is done\n" |
| 2305 | "using spaces."; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2306 | |
| 2307 | static PyObject * |
| 2308 | string_center(PyStringObject *self, PyObject *args) |
| 2309 | { |
| 2310 | int marg, left; |
| 2311 | int width; |
| 2312 | |
| 2313 | if (!PyArg_ParseTuple(args, "i:center", &width)) |
| 2314 | return NULL; |
| 2315 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2316 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2317 | Py_INCREF(self); |
| 2318 | return (PyObject*) self; |
| 2319 | } |
| 2320 | |
| 2321 | marg = width - PyString_GET_SIZE(self); |
| 2322 | left = marg / 2 + (marg & width & 1); |
| 2323 | |
| 2324 | return pad(self, left, marg - left, ' '); |
| 2325 | } |
| 2326 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2327 | static char isspace__doc__[] = |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2328 | "S.isspace() -> int\n" |
| 2329 | "\n" |
| 2330 | "Return 1 if there are only whitespace characters in S,\n" |
| 2331 | "0 otherwise."; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2332 | |
| 2333 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2334 | string_isspace(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2335 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2336 | register const unsigned char *p |
| 2337 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2338 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2339 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2340 | /* Shortcut for single character strings */ |
| 2341 | if (PyString_GET_SIZE(self) == 1 && |
| 2342 | isspace(*p)) |
| 2343 | return PyInt_FromLong(1); |
| 2344 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2345 | /* Special case for empty strings */ |
| 2346 | if (PyString_GET_SIZE(self) == 0) |
| 2347 | return PyInt_FromLong(0); |
| 2348 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2349 | e = p + PyString_GET_SIZE(self); |
| 2350 | for (; p < e; p++) { |
| 2351 | if (!isspace(*p)) |
| 2352 | return PyInt_FromLong(0); |
| 2353 | } |
| 2354 | return PyInt_FromLong(1); |
| 2355 | } |
| 2356 | |
| 2357 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2358 | static char isalpha__doc__[] = |
| 2359 | "S.isalpha() -> int\n\ |
| 2360 | \n\ |
| 2361 | Return 1 if all characters in S are alphabetic\n\ |
| 2362 | and there is at least one character in S, 0 otherwise."; |
| 2363 | |
| 2364 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2365 | string_isalpha(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2366 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2367 | register const unsigned char *p |
| 2368 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2369 | register const unsigned char *e; |
| 2370 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2371 | /* Shortcut for single character strings */ |
| 2372 | if (PyString_GET_SIZE(self) == 1 && |
| 2373 | isalpha(*p)) |
| 2374 | return PyInt_FromLong(1); |
| 2375 | |
| 2376 | /* Special case for empty strings */ |
| 2377 | if (PyString_GET_SIZE(self) == 0) |
| 2378 | return PyInt_FromLong(0); |
| 2379 | |
| 2380 | e = p + PyString_GET_SIZE(self); |
| 2381 | for (; p < e; p++) { |
| 2382 | if (!isalpha(*p)) |
| 2383 | return PyInt_FromLong(0); |
| 2384 | } |
| 2385 | return PyInt_FromLong(1); |
| 2386 | } |
| 2387 | |
| 2388 | |
| 2389 | static char isalnum__doc__[] = |
| 2390 | "S.isalnum() -> int\n\ |
| 2391 | \n\ |
| 2392 | Return 1 if all characters in S are alphanumeric\n\ |
| 2393 | and there is at least one character in S, 0 otherwise."; |
| 2394 | |
| 2395 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2396 | string_isalnum(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2397 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2398 | register const unsigned char *p |
| 2399 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2400 | register const unsigned char *e; |
| 2401 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2402 | /* Shortcut for single character strings */ |
| 2403 | if (PyString_GET_SIZE(self) == 1 && |
| 2404 | isalnum(*p)) |
| 2405 | return PyInt_FromLong(1); |
| 2406 | |
| 2407 | /* Special case for empty strings */ |
| 2408 | if (PyString_GET_SIZE(self) == 0) |
| 2409 | return PyInt_FromLong(0); |
| 2410 | |
| 2411 | e = p + PyString_GET_SIZE(self); |
| 2412 | for (; p < e; p++) { |
| 2413 | if (!isalnum(*p)) |
| 2414 | return PyInt_FromLong(0); |
| 2415 | } |
| 2416 | return PyInt_FromLong(1); |
| 2417 | } |
| 2418 | |
| 2419 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2420 | static char isdigit__doc__[] = |
| 2421 | "S.isdigit() -> int\n\ |
| 2422 | \n\ |
| 2423 | Return 1 if there are only digit characters in S,\n\ |
| 2424 | 0 otherwise."; |
| 2425 | |
| 2426 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2427 | string_isdigit(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2428 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2429 | register const unsigned char *p |
| 2430 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2431 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2432 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2433 | /* Shortcut for single character strings */ |
| 2434 | if (PyString_GET_SIZE(self) == 1 && |
| 2435 | isdigit(*p)) |
| 2436 | return PyInt_FromLong(1); |
| 2437 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2438 | /* Special case for empty strings */ |
| 2439 | if (PyString_GET_SIZE(self) == 0) |
| 2440 | return PyInt_FromLong(0); |
| 2441 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2442 | e = p + PyString_GET_SIZE(self); |
| 2443 | for (; p < e; p++) { |
| 2444 | if (!isdigit(*p)) |
| 2445 | return PyInt_FromLong(0); |
| 2446 | } |
| 2447 | return PyInt_FromLong(1); |
| 2448 | } |
| 2449 | |
| 2450 | |
| 2451 | static char islower__doc__[] = |
| 2452 | "S.islower() -> int\n\ |
| 2453 | \n\ |
| 2454 | Return 1 if all cased characters in S are lowercase and there is\n\ |
| 2455 | at least one cased character in S, 0 otherwise."; |
| 2456 | |
| 2457 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2458 | string_islower(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2459 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2460 | register const unsigned char *p |
| 2461 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2462 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2463 | int cased; |
| 2464 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2465 | /* Shortcut for single character strings */ |
| 2466 | if (PyString_GET_SIZE(self) == 1) |
| 2467 | return PyInt_FromLong(islower(*p) != 0); |
| 2468 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2469 | /* Special case for empty strings */ |
| 2470 | if (PyString_GET_SIZE(self) == 0) |
| 2471 | return PyInt_FromLong(0); |
| 2472 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2473 | e = p + PyString_GET_SIZE(self); |
| 2474 | cased = 0; |
| 2475 | for (; p < e; p++) { |
| 2476 | if (isupper(*p)) |
| 2477 | return PyInt_FromLong(0); |
| 2478 | else if (!cased && islower(*p)) |
| 2479 | cased = 1; |
| 2480 | } |
| 2481 | return PyInt_FromLong(cased); |
| 2482 | } |
| 2483 | |
| 2484 | |
| 2485 | static char isupper__doc__[] = |
| 2486 | "S.isupper() -> int\n\ |
| 2487 | \n\ |
| 2488 | Return 1 if all cased characters in S are uppercase and there is\n\ |
| 2489 | at least one cased character in S, 0 otherwise."; |
| 2490 | |
| 2491 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2492 | string_isupper(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2493 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2494 | register const unsigned char *p |
| 2495 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2496 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2497 | int cased; |
| 2498 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2499 | /* Shortcut for single character strings */ |
| 2500 | if (PyString_GET_SIZE(self) == 1) |
| 2501 | return PyInt_FromLong(isupper(*p) != 0); |
| 2502 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2503 | /* Special case for empty strings */ |
| 2504 | if (PyString_GET_SIZE(self) == 0) |
| 2505 | return PyInt_FromLong(0); |
| 2506 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2507 | e = p + PyString_GET_SIZE(self); |
| 2508 | cased = 0; |
| 2509 | for (; p < e; p++) { |
| 2510 | if (islower(*p)) |
| 2511 | return PyInt_FromLong(0); |
| 2512 | else if (!cased && isupper(*p)) |
| 2513 | cased = 1; |
| 2514 | } |
| 2515 | return PyInt_FromLong(cased); |
| 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | static char istitle__doc__[] = |
| 2520 | "S.istitle() -> int\n\ |
| 2521 | \n\ |
| 2522 | Return 1 if S is a titlecased string, i.e. uppercase characters\n\ |
| 2523 | may only follow uncased characters and lowercase characters only cased\n\ |
| 2524 | ones. Return 0 otherwise."; |
| 2525 | |
| 2526 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2527 | string_istitle(PyStringObject *self, PyObject *uncased) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2528 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2529 | register const unsigned char *p |
| 2530 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2531 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2532 | int cased, previous_is_cased; |
| 2533 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2534 | /* Shortcut for single character strings */ |
| 2535 | if (PyString_GET_SIZE(self) == 1) |
| 2536 | return PyInt_FromLong(isupper(*p) != 0); |
| 2537 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2538 | /* Special case for empty strings */ |
| 2539 | if (PyString_GET_SIZE(self) == 0) |
| 2540 | return PyInt_FromLong(0); |
| 2541 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2542 | e = p + PyString_GET_SIZE(self); |
| 2543 | cased = 0; |
| 2544 | previous_is_cased = 0; |
| 2545 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2546 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2547 | |
| 2548 | if (isupper(ch)) { |
| 2549 | if (previous_is_cased) |
| 2550 | return PyInt_FromLong(0); |
| 2551 | previous_is_cased = 1; |
| 2552 | cased = 1; |
| 2553 | } |
| 2554 | else if (islower(ch)) { |
| 2555 | if (!previous_is_cased) |
| 2556 | return PyInt_FromLong(0); |
| 2557 | previous_is_cased = 1; |
| 2558 | cased = 1; |
| 2559 | } |
| 2560 | else |
| 2561 | previous_is_cased = 0; |
| 2562 | } |
| 2563 | return PyInt_FromLong(cased); |
| 2564 | } |
| 2565 | |
| 2566 | |
| 2567 | static char splitlines__doc__[] = |
Fred Drake | 2bae4fa | 2001-10-13 15:57:55 +0000 | [diff] [blame] | 2568 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2569 | \n\ |
| 2570 | Return a list of the lines in S, breaking at line boundaries.\n\ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2571 | Line breaks are not included in the resulting list unless keepends\n\ |
| 2572 | is given and true."; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2573 | |
| 2574 | #define SPLIT_APPEND(data, left, right) \ |
| 2575 | str = PyString_FromStringAndSize(data + left, right - left); \ |
| 2576 | if (!str) \ |
| 2577 | goto onError; \ |
| 2578 | if (PyList_Append(list, str)) { \ |
| 2579 | Py_DECREF(str); \ |
| 2580 | goto onError; \ |
| 2581 | } \ |
| 2582 | else \ |
| 2583 | Py_DECREF(str); |
| 2584 | |
| 2585 | static PyObject* |
| 2586 | string_splitlines(PyStringObject *self, PyObject *args) |
| 2587 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2588 | register int i; |
| 2589 | register int j; |
| 2590 | int len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2591 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2592 | PyObject *list; |
| 2593 | PyObject *str; |
| 2594 | char *data; |
| 2595 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2596 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2597 | return NULL; |
| 2598 | |
| 2599 | data = PyString_AS_STRING(self); |
| 2600 | len = PyString_GET_SIZE(self); |
| 2601 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2602 | list = PyList_New(0); |
| 2603 | if (!list) |
| 2604 | goto onError; |
| 2605 | |
| 2606 | for (i = j = 0; i < len; ) { |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2607 | int eol; |
| 2608 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2609 | /* Find a line and append it */ |
| 2610 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 2611 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2612 | |
| 2613 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2614 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2615 | if (i < len) { |
| 2616 | if (data[i] == '\r' && i + 1 < len && |
| 2617 | data[i+1] == '\n') |
| 2618 | i += 2; |
| 2619 | else |
| 2620 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2621 | if (keepends) |
| 2622 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2623 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2624 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2625 | j = i; |
| 2626 | } |
| 2627 | if (j < len) { |
| 2628 | SPLIT_APPEND(data, j, len); |
| 2629 | } |
| 2630 | |
| 2631 | return list; |
| 2632 | |
| 2633 | onError: |
| 2634 | Py_DECREF(list); |
| 2635 | return NULL; |
| 2636 | } |
| 2637 | |
| 2638 | #undef SPLIT_APPEND |
| 2639 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2640 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2641 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2642 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2643 | /* Counterparts of the obsolete stropmodule functions; except |
| 2644 | string.maketrans(). */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2645 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 2646 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
| 2647 | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
| 2648 | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
| 2649 | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
| 2650 | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
| 2651 | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
| 2652 | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
| 2653 | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
| 2654 | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
| 2655 | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
| 2656 | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, capitalize__doc__}, |
| 2657 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
| 2658 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, endswith__doc__}, |
| 2659 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
| 2660 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
| 2661 | {"lstrip", (PyCFunction)string_lstrip, METH_NOARGS, lstrip__doc__}, |
| 2662 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 2663 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 2664 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
| 2665 | {"rstrip", (PyCFunction)string_rstrip, METH_NOARGS, rstrip__doc__}, |
| 2666 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, startswith__doc__}, |
| 2667 | {"strip", (PyCFunction)string_strip, METH_NOARGS, strip__doc__}, |
| 2668 | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, swapcase__doc__}, |
| 2669 | {"translate", (PyCFunction)string_translate, METH_VARARGS, translate__doc__}, |
| 2670 | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
| 2671 | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
| 2672 | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
| 2673 | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
| 2674 | {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
| 2675 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
| 2676 | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 2677 | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, splitlines__doc__}, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2678 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2679 | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2680 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2681 | {NULL, NULL} /* sentinel */ |
| 2682 | }; |
| 2683 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2684 | staticforward PyObject * |
| 2685 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2686 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2687 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2688 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2689 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2690 | PyObject *x = NULL; |
| 2691 | static char *kwlist[] = {"object", 0}; |
| 2692 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2693 | if (type != &PyString_Type) |
| 2694 | return str_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2695 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
| 2696 | return NULL; |
| 2697 | if (x == NULL) |
| 2698 | return PyString_FromString(""); |
| 2699 | return PyObject_Str(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2702 | static PyObject * |
| 2703 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2704 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 2705 | PyObject *tmp, *pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2706 | int n; |
| 2707 | |
| 2708 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 2709 | tmp = string_new(&PyString_Type, args, kwds); |
| 2710 | if (tmp == NULL) |
| 2711 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 2712 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 2713 | n = PyString_GET_SIZE(tmp); |
| 2714 | pnew = type->tp_alloc(type, n); |
| 2715 | if (pnew != NULL) { |
| 2716 | memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
| 2717 | #ifdef CACHE_HASH |
| 2718 | ((PyStringObject *)pnew)->ob_shash = |
| 2719 | ((PyStringObject *)tmp)->ob_shash; |
| 2720 | #endif |
| 2721 | #ifdef INTERN_STRINGS |
| 2722 | ((PyStringObject *)pnew)->ob_sinterned = |
| 2723 | ((PyStringObject *)tmp)->ob_sinterned; |
| 2724 | #endif |
| 2725 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 2726 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 2727 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2730 | static char string_doc[] = |
| 2731 | "str(object) -> string\n\ |
| 2732 | \n\ |
| 2733 | Return a nice string representation of the object.\n\ |
| 2734 | If the argument is a string, the return value is the same object."; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2735 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2736 | PyTypeObject PyString_Type = { |
| 2737 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2738 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2739 | "str", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2740 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2741 | sizeof(char), |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2742 | (destructor)string_dealloc, /* tp_dealloc */ |
| 2743 | (printfunc)string_print, /* tp_print */ |
| 2744 | 0, /* tp_getattr */ |
| 2745 | 0, /* tp_setattr */ |
| 2746 | 0, /* tp_compare */ |
| 2747 | (reprfunc)string_repr, /* tp_repr */ |
| 2748 | 0, /* tp_as_number */ |
| 2749 | &string_as_sequence, /* tp_as_sequence */ |
| 2750 | 0, /* tp_as_mapping */ |
| 2751 | (hashfunc)string_hash, /* tp_hash */ |
| 2752 | 0, /* tp_call */ |
| 2753 | (reprfunc)string_str, /* tp_str */ |
| 2754 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2755 | 0, /* tp_setattro */ |
| 2756 | &string_as_buffer, /* tp_as_buffer */ |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2757 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2758 | string_doc, /* tp_doc */ |
| 2759 | 0, /* tp_traverse */ |
| 2760 | 0, /* tp_clear */ |
| 2761 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 2762 | 0, /* tp_weaklistoffset */ |
| 2763 | 0, /* tp_iter */ |
| 2764 | 0, /* tp_iternext */ |
| 2765 | string_methods, /* tp_methods */ |
| 2766 | 0, /* tp_members */ |
| 2767 | 0, /* tp_getset */ |
| 2768 | 0, /* tp_base */ |
| 2769 | 0, /* tp_dict */ |
| 2770 | 0, /* tp_descr_get */ |
| 2771 | 0, /* tp_descr_set */ |
| 2772 | 0, /* tp_dictoffset */ |
| 2773 | 0, /* tp_init */ |
| 2774 | 0, /* tp_alloc */ |
| 2775 | string_new, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2776 | _PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2777 | }; |
| 2778 | |
| 2779 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2780 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2781 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2782 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2783 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2784 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2785 | if (w == NULL || !PyString_Check(*pv)) { |
| 2786 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2787 | *pv = NULL; |
| 2788 | return; |
| 2789 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2790 | v = string_concat((PyStringObject *) *pv, w); |
| 2791 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2792 | *pv = v; |
| 2793 | } |
| 2794 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2795 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2796 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2797 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2798 | PyString_Concat(pv, w); |
| 2799 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2803 | /* The following function breaks the notion that strings are immutable: |
| 2804 | it changes the size of a string. We get away with this only if there |
| 2805 | is only one module referencing the object. You can also think of it |
| 2806 | as creating a new string object and destroying the old one, only |
| 2807 | more efficiently. In any case, don't use this if the string may |
| 2808 | already be known to some other part of the code... */ |
| 2809 | |
| 2810 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2811 | _PyString_Resize(PyObject **pv, int newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2812 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2813 | register PyObject *v; |
| 2814 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2815 | v = *pv; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2816 | if (!PyString_Check(v) || v->ob_refcnt != 1) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2817 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2818 | Py_DECREF(v); |
| 2819 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 2820 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2821 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2822 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 2823 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 2824 | --_Py_RefTotal; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2825 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2826 | _Py_ForgetReference(v); |
| 2827 | *pv = (PyObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2828 | PyObject_REALLOC((char *)v, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2829 | sizeof(PyStringObject) + newsize * sizeof(char)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2830 | if (*pv == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2831 | PyObject_DEL(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2832 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 2833 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2834 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2835 | _Py_NewReference(*pv); |
| 2836 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2837 | sv->ob_size = newsize; |
| 2838 | sv->ob_sval[newsize] = '\0'; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2839 | return 0; |
| 2840 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2841 | |
| 2842 | /* Helpers for formatstring */ |
| 2843 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2844 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2845 | getnextarg(PyObject *args, int arglen, int *p_argidx) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2846 | { |
| 2847 | int argidx = *p_argidx; |
| 2848 | if (argidx < arglen) { |
| 2849 | (*p_argidx)++; |
| 2850 | if (arglen < 0) |
| 2851 | return args; |
| 2852 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2853 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2854 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2855 | PyErr_SetString(PyExc_TypeError, |
| 2856 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2857 | return NULL; |
| 2858 | } |
| 2859 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2860 | /* Format codes |
| 2861 | * F_LJUST '-' |
| 2862 | * F_SIGN '+' |
| 2863 | * F_BLANK ' ' |
| 2864 | * F_ALT '#' |
| 2865 | * F_ZERO '0' |
| 2866 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2867 | #define F_LJUST (1<<0) |
| 2868 | #define F_SIGN (1<<1) |
| 2869 | #define F_BLANK (1<<2) |
| 2870 | #define F_ALT (1<<3) |
| 2871 | #define F_ZERO (1<<4) |
| 2872 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2873 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2874 | formatfloat(char *buf, size_t buflen, int flags, |
| 2875 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2876 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2877 | /* fmt = '%#.' + `prec` + `type` |
| 2878 | worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2879 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2880 | double x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2881 | if (!PyArg_Parse(v, "d;float argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2882 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2883 | if (prec < 0) |
| 2884 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2885 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 2886 | type = 'g'; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 2887 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 2888 | (flags&F_ALT) ? "#" : "", |
| 2889 | prec, type); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2890 | /* worst case length calc to ensure no buffer overrun: |
| 2891 | fmt = %#.<prec>g |
| 2892 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2893 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2894 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 2895 | If prec=0 the effective precision is 1 (the leading digit is |
| 2896 | always given), therefore increase by one to 10+prec. */ |
| 2897 | if (buflen <= (size_t)10 + (size_t)prec) { |
| 2898 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2899 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2900 | return -1; |
| 2901 | } |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 2902 | PyOS_snprintf(buf, buflen, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2903 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2906 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 2907 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 2908 | * Python's regular ints. |
| 2909 | * Return value: a new PyString*, or NULL if error. |
| 2910 | * . *pbuf is set to point into it, |
| 2911 | * *plen set to the # of chars following that. |
| 2912 | * Caller must decref it when done using pbuf. |
| 2913 | * The string starting at *pbuf is of the form |
| 2914 | * "-"? ("0x" | "0X")? digit+ |
| 2915 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2916 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2917 | * There will be at least prec digits, zero-filled on the left if |
| 2918 | * necessary to get that many. |
| 2919 | * val object to be converted |
| 2920 | * flags bitmask of format flags; only F_ALT is looked at |
| 2921 | * prec minimum number of digits; 0-fill on left if needed |
| 2922 | * type a character in [duoxX]; u acts the same as d |
| 2923 | * |
| 2924 | * CAUTION: o, x and X conversions on regular ints can never |
| 2925 | * produce a '-' sign, but can for Python's unbounded ints. |
| 2926 | */ |
| 2927 | PyObject* |
| 2928 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 2929 | char **pbuf, int *plen) |
| 2930 | { |
| 2931 | PyObject *result = NULL; |
| 2932 | char *buf; |
| 2933 | int i; |
| 2934 | int sign; /* 1 if '-', else 0 */ |
| 2935 | int len; /* number of characters */ |
| 2936 | int numdigits; /* len == numnondigits + numdigits */ |
| 2937 | int numnondigits = 0; |
| 2938 | |
| 2939 | switch (type) { |
| 2940 | case 'd': |
| 2941 | case 'u': |
| 2942 | result = val->ob_type->tp_str(val); |
| 2943 | break; |
| 2944 | case 'o': |
| 2945 | result = val->ob_type->tp_as_number->nb_oct(val); |
| 2946 | break; |
| 2947 | case 'x': |
| 2948 | case 'X': |
| 2949 | numnondigits = 2; |
| 2950 | result = val->ob_type->tp_as_number->nb_hex(val); |
| 2951 | break; |
| 2952 | default: |
| 2953 | assert(!"'type' not in [duoxX]"); |
| 2954 | } |
| 2955 | if (!result) |
| 2956 | return NULL; |
| 2957 | |
| 2958 | /* To modify the string in-place, there can only be one reference. */ |
| 2959 | if (result->ob_refcnt != 1) { |
| 2960 | PyErr_BadInternalCall(); |
| 2961 | return NULL; |
| 2962 | } |
| 2963 | buf = PyString_AsString(result); |
| 2964 | len = PyString_Size(result); |
| 2965 | if (buf[len-1] == 'L') { |
| 2966 | --len; |
| 2967 | buf[len] = '\0'; |
| 2968 | } |
| 2969 | sign = buf[0] == '-'; |
| 2970 | numnondigits += sign; |
| 2971 | numdigits = len - numnondigits; |
| 2972 | assert(numdigits > 0); |
| 2973 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 2974 | /* Get rid of base marker unless F_ALT */ |
| 2975 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2976 | /* Need to skip 0x, 0X or 0. */ |
| 2977 | int skipped = 0; |
| 2978 | switch (type) { |
| 2979 | case 'o': |
| 2980 | assert(buf[sign] == '0'); |
| 2981 | /* If 0 is only digit, leave it alone. */ |
| 2982 | if (numdigits > 1) { |
| 2983 | skipped = 1; |
| 2984 | --numdigits; |
| 2985 | } |
| 2986 | break; |
| 2987 | case 'x': |
| 2988 | case 'X': |
| 2989 | assert(buf[sign] == '0'); |
| 2990 | assert(buf[sign + 1] == 'x'); |
| 2991 | skipped = 2; |
| 2992 | numnondigits -= 2; |
| 2993 | break; |
| 2994 | } |
| 2995 | if (skipped) { |
| 2996 | buf += skipped; |
| 2997 | len -= skipped; |
| 2998 | if (sign) |
| 2999 | buf[0] = '-'; |
| 3000 | } |
| 3001 | assert(len == numnondigits + numdigits); |
| 3002 | assert(numdigits > 0); |
| 3003 | } |
| 3004 | |
| 3005 | /* Fill with leading zeroes to meet minimum width. */ |
| 3006 | if (prec > numdigits) { |
| 3007 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 3008 | numnondigits + prec); |
| 3009 | char *b1; |
| 3010 | if (!r1) { |
| 3011 | Py_DECREF(result); |
| 3012 | return NULL; |
| 3013 | } |
| 3014 | b1 = PyString_AS_STRING(r1); |
| 3015 | for (i = 0; i < numnondigits; ++i) |
| 3016 | *b1++ = *buf++; |
| 3017 | for (i = 0; i < prec - numdigits; i++) |
| 3018 | *b1++ = '0'; |
| 3019 | for (i = 0; i < numdigits; i++) |
| 3020 | *b1++ = *buf++; |
| 3021 | *b1 = '\0'; |
| 3022 | Py_DECREF(result); |
| 3023 | result = r1; |
| 3024 | buf = PyString_AS_STRING(result); |
| 3025 | len = numnondigits + prec; |
| 3026 | } |
| 3027 | |
| 3028 | /* Fix up case for hex conversions. */ |
| 3029 | switch (type) { |
| 3030 | case 'x': |
| 3031 | /* Need to convert all upper case letters to lower case. */ |
| 3032 | for (i = 0; i < len; i++) |
| 3033 | if (buf[i] >= 'A' && buf[i] <= 'F') |
| 3034 | buf[i] += 'a'-'A'; |
| 3035 | break; |
| 3036 | case 'X': |
| 3037 | /* Need to convert 0x to 0X (and -0x to -0X). */ |
| 3038 | if (buf[sign + 1] == 'x') |
| 3039 | buf[sign + 1] = 'X'; |
| 3040 | break; |
| 3041 | } |
| 3042 | *pbuf = buf; |
| 3043 | *plen = len; |
| 3044 | return result; |
| 3045 | } |
| 3046 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3047 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3048 | formatint(char *buf, size_t buflen, int flags, |
| 3049 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3050 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3051 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3052 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 3053 | + 1 + 1 = 24 */ |
| 3054 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3055 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3056 | if (!PyArg_Parse(v, "l;int argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3057 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3058 | if (prec < 0) |
| 3059 | prec = 1; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 3060 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c", |
| 3061 | (flags&F_ALT) ? "#" : "", |
| 3062 | prec, type); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3063 | /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal)) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3064 | worst case buf = '0x' + [0-9]*prec, where prec >= 11 */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3065 | if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3066 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3067 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3068 | return -1; |
| 3069 | } |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 3070 | PyOS_snprintf(buf, buflen, fmt, x); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3071 | /* When converting 0 under %#x or %#X, C leaves off the base marker, |
| 3072 | * but we want it (for consistency with other %#x conversions, and |
| 3073 | * for consistency with Python's hex() function). |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3074 | * BUG 28-Apr-2001 tim: At least two platform Cs (Metrowerks & |
| 3075 | * Compaq Tru64) violate the std by converting 0 w/ leading 0x anyway. |
| 3076 | * So add it only if the platform didn't already. |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3077 | */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3078 | if (x == 0 && |
| 3079 | (flags & F_ALT) && |
| 3080 | (type == 'x' || type == 'X') && |
| 3081 | buf[1] != (char)type) /* this last always true under std C */ |
| 3082 | { |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3083 | memmove(buf+2, buf, strlen(buf) + 1); |
| 3084 | buf[0] = '0'; |
| 3085 | buf[1] = (char)type; |
| 3086 | } |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3087 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3090 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3091 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3092 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3093 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3094 | if (PyString_Check(v)) { |
| 3095 | if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0])) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3096 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3097 | } |
| 3098 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3099 | if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0])) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3100 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3101 | } |
| 3102 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3103 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3106 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3107 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 3108 | |
| 3109 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 3110 | chars are formatted. XXX This is a magic number. Each formatting |
| 3111 | routine does bounds checking to ensure no overflow, but a better |
| 3112 | solution may be to malloc a buffer of appropriate size for each |
| 3113 | format. For now, the current solution is sufficient. |
| 3114 | */ |
| 3115 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3116 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3117 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3118 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3119 | { |
| 3120 | char *fmt, *res; |
| 3121 | int fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 3122 | int args_owned = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3123 | PyObject *result, *orig_args; |
| 3124 | #ifdef Py_USING_UNICODE |
| 3125 | PyObject *v, *w; |
| 3126 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3127 | PyObject *dict = NULL; |
| 3128 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 3129 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3130 | return NULL; |
| 3131 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3132 | orig_args = args; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3133 | fmt = PyString_AsString(format); |
| 3134 | fmtcnt = PyString_Size(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 3135 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3136 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3137 | if (result == NULL) |
| 3138 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3139 | res = PyString_AsString(result); |
| 3140 | if (PyTuple_Check(args)) { |
| 3141 | arglen = PyTuple_Size(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3142 | argidx = 0; |
| 3143 | } |
| 3144 | else { |
| 3145 | arglen = -1; |
| 3146 | argidx = -2; |
| 3147 | } |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3148 | if (args->ob_type->tp_as_mapping) |
| 3149 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3150 | while (--fmtcnt >= 0) { |
| 3151 | if (*fmt != '%') { |
| 3152 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 3153 | rescnt = fmtcnt + 100; |
| 3154 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3155 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3156 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3157 | res = PyString_AsString(result) |
| 3158 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3159 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3160 | } |
| 3161 | *res++ = *fmt++; |
| 3162 | } |
| 3163 | else { |
| 3164 | /* Got a format specifier */ |
| 3165 | int flags = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3166 | int width = -1; |
| 3167 | int prec = -1; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 3168 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3169 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3170 | PyObject *v = NULL; |
| 3171 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3172 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3173 | int sign; |
| 3174 | int len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3175 | char formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3176 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3177 | char *fmt_start = fmt; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 3178 | int argidx_start = argidx; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3179 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3180 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 3181 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3182 | if (*fmt == '(') { |
| 3183 | char *keystart; |
| 3184 | int keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3185 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3186 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3187 | |
| 3188 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3189 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3190 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3191 | goto error; |
| 3192 | } |
| 3193 | ++fmt; |
| 3194 | --fmtcnt; |
| 3195 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3196 | /* Skip over balanced parentheses */ |
| 3197 | while (pcount > 0 && --fmtcnt >= 0) { |
| 3198 | if (*fmt == ')') |
| 3199 | --pcount; |
| 3200 | else if (*fmt == '(') |
| 3201 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3202 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3203 | } |
| 3204 | keylen = fmt - keystart - 1; |
| 3205 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3206 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3207 | "incomplete format key"); |
| 3208 | goto error; |
| 3209 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3210 | key = PyString_FromStringAndSize(keystart, |
| 3211 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3212 | if (key == NULL) |
| 3213 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 3214 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3215 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 3216 | args_owned = 0; |
| 3217 | } |
| 3218 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3219 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3220 | if (args == NULL) { |
| 3221 | goto error; |
| 3222 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 3223 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3224 | arglen = -1; |
| 3225 | argidx = -2; |
| 3226 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3227 | while (--fmtcnt >= 0) { |
| 3228 | switch (c = *fmt++) { |
| 3229 | case '-': flags |= F_LJUST; continue; |
| 3230 | case '+': flags |= F_SIGN; continue; |
| 3231 | case ' ': flags |= F_BLANK; continue; |
| 3232 | case '#': flags |= F_ALT; continue; |
| 3233 | case '0': flags |= F_ZERO; continue; |
| 3234 | } |
| 3235 | break; |
| 3236 | } |
| 3237 | if (c == '*') { |
| 3238 | v = getnextarg(args, arglen, &argidx); |
| 3239 | if (v == NULL) |
| 3240 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3241 | if (!PyInt_Check(v)) { |
| 3242 | PyErr_SetString(PyExc_TypeError, |
| 3243 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3244 | goto error; |
| 3245 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3246 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 3247 | if (width < 0) { |
| 3248 | flags |= F_LJUST; |
| 3249 | width = -width; |
| 3250 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3251 | if (--fmtcnt >= 0) |
| 3252 | c = *fmt++; |
| 3253 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 3254 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3255 | width = c - '0'; |
| 3256 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 3257 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3258 | if (!isdigit(c)) |
| 3259 | break; |
| 3260 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3261 | PyErr_SetString( |
| 3262 | PyExc_ValueError, |
| 3263 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3264 | goto error; |
| 3265 | } |
| 3266 | width = width*10 + (c - '0'); |
| 3267 | } |
| 3268 | } |
| 3269 | if (c == '.') { |
| 3270 | prec = 0; |
| 3271 | if (--fmtcnt >= 0) |
| 3272 | c = *fmt++; |
| 3273 | if (c == '*') { |
| 3274 | v = getnextarg(args, arglen, &argidx); |
| 3275 | if (v == NULL) |
| 3276 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3277 | if (!PyInt_Check(v)) { |
| 3278 | PyErr_SetString( |
| 3279 | PyExc_TypeError, |
| 3280 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3281 | goto error; |
| 3282 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3283 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3284 | if (prec < 0) |
| 3285 | prec = 0; |
| 3286 | if (--fmtcnt >= 0) |
| 3287 | c = *fmt++; |
| 3288 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 3289 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3290 | prec = c - '0'; |
| 3291 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 3292 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3293 | if (!isdigit(c)) |
| 3294 | break; |
| 3295 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3296 | PyErr_SetString( |
| 3297 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3298 | "prec too big"); |
| 3299 | goto error; |
| 3300 | } |
| 3301 | prec = prec*10 + (c - '0'); |
| 3302 | } |
| 3303 | } |
| 3304 | } /* prec */ |
| 3305 | if (fmtcnt >= 0) { |
| 3306 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3307 | if (--fmtcnt >= 0) |
| 3308 | c = *fmt++; |
| 3309 | } |
| 3310 | } |
| 3311 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3312 | PyErr_SetString(PyExc_ValueError, |
| 3313 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3314 | goto error; |
| 3315 | } |
| 3316 | if (c != '%') { |
| 3317 | v = getnextarg(args, arglen, &argidx); |
| 3318 | if (v == NULL) |
| 3319 | goto error; |
| 3320 | } |
| 3321 | sign = 0; |
| 3322 | fill = ' '; |
| 3323 | switch (c) { |
| 3324 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3325 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3326 | len = 1; |
| 3327 | break; |
| 3328 | case 's': |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3329 | case 'r': |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3330 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3331 | if (PyUnicode_Check(v)) { |
| 3332 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 3333 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3334 | goto unicode; |
| 3335 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3336 | #endif |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3337 | if (c == 's') |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3338 | temp = PyObject_Str(v); |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3339 | else |
| 3340 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3341 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3342 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 3343 | if (!PyString_Check(temp)) { |
| 3344 | PyErr_SetString(PyExc_TypeError, |
| 3345 | "%s argument has non-string str()"); |
| 3346 | goto error; |
| 3347 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3348 | pbuf = PyString_AsString(temp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3349 | len = PyString_Size(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3350 | if (prec >= 0 && len > prec) |
| 3351 | len = prec; |
| 3352 | break; |
| 3353 | case 'i': |
| 3354 | case 'd': |
| 3355 | case 'u': |
| 3356 | case 'o': |
| 3357 | case 'x': |
| 3358 | case 'X': |
| 3359 | if (c == 'i') |
| 3360 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 3361 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3362 | temp = _PyString_FormatLong(v, flags, |
| 3363 | prec, c, &pbuf, &len); |
| 3364 | if (!temp) |
| 3365 | goto error; |
| 3366 | /* unbounded ints can always produce |
| 3367 | a sign character! */ |
| 3368 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 3369 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3370 | else { |
| 3371 | pbuf = formatbuf; |
| 3372 | len = formatint(pbuf, sizeof(formatbuf), |
| 3373 | flags, prec, c, v); |
| 3374 | if (len < 0) |
| 3375 | goto error; |
| 3376 | /* only d conversion is signed */ |
| 3377 | sign = c == 'd'; |
| 3378 | } |
| 3379 | if (flags & F_ZERO) |
| 3380 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3381 | break; |
| 3382 | case 'e': |
| 3383 | case 'E': |
| 3384 | case 'f': |
| 3385 | case 'g': |
| 3386 | case 'G': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3387 | pbuf = formatbuf; |
| 3388 | len = formatfloat(pbuf, sizeof(formatbuf), flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3389 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3390 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3391 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3392 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3393 | fill = '0'; |
| 3394 | break; |
| 3395 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3396 | pbuf = formatbuf; |
| 3397 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3398 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3399 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3402 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 3403 | "unsupported format character '%c' (0x%x) " |
| 3404 | "at index %i", |
| 3405 | c, c, fmt - 1 - PyString_AsString(format)); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3406 | goto error; |
| 3407 | } |
| 3408 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3409 | if (*pbuf == '-' || *pbuf == '+') { |
| 3410 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3411 | len--; |
| 3412 | } |
| 3413 | else if (flags & F_SIGN) |
| 3414 | sign = '+'; |
| 3415 | else if (flags & F_BLANK) |
| 3416 | sign = ' '; |
| 3417 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3418 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3419 | } |
| 3420 | if (width < len) |
| 3421 | width = len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3422 | if (rescnt < width + (sign != 0)) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 3423 | reslen -= rescnt; |
| 3424 | rescnt = width + fmtcnt + 100; |
| 3425 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3426 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3427 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3428 | res = PyString_AsString(result) |
| 3429 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3430 | } |
| 3431 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 3432 | if (fill != ' ') |
| 3433 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3434 | rescnt--; |
| 3435 | if (width > len) |
| 3436 | width--; |
| 3437 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3438 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 3439 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3440 | assert(pbuf[1] == c); |
| 3441 | if (fill != ' ') { |
| 3442 | *res++ = *pbuf++; |
| 3443 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3444 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3445 | rescnt -= 2; |
| 3446 | width -= 2; |
| 3447 | if (width < 0) |
| 3448 | width = 0; |
| 3449 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3450 | } |
| 3451 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3452 | do { |
| 3453 | --rescnt; |
| 3454 | *res++ = fill; |
| 3455 | } while (--width > len); |
| 3456 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3457 | if (fill == ' ') { |
| 3458 | if (sign) |
| 3459 | *res++ = sign; |
| 3460 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3461 | (c == 'x' || c == 'X')) { |
| 3462 | assert(pbuf[0] == '0'); |
| 3463 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3464 | *res++ = *pbuf++; |
| 3465 | *res++ = *pbuf++; |
| 3466 | } |
| 3467 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3468 | memcpy(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3469 | res += len; |
| 3470 | rescnt -= len; |
| 3471 | while (--width >= len) { |
| 3472 | --rescnt; |
| 3473 | *res++ = ' '; |
| 3474 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 3475 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3476 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3477 | "not all arguments converted"); |
| 3478 | goto error; |
| 3479 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3480 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3481 | } /* '%' */ |
| 3482 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 3483 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3484 | PyErr_SetString(PyExc_TypeError, |
| 3485 | "not all arguments converted"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3486 | goto error; |
| 3487 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3488 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3489 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3490 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3491 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3492 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3493 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3494 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3495 | unicode: |
| 3496 | if (args_owned) { |
| 3497 | Py_DECREF(args); |
| 3498 | args_owned = 0; |
| 3499 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 3500 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3501 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 3502 | PyObject *v; |
| 3503 | int n = PyTuple_GET_SIZE(orig_args) - argidx; |
| 3504 | v = PyTuple_New(n); |
| 3505 | if (v == NULL) |
| 3506 | goto error; |
| 3507 | while (--n >= 0) { |
| 3508 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 3509 | Py_INCREF(w); |
| 3510 | PyTuple_SET_ITEM(v, n, w); |
| 3511 | } |
| 3512 | args = v; |
| 3513 | } else { |
| 3514 | Py_INCREF(orig_args); |
| 3515 | args = orig_args; |
| 3516 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3517 | args_owned = 1; |
| 3518 | /* Take what we have of the result and let the Unicode formatting |
| 3519 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3520 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3521 | if (_PyString_Resize(&result, rescnt)) |
| 3522 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3523 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 3524 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3525 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 3526 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3527 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3528 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3529 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3530 | if (v == NULL) |
| 3531 | goto error; |
| 3532 | /* Paste what we have (result) to what the Unicode formatting |
| 3533 | function returned (v) and return the result (or error) */ |
| 3534 | w = PyUnicode_Concat(result, v); |
| 3535 | Py_DECREF(result); |
| 3536 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3537 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3538 | return w; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3539 | #endif /* Py_USING_UNICODE */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3540 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3541 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3542 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3543 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3544 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3545 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3546 | return NULL; |
| 3547 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3548 | |
| 3549 | |
| 3550 | #ifdef INTERN_STRINGS |
| 3551 | |
Barry Warsaw | 4df762f | 2000-08-16 23:41:01 +0000 | [diff] [blame] | 3552 | /* This dictionary will leak at PyString_Fini() time. That's acceptable |
| 3553 | * because PyString_Fini() specifically frees interned strings that are |
| 3554 | * only referenced by this dictionary. The CVS log entry for revision 2.45 |
| 3555 | * says: |
| 3556 | * |
| 3557 | * Change the Fini function to only remove otherwise unreferenced |
| 3558 | * strings from the interned table. There are references in |
| 3559 | * hard-to-find static variables all over the interpreter, and it's not |
| 3560 | * worth trying to get rid of all those; but "uninterning" isn't fair |
| 3561 | * either and may cause subtle failures later -- so we have to keep them |
| 3562 | * in the interned table. |
| 3563 | */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3564 | static PyObject *interned; |
| 3565 | |
| 3566 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3567 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3568 | { |
| 3569 | register PyStringObject *s = (PyStringObject *)(*p); |
| 3570 | PyObject *t; |
| 3571 | if (s == NULL || !PyString_Check(s)) |
| 3572 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
| 3573 | if ((t = s->ob_sinterned) != NULL) { |
| 3574 | if (t == (PyObject *)s) |
| 3575 | return; |
| 3576 | Py_INCREF(t); |
| 3577 | *p = t; |
| 3578 | Py_DECREF(s); |
| 3579 | return; |
| 3580 | } |
| 3581 | if (interned == NULL) { |
| 3582 | interned = PyDict_New(); |
| 3583 | if (interned == NULL) |
| 3584 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3585 | } |
| 3586 | if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { |
| 3587 | Py_INCREF(t); |
| 3588 | *p = s->ob_sinterned = t; |
| 3589 | Py_DECREF(s); |
| 3590 | return; |
| 3591 | } |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 3592 | /* Ensure that only true string objects appear in the intern dict, |
| 3593 | and as the value of ob_sinterned. */ |
| 3594 | if (PyString_CheckExact(s)) { |
| 3595 | t = (PyObject *)s; |
| 3596 | if (PyDict_SetItem(interned, t, t) == 0) { |
| 3597 | s->ob_sinterned = t; |
| 3598 | return; |
| 3599 | } |
| 3600 | } |
| 3601 | else { |
| 3602 | t = PyString_FromStringAndSize(PyString_AS_STRING(s), |
| 3603 | PyString_GET_SIZE(s)); |
| 3604 | if (t != NULL) { |
| 3605 | if (PyDict_SetItem(interned, t, t) == 0) { |
| 3606 | *p = s->ob_sinterned = t; |
| 3607 | Py_DECREF(s); |
| 3608 | return; |
| 3609 | } |
| 3610 | Py_DECREF(t); |
| 3611 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3612 | } |
| 3613 | PyErr_Clear(); |
| 3614 | } |
| 3615 | |
| 3616 | |
| 3617 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3618 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3619 | { |
| 3620 | PyObject *s = PyString_FromString(cp); |
| 3621 | if (s == NULL) |
| 3622 | return NULL; |
| 3623 | PyString_InternInPlace(&s); |
| 3624 | return s; |
| 3625 | } |
| 3626 | |
| 3627 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3628 | |
| 3629 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3630 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3631 | { |
| 3632 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3633 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 3634 | Py_XDECREF(characters[i]); |
| 3635 | characters[i] = NULL; |
| 3636 | } |
| 3637 | #ifndef DONT_SHARE_SHORT_STRINGS |
| 3638 | Py_XDECREF(nullstring); |
| 3639 | nullstring = NULL; |
| 3640 | #endif |
Guido van Rossum | 971a7aa | 1997-08-05 02:15:12 +0000 | [diff] [blame] | 3641 | #ifdef INTERN_STRINGS |
| 3642 | if (interned) { |
| 3643 | int pos, changed; |
| 3644 | PyObject *key, *value; |
| 3645 | do { |
| 3646 | changed = 0; |
| 3647 | pos = 0; |
| 3648 | while (PyDict_Next(interned, &pos, &key, &value)) { |
| 3649 | if (key->ob_refcnt == 2 && key == value) { |
| 3650 | PyDict_DelItem(interned, key); |
| 3651 | changed = 1; |
| 3652 | } |
| 3653 | } |
| 3654 | } while (changed); |
| 3655 | } |
| 3656 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3657 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 3658 | |
| 3659 | #ifdef INTERN_STRINGS |
| 3660 | void _Py_ReleaseInternedStrings(void) |
| 3661 | { |
| 3662 | if (interned) { |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3663 | fprintf(stderr, "releasing interned strings\n"); |
| 3664 | PyDict_Clear(interned); |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 3665 | Py_DECREF(interned); |
| 3666 | interned = NULL; |
| 3667 | } |
| 3668 | } |
| 3669 | #endif /* INTERN_STRINGS */ |