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