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