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