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