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