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