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)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 864 | /* Defer to Unicode join. |
| 865 | * CAUTION: There's no gurantee that the |
| 866 | * original sequence can be iterated over |
| 867 | * again, so we must pass seq here. |
| 868 | */ |
| 869 | PyObject *result; |
| 870 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 871 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 872 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 873 | } |
| 874 | PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 875 | "sequence item %i: expected string," |
| 876 | " %.80s found", |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 877 | i, item->ob_type->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 878 | Py_DECREF(seq); |
| 879 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 880 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 881 | sz += PyString_GET_SIZE(item); |
| 882 | if (i != 0) |
| 883 | sz += seplen; |
| 884 | if (sz < old_sz || sz > INT_MAX) { |
| 885 | PyErr_SetString(PyExc_OverflowError, |
| 886 | "join() is too long for a Python string"); |
| 887 | Py_DECREF(seq); |
| 888 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 889 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* Allocate result space. */ |
| 893 | res = PyString_FromStringAndSize((char*)NULL, (int)sz); |
| 894 | if (res == NULL) { |
| 895 | Py_DECREF(seq); |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | /* Catenate everything. */ |
| 900 | p = PyString_AS_STRING(res); |
| 901 | for (i = 0; i < seqlen; ++i) { |
| 902 | size_t n; |
| 903 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 904 | n = PyString_GET_SIZE(item); |
| 905 | memcpy(p, PyString_AS_STRING(item), n); |
| 906 | p += n; |
| 907 | if (i < seqlen - 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 908 | memcpy(p, sep, seplen); |
| 909 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 910 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 911 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 912 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 913 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 914 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 917 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 918 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 919 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 920 | const char *s = PyString_AS_STRING(self), *sub; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 921 | int len = PyString_GET_SIZE(self); |
| 922 | int n, i = 0, last = INT_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 923 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 924 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 925 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 926 | &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 927 | return -2; |
| 928 | if (PyString_Check(subobj)) { |
| 929 | sub = PyString_AS_STRING(subobj); |
| 930 | n = PyString_GET_SIZE(subobj); |
| 931 | } |
| 932 | else if (PyUnicode_Check(subobj)) |
| 933 | return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); |
| 934 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 935 | return -2; |
| 936 | |
| 937 | if (last > len) |
| 938 | last = len; |
| 939 | if (last < 0) |
| 940 | last += len; |
| 941 | if (last < 0) |
| 942 | last = 0; |
| 943 | if (i < 0) |
| 944 | i += len; |
| 945 | if (i < 0) |
| 946 | i = 0; |
| 947 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 948 | if (dir > 0) { |
| 949 | if (n == 0 && i <= last) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 950 | return (long)i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 951 | last -= n; |
| 952 | for (; i <= last; ++i) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 953 | if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 954 | return (long)i; |
| 955 | } |
| 956 | else { |
| 957 | int j; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 959 | if (n == 0 && i <= last) |
| 960 | return (long)last; |
| 961 | for (j = last-n; j >= i; --j) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 962 | if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 963 | return (long)j; |
| 964 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 965 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 966 | return -1; |
| 967 | } |
| 968 | |
| 969 | |
| 970 | static char find__doc__[] = |
| 971 | "S.find(sub [,start [,end]]) -> int\n\ |
| 972 | \n\ |
| 973 | Return the lowest index in S where substring sub is found,\n\ |
| 974 | such that sub is contained within s[start,end]. Optional\n\ |
| 975 | arguments start and end are interpreted as in slice notation.\n\ |
| 976 | \n\ |
| 977 | Return -1 on failure."; |
| 978 | |
| 979 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 980 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 981 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 982 | long result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 983 | if (result == -2) |
| 984 | return NULL; |
| 985 | return PyInt_FromLong(result); |
| 986 | } |
| 987 | |
| 988 | |
| 989 | static char index__doc__[] = |
| 990 | "S.index(sub [,start [,end]]) -> int\n\ |
| 991 | \n\ |
| 992 | Like S.find() but raise ValueError when the substring is not found."; |
| 993 | |
| 994 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 995 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 996 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 997 | long result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 998 | if (result == -2) |
| 999 | return NULL; |
| 1000 | if (result == -1) { |
| 1001 | PyErr_SetString(PyExc_ValueError, |
| 1002 | "substring not found in string.index"); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | return PyInt_FromLong(result); |
| 1006 | } |
| 1007 | |
| 1008 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1009 | static char rfind__doc__[] = |
| 1010 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 1011 | \n\ |
| 1012 | Return the highest index in S where substring sub is found,\n\ |
| 1013 | such that sub is contained within s[start,end]. Optional\n\ |
| 1014 | arguments start and end are interpreted as in slice notation.\n\ |
| 1015 | \n\ |
| 1016 | Return -1 on failure."; |
| 1017 | |
| 1018 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1019 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1020 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1021 | long result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1022 | if (result == -2) |
| 1023 | return NULL; |
| 1024 | return PyInt_FromLong(result); |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | static char rindex__doc__[] = |
| 1029 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 1030 | \n\ |
| 1031 | Like S.rfind() but raise ValueError when the substring is not found."; |
| 1032 | |
| 1033 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1034 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1035 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1036 | long result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1037 | if (result == -2) |
| 1038 | return NULL; |
| 1039 | if (result == -1) { |
| 1040 | PyErr_SetString(PyExc_ValueError, |
| 1041 | "substring not found in string.rindex"); |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | return PyInt_FromLong(result); |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1049 | do_strip(PyStringObject *self, PyObject *args, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1050 | { |
| 1051 | char *s = PyString_AS_STRING(self); |
| 1052 | int len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1053 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1054 | if (!PyArg_ParseTuple(args, ":strip")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1055 | return NULL; |
| 1056 | |
| 1057 | i = 0; |
| 1058 | if (striptype != RIGHTSTRIP) { |
| 1059 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 1060 | i++; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | j = len; |
| 1065 | if (striptype != LEFTSTRIP) { |
| 1066 | do { |
| 1067 | j--; |
| 1068 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 1069 | j++; |
| 1070 | } |
| 1071 | |
| 1072 | if (i == 0 && j == len) { |
| 1073 | Py_INCREF(self); |
| 1074 | return (PyObject*)self; |
| 1075 | } |
| 1076 | else |
| 1077 | return PyString_FromStringAndSize(s+i, j-i); |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | static char strip__doc__[] = |
| 1082 | "S.strip() -> string\n\ |
| 1083 | \n\ |
| 1084 | Return a copy of the string S with leading and trailing\n\ |
| 1085 | whitespace removed."; |
| 1086 | |
| 1087 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1088 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1089 | { |
| 1090 | return do_strip(self, args, BOTHSTRIP); |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | static char lstrip__doc__[] = |
| 1095 | "S.lstrip() -> string\n\ |
| 1096 | \n\ |
| 1097 | Return a copy of the string S with leading whitespace removed."; |
| 1098 | |
| 1099 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1100 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1101 | { |
| 1102 | return do_strip(self, args, LEFTSTRIP); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | static char rstrip__doc__[] = |
| 1107 | "S.rstrip() -> string\n\ |
| 1108 | \n\ |
| 1109 | Return a copy of the string S with trailing whitespace removed."; |
| 1110 | |
| 1111 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1112 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1113 | { |
| 1114 | return do_strip(self, args, RIGHTSTRIP); |
| 1115 | } |
| 1116 | |
| 1117 | |
| 1118 | static char lower__doc__[] = |
| 1119 | "S.lower() -> string\n\ |
| 1120 | \n\ |
| 1121 | Return a copy of the string S converted to lowercase."; |
| 1122 | |
| 1123 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1124 | string_lower(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1125 | { |
| 1126 | char *s = PyString_AS_STRING(self), *s_new; |
| 1127 | int i, n = PyString_GET_SIZE(self); |
| 1128 | PyObject *new; |
| 1129 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1130 | if (!PyArg_ParseTuple(args, ":lower")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1131 | return NULL; |
| 1132 | new = PyString_FromStringAndSize(NULL, n); |
| 1133 | if (new == NULL) |
| 1134 | return NULL; |
| 1135 | s_new = PyString_AsString(new); |
| 1136 | for (i = 0; i < n; i++) { |
| 1137 | int c = Py_CHARMASK(*s++); |
| 1138 | if (isupper(c)) { |
| 1139 | *s_new = tolower(c); |
| 1140 | } else |
| 1141 | *s_new = c; |
| 1142 | s_new++; |
| 1143 | } |
| 1144 | return new; |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | static char upper__doc__[] = |
| 1149 | "S.upper() -> string\n\ |
| 1150 | \n\ |
| 1151 | Return a copy of the string S converted to uppercase."; |
| 1152 | |
| 1153 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1154 | string_upper(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1155 | { |
| 1156 | char *s = PyString_AS_STRING(self), *s_new; |
| 1157 | int i, n = PyString_GET_SIZE(self); |
| 1158 | PyObject *new; |
| 1159 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1160 | if (!PyArg_ParseTuple(args, ":upper")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1161 | return NULL; |
| 1162 | new = PyString_FromStringAndSize(NULL, n); |
| 1163 | if (new == NULL) |
| 1164 | return NULL; |
| 1165 | s_new = PyString_AsString(new); |
| 1166 | for (i = 0; i < n; i++) { |
| 1167 | int c = Py_CHARMASK(*s++); |
| 1168 | if (islower(c)) { |
| 1169 | *s_new = toupper(c); |
| 1170 | } else |
| 1171 | *s_new = c; |
| 1172 | s_new++; |
| 1173 | } |
| 1174 | return new; |
| 1175 | } |
| 1176 | |
| 1177 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1178 | static char title__doc__[] = |
| 1179 | "S.title() -> string\n\ |
| 1180 | \n\ |
| 1181 | Return a titlecased version of S, i.e. words start with uppercase\n\ |
| 1182 | characters, all remaining cased characters have lowercase."; |
| 1183 | |
| 1184 | static PyObject* |
Fred Drake | 49312a5 | 2000-12-06 14:27:49 +0000 | [diff] [blame] | 1185 | string_title(PyStringObject *self, PyObject *args) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1186 | { |
| 1187 | char *s = PyString_AS_STRING(self), *s_new; |
| 1188 | int i, n = PyString_GET_SIZE(self); |
| 1189 | int previous_is_cased = 0; |
| 1190 | PyObject *new; |
| 1191 | |
| 1192 | if (!PyArg_ParseTuple(args, ":title")) |
| 1193 | return NULL; |
| 1194 | new = PyString_FromStringAndSize(NULL, n); |
| 1195 | if (new == NULL) |
| 1196 | return NULL; |
| 1197 | s_new = PyString_AsString(new); |
| 1198 | for (i = 0; i < n; i++) { |
| 1199 | int c = Py_CHARMASK(*s++); |
| 1200 | if (islower(c)) { |
| 1201 | if (!previous_is_cased) |
| 1202 | c = toupper(c); |
| 1203 | previous_is_cased = 1; |
| 1204 | } else if (isupper(c)) { |
| 1205 | if (previous_is_cased) |
| 1206 | c = tolower(c); |
| 1207 | previous_is_cased = 1; |
| 1208 | } else |
| 1209 | previous_is_cased = 0; |
| 1210 | *s_new++ = c; |
| 1211 | } |
| 1212 | return new; |
| 1213 | } |
| 1214 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1215 | static char capitalize__doc__[] = |
| 1216 | "S.capitalize() -> string\n\ |
| 1217 | \n\ |
| 1218 | Return a copy of the string S with only its first character\n\ |
| 1219 | capitalized."; |
| 1220 | |
| 1221 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1222 | string_capitalize(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1223 | { |
| 1224 | char *s = PyString_AS_STRING(self), *s_new; |
| 1225 | int i, n = PyString_GET_SIZE(self); |
| 1226 | PyObject *new; |
| 1227 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1228 | if (!PyArg_ParseTuple(args, ":capitalize")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1229 | return NULL; |
| 1230 | new = PyString_FromStringAndSize(NULL, n); |
| 1231 | if (new == NULL) |
| 1232 | return NULL; |
| 1233 | s_new = PyString_AsString(new); |
| 1234 | if (0 < n) { |
| 1235 | int c = Py_CHARMASK(*s++); |
| 1236 | if (islower(c)) |
| 1237 | *s_new = toupper(c); |
| 1238 | else |
| 1239 | *s_new = c; |
| 1240 | s_new++; |
| 1241 | } |
| 1242 | for (i = 1; i < n; i++) { |
| 1243 | int c = Py_CHARMASK(*s++); |
| 1244 | if (isupper(c)) |
| 1245 | *s_new = tolower(c); |
| 1246 | else |
| 1247 | *s_new = c; |
| 1248 | s_new++; |
| 1249 | } |
| 1250 | return new; |
| 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | static char count__doc__[] = |
| 1255 | "S.count(sub[, start[, end]]) -> int\n\ |
| 1256 | \n\ |
| 1257 | Return the number of occurrences of substring sub in string\n\ |
| 1258 | S[start:end]. Optional arguments start and end are\n\ |
| 1259 | interpreted as in slice notation."; |
| 1260 | |
| 1261 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1262 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1263 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1264 | const char *s = PyString_AS_STRING(self), *sub; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1265 | int len = PyString_GET_SIZE(self), n; |
| 1266 | int i = 0, last = INT_MAX; |
| 1267 | int m, r; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1268 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1269 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1270 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, |
| 1271 | _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1272 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1273 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1274 | if (PyString_Check(subobj)) { |
| 1275 | sub = PyString_AS_STRING(subobj); |
| 1276 | n = PyString_GET_SIZE(subobj); |
| 1277 | } |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1278 | else if (PyUnicode_Check(subobj)) { |
| 1279 | int count; |
| 1280 | count = PyUnicode_Count((PyObject *)self, subobj, i, last); |
| 1281 | if (count == -1) |
| 1282 | return NULL; |
| 1283 | else |
| 1284 | return PyInt_FromLong((long) count); |
| 1285 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1286 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1287 | return NULL; |
| 1288 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1289 | if (last > len) |
| 1290 | last = len; |
| 1291 | if (last < 0) |
| 1292 | last += len; |
| 1293 | if (last < 0) |
| 1294 | last = 0; |
| 1295 | if (i < 0) |
| 1296 | i += len; |
| 1297 | if (i < 0) |
| 1298 | i = 0; |
| 1299 | m = last + 1 - n; |
| 1300 | if (n == 0) |
| 1301 | return PyInt_FromLong((long) (m-i)); |
| 1302 | |
| 1303 | r = 0; |
| 1304 | while (i < m) { |
| 1305 | if (!memcmp(s+i, sub, n)) { |
| 1306 | r++; |
| 1307 | i += n; |
| 1308 | } else { |
| 1309 | i++; |
| 1310 | } |
| 1311 | } |
| 1312 | return PyInt_FromLong((long) r); |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | static char swapcase__doc__[] = |
| 1317 | "S.swapcase() -> string\n\ |
| 1318 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1319 | Return a copy of the string S with uppercase characters\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1320 | converted to lowercase and vice versa."; |
| 1321 | |
| 1322 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1323 | string_swapcase(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1324 | { |
| 1325 | char *s = PyString_AS_STRING(self), *s_new; |
| 1326 | int i, n = PyString_GET_SIZE(self); |
| 1327 | PyObject *new; |
| 1328 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1329 | if (!PyArg_ParseTuple(args, ":swapcase")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1330 | return NULL; |
| 1331 | new = PyString_FromStringAndSize(NULL, n); |
| 1332 | if (new == NULL) |
| 1333 | return NULL; |
| 1334 | s_new = PyString_AsString(new); |
| 1335 | for (i = 0; i < n; i++) { |
| 1336 | int c = Py_CHARMASK(*s++); |
| 1337 | if (islower(c)) { |
| 1338 | *s_new = toupper(c); |
| 1339 | } |
| 1340 | else if (isupper(c)) { |
| 1341 | *s_new = tolower(c); |
| 1342 | } |
| 1343 | else |
| 1344 | *s_new = c; |
| 1345 | s_new++; |
| 1346 | } |
| 1347 | return new; |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | static char translate__doc__[] = |
| 1352 | "S.translate(table [,deletechars]) -> string\n\ |
| 1353 | \n\ |
| 1354 | Return a copy of the string S, where all characters occurring\n\ |
| 1355 | in the optional argument deletechars are removed, and the\n\ |
| 1356 | remaining characters have been mapped through the given\n\ |
| 1357 | translation table, which must be a string of length 256."; |
| 1358 | |
| 1359 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1360 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1361 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1362 | register char *input, *output; |
| 1363 | register const char *table; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1364 | register int i, c, changed = 0; |
| 1365 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1366 | const char *table1, *output_start, *del_table=NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1367 | int inlen, tablen, dellen = 0; |
| 1368 | PyObject *result; |
| 1369 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1370 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1371 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1372 | if (!PyArg_ParseTuple(args, "O|O:translate", |
| 1373 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1374 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1375 | |
| 1376 | if (PyString_Check(tableobj)) { |
| 1377 | table1 = PyString_AS_STRING(tableobj); |
| 1378 | tablen = PyString_GET_SIZE(tableobj); |
| 1379 | } |
| 1380 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1381 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1382 | parameter; instead a mapping to None will cause characters |
| 1383 | to be deleted. */ |
| 1384 | if (delobj != NULL) { |
| 1385 | PyErr_SetString(PyExc_TypeError, |
| 1386 | "deletions are implemented differently for unicode"); |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 1390 | } |
| 1391 | else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1392 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1393 | |
| 1394 | if (delobj != NULL) { |
| 1395 | if (PyString_Check(delobj)) { |
| 1396 | del_table = PyString_AS_STRING(delobj); |
| 1397 | dellen = PyString_GET_SIZE(delobj); |
| 1398 | } |
| 1399 | else if (PyUnicode_Check(delobj)) { |
| 1400 | PyErr_SetString(PyExc_TypeError, |
| 1401 | "deletions are implemented differently for unicode"); |
| 1402 | return NULL; |
| 1403 | } |
| 1404 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 1405 | return NULL; |
| 1406 | |
| 1407 | if (tablen != 256) { |
| 1408 | PyErr_SetString(PyExc_ValueError, |
| 1409 | "translation table must be 256 characters long"); |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | } |
| 1413 | else { |
| 1414 | del_table = NULL; |
| 1415 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | table = table1; |
| 1419 | inlen = PyString_Size(input_obj); |
| 1420 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 1421 | if (result == NULL) |
| 1422 | return NULL; |
| 1423 | output_start = output = PyString_AsString(result); |
| 1424 | input = PyString_AsString(input_obj); |
| 1425 | |
| 1426 | if (dellen == 0) { |
| 1427 | /* If no deletions are required, use faster code */ |
| 1428 | for (i = inlen; --i >= 0; ) { |
| 1429 | c = Py_CHARMASK(*input++); |
| 1430 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1431 | changed = 1; |
| 1432 | } |
| 1433 | if (changed) |
| 1434 | return result; |
| 1435 | Py_DECREF(result); |
| 1436 | Py_INCREF(input_obj); |
| 1437 | return input_obj; |
| 1438 | } |
| 1439 | |
| 1440 | for (i = 0; i < 256; i++) |
| 1441 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1442 | |
| 1443 | for (i = 0; i < dellen; i++) |
| 1444 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 1445 | |
| 1446 | for (i = inlen; --i >= 0; ) { |
| 1447 | c = Py_CHARMASK(*input++); |
| 1448 | if (trans_table[c] != -1) |
| 1449 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1450 | continue; |
| 1451 | changed = 1; |
| 1452 | } |
| 1453 | if (!changed) { |
| 1454 | Py_DECREF(result); |
| 1455 | Py_INCREF(input_obj); |
| 1456 | return input_obj; |
| 1457 | } |
| 1458 | /* Fix the size of the resulting string */ |
| 1459 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 1460 | return NULL; |
| 1461 | return result; |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 1466 | |
| 1467 | /* |
| 1468 | mymemfind |
| 1469 | |
| 1470 | strstr replacement for arbitrary blocks of memory. |
| 1471 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1472 | 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] | 1473 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 1474 | found, or -1 if not found. If len of PAT is greater than length of |
| 1475 | MEM, the function returns -1. |
| 1476 | */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1477 | static int |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1478 | mymemfind(const char *mem, int len, const char *pat, int pat_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1479 | { |
| 1480 | register int ii; |
| 1481 | |
| 1482 | /* pattern can not occur in the last pat_len-1 chars */ |
| 1483 | len -= pat_len; |
| 1484 | |
| 1485 | for (ii = 0; ii <= len; ii++) { |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1486 | if (mem[ii] == pat[0] && memcmp(&mem[ii], pat, pat_len) == 0) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1487 | return ii; |
| 1488 | } |
| 1489 | } |
| 1490 | return -1; |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | mymemcnt |
| 1495 | |
| 1496 | Return the number of distinct times PAT is found in MEM. |
| 1497 | meaning mem=1111 and pat==11 returns 2. |
| 1498 | mem=11111 and pat==11 also return 2. |
| 1499 | */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1500 | static int |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1501 | mymemcnt(const char *mem, int len, const char *pat, int pat_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1502 | { |
| 1503 | register int offset = 0; |
| 1504 | int nfound = 0; |
| 1505 | |
| 1506 | while (len >= 0) { |
| 1507 | offset = mymemfind(mem, len, pat, pat_len); |
| 1508 | if (offset == -1) |
| 1509 | break; |
| 1510 | mem += offset + pat_len; |
| 1511 | len -= offset + pat_len; |
| 1512 | nfound++; |
| 1513 | } |
| 1514 | return nfound; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | mymemreplace |
| 1519 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1520 | 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] | 1521 | replaced with SUB. |
| 1522 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1523 | 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] | 1524 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 1525 | string is allocated here and returned. |
| 1526 | |
| 1527 | on return, out_len is: |
| 1528 | the length of output string, or |
| 1529 | -1 if the input string is returned, or |
| 1530 | unchanged if an error occurs (no memory). |
| 1531 | |
| 1532 | return value is: |
| 1533 | the new string allocated locally, or |
| 1534 | NULL if an error occurred. |
| 1535 | */ |
| 1536 | static char * |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1537 | mymemreplace(const char *str, int len, /* input string */ |
| 1538 | const char *pat, int pat_len, /* pattern string to find */ |
| 1539 | const char *sub, int sub_len, /* substitution string */ |
| 1540 | int count, /* number of replacements */ |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1541 | int *out_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1542 | { |
| 1543 | char *out_s; |
| 1544 | char *new_s; |
| 1545 | int nfound, offset, new_len; |
| 1546 | |
| 1547 | if (len == 0 || pat_len > len) |
| 1548 | goto return_same; |
| 1549 | |
| 1550 | /* find length of output string */ |
| 1551 | nfound = mymemcnt(str, len, pat, pat_len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1552 | if (count < 0) |
| 1553 | count = INT_MAX; |
| 1554 | else if (nfound > count) |
| 1555 | nfound = count; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1556 | if (nfound == 0) |
| 1557 | goto return_same; |
| 1558 | new_len = len + nfound*(sub_len - pat_len); |
| 1559 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1560 | new_s = (char *)PyMem_MALLOC(new_len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1561 | if (new_s == NULL) return NULL; |
| 1562 | |
| 1563 | *out_len = new_len; |
| 1564 | out_s = new_s; |
| 1565 | |
| 1566 | while (len > 0) { |
| 1567 | /* find index of next instance of pattern */ |
| 1568 | offset = mymemfind(str, len, pat, pat_len); |
| 1569 | /* if not found, break out of loop */ |
| 1570 | if (offset == -1) break; |
| 1571 | |
| 1572 | /* copy non matching part of input string */ |
| 1573 | memcpy(new_s, str, offset); /* copy part of str before pat */ |
| 1574 | str += offset + pat_len; /* move str past pattern */ |
| 1575 | len -= offset + pat_len; /* reduce length of str remaining */ |
| 1576 | |
| 1577 | /* copy substitute into the output string */ |
| 1578 | new_s += offset; /* move new_s to dest for sub string */ |
| 1579 | memcpy(new_s, sub, sub_len); /* copy substring into new_s */ |
| 1580 | new_s += sub_len; /* offset new_s past sub string */ |
| 1581 | |
| 1582 | /* break when we've done count replacements */ |
| 1583 | if (--count == 0) break; |
| 1584 | } |
| 1585 | /* copy any remaining values into output string */ |
| 1586 | if (len > 0) |
| 1587 | memcpy(new_s, str, len); |
| 1588 | return out_s; |
| 1589 | |
| 1590 | return_same: |
| 1591 | *out_len = -1; |
Tim Peters | c2e7da9 | 2000-07-09 08:02:21 +0000 | [diff] [blame] | 1592 | return (char*)str; /* have to cast away constness here */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | static char replace__doc__[] = |
| 1597 | "S.replace (old, new[, maxsplit]) -> string\n\ |
| 1598 | \n\ |
| 1599 | Return a copy of string S with all occurrences of substring\n\ |
| 1600 | old replaced by new. If the optional argument maxsplit is\n\ |
| 1601 | given, only the first maxsplit occurrences are replaced."; |
| 1602 | |
| 1603 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1604 | string_replace(PyStringObject *self, PyObject *args) |
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 | const char *str = PyString_AS_STRING(self), *sub, *repl; |
| 1607 | char *new_s; |
| 1608 | int len = PyString_GET_SIZE(self), sub_len, repl_len, out_len; |
| 1609 | int count = -1; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1610 | PyObject *new; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1611 | PyObject *subobj, *replobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1612 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1613 | if (!PyArg_ParseTuple(args, "OO|i:replace", |
| 1614 | &subobj, &replobj, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1615 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1616 | |
| 1617 | if (PyString_Check(subobj)) { |
| 1618 | sub = PyString_AS_STRING(subobj); |
| 1619 | sub_len = PyString_GET_SIZE(subobj); |
| 1620 | } |
| 1621 | else if (PyUnicode_Check(subobj)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1622 | return PyUnicode_Replace((PyObject *)self, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1623 | subobj, replobj, count); |
| 1624 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
| 1625 | return NULL; |
| 1626 | |
| 1627 | if (PyString_Check(replobj)) { |
| 1628 | repl = PyString_AS_STRING(replobj); |
| 1629 | repl_len = PyString_GET_SIZE(replobj); |
| 1630 | } |
| 1631 | else if (PyUnicode_Check(replobj)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1632 | return PyUnicode_Replace((PyObject *)self, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1633 | subobj, replobj, count); |
| 1634 | else if (PyObject_AsCharBuffer(replobj, &repl, &repl_len)) |
| 1635 | return NULL; |
| 1636 | |
Guido van Rossum | 96a45ad | 2000-03-13 15:56:08 +0000 | [diff] [blame] | 1637 | if (sub_len <= 0) { |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1638 | PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1639 | return NULL; |
| 1640 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1641 | 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] | 1642 | if (new_s == NULL) { |
| 1643 | PyErr_NoMemory(); |
| 1644 | return NULL; |
| 1645 | } |
| 1646 | if (out_len == -1) { |
| 1647 | /* we're returning another reference to self */ |
| 1648 | new = (PyObject*)self; |
| 1649 | Py_INCREF(new); |
| 1650 | } |
| 1651 | else { |
| 1652 | new = PyString_FromStringAndSize(new_s, out_len); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1653 | PyMem_FREE(new_s); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1654 | } |
| 1655 | return new; |
| 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | static char startswith__doc__[] = |
| 1660 | "S.startswith(prefix[, start[, end]]) -> int\n\ |
| 1661 | \n\ |
| 1662 | Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ |
| 1663 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1664 | comparing S at that position."; |
| 1665 | |
| 1666 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1667 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1668 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1669 | const char* str = PyString_AS_STRING(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1670 | int len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1671 | const char* prefix; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1672 | int plen; |
| 1673 | int start = 0; |
| 1674 | int end = -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1675 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1676 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1677 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 1678 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1679 | return NULL; |
| 1680 | if (PyString_Check(subobj)) { |
| 1681 | prefix = PyString_AS_STRING(subobj); |
| 1682 | plen = PyString_GET_SIZE(subobj); |
| 1683 | } |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1684 | else if (PyUnicode_Check(subobj)) { |
| 1685 | int rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1686 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1687 | subobj, start, end, -1); |
| 1688 | if (rc == -1) |
| 1689 | return NULL; |
| 1690 | else |
| 1691 | return PyInt_FromLong((long) rc); |
| 1692 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1693 | else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1694 | return NULL; |
| 1695 | |
| 1696 | /* adopt Java semantics for index out of range. it is legal for |
| 1697 | * offset to be == plen, but this only returns true if prefix is |
| 1698 | * the empty string. |
| 1699 | */ |
| 1700 | if (start < 0 || start+plen > len) |
| 1701 | return PyInt_FromLong(0); |
| 1702 | |
| 1703 | if (!memcmp(str+start, prefix, plen)) { |
| 1704 | /* did the match end after the specified end? */ |
| 1705 | if (end < 0) |
| 1706 | return PyInt_FromLong(1); |
| 1707 | else if (end - start < plen) |
| 1708 | return PyInt_FromLong(0); |
| 1709 | else |
| 1710 | return PyInt_FromLong(1); |
| 1711 | } |
| 1712 | else return PyInt_FromLong(0); |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | static char endswith__doc__[] = |
| 1717 | "S.endswith(suffix[, start[, end]]) -> int\n\ |
| 1718 | \n\ |
| 1719 | Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ |
| 1720 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1721 | comparing S at that position."; |
| 1722 | |
| 1723 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1724 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1725 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1726 | const char* str = PyString_AS_STRING(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1727 | int len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1728 | const char* suffix; |
| 1729 | int slen; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1730 | int start = 0; |
| 1731 | int end = -1; |
| 1732 | int lower, upper; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1733 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1734 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1735 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 1736 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1737 | return NULL; |
| 1738 | if (PyString_Check(subobj)) { |
| 1739 | suffix = PyString_AS_STRING(subobj); |
| 1740 | slen = PyString_GET_SIZE(subobj); |
| 1741 | } |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1742 | else if (PyUnicode_Check(subobj)) { |
| 1743 | int rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1744 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1745 | subobj, start, end, +1); |
| 1746 | if (rc == -1) |
| 1747 | return NULL; |
| 1748 | else |
| 1749 | return PyInt_FromLong((long) rc); |
| 1750 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1751 | else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1752 | return NULL; |
| 1753 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1754 | if (start < 0 || start > len || slen > len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1755 | return PyInt_FromLong(0); |
| 1756 | |
| 1757 | upper = (end >= 0 && end <= len) ? end : len; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1758 | lower = (upper - slen) > start ? (upper - slen) : start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1759 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1760 | if (upper-lower >= slen && !memcmp(str+lower, suffix, slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1761 | return PyInt_FromLong(1); |
| 1762 | else return PyInt_FromLong(0); |
| 1763 | } |
| 1764 | |
| 1765 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 1766 | static char encode__doc__[] = |
| 1767 | "S.encode([encoding[,errors]]) -> string\n\ |
| 1768 | \n\ |
| 1769 | Return an encoded string version of S. Default encoding is the current\n\ |
| 1770 | default string encoding. errors may be given to set a different error\n\ |
| 1771 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 1772 | a ValueError. Other possible values are 'ignore' and 'replace'."; |
| 1773 | |
| 1774 | static PyObject * |
| 1775 | string_encode(PyStringObject *self, PyObject *args) |
| 1776 | { |
| 1777 | char *encoding = NULL; |
| 1778 | char *errors = NULL; |
| 1779 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 1780 | return NULL; |
| 1781 | return PyString_AsEncodedString((PyObject *)self, encoding, errors); |
| 1782 | } |
| 1783 | |
| 1784 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1785 | static char expandtabs__doc__[] = |
| 1786 | "S.expandtabs([tabsize]) -> string\n\ |
| 1787 | \n\ |
| 1788 | Return a copy of S where all tab characters are expanded using spaces.\n\ |
| 1789 | If tabsize is not given, a tab size of 8 characters is assumed."; |
| 1790 | |
| 1791 | static PyObject* |
| 1792 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 1793 | { |
| 1794 | const char *e, *p; |
| 1795 | char *q; |
| 1796 | int i, j; |
| 1797 | PyObject *u; |
| 1798 | int tabsize = 8; |
| 1799 | |
| 1800 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 1801 | return NULL; |
| 1802 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1803 | /* First pass: determine size of output string */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1804 | i = j = 0; |
| 1805 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 1806 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 1807 | if (*p == '\t') { |
| 1808 | if (tabsize > 0) |
| 1809 | j += tabsize - (j % tabsize); |
| 1810 | } |
| 1811 | else { |
| 1812 | j++; |
| 1813 | if (*p == '\n' || *p == '\r') { |
| 1814 | i += j; |
| 1815 | j = 0; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | /* Second pass: create output string and fill it */ |
| 1820 | u = PyString_FromStringAndSize(NULL, i + j); |
| 1821 | if (!u) |
| 1822 | return NULL; |
| 1823 | |
| 1824 | j = 0; |
| 1825 | q = PyString_AS_STRING(u); |
| 1826 | |
| 1827 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 1828 | if (*p == '\t') { |
| 1829 | if (tabsize > 0) { |
| 1830 | i = tabsize - (j % tabsize); |
| 1831 | j += i; |
| 1832 | while (i--) |
| 1833 | *q++ = ' '; |
| 1834 | } |
| 1835 | } |
| 1836 | else { |
| 1837 | j++; |
| 1838 | *q++ = *p; |
| 1839 | if (*p == '\n' || *p == '\r') |
| 1840 | j = 0; |
| 1841 | } |
| 1842 | |
| 1843 | return u; |
| 1844 | } |
| 1845 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1846 | static |
| 1847 | PyObject *pad(PyStringObject *self, |
| 1848 | int left, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1849 | int right, |
| 1850 | char fill) |
| 1851 | { |
| 1852 | PyObject *u; |
| 1853 | |
| 1854 | if (left < 0) |
| 1855 | left = 0; |
| 1856 | if (right < 0) |
| 1857 | right = 0; |
| 1858 | |
| 1859 | if (left == 0 && right == 0) { |
| 1860 | Py_INCREF(self); |
| 1861 | return (PyObject *)self; |
| 1862 | } |
| 1863 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1864 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1865 | left + PyString_GET_SIZE(self) + right); |
| 1866 | if (u) { |
| 1867 | if (left) |
| 1868 | memset(PyString_AS_STRING(u), fill, left); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1869 | memcpy(PyString_AS_STRING(u) + left, |
| 1870 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1871 | PyString_GET_SIZE(self)); |
| 1872 | if (right) |
| 1873 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 1874 | fill, right); |
| 1875 | } |
| 1876 | |
| 1877 | return u; |
| 1878 | } |
| 1879 | |
| 1880 | static char ljust__doc__[] = |
| 1881 | "S.ljust(width) -> string\n\ |
| 1882 | \n\ |
| 1883 | Return S left justified in a string of length width. Padding is\n\ |
| 1884 | done using spaces."; |
| 1885 | |
| 1886 | static PyObject * |
| 1887 | string_ljust(PyStringObject *self, PyObject *args) |
| 1888 | { |
| 1889 | int width; |
| 1890 | if (!PyArg_ParseTuple(args, "i:ljust", &width)) |
| 1891 | return NULL; |
| 1892 | |
| 1893 | if (PyString_GET_SIZE(self) >= width) { |
| 1894 | Py_INCREF(self); |
| 1895 | return (PyObject*) self; |
| 1896 | } |
| 1897 | |
| 1898 | return pad(self, 0, width - PyString_GET_SIZE(self), ' '); |
| 1899 | } |
| 1900 | |
| 1901 | |
| 1902 | static char rjust__doc__[] = |
| 1903 | "S.rjust(width) -> string\n\ |
| 1904 | \n\ |
| 1905 | Return S right justified in a string of length width. Padding is\n\ |
| 1906 | done using spaces."; |
| 1907 | |
| 1908 | static PyObject * |
| 1909 | string_rjust(PyStringObject *self, PyObject *args) |
| 1910 | { |
| 1911 | int width; |
| 1912 | if (!PyArg_ParseTuple(args, "i:rjust", &width)) |
| 1913 | return NULL; |
| 1914 | |
| 1915 | if (PyString_GET_SIZE(self) >= width) { |
| 1916 | Py_INCREF(self); |
| 1917 | return (PyObject*) self; |
| 1918 | } |
| 1919 | |
| 1920 | return pad(self, width - PyString_GET_SIZE(self), 0, ' '); |
| 1921 | } |
| 1922 | |
| 1923 | |
| 1924 | static char center__doc__[] = |
| 1925 | "S.center(width) -> string\n\ |
| 1926 | \n\ |
| 1927 | Return S centered in a string of length width. Padding is done\n\ |
| 1928 | using spaces."; |
| 1929 | |
| 1930 | static PyObject * |
| 1931 | string_center(PyStringObject *self, PyObject *args) |
| 1932 | { |
| 1933 | int marg, left; |
| 1934 | int width; |
| 1935 | |
| 1936 | if (!PyArg_ParseTuple(args, "i:center", &width)) |
| 1937 | return NULL; |
| 1938 | |
| 1939 | if (PyString_GET_SIZE(self) >= width) { |
| 1940 | Py_INCREF(self); |
| 1941 | return (PyObject*) self; |
| 1942 | } |
| 1943 | |
| 1944 | marg = width - PyString_GET_SIZE(self); |
| 1945 | left = marg / 2 + (marg & width & 1); |
| 1946 | |
| 1947 | return pad(self, left, marg - left, ' '); |
| 1948 | } |
| 1949 | |
| 1950 | #if 0 |
| 1951 | static char zfill__doc__[] = |
| 1952 | "S.zfill(width) -> string\n\ |
| 1953 | \n\ |
| 1954 | Pad a numeric string x with zeros on the left, to fill a field\n\ |
| 1955 | of the specified width. The string x is never truncated."; |
| 1956 | |
| 1957 | static PyObject * |
| 1958 | string_zfill(PyStringObject *self, PyObject *args) |
| 1959 | { |
| 1960 | int fill; |
| 1961 | PyObject *u; |
| 1962 | char *str; |
| 1963 | |
| 1964 | int width; |
| 1965 | if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
| 1966 | return NULL; |
| 1967 | |
| 1968 | if (PyString_GET_SIZE(self) >= width) { |
| 1969 | Py_INCREF(self); |
| 1970 | return (PyObject*) self; |
| 1971 | } |
| 1972 | |
| 1973 | fill = width - PyString_GET_SIZE(self); |
| 1974 | |
| 1975 | u = pad(self, fill, 0, '0'); |
| 1976 | if (u == NULL) |
| 1977 | return NULL; |
| 1978 | |
| 1979 | str = PyString_AS_STRING(u); |
| 1980 | if (str[fill] == '+' || str[fill] == '-') { |
| 1981 | /* move sign to beginning of string */ |
| 1982 | str[0] = str[fill]; |
| 1983 | str[fill] = '0'; |
| 1984 | } |
| 1985 | |
| 1986 | return u; |
| 1987 | } |
| 1988 | #endif |
| 1989 | |
| 1990 | static char isspace__doc__[] = |
| 1991 | "S.isspace() -> int\n\ |
| 1992 | \n\ |
| 1993 | Return 1 if there are only whitespace characters in S,\n\ |
| 1994 | 0 otherwise."; |
| 1995 | |
| 1996 | static PyObject* |
| 1997 | string_isspace(PyStringObject *self, PyObject *args) |
| 1998 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1999 | register const unsigned char *p |
| 2000 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2001 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2002 | |
| 2003 | if (!PyArg_NoArgs(args)) |
| 2004 | return NULL; |
| 2005 | |
| 2006 | /* Shortcut for single character strings */ |
| 2007 | if (PyString_GET_SIZE(self) == 1 && |
| 2008 | isspace(*p)) |
| 2009 | return PyInt_FromLong(1); |
| 2010 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2011 | /* Special case for empty strings */ |
| 2012 | if (PyString_GET_SIZE(self) == 0) |
| 2013 | return PyInt_FromLong(0); |
| 2014 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2015 | e = p + PyString_GET_SIZE(self); |
| 2016 | for (; p < e; p++) { |
| 2017 | if (!isspace(*p)) |
| 2018 | return PyInt_FromLong(0); |
| 2019 | } |
| 2020 | return PyInt_FromLong(1); |
| 2021 | } |
| 2022 | |
| 2023 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2024 | static char isalpha__doc__[] = |
| 2025 | "S.isalpha() -> int\n\ |
| 2026 | \n\ |
| 2027 | Return 1 if all characters in S are alphabetic\n\ |
| 2028 | and there is at least one character in S, 0 otherwise."; |
| 2029 | |
| 2030 | static PyObject* |
Fred Drake | 49312a5 | 2000-12-06 14:27:49 +0000 | [diff] [blame] | 2031 | string_isalpha(PyStringObject *self, PyObject *args) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2032 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2033 | register const unsigned char *p |
| 2034 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2035 | register const unsigned char *e; |
| 2036 | |
| 2037 | if (!PyArg_NoArgs(args)) |
| 2038 | return NULL; |
| 2039 | |
| 2040 | /* Shortcut for single character strings */ |
| 2041 | if (PyString_GET_SIZE(self) == 1 && |
| 2042 | isalpha(*p)) |
| 2043 | return PyInt_FromLong(1); |
| 2044 | |
| 2045 | /* Special case for empty strings */ |
| 2046 | if (PyString_GET_SIZE(self) == 0) |
| 2047 | return PyInt_FromLong(0); |
| 2048 | |
| 2049 | e = p + PyString_GET_SIZE(self); |
| 2050 | for (; p < e; p++) { |
| 2051 | if (!isalpha(*p)) |
| 2052 | return PyInt_FromLong(0); |
| 2053 | } |
| 2054 | return PyInt_FromLong(1); |
| 2055 | } |
| 2056 | |
| 2057 | |
| 2058 | static char isalnum__doc__[] = |
| 2059 | "S.isalnum() -> int\n\ |
| 2060 | \n\ |
| 2061 | Return 1 if all characters in S are alphanumeric\n\ |
| 2062 | and there is at least one character in S, 0 otherwise."; |
| 2063 | |
| 2064 | static PyObject* |
Fred Drake | 49312a5 | 2000-12-06 14:27:49 +0000 | [diff] [blame] | 2065 | string_isalnum(PyStringObject *self, PyObject *args) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2066 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2067 | register const unsigned char *p |
| 2068 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2069 | register const unsigned char *e; |
| 2070 | |
| 2071 | if (!PyArg_NoArgs(args)) |
| 2072 | return NULL; |
| 2073 | |
| 2074 | /* Shortcut for single character strings */ |
| 2075 | if (PyString_GET_SIZE(self) == 1 && |
| 2076 | isalnum(*p)) |
| 2077 | return PyInt_FromLong(1); |
| 2078 | |
| 2079 | /* Special case for empty strings */ |
| 2080 | if (PyString_GET_SIZE(self) == 0) |
| 2081 | return PyInt_FromLong(0); |
| 2082 | |
| 2083 | e = p + PyString_GET_SIZE(self); |
| 2084 | for (; p < e; p++) { |
| 2085 | if (!isalnum(*p)) |
| 2086 | return PyInt_FromLong(0); |
| 2087 | } |
| 2088 | return PyInt_FromLong(1); |
| 2089 | } |
| 2090 | |
| 2091 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2092 | static char isdigit__doc__[] = |
| 2093 | "S.isdigit() -> int\n\ |
| 2094 | \n\ |
| 2095 | Return 1 if there are only digit characters in S,\n\ |
| 2096 | 0 otherwise."; |
| 2097 | |
| 2098 | static PyObject* |
| 2099 | string_isdigit(PyStringObject *self, PyObject *args) |
| 2100 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2101 | register const unsigned char *p |
| 2102 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2103 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2104 | |
| 2105 | if (!PyArg_NoArgs(args)) |
| 2106 | return NULL; |
| 2107 | |
| 2108 | /* Shortcut for single character strings */ |
| 2109 | if (PyString_GET_SIZE(self) == 1 && |
| 2110 | isdigit(*p)) |
| 2111 | return PyInt_FromLong(1); |
| 2112 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2113 | /* Special case for empty strings */ |
| 2114 | if (PyString_GET_SIZE(self) == 0) |
| 2115 | return PyInt_FromLong(0); |
| 2116 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2117 | e = p + PyString_GET_SIZE(self); |
| 2118 | for (; p < e; p++) { |
| 2119 | if (!isdigit(*p)) |
| 2120 | return PyInt_FromLong(0); |
| 2121 | } |
| 2122 | return PyInt_FromLong(1); |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | static char islower__doc__[] = |
| 2127 | "S.islower() -> int\n\ |
| 2128 | \n\ |
| 2129 | Return 1 if all cased characters in S are lowercase and there is\n\ |
| 2130 | at least one cased character in S, 0 otherwise."; |
| 2131 | |
| 2132 | static PyObject* |
| 2133 | string_islower(PyStringObject *self, PyObject *args) |
| 2134 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2135 | register const unsigned char *p |
| 2136 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2137 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2138 | int cased; |
| 2139 | |
| 2140 | if (!PyArg_NoArgs(args)) |
| 2141 | return NULL; |
| 2142 | |
| 2143 | /* Shortcut for single character strings */ |
| 2144 | if (PyString_GET_SIZE(self) == 1) |
| 2145 | return PyInt_FromLong(islower(*p) != 0); |
| 2146 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2147 | /* Special case for empty strings */ |
| 2148 | if (PyString_GET_SIZE(self) == 0) |
| 2149 | return PyInt_FromLong(0); |
| 2150 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2151 | e = p + PyString_GET_SIZE(self); |
| 2152 | cased = 0; |
| 2153 | for (; p < e; p++) { |
| 2154 | if (isupper(*p)) |
| 2155 | return PyInt_FromLong(0); |
| 2156 | else if (!cased && islower(*p)) |
| 2157 | cased = 1; |
| 2158 | } |
| 2159 | return PyInt_FromLong(cased); |
| 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | static char isupper__doc__[] = |
| 2164 | "S.isupper() -> int\n\ |
| 2165 | \n\ |
| 2166 | Return 1 if all cased characters in S are uppercase and there is\n\ |
| 2167 | at least one cased character in S, 0 otherwise."; |
| 2168 | |
| 2169 | static PyObject* |
| 2170 | string_isupper(PyStringObject *self, PyObject *args) |
| 2171 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2172 | register const unsigned char *p |
| 2173 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2174 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2175 | int cased; |
| 2176 | |
| 2177 | if (!PyArg_NoArgs(args)) |
| 2178 | return NULL; |
| 2179 | |
| 2180 | /* Shortcut for single character strings */ |
| 2181 | if (PyString_GET_SIZE(self) == 1) |
| 2182 | return PyInt_FromLong(isupper(*p) != 0); |
| 2183 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2184 | /* Special case for empty strings */ |
| 2185 | if (PyString_GET_SIZE(self) == 0) |
| 2186 | return PyInt_FromLong(0); |
| 2187 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2188 | e = p + PyString_GET_SIZE(self); |
| 2189 | cased = 0; |
| 2190 | for (; p < e; p++) { |
| 2191 | if (islower(*p)) |
| 2192 | return PyInt_FromLong(0); |
| 2193 | else if (!cased && isupper(*p)) |
| 2194 | cased = 1; |
| 2195 | } |
| 2196 | return PyInt_FromLong(cased); |
| 2197 | } |
| 2198 | |
| 2199 | |
| 2200 | static char istitle__doc__[] = |
| 2201 | "S.istitle() -> int\n\ |
| 2202 | \n\ |
| 2203 | Return 1 if S is a titlecased string, i.e. uppercase characters\n\ |
| 2204 | may only follow uncased characters and lowercase characters only cased\n\ |
| 2205 | ones. Return 0 otherwise."; |
| 2206 | |
| 2207 | static PyObject* |
| 2208 | string_istitle(PyStringObject *self, PyObject *args) |
| 2209 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2210 | register const unsigned char *p |
| 2211 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2212 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2213 | int cased, previous_is_cased; |
| 2214 | |
| 2215 | if (!PyArg_NoArgs(args)) |
| 2216 | return NULL; |
| 2217 | |
| 2218 | /* Shortcut for single character strings */ |
| 2219 | if (PyString_GET_SIZE(self) == 1) |
| 2220 | return PyInt_FromLong(isupper(*p) != 0); |
| 2221 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 2222 | /* Special case for empty strings */ |
| 2223 | if (PyString_GET_SIZE(self) == 0) |
| 2224 | return PyInt_FromLong(0); |
| 2225 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2226 | e = p + PyString_GET_SIZE(self); |
| 2227 | cased = 0; |
| 2228 | previous_is_cased = 0; |
| 2229 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 2230 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2231 | |
| 2232 | if (isupper(ch)) { |
| 2233 | if (previous_is_cased) |
| 2234 | return PyInt_FromLong(0); |
| 2235 | previous_is_cased = 1; |
| 2236 | cased = 1; |
| 2237 | } |
| 2238 | else if (islower(ch)) { |
| 2239 | if (!previous_is_cased) |
| 2240 | return PyInt_FromLong(0); |
| 2241 | previous_is_cased = 1; |
| 2242 | cased = 1; |
| 2243 | } |
| 2244 | else |
| 2245 | previous_is_cased = 0; |
| 2246 | } |
| 2247 | return PyInt_FromLong(cased); |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | static char splitlines__doc__[] = |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2252 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2253 | \n\ |
| 2254 | 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] | 2255 | Line breaks are not included in the resulting list unless keepends\n\ |
| 2256 | is given and true."; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2257 | |
| 2258 | #define SPLIT_APPEND(data, left, right) \ |
| 2259 | str = PyString_FromStringAndSize(data + left, right - left); \ |
| 2260 | if (!str) \ |
| 2261 | goto onError; \ |
| 2262 | if (PyList_Append(list, str)) { \ |
| 2263 | Py_DECREF(str); \ |
| 2264 | goto onError; \ |
| 2265 | } \ |
| 2266 | else \ |
| 2267 | Py_DECREF(str); |
| 2268 | |
| 2269 | static PyObject* |
| 2270 | string_splitlines(PyStringObject *self, PyObject *args) |
| 2271 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2272 | register int i; |
| 2273 | register int j; |
| 2274 | int len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2275 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2276 | PyObject *list; |
| 2277 | PyObject *str; |
| 2278 | char *data; |
| 2279 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2280 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2281 | return NULL; |
| 2282 | |
| 2283 | data = PyString_AS_STRING(self); |
| 2284 | len = PyString_GET_SIZE(self); |
| 2285 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2286 | list = PyList_New(0); |
| 2287 | if (!list) |
| 2288 | goto onError; |
| 2289 | |
| 2290 | for (i = j = 0; i < len; ) { |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2291 | int eol; |
| 2292 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2293 | /* Find a line and append it */ |
| 2294 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 2295 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2296 | |
| 2297 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2298 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2299 | if (i < len) { |
| 2300 | if (data[i] == '\r' && i + 1 < len && |
| 2301 | data[i+1] == '\n') |
| 2302 | i += 2; |
| 2303 | else |
| 2304 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2305 | if (keepends) |
| 2306 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2307 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2308 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2309 | j = i; |
| 2310 | } |
| 2311 | if (j < len) { |
| 2312 | SPLIT_APPEND(data, j, len); |
| 2313 | } |
| 2314 | |
| 2315 | return list; |
| 2316 | |
| 2317 | onError: |
| 2318 | Py_DECREF(list); |
| 2319 | return NULL; |
| 2320 | } |
| 2321 | |
| 2322 | #undef SPLIT_APPEND |
| 2323 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2324 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2325 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2326 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2327 | /* Counterparts of the obsolete stropmodule functions; except |
| 2328 | string.maketrans(). */ |
| 2329 | {"join", (PyCFunction)string_join, 1, join__doc__}, |
| 2330 | {"split", (PyCFunction)string_split, 1, split__doc__}, |
| 2331 | {"lower", (PyCFunction)string_lower, 1, lower__doc__}, |
| 2332 | {"upper", (PyCFunction)string_upper, 1, upper__doc__}, |
| 2333 | {"islower", (PyCFunction)string_islower, 0, islower__doc__}, |
| 2334 | {"isupper", (PyCFunction)string_isupper, 0, isupper__doc__}, |
| 2335 | {"isspace", (PyCFunction)string_isspace, 0, isspace__doc__}, |
| 2336 | {"isdigit", (PyCFunction)string_isdigit, 0, isdigit__doc__}, |
| 2337 | {"istitle", (PyCFunction)string_istitle, 0, istitle__doc__}, |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 2338 | {"isalpha", (PyCFunction)string_isalpha, 0, isalpha__doc__}, |
| 2339 | {"isalnum", (PyCFunction)string_isalnum, 0, isalnum__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2340 | {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__}, |
| 2341 | {"count", (PyCFunction)string_count, 1, count__doc__}, |
| 2342 | {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__}, |
| 2343 | {"find", (PyCFunction)string_find, 1, find__doc__}, |
| 2344 | {"index", (PyCFunction)string_index, 1, index__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2345 | {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2346 | {"replace", (PyCFunction)string_replace, 1, replace__doc__}, |
| 2347 | {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__}, |
| 2348 | {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__}, |
| 2349 | {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2350 | {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__}, |
| 2351 | {"strip", (PyCFunction)string_strip, 1, strip__doc__}, |
| 2352 | {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__}, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2353 | {"translate", (PyCFunction)string_translate, 1, translate__doc__}, |
| 2354 | {"title", (PyCFunction)string_title, 1, title__doc__}, |
| 2355 | {"ljust", (PyCFunction)string_ljust, 1, ljust__doc__}, |
| 2356 | {"rjust", (PyCFunction)string_rjust, 1, rjust__doc__}, |
| 2357 | {"center", (PyCFunction)string_center, 1, center__doc__}, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2358 | {"encode", (PyCFunction)string_encode, 1, encode__doc__}, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2359 | {"expandtabs", (PyCFunction)string_expandtabs, 1, expandtabs__doc__}, |
| 2360 | {"splitlines", (PyCFunction)string_splitlines, 1, splitlines__doc__}, |
| 2361 | #if 0 |
| 2362 | {"zfill", (PyCFunction)string_zfill, 1, zfill__doc__}, |
| 2363 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2364 | {NULL, NULL} /* sentinel */ |
| 2365 | }; |
| 2366 | |
| 2367 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2368 | string_getattr(PyStringObject *s, char *name) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2369 | { |
| 2370 | return Py_FindMethod(string_methods, (PyObject*)s, name); |
| 2371 | } |
| 2372 | |
| 2373 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2374 | PyTypeObject PyString_Type = { |
| 2375 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2376 | 0, |
| 2377 | "string", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2378 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2379 | sizeof(char), |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2380 | (destructor)string_dealloc, /*tp_dealloc*/ |
| 2381 | (printfunc)string_print, /*tp_print*/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2382 | (getattrfunc)string_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2383 | 0, /*tp_setattr*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2384 | (cmpfunc)string_compare, /*tp_compare*/ |
| 2385 | (reprfunc)string_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2386 | 0, /*tp_as_number*/ |
| 2387 | &string_as_sequence, /*tp_as_sequence*/ |
| 2388 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2389 | (hashfunc)string_hash, /*tp_hash*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2390 | 0, /*tp_call*/ |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 2391 | (reprfunc)string_str, /*tp_str*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2392 | 0, /*tp_getattro*/ |
| 2393 | 0, /*tp_setattro*/ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2394 | &string_as_buffer, /*tp_as_buffer*/ |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 2395 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2396 | 0, /*tp_doc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2397 | }; |
| 2398 | |
| 2399 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2400 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2401 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2402 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2403 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2404 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2405 | if (w == NULL || !PyString_Check(*pv)) { |
| 2406 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2407 | *pv = NULL; |
| 2408 | return; |
| 2409 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2410 | v = string_concat((PyStringObject *) *pv, w); |
| 2411 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2412 | *pv = v; |
| 2413 | } |
| 2414 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2415 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2416 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2417 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2418 | PyString_Concat(pv, w); |
| 2419 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2423 | /* The following function breaks the notion that strings are immutable: |
| 2424 | it changes the size of a string. We get away with this only if there |
| 2425 | is only one module referencing the object. You can also think of it |
| 2426 | as creating a new string object and destroying the old one, only |
| 2427 | more efficiently. In any case, don't use this if the string may |
| 2428 | already be known to some other part of the code... */ |
| 2429 | |
| 2430 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2431 | _PyString_Resize(PyObject **pv, int newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2432 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2433 | register PyObject *v; |
| 2434 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2435 | v = *pv; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2436 | if (!PyString_Check(v) || v->ob_refcnt != 1) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2437 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2438 | Py_DECREF(v); |
| 2439 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 2440 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2441 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2442 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 2443 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 2444 | --_Py_RefTotal; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2445 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2446 | _Py_ForgetReference(v); |
| 2447 | *pv = (PyObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2448 | PyObject_REALLOC((char *)v, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2449 | sizeof(PyStringObject) + newsize * sizeof(char)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2450 | if (*pv == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2451 | PyObject_DEL(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2452 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 2453 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2454 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2455 | _Py_NewReference(*pv); |
| 2456 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2457 | sv->ob_size = newsize; |
| 2458 | sv->ob_sval[newsize] = '\0'; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2459 | return 0; |
| 2460 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2461 | |
| 2462 | /* Helpers for formatstring */ |
| 2463 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2464 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2465 | getnextarg(PyObject *args, int arglen, int *p_argidx) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2466 | { |
| 2467 | int argidx = *p_argidx; |
| 2468 | if (argidx < arglen) { |
| 2469 | (*p_argidx)++; |
| 2470 | if (arglen < 0) |
| 2471 | return args; |
| 2472 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2473 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2474 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2475 | PyErr_SetString(PyExc_TypeError, |
| 2476 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2477 | return NULL; |
| 2478 | } |
| 2479 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2480 | /* Format codes |
| 2481 | * F_LJUST '-' |
| 2482 | * F_SIGN '+' |
| 2483 | * F_BLANK ' ' |
| 2484 | * F_ALT '#' |
| 2485 | * F_ZERO '0' |
| 2486 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2487 | #define F_LJUST (1<<0) |
| 2488 | #define F_SIGN (1<<1) |
| 2489 | #define F_BLANK (1<<2) |
| 2490 | #define F_ALT (1<<3) |
| 2491 | #define F_ZERO (1<<4) |
| 2492 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2493 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2494 | formatfloat(char *buf, size_t buflen, int flags, |
| 2495 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2496 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2497 | /* fmt = '%#.' + `prec` + `type` |
| 2498 | 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] | 2499 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2500 | double x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2501 | if (!PyArg_Parse(v, "d;float argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2502 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2503 | if (prec < 0) |
| 2504 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2505 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 2506 | type = 'g'; |
| 2507 | sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2508 | /* worst case length calc to ensure no buffer overrun: |
| 2509 | fmt = %#.<prec>g |
| 2510 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2511 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2512 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 2513 | If prec=0 the effective precision is 1 (the leading digit is |
| 2514 | always given), therefore increase by one to 10+prec. */ |
| 2515 | if (buflen <= (size_t)10 + (size_t)prec) { |
| 2516 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2517 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2518 | return -1; |
| 2519 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2520 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2521 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2524 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 2525 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 2526 | * Python's regular ints. |
| 2527 | * Return value: a new PyString*, or NULL if error. |
| 2528 | * . *pbuf is set to point into it, |
| 2529 | * *plen set to the # of chars following that. |
| 2530 | * Caller must decref it when done using pbuf. |
| 2531 | * The string starting at *pbuf is of the form |
| 2532 | * "-"? ("0x" | "0X")? digit+ |
| 2533 | * "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] | 2534 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2535 | * There will be at least prec digits, zero-filled on the left if |
| 2536 | * necessary to get that many. |
| 2537 | * val object to be converted |
| 2538 | * flags bitmask of format flags; only F_ALT is looked at |
| 2539 | * prec minimum number of digits; 0-fill on left if needed |
| 2540 | * type a character in [duoxX]; u acts the same as d |
| 2541 | * |
| 2542 | * CAUTION: o, x and X conversions on regular ints can never |
| 2543 | * produce a '-' sign, but can for Python's unbounded ints. |
| 2544 | */ |
| 2545 | PyObject* |
| 2546 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 2547 | char **pbuf, int *plen) |
| 2548 | { |
| 2549 | PyObject *result = NULL; |
| 2550 | char *buf; |
| 2551 | int i; |
| 2552 | int sign; /* 1 if '-', else 0 */ |
| 2553 | int len; /* number of characters */ |
| 2554 | int numdigits; /* len == numnondigits + numdigits */ |
| 2555 | int numnondigits = 0; |
| 2556 | |
| 2557 | switch (type) { |
| 2558 | case 'd': |
| 2559 | case 'u': |
| 2560 | result = val->ob_type->tp_str(val); |
| 2561 | break; |
| 2562 | case 'o': |
| 2563 | result = val->ob_type->tp_as_number->nb_oct(val); |
| 2564 | break; |
| 2565 | case 'x': |
| 2566 | case 'X': |
| 2567 | numnondigits = 2; |
| 2568 | result = val->ob_type->tp_as_number->nb_hex(val); |
| 2569 | break; |
| 2570 | default: |
| 2571 | assert(!"'type' not in [duoxX]"); |
| 2572 | } |
| 2573 | if (!result) |
| 2574 | return NULL; |
| 2575 | |
| 2576 | /* To modify the string in-place, there can only be one reference. */ |
| 2577 | if (result->ob_refcnt != 1) { |
| 2578 | PyErr_BadInternalCall(); |
| 2579 | return NULL; |
| 2580 | } |
| 2581 | buf = PyString_AsString(result); |
| 2582 | len = PyString_Size(result); |
| 2583 | if (buf[len-1] == 'L') { |
| 2584 | --len; |
| 2585 | buf[len] = '\0'; |
| 2586 | } |
| 2587 | sign = buf[0] == '-'; |
| 2588 | numnondigits += sign; |
| 2589 | numdigits = len - numnondigits; |
| 2590 | assert(numdigits > 0); |
| 2591 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 2592 | /* Get rid of base marker unless F_ALT */ |
| 2593 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2594 | /* Need to skip 0x, 0X or 0. */ |
| 2595 | int skipped = 0; |
| 2596 | switch (type) { |
| 2597 | case 'o': |
| 2598 | assert(buf[sign] == '0'); |
| 2599 | /* If 0 is only digit, leave it alone. */ |
| 2600 | if (numdigits > 1) { |
| 2601 | skipped = 1; |
| 2602 | --numdigits; |
| 2603 | } |
| 2604 | break; |
| 2605 | case 'x': |
| 2606 | case 'X': |
| 2607 | assert(buf[sign] == '0'); |
| 2608 | assert(buf[sign + 1] == 'x'); |
| 2609 | skipped = 2; |
| 2610 | numnondigits -= 2; |
| 2611 | break; |
| 2612 | } |
| 2613 | if (skipped) { |
| 2614 | buf += skipped; |
| 2615 | len -= skipped; |
| 2616 | if (sign) |
| 2617 | buf[0] = '-'; |
| 2618 | } |
| 2619 | assert(len == numnondigits + numdigits); |
| 2620 | assert(numdigits > 0); |
| 2621 | } |
| 2622 | |
| 2623 | /* Fill with leading zeroes to meet minimum width. */ |
| 2624 | if (prec > numdigits) { |
| 2625 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 2626 | numnondigits + prec); |
| 2627 | char *b1; |
| 2628 | if (!r1) { |
| 2629 | Py_DECREF(result); |
| 2630 | return NULL; |
| 2631 | } |
| 2632 | b1 = PyString_AS_STRING(r1); |
| 2633 | for (i = 0; i < numnondigits; ++i) |
| 2634 | *b1++ = *buf++; |
| 2635 | for (i = 0; i < prec - numdigits; i++) |
| 2636 | *b1++ = '0'; |
| 2637 | for (i = 0; i < numdigits; i++) |
| 2638 | *b1++ = *buf++; |
| 2639 | *b1 = '\0'; |
| 2640 | Py_DECREF(result); |
| 2641 | result = r1; |
| 2642 | buf = PyString_AS_STRING(result); |
| 2643 | len = numnondigits + prec; |
| 2644 | } |
| 2645 | |
| 2646 | /* Fix up case for hex conversions. */ |
| 2647 | switch (type) { |
| 2648 | case 'x': |
| 2649 | /* Need to convert all upper case letters to lower case. */ |
| 2650 | for (i = 0; i < len; i++) |
| 2651 | if (buf[i] >= 'A' && buf[i] <= 'F') |
| 2652 | buf[i] += 'a'-'A'; |
| 2653 | break; |
| 2654 | case 'X': |
| 2655 | /* Need to convert 0x to 0X (and -0x to -0X). */ |
| 2656 | if (buf[sign + 1] == 'x') |
| 2657 | buf[sign + 1] = 'X'; |
| 2658 | break; |
| 2659 | } |
| 2660 | *pbuf = buf; |
| 2661 | *plen = len; |
| 2662 | return result; |
| 2663 | } |
| 2664 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2665 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2666 | formatint(char *buf, size_t buflen, int flags, |
| 2667 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2668 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2669 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2670 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 2671 | + 1 + 1 = 24 */ |
| 2672 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2673 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2674 | if (!PyArg_Parse(v, "l;int argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2675 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2676 | if (prec < 0) |
| 2677 | prec = 1; |
| 2678 | sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2679 | /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal)) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2680 | worst case buf = '0x' + [0-9]*prec, where prec >= 11 */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2681 | if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2682 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2683 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2684 | return -1; |
| 2685 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2686 | sprintf(buf, fmt, x); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 2687 | /* When converting 0 under %#x or %#X, C leaves off the base marker, |
| 2688 | * but we want it (for consistency with other %#x conversions, and |
| 2689 | * for consistency with Python's hex() function). |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2690 | * BUG 28-Apr-2001 tim: At least two platform Cs (Metrowerks & |
| 2691 | * Compaq Tru64) violate the std by converting 0 w/ leading 0x anyway. |
| 2692 | * So add it only if the platform didn't already. |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 2693 | */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2694 | if (x == 0 && |
| 2695 | (flags & F_ALT) && |
| 2696 | (type == 'x' || type == 'X') && |
| 2697 | buf[1] != (char)type) /* this last always true under std C */ |
| 2698 | { |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 2699 | memmove(buf+2, buf, strlen(buf) + 1); |
| 2700 | buf[0] = '0'; |
| 2701 | buf[1] = (char)type; |
| 2702 | } |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2703 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2706 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2707 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2708 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2709 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2710 | if (PyString_Check(v)) { |
| 2711 | 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] | 2712 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2713 | } |
| 2714 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2715 | 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] | 2716 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2717 | } |
| 2718 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2719 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2722 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2723 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 2724 | |
| 2725 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 2726 | chars are formatted. XXX This is a magic number. Each formatting |
| 2727 | routine does bounds checking to ensure no overflow, but a better |
| 2728 | solution may be to malloc a buffer of appropriate size for each |
| 2729 | format. For now, the current solution is sufficient. |
| 2730 | */ |
| 2731 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2732 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2733 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2734 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2735 | { |
| 2736 | char *fmt, *res; |
| 2737 | int fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2738 | int args_owned = 0; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 2739 | PyObject *result, *orig_args, *v, *w; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2740 | PyObject *dict = NULL; |
| 2741 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 2742 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2743 | return NULL; |
| 2744 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 2745 | orig_args = args; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2746 | fmt = PyString_AsString(format); |
| 2747 | fmtcnt = PyString_Size(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2748 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2749 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2750 | if (result == NULL) |
| 2751 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2752 | res = PyString_AsString(result); |
| 2753 | if (PyTuple_Check(args)) { |
| 2754 | arglen = PyTuple_Size(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2755 | argidx = 0; |
| 2756 | } |
| 2757 | else { |
| 2758 | arglen = -1; |
| 2759 | argidx = -2; |
| 2760 | } |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2761 | if (args->ob_type->tp_as_mapping) |
| 2762 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2763 | while (--fmtcnt >= 0) { |
| 2764 | if (*fmt != '%') { |
| 2765 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2766 | rescnt = fmtcnt + 100; |
| 2767 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2768 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2769 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2770 | res = PyString_AsString(result) |
| 2771 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2772 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2773 | } |
| 2774 | *res++ = *fmt++; |
| 2775 | } |
| 2776 | else { |
| 2777 | /* Got a format specifier */ |
| 2778 | int flags = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2779 | int width = -1; |
| 2780 | int prec = -1; |
| 2781 | int size = 0; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 2782 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2783 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2784 | PyObject *v = NULL; |
| 2785 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2786 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2787 | int sign; |
| 2788 | int len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2789 | char formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 2790 | char *fmt_start = fmt; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 2791 | int argidx_start = argidx; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2792 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 2793 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2794 | if (*fmt == '(') { |
| 2795 | char *keystart; |
| 2796 | int keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2797 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2798 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2799 | |
| 2800 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2801 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2802 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2803 | goto error; |
| 2804 | } |
| 2805 | ++fmt; |
| 2806 | --fmtcnt; |
| 2807 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2808 | /* Skip over balanced parentheses */ |
| 2809 | while (pcount > 0 && --fmtcnt >= 0) { |
| 2810 | if (*fmt == ')') |
| 2811 | --pcount; |
| 2812 | else if (*fmt == '(') |
| 2813 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2814 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2815 | } |
| 2816 | keylen = fmt - keystart - 1; |
| 2817 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2818 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2819 | "incomplete format key"); |
| 2820 | goto error; |
| 2821 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2822 | key = PyString_FromStringAndSize(keystart, |
| 2823 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2824 | if (key == NULL) |
| 2825 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2826 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2827 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2828 | args_owned = 0; |
| 2829 | } |
| 2830 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2831 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2832 | if (args == NULL) { |
| 2833 | goto error; |
| 2834 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2835 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2836 | arglen = -1; |
| 2837 | argidx = -2; |
| 2838 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2839 | while (--fmtcnt >= 0) { |
| 2840 | switch (c = *fmt++) { |
| 2841 | case '-': flags |= F_LJUST; continue; |
| 2842 | case '+': flags |= F_SIGN; continue; |
| 2843 | case ' ': flags |= F_BLANK; continue; |
| 2844 | case '#': flags |= F_ALT; continue; |
| 2845 | case '0': flags |= F_ZERO; continue; |
| 2846 | } |
| 2847 | break; |
| 2848 | } |
| 2849 | if (c == '*') { |
| 2850 | v = getnextarg(args, arglen, &argidx); |
| 2851 | if (v == NULL) |
| 2852 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2853 | if (!PyInt_Check(v)) { |
| 2854 | PyErr_SetString(PyExc_TypeError, |
| 2855 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2856 | goto error; |
| 2857 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2858 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 2859 | if (width < 0) { |
| 2860 | flags |= F_LJUST; |
| 2861 | width = -width; |
| 2862 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2863 | if (--fmtcnt >= 0) |
| 2864 | c = *fmt++; |
| 2865 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2866 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2867 | width = c - '0'; |
| 2868 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2869 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2870 | if (!isdigit(c)) |
| 2871 | break; |
| 2872 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2873 | PyErr_SetString( |
| 2874 | PyExc_ValueError, |
| 2875 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2876 | goto error; |
| 2877 | } |
| 2878 | width = width*10 + (c - '0'); |
| 2879 | } |
| 2880 | } |
| 2881 | if (c == '.') { |
| 2882 | prec = 0; |
| 2883 | if (--fmtcnt >= 0) |
| 2884 | c = *fmt++; |
| 2885 | if (c == '*') { |
| 2886 | v = getnextarg(args, arglen, &argidx); |
| 2887 | if (v == NULL) |
| 2888 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2889 | if (!PyInt_Check(v)) { |
| 2890 | PyErr_SetString( |
| 2891 | PyExc_TypeError, |
| 2892 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2893 | goto error; |
| 2894 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2895 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2896 | if (prec < 0) |
| 2897 | prec = 0; |
| 2898 | if (--fmtcnt >= 0) |
| 2899 | c = *fmt++; |
| 2900 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2901 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2902 | prec = c - '0'; |
| 2903 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2904 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2905 | if (!isdigit(c)) |
| 2906 | break; |
| 2907 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2908 | PyErr_SetString( |
| 2909 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2910 | "prec too big"); |
| 2911 | goto error; |
| 2912 | } |
| 2913 | prec = prec*10 + (c - '0'); |
| 2914 | } |
| 2915 | } |
| 2916 | } /* prec */ |
| 2917 | if (fmtcnt >= 0) { |
| 2918 | if (c == 'h' || c == 'l' || c == 'L') { |
| 2919 | size = c; |
| 2920 | if (--fmtcnt >= 0) |
| 2921 | c = *fmt++; |
| 2922 | } |
| 2923 | } |
| 2924 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2925 | PyErr_SetString(PyExc_ValueError, |
| 2926 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2927 | goto error; |
| 2928 | } |
| 2929 | if (c != '%') { |
| 2930 | v = getnextarg(args, arglen, &argidx); |
| 2931 | if (v == NULL) |
| 2932 | goto error; |
| 2933 | } |
| 2934 | sign = 0; |
| 2935 | fill = ' '; |
| 2936 | switch (c) { |
| 2937 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2938 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2939 | len = 1; |
| 2940 | break; |
| 2941 | case 's': |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 2942 | case 'r': |
| 2943 | if (PyUnicode_Check(v)) { |
| 2944 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 2945 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 2946 | goto unicode; |
| 2947 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2948 | if (c == 's') |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2949 | temp = PyObject_Str(v); |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 2950 | else |
| 2951 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2952 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2953 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 2954 | if (!PyString_Check(temp)) { |
| 2955 | PyErr_SetString(PyExc_TypeError, |
| 2956 | "%s argument has non-string str()"); |
| 2957 | goto error; |
| 2958 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2959 | pbuf = PyString_AsString(temp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2960 | len = PyString_Size(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2961 | if (prec >= 0 && len > prec) |
| 2962 | len = prec; |
| 2963 | break; |
| 2964 | case 'i': |
| 2965 | case 'd': |
| 2966 | case 'u': |
| 2967 | case 'o': |
| 2968 | case 'x': |
| 2969 | case 'X': |
| 2970 | if (c == 'i') |
| 2971 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 2972 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2973 | temp = _PyString_FormatLong(v, flags, |
| 2974 | prec, c, &pbuf, &len); |
| 2975 | if (!temp) |
| 2976 | goto error; |
| 2977 | /* unbounded ints can always produce |
| 2978 | a sign character! */ |
| 2979 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 2980 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 2981 | else { |
| 2982 | pbuf = formatbuf; |
| 2983 | len = formatint(pbuf, sizeof(formatbuf), |
| 2984 | flags, prec, c, v); |
| 2985 | if (len < 0) |
| 2986 | goto error; |
| 2987 | /* only d conversion is signed */ |
| 2988 | sign = c == 'd'; |
| 2989 | } |
| 2990 | if (flags & F_ZERO) |
| 2991 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2992 | break; |
| 2993 | case 'e': |
| 2994 | case 'E': |
| 2995 | case 'f': |
| 2996 | case 'g': |
| 2997 | case 'G': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 2998 | pbuf = formatbuf; |
| 2999 | len = formatfloat(pbuf, sizeof(formatbuf), flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3000 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3001 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3002 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3003 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3004 | fill = '0'; |
| 3005 | break; |
| 3006 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3007 | pbuf = formatbuf; |
| 3008 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3009 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3010 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3011 | break; |
| 3012 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3013 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 3014 | "unsupported format character '%c' (0x%x) " |
| 3015 | "at index %i", |
| 3016 | c, c, fmt - 1 - PyString_AsString(format)); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3017 | goto error; |
| 3018 | } |
| 3019 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3020 | if (*pbuf == '-' || *pbuf == '+') { |
| 3021 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3022 | len--; |
| 3023 | } |
| 3024 | else if (flags & F_SIGN) |
| 3025 | sign = '+'; |
| 3026 | else if (flags & F_BLANK) |
| 3027 | sign = ' '; |
| 3028 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3029 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3030 | } |
| 3031 | if (width < len) |
| 3032 | width = len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3033 | if (rescnt < width + (sign != 0)) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 3034 | reslen -= rescnt; |
| 3035 | rescnt = width + fmtcnt + 100; |
| 3036 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3037 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3038 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3039 | res = PyString_AsString(result) |
| 3040 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3041 | } |
| 3042 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 3043 | if (fill != ' ') |
| 3044 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3045 | rescnt--; |
| 3046 | if (width > len) |
| 3047 | width--; |
| 3048 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3049 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 3050 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3051 | assert(pbuf[1] == c); |
| 3052 | if (fill != ' ') { |
| 3053 | *res++ = *pbuf++; |
| 3054 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3055 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3056 | rescnt -= 2; |
| 3057 | width -= 2; |
| 3058 | if (width < 0) |
| 3059 | width = 0; |
| 3060 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3061 | } |
| 3062 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3063 | do { |
| 3064 | --rescnt; |
| 3065 | *res++ = fill; |
| 3066 | } while (--width > len); |
| 3067 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3068 | if (fill == ' ') { |
| 3069 | if (sign) |
| 3070 | *res++ = sign; |
| 3071 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3072 | (c == 'x' || c == 'X')) { |
| 3073 | assert(pbuf[0] == '0'); |
| 3074 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3075 | *res++ = *pbuf++; |
| 3076 | *res++ = *pbuf++; |
| 3077 | } |
| 3078 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3079 | memcpy(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3080 | res += len; |
| 3081 | rescnt -= len; |
| 3082 | while (--width >= len) { |
| 3083 | --rescnt; |
| 3084 | *res++ = ' '; |
| 3085 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 3086 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3087 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3088 | "not all arguments converted"); |
| 3089 | goto error; |
| 3090 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3091 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3092 | } /* '%' */ |
| 3093 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 3094 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3095 | PyErr_SetString(PyExc_TypeError, |
| 3096 | "not all arguments converted"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3097 | goto error; |
| 3098 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3099 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3100 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3101 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3102 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3103 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3104 | |
| 3105 | unicode: |
| 3106 | if (args_owned) { |
| 3107 | Py_DECREF(args); |
| 3108 | args_owned = 0; |
| 3109 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 3110 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3111 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 3112 | PyObject *v; |
| 3113 | int n = PyTuple_GET_SIZE(orig_args) - argidx; |
| 3114 | v = PyTuple_New(n); |
| 3115 | if (v == NULL) |
| 3116 | goto error; |
| 3117 | while (--n >= 0) { |
| 3118 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 3119 | Py_INCREF(w); |
| 3120 | PyTuple_SET_ITEM(v, n, w); |
| 3121 | } |
| 3122 | args = v; |
| 3123 | } else { |
| 3124 | Py_INCREF(orig_args); |
| 3125 | args = orig_args; |
| 3126 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3127 | args_owned = 1; |
| 3128 | /* Take what we have of the result and let the Unicode formatting |
| 3129 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3130 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3131 | if (_PyString_Resize(&result, rescnt)) |
| 3132 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3133 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 3134 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3135 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 3136 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3137 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3138 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3139 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3140 | if (v == NULL) |
| 3141 | goto error; |
| 3142 | /* Paste what we have (result) to what the Unicode formatting |
| 3143 | function returned (v) and return the result (or error) */ |
| 3144 | w = PyUnicode_Concat(result, v); |
| 3145 | Py_DECREF(result); |
| 3146 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3147 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 3148 | return w; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3149 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3150 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3151 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3152 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3153 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 3154 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3155 | return NULL; |
| 3156 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3157 | |
| 3158 | |
| 3159 | #ifdef INTERN_STRINGS |
| 3160 | |
Barry Warsaw | 4df762f | 2000-08-16 23:41:01 +0000 | [diff] [blame] | 3161 | /* This dictionary will leak at PyString_Fini() time. That's acceptable |
| 3162 | * because PyString_Fini() specifically frees interned strings that are |
| 3163 | * only referenced by this dictionary. The CVS log entry for revision 2.45 |
| 3164 | * says: |
| 3165 | * |
| 3166 | * Change the Fini function to only remove otherwise unreferenced |
| 3167 | * strings from the interned table. There are references in |
| 3168 | * hard-to-find static variables all over the interpreter, and it's not |
| 3169 | * worth trying to get rid of all those; but "uninterning" isn't fair |
| 3170 | * either and may cause subtle failures later -- so we have to keep them |
| 3171 | * in the interned table. |
| 3172 | */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3173 | static PyObject *interned; |
| 3174 | |
| 3175 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3176 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3177 | { |
| 3178 | register PyStringObject *s = (PyStringObject *)(*p); |
| 3179 | PyObject *t; |
| 3180 | if (s == NULL || !PyString_Check(s)) |
| 3181 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
| 3182 | if ((t = s->ob_sinterned) != NULL) { |
| 3183 | if (t == (PyObject *)s) |
| 3184 | return; |
| 3185 | Py_INCREF(t); |
| 3186 | *p = t; |
| 3187 | Py_DECREF(s); |
| 3188 | return; |
| 3189 | } |
| 3190 | if (interned == NULL) { |
| 3191 | interned = PyDict_New(); |
| 3192 | if (interned == NULL) |
| 3193 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3194 | } |
| 3195 | if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { |
| 3196 | Py_INCREF(t); |
| 3197 | *p = s->ob_sinterned = t; |
| 3198 | Py_DECREF(s); |
| 3199 | return; |
| 3200 | } |
| 3201 | t = (PyObject *)s; |
| 3202 | if (PyDict_SetItem(interned, t, t) == 0) { |
| 3203 | s->ob_sinterned = t; |
| 3204 | return; |
| 3205 | } |
| 3206 | PyErr_Clear(); |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3211 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 3212 | { |
| 3213 | PyObject *s = PyString_FromString(cp); |
| 3214 | if (s == NULL) |
| 3215 | return NULL; |
| 3216 | PyString_InternInPlace(&s); |
| 3217 | return s; |
| 3218 | } |
| 3219 | |
| 3220 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3221 | |
| 3222 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3223 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3224 | { |
| 3225 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3226 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 3227 | Py_XDECREF(characters[i]); |
| 3228 | characters[i] = NULL; |
| 3229 | } |
| 3230 | #ifndef DONT_SHARE_SHORT_STRINGS |
| 3231 | Py_XDECREF(nullstring); |
| 3232 | nullstring = NULL; |
| 3233 | #endif |
Guido van Rossum | 971a7aa | 1997-08-05 02:15:12 +0000 | [diff] [blame] | 3234 | #ifdef INTERN_STRINGS |
| 3235 | if (interned) { |
| 3236 | int pos, changed; |
| 3237 | PyObject *key, *value; |
| 3238 | do { |
| 3239 | changed = 0; |
| 3240 | pos = 0; |
| 3241 | while (PyDict_Next(interned, &pos, &key, &value)) { |
| 3242 | if (key->ob_refcnt == 2 && key == value) { |
| 3243 | PyDict_DelItem(interned, key); |
| 3244 | changed = 1; |
| 3245 | } |
| 3246 | } |
| 3247 | } while (changed); |
| 3248 | } |
| 3249 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3250 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 3251 | |
| 3252 | #ifdef INTERN_STRINGS |
| 3253 | void _Py_ReleaseInternedStrings(void) |
| 3254 | { |
| 3255 | if (interned) { |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3256 | fprintf(stderr, "releasing interned strings\n"); |
| 3257 | PyDict_Clear(interned); |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 3258 | Py_DECREF(interned); |
| 3259 | interned = NULL; |
| 3260 | } |
| 3261 | } |
| 3262 | #endif /* INTERN_STRINGS */ |