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