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