Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* String object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 71160aa | 1997-06-03 18:03:18 +0000 | [diff] [blame] | 36 | #include "mymath.h" |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 37 | #include <ctype.h> |
| 38 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 39 | #ifdef COUNT_ALLOCS |
| 40 | int null_strings, one_strings; |
| 41 | #endif |
| 42 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 43 | #ifdef HAVE_LIMITS_H |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 44 | #include <limits.h> |
| 45 | #else |
| 46 | #ifndef UCHAR_MAX |
| 47 | #define UCHAR_MAX 255 |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 51 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 52 | #ifndef DONT_SHARE_SHORT_STRINGS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 53 | static PyStringObject *nullstring; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 54 | #endif |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | Newsizedstringobject() and newstringobject() try in certain cases |
| 58 | to share string objects. When the size of the string is zero, |
| 59 | these routines always return a pointer to the same string object; |
| 60 | when the size is one, they return a pointer to an already existing |
| 61 | object if the contents of the string is known. For |
| 62 | newstringobject() this is always the case, for |
| 63 | newsizedstringobject() this is the case when the first argument in |
| 64 | not NULL. |
| 65 | A common practice to allocate a string and then fill it in or |
| 66 | change it must be done carefully. It is only allowed to change the |
| 67 | contents of the string if the obect was gotten from |
| 68 | newsizedstringobject() with a NULL first argument, because in the |
| 69 | future these routines may try to do even more sharing of objects. |
| 70 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 71 | PyObject * |
| 72 | PyString_FromStringAndSize(str, size) |
Guido van Rossum | 067998f | 1996-12-10 15:33:34 +0000 | [diff] [blame] | 73 | const char *str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | int size; |
| 75 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | register PyStringObject *op; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 77 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 78 | if (size == 0 && (op = nullstring) != NULL) { |
| 79 | #ifdef COUNT_ALLOCS |
| 80 | null_strings++; |
| 81 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | Py_INCREF(op); |
| 83 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 84 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | if (size == 1 && str != NULL && |
| 86 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 87 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 88 | #ifdef COUNT_ALLOCS |
| 89 | one_strings++; |
| 90 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | Py_INCREF(op); |
| 92 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 93 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 94 | #endif /* DONT_SHARE_SHORT_STRINGS */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | op = (PyStringObject *) |
| 96 | malloc(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 97 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | return PyErr_NoMemory(); |
| 99 | op->ob_type = &PyString_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 100 | op->ob_size = size; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 101 | #ifdef CACHE_HASH |
| 102 | op->ob_shash = -1; |
| 103 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 104 | #ifdef INTERN_STRINGS |
| 105 | op->ob_sinterned = NULL; |
| 106 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | _Py_NewReference(op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 108 | if (str != NULL) |
| 109 | memcpy(op->ob_sval, str, size); |
| 110 | op->ob_sval[size] = '\0'; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 111 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 112 | if (size == 0) { |
| 113 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 114 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 115 | } else if (size == 1 && str != NULL) { |
| 116 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 117 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 118 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 119 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 123 | PyObject * |
| 124 | PyString_FromString(str) |
Guido van Rossum | 067998f | 1996-12-10 15:33:34 +0000 | [diff] [blame] | 125 | const char *str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | { |
| 127 | register unsigned int size = strlen(str); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | register PyStringObject *op; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 129 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 130 | if (size == 0 && (op = nullstring) != NULL) { |
| 131 | #ifdef COUNT_ALLOCS |
| 132 | null_strings++; |
| 133 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 134 | Py_INCREF(op); |
| 135 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 136 | } |
| 137 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 138 | #ifdef COUNT_ALLOCS |
| 139 | one_strings++; |
| 140 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 141 | Py_INCREF(op); |
| 142 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 143 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 144 | #endif /* DONT_SHARE_SHORT_STRINGS */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | op = (PyStringObject *) |
| 146 | malloc(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 147 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | return PyErr_NoMemory(); |
| 149 | op->ob_type = &PyString_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 150 | op->ob_size = size; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 151 | #ifdef CACHE_HASH |
| 152 | op->ob_shash = -1; |
| 153 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 154 | #ifdef INTERN_STRINGS |
| 155 | op->ob_sinterned = NULL; |
| 156 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | _Py_NewReference(op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 158 | strcpy(op->ob_sval, str); |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 159 | #ifndef DONT_SHARE_SHORT_STRINGS |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 160 | if (size == 0) { |
| 161 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 163 | } else if (size == 1) { |
| 164 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 166 | } |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 167 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 168 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 171 | static void |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 172 | string_dealloc(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 173 | PyObject *op; |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 174 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | PyMem_DEL(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 178 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 179 | PyString_Size(op) |
| 180 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | if (!PyString_Check(op)) { |
| 183 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | return -1; |
| 185 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | return ((PyStringObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /*const*/ char * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 190 | PyString_AsString(op) |
| 191 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 193 | if (!PyString_Check(op)) { |
| 194 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | return NULL; |
| 196 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 197 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Methods */ |
| 201 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 202 | static int |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 203 | string_print(op, fp, flags) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | PyStringObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | FILE *fp; |
| 206 | int flags; |
| 207 | { |
| 208 | int i; |
| 209 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 210 | int quote; |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 211 | /* XXX Ought to check for interrupts when writing long strings */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | if (flags & Py_PRINT_RAW) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | fwrite(op->ob_sval, 1, (int) op->ob_size, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 214 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 216 | |
| 217 | /* figure out which quote to use; single is prefered */ |
| 218 | quote = '\''; |
| 219 | if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) |
| 220 | quote = '"'; |
| 221 | |
| 222 | fputc(quote, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | for (i = 0; i < op->ob_size; i++) { |
| 224 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 225 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | fprintf(fp, "\\%c", c); |
| 227 | else if (c < ' ' || c >= 0177) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 228 | fprintf(fp, "\\%03o", c & 0377); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | else |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 230 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 232 | fputc(quote, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 233 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 236 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 237 | string_repr(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | register PyStringObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | { |
| 240 | /* XXX overflow? */ |
| 241 | int newsize = 2 + 4 * op->ob_size * sizeof(char); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | PyObject *v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 244 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | } |
| 246 | else { |
| 247 | register int i; |
| 248 | register char c; |
| 249 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 250 | int quote; |
| 251 | |
| 252 | /* figure out which quote to use; single is prefered */ |
| 253 | quote = '\''; |
| 254 | if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) |
| 255 | quote = '"'; |
| 256 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 257 | p = ((PyStringObject *)v)->ob_sval; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 258 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | for (i = 0; i < op->ob_size; i++) { |
| 260 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 261 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 262 | *p++ = '\\', *p++ = c; |
| 263 | else if (c < ' ' || c >= 0177) { |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 264 | sprintf(p, "\\%03o", c & 0377); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | while (*p != '\0') |
| 266 | p++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | } |
| 268 | else |
| 269 | *p++ = c; |
| 270 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 271 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 272 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | _PyString_Resize( |
| 274 | &v, (int) (p - ((PyStringObject *)v)->ob_sval)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 275 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 280 | string_length(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 281 | PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 282 | { |
| 283 | return a->ob_size; |
| 284 | } |
| 285 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 286 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 287 | string_concat(a, bb) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 288 | register PyStringObject *a; |
| 289 | register PyObject *bb; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | { |
| 291 | register unsigned int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 292 | register PyStringObject *op; |
| 293 | if (!PyString_Check(bb)) { |
| 294 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 297 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | /* Optimize cases with empty left or right operand */ |
| 299 | if (a->ob_size == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 300 | Py_INCREF(bb); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | return bb; |
| 302 | } |
| 303 | if (b->ob_size == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 304 | Py_INCREF(a); |
| 305 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 306 | } |
| 307 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | op = (PyStringObject *) |
| 309 | malloc(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 310 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 311 | return PyErr_NoMemory(); |
| 312 | op->ob_type = &PyString_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 313 | op->ob_size = size; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 314 | #ifdef CACHE_HASH |
| 315 | op->ob_shash = -1; |
| 316 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 317 | #ifdef INTERN_STRINGS |
| 318 | op->ob_sinterned = NULL; |
| 319 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | _Py_NewReference(op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 321 | memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); |
| 322 | memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); |
| 323 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | #undef b |
| 326 | } |
| 327 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 328 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 329 | string_repeat(a, n) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 330 | register PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | register int n; |
| 332 | { |
| 333 | register int i; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 334 | register int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 335 | register PyStringObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | if (n < 0) |
| 337 | n = 0; |
| 338 | size = a->ob_size * n; |
| 339 | if (size == a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | Py_INCREF(a); |
| 341 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 343 | op = (PyStringObject *) |
| 344 | malloc(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 345 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | return PyErr_NoMemory(); |
| 347 | op->ob_type = &PyString_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 348 | op->ob_size = size; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 349 | #ifdef CACHE_HASH |
| 350 | op->ob_shash = -1; |
| 351 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 352 | #ifdef INTERN_STRINGS |
| 353 | op->ob_sinterned = NULL; |
| 354 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | _Py_NewReference(op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 356 | for (i = 0; i < size; i += a->ob_size) |
| 357 | memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); |
| 358 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 363 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 365 | string_slice(a, i, j) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | register PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 367 | register int i, j; /* May be negative! */ |
| 368 | { |
| 369 | if (i < 0) |
| 370 | i = 0; |
| 371 | if (j < 0) |
| 372 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 373 | if (j > a->ob_size) |
| 374 | j = a->ob_size; |
| 375 | 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] | 376 | Py_INCREF(a); |
| 377 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 378 | } |
| 379 | if (j < i) |
| 380 | j = i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 381 | return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 384 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 385 | string_item(a, i) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | register int i; |
| 388 | { |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 389 | int c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | return NULL; |
| 394 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 395 | c = a->ob_sval[i] & UCHAR_MAX; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 396 | v = (PyObject *) characters[c]; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 397 | #ifdef COUNT_ALLOCS |
| 398 | if (v != NULL) |
| 399 | one_strings++; |
| 400 | #endif |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 401 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | v = PyString_FromStringAndSize((char *)NULL, 1); |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 403 | if (v == NULL) |
| 404 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | characters[c] = (PyStringObject *) v; |
| 406 | ((PyStringObject *)v)->ob_sval[0] = c; |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 407 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | Py_INCREF(v); |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 409 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static int |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 413 | string_compare(a, b) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 414 | PyStringObject *a, *b; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 415 | { |
Guido van Rossum | 253919f | 1991-02-13 23:18:39 +0000 | [diff] [blame] | 416 | int len_a = a->ob_size, len_b = b->ob_size; |
| 417 | int min_len = (len_a < len_b) ? len_a : len_b; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 418 | int cmp; |
| 419 | if (min_len > 0) { |
Guido van Rossum | fde7a75 | 1996-10-23 14:19:40 +0000 | [diff] [blame] | 420 | cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 421 | if (cmp == 0) |
| 422 | cmp = memcmp(a->ob_sval, b->ob_sval, min_len); |
| 423 | if (cmp != 0) |
| 424 | return cmp; |
| 425 | } |
Guido van Rossum | 253919f | 1991-02-13 23:18:39 +0000 | [diff] [blame] | 426 | return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 429 | static long |
| 430 | string_hash(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 431 | PyStringObject *a; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 432 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 433 | register int len; |
| 434 | register unsigned char *p; |
| 435 | register long x; |
| 436 | |
| 437 | #ifdef CACHE_HASH |
| 438 | if (a->ob_shash != -1) |
| 439 | return a->ob_shash; |
Guido van Rossum | 36b9f79 | 1997-02-14 16:29:22 +0000 | [diff] [blame] | 440 | #ifdef INTERN_STRINGS |
| 441 | if (a->ob_sinterned != NULL) |
| 442 | return (a->ob_shash = |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | ((PyStringObject *)(a->ob_sinterned))->ob_shash); |
Guido van Rossum | 36b9f79 | 1997-02-14 16:29:22 +0000 | [diff] [blame] | 444 | #endif |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 445 | #endif |
| 446 | len = a->ob_size; |
| 447 | p = (unsigned char *) a->ob_sval; |
| 448 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 449 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 450 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 451 | x ^= a->ob_size; |
| 452 | if (x == -1) |
| 453 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 454 | #ifdef CACHE_HASH |
| 455 | a->ob_shash = x; |
| 456 | #endif |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 457 | return x; |
| 458 | } |
| 459 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 460 | static int |
| 461 | string_buffer_getreadbuf(self, index, ptr) |
| 462 | PyStringObject *self; |
| 463 | int index; |
| 464 | const void **ptr; |
| 465 | { |
| 466 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 467 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 468 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 469 | return -1; |
| 470 | } |
| 471 | *ptr = (void *)self->ob_sval; |
| 472 | return self->ob_size; |
| 473 | } |
| 474 | |
| 475 | static int |
| 476 | string_buffer_getwritebuf(self, index, ptr) |
| 477 | PyStringObject *self; |
| 478 | int index; |
| 479 | const void **ptr; |
| 480 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 481 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 482 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | static int |
| 487 | string_buffer_getsegcount(self, lenp) |
| 488 | PyStringObject *self; |
| 489 | int *lenp; |
| 490 | { |
| 491 | if ( lenp ) |
| 492 | *lenp = self->ob_size; |
| 493 | return 1; |
| 494 | } |
| 495 | |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 496 | static int |
| 497 | string_buffer_getcharbuf(self, index, ptr) |
| 498 | PyStringObject *self; |
| 499 | int index; |
| 500 | const char **ptr; |
| 501 | { |
| 502 | if ( index != 0 ) { |
| 503 | PyErr_SetString(PyExc_SystemError, |
| 504 | "accessing non-existent string segment"); |
| 505 | return -1; |
| 506 | } |
| 507 | *ptr = self->ob_sval; |
| 508 | return self->ob_size; |
| 509 | } |
| 510 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 511 | static PySequenceMethods string_as_sequence = { |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 512 | (inquiry)string_length, /*sq_length*/ |
| 513 | (binaryfunc)string_concat, /*sq_concat*/ |
| 514 | (intargfunc)string_repeat, /*sq_repeat*/ |
| 515 | (intargfunc)string_item, /*sq_item*/ |
| 516 | (intintargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 517 | 0, /*sq_ass_item*/ |
| 518 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 519 | }; |
| 520 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 521 | static PyBufferProcs string_as_buffer = { |
| 522 | (getreadbufferproc)string_buffer_getreadbuf, |
| 523 | (getwritebufferproc)string_buffer_getwritebuf, |
| 524 | (getsegcountproc)string_buffer_getsegcount, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 525 | (getcharbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 528 | |
| 529 | |
| 530 | #define LEFTSTRIP 0 |
| 531 | #define RIGHTSTRIP 1 |
| 532 | #define BOTHSTRIP 2 |
| 533 | |
| 534 | |
| 535 | static PyObject * |
| 536 | split_whitespace(s, len, maxsplit) |
| 537 | char *s; |
| 538 | int len; |
| 539 | int maxsplit; |
| 540 | { |
| 541 | int i = 0, j, err; |
| 542 | int countsplit = 0; |
| 543 | PyObject* item; |
| 544 | PyObject *list = PyList_New(0); |
| 545 | |
| 546 | if (list == NULL) |
| 547 | return NULL; |
| 548 | |
| 549 | while (i < len) { |
| 550 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 551 | i = i+1; |
| 552 | } |
| 553 | j = i; |
| 554 | while (i < len && !isspace(Py_CHARMASK(s[i]))) { |
| 555 | i = i+1; |
| 556 | } |
| 557 | if (j < i) { |
| 558 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 559 | if (item == NULL) |
| 560 | goto finally; |
| 561 | |
| 562 | err = PyList_Append(list, item); |
| 563 | Py_DECREF(item); |
| 564 | if (err < 0) |
| 565 | goto finally; |
| 566 | |
| 567 | countsplit++; |
| 568 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 569 | i = i+1; |
| 570 | } |
| 571 | if (maxsplit && (countsplit >= maxsplit) && i < len) { |
| 572 | item = PyString_FromStringAndSize( |
| 573 | s+i, (int)(len - i)); |
| 574 | if (item == NULL) |
| 575 | goto finally; |
| 576 | |
| 577 | err = PyList_Append(list, item); |
| 578 | Py_DECREF(item); |
| 579 | if (err < 0) |
| 580 | goto finally; |
| 581 | |
| 582 | i = len; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | return list; |
| 587 | finally: |
| 588 | Py_DECREF(list); |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | static char split__doc__[] = |
| 594 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 595 | \n\ |
| 596 | Return a list of the words in the string S, using sep as the\n\ |
| 597 | delimiter string. If maxsplit is nonzero, splits into at most\n\ |
| 598 | maxsplit words If sep is not specified, any whitespace string\n\ |
| 599 | is a separator. Maxsplit defaults to 0."; |
| 600 | |
| 601 | static PyObject * |
| 602 | string_split(self, args) |
| 603 | PyStringObject *self; |
| 604 | PyObject *args; |
| 605 | { |
| 606 | int len = PyString_GET_SIZE(self), n, i, j, err; |
| 607 | int splitcount, maxsplit; |
| 608 | char *s = PyString_AS_STRING(self), *sub; |
| 609 | PyObject *list, *item; |
| 610 | |
| 611 | sub = NULL; |
| 612 | n = 0; |
| 613 | splitcount = 0; |
| 614 | maxsplit = 0; |
| 615 | if (!PyArg_ParseTuple(args, "|z#i", &sub, &n, &maxsplit)) |
| 616 | return NULL; |
| 617 | if (sub == NULL) |
| 618 | return split_whitespace(s, len, maxsplit); |
| 619 | if (n == 0) { |
| 620 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 621 | return NULL; |
| 622 | } |
| 623 | |
| 624 | list = PyList_New(0); |
| 625 | if (list == NULL) |
| 626 | return NULL; |
| 627 | |
| 628 | i = j = 0; |
| 629 | while (i+n <= len) { |
| 630 | if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) { |
| 631 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 632 | if (item == NULL) |
| 633 | goto fail; |
| 634 | err = PyList_Append(list, item); |
| 635 | Py_DECREF(item); |
| 636 | if (err < 0) |
| 637 | goto fail; |
| 638 | i = j = i + n; |
| 639 | splitcount++; |
| 640 | if (maxsplit && (splitcount >= maxsplit)) |
| 641 | break; |
| 642 | } |
| 643 | else |
| 644 | i++; |
| 645 | } |
| 646 | item = PyString_FromStringAndSize(s+j, (int)(len-j)); |
| 647 | if (item == NULL) |
| 648 | goto fail; |
| 649 | err = PyList_Append(list, item); |
| 650 | Py_DECREF(item); |
| 651 | if (err < 0) |
| 652 | goto fail; |
| 653 | |
| 654 | return list; |
| 655 | |
| 656 | fail: |
| 657 | Py_DECREF(list); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | static char join__doc__[] = |
| 663 | "S.join(sequence) -> string\n\ |
| 664 | \n\ |
| 665 | Return a string which is the concatenation of the string representation\n\ |
| 666 | of every element in the sequence. The separator between elements is S."; |
| 667 | |
| 668 | static PyObject * |
| 669 | string_join(self, args) |
| 670 | PyStringObject *self; |
| 671 | PyObject *args; |
| 672 | { |
| 673 | char *sep = PyString_AS_STRING(self); |
| 674 | int seplen = PyString_GET_SIZE(self); |
| 675 | PyObject *res = NULL; |
| 676 | int reslen = 0; |
| 677 | char *p; |
| 678 | int seqlen = 0; |
| 679 | int sz = 100; |
| 680 | int i, slen; |
| 681 | PyObject *seq; |
| 682 | |
| 683 | if (!PyArg_ParseTuple(args, "O", &seq)) |
| 684 | return NULL; |
| 685 | |
| 686 | seqlen = PySequence_Length(seq); |
| 687 | if (seqlen < 0 && PyErr_Occurred()) |
| 688 | return NULL; |
| 689 | |
| 690 | if (seqlen == 1) { |
| 691 | /* Optimization if there's only one item */ |
| 692 | PyObject *item = PySequence_GetItem(seq, 0); |
| 693 | PyObject *stritem = PyObject_Str(item); |
| 694 | Py_DECREF(item); |
| 695 | return stritem; |
| 696 | } |
| 697 | if (!(res = PyString_FromStringAndSize((char*)NULL, sz))) |
| 698 | return NULL; |
| 699 | p = PyString_AsString(res); |
| 700 | |
| 701 | /* optimize for lists. all others (tuples and arbitrary sequences) |
| 702 | * just use the abstract interface. |
| 703 | */ |
| 704 | if (PyList_Check(seq)) { |
| 705 | for (i = 0; i < seqlen; i++) { |
| 706 | PyObject *item = PyList_GET_ITEM(seq, i); |
| 707 | PyObject *sitem = PyObject_Str(item); |
| 708 | if (!sitem) |
| 709 | goto finally; |
| 710 | slen = PyString_GET_SIZE(sitem); |
| 711 | while (reslen + slen + seplen >= sz) { |
| 712 | if (_PyString_Resize(&res, sz*2)) |
| 713 | goto finally; |
| 714 | sz *= 2; |
| 715 | p = PyString_AsString(res) + reslen; |
| 716 | } |
| 717 | if (i > 0) { |
| 718 | memcpy(p, sep, seplen); |
| 719 | p += seplen; |
| 720 | reslen += seplen; |
| 721 | } |
| 722 | memcpy(p, PyString_AS_STRING(sitem), slen); |
| 723 | p += slen; |
| 724 | reslen += slen; |
| 725 | } |
| 726 | } |
| 727 | else { |
| 728 | for (i = 0; i < seqlen; i++) { |
| 729 | PyObject *item = PySequence_GetItem(seq, i); |
| 730 | PyObject *sitem; |
| 731 | if (!item || !(sitem = PyObject_Str(item))) { |
| 732 | Py_XDECREF(item); |
| 733 | goto finally; |
| 734 | } |
| 735 | slen = PyString_GET_SIZE(sitem); |
| 736 | while (reslen + slen + seplen >= sz) { |
| 737 | if (_PyString_Resize(&res, sz*2)) |
| 738 | goto finally; |
| 739 | sz *= 2; |
| 740 | p = PyString_AsString(res) + reslen; |
| 741 | } |
| 742 | if (i > 0) { |
| 743 | memcpy(p, sep, seplen); |
| 744 | p += seplen; |
| 745 | reslen += seplen; |
| 746 | } |
| 747 | memcpy(p, PyString_AS_STRING(sitem), slen); |
| 748 | p += slen; |
| 749 | reslen += slen; |
| 750 | } |
| 751 | } |
| 752 | if (_PyString_Resize(&res, reslen)) |
| 753 | goto finally; |
| 754 | return res; |
| 755 | |
| 756 | finally: |
| 757 | Py_DECREF(res); |
| 758 | return NULL; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | |
| 763 | static long |
| 764 | string_find_internal(self, args) |
| 765 | PyStringObject *self; |
| 766 | PyObject *args; |
| 767 | { |
| 768 | char *s = PyString_AS_STRING(self), *sub; |
| 769 | int len = PyString_GET_SIZE(self); |
| 770 | int n, i = 0, last = INT_MAX; |
| 771 | |
| 772 | if (!PyArg_ParseTuple(args, "t#|ii", &sub, &n, &i, &last)) |
| 773 | return -2; |
| 774 | |
| 775 | if (last > len) |
| 776 | last = len; |
| 777 | if (last < 0) |
| 778 | last += len; |
| 779 | if (last < 0) |
| 780 | last = 0; |
| 781 | if (i < 0) |
| 782 | i += len; |
| 783 | if (i < 0) |
| 784 | i = 0; |
| 785 | |
| 786 | if (n == 0 && i <= last) |
| 787 | return (long)i; |
| 788 | |
| 789 | last -= n; |
| 790 | for (; i <= last; ++i) |
| 791 | if (s[i] == sub[0] && |
| 792 | (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0)) |
| 793 | return (long)i; |
| 794 | |
| 795 | return -1; |
| 796 | } |
| 797 | |
| 798 | |
| 799 | static char find__doc__[] = |
| 800 | "S.find(sub [,start [,end]]) -> int\n\ |
| 801 | \n\ |
| 802 | Return the lowest index in S where substring sub is found,\n\ |
| 803 | such that sub is contained within s[start,end]. Optional\n\ |
| 804 | arguments start and end are interpreted as in slice notation.\n\ |
| 805 | \n\ |
| 806 | Return -1 on failure."; |
| 807 | |
| 808 | static PyObject * |
| 809 | string_find(self, args) |
| 810 | PyStringObject *self; |
| 811 | PyObject *args; |
| 812 | { |
| 813 | long result = string_find_internal(self, args); |
| 814 | if (result == -2) |
| 815 | return NULL; |
| 816 | return PyInt_FromLong(result); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | static char index__doc__[] = |
| 821 | "S.index(sub [,start [,end]]) -> int\n\ |
| 822 | \n\ |
| 823 | Like S.find() but raise ValueError when the substring is not found."; |
| 824 | |
| 825 | static PyObject * |
| 826 | string_index(self, args) |
| 827 | PyStringObject *self; |
| 828 | PyObject *args; |
| 829 | { |
| 830 | long result = string_find_internal(self, args); |
| 831 | if (result == -2) |
| 832 | return NULL; |
| 833 | if (result == -1) { |
| 834 | PyErr_SetString(PyExc_ValueError, |
| 835 | "substring not found in string.index"); |
| 836 | return NULL; |
| 837 | } |
| 838 | return PyInt_FromLong(result); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | static long |
| 843 | string_rfind_internal(self, args) |
| 844 | PyStringObject *self; |
| 845 | PyObject *args; |
| 846 | { |
| 847 | char *s = PyString_AS_STRING(self), *sub; |
| 848 | int len = PyString_GET_SIZE(self), n, j; |
| 849 | int i = 0, last = INT_MAX; |
| 850 | |
| 851 | if (!PyArg_ParseTuple(args, "t#|ii", &sub, &n, &i, &last)) |
| 852 | return -2; |
| 853 | |
| 854 | if (last > len) |
| 855 | last = len; |
| 856 | if (last < 0) |
| 857 | last += len; |
| 858 | if (last < 0) |
| 859 | last = 0; |
| 860 | if (i < 0) |
| 861 | i += len; |
| 862 | if (i < 0) |
| 863 | i = 0; |
| 864 | |
| 865 | if (n == 0 && i <= last) |
| 866 | return (long)last; |
| 867 | |
| 868 | for (j = last-n; j >= i; --j) |
| 869 | if (s[j] == sub[0] && |
| 870 | (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0)) |
| 871 | return (long)j; |
| 872 | |
| 873 | return -1; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | static char rfind__doc__[] = |
| 878 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 879 | \n\ |
| 880 | Return the highest index in S where substring sub is found,\n\ |
| 881 | such that sub is contained within s[start,end]. Optional\n\ |
| 882 | arguments start and end are interpreted as in slice notation.\n\ |
| 883 | \n\ |
| 884 | Return -1 on failure."; |
| 885 | |
| 886 | static PyObject * |
| 887 | string_rfind(self, args) |
| 888 | PyStringObject *self; |
| 889 | PyObject *args; |
| 890 | { |
| 891 | long result = string_rfind_internal(self, args); |
| 892 | if (result == -2) |
| 893 | return NULL; |
| 894 | return PyInt_FromLong(result); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | static char rindex__doc__[] = |
| 899 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 900 | \n\ |
| 901 | Like S.rfind() but raise ValueError when the substring is not found."; |
| 902 | |
| 903 | static PyObject * |
| 904 | string_rindex(self, args) |
| 905 | PyStringObject *self; |
| 906 | PyObject *args; |
| 907 | { |
| 908 | long result = string_rfind_internal(self, args); |
| 909 | if (result == -2) |
| 910 | return NULL; |
| 911 | if (result == -1) { |
| 912 | PyErr_SetString(PyExc_ValueError, |
| 913 | "substring not found in string.rindex"); |
| 914 | return NULL; |
| 915 | } |
| 916 | return PyInt_FromLong(result); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | static PyObject * |
| 921 | do_strip(self, args, striptype) |
| 922 | PyStringObject *self; |
| 923 | PyObject *args; |
| 924 | int striptype; |
| 925 | { |
| 926 | char *s = PyString_AS_STRING(self); |
| 927 | int len = PyString_GET_SIZE(self), i, j; |
| 928 | PyObject *scobj = NULL; |
| 929 | int count = -1; |
| 930 | |
| 931 | if (!PyArg_ParseTuple(args, "|Oi", scobj, count)) |
| 932 | return NULL; |
| 933 | |
| 934 | i = 0; |
| 935 | if (striptype != RIGHTSTRIP) { |
| 936 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 937 | i++; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | j = len; |
| 942 | if (striptype != LEFTSTRIP) { |
| 943 | do { |
| 944 | j--; |
| 945 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 946 | j++; |
| 947 | } |
| 948 | |
| 949 | if (i == 0 && j == len) { |
| 950 | Py_INCREF(self); |
| 951 | return (PyObject*)self; |
| 952 | } |
| 953 | else |
| 954 | return PyString_FromStringAndSize(s+i, j-i); |
| 955 | } |
| 956 | |
| 957 | |
| 958 | static char strip__doc__[] = |
| 959 | "S.strip() -> string\n\ |
| 960 | \n\ |
| 961 | Return a copy of the string S with leading and trailing\n\ |
| 962 | whitespace removed."; |
| 963 | |
| 964 | static PyObject * |
| 965 | string_strip(self, args) |
| 966 | PyStringObject *self; |
| 967 | PyObject *args; |
| 968 | { |
| 969 | return do_strip(self, args, BOTHSTRIP); |
| 970 | } |
| 971 | |
| 972 | |
| 973 | static char lstrip__doc__[] = |
| 974 | "S.lstrip() -> string\n\ |
| 975 | \n\ |
| 976 | Return a copy of the string S with leading whitespace removed."; |
| 977 | |
| 978 | static PyObject * |
| 979 | string_lstrip(self, args) |
| 980 | PyStringObject *self; |
| 981 | PyObject *args; |
| 982 | { |
| 983 | return do_strip(self, args, LEFTSTRIP); |
| 984 | } |
| 985 | |
| 986 | |
| 987 | static char rstrip__doc__[] = |
| 988 | "S.rstrip() -> string\n\ |
| 989 | \n\ |
| 990 | Return a copy of the string S with trailing whitespace removed."; |
| 991 | |
| 992 | static PyObject * |
| 993 | string_rstrip(self, args) |
| 994 | PyStringObject *self; |
| 995 | PyObject *args; |
| 996 | { |
| 997 | return do_strip(self, args, RIGHTSTRIP); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | static char lower__doc__[] = |
| 1002 | "S.lower() -> string\n\ |
| 1003 | \n\ |
| 1004 | Return a copy of the string S converted to lowercase."; |
| 1005 | |
| 1006 | static PyObject * |
| 1007 | string_lower(self, args) |
| 1008 | PyStringObject *self; |
| 1009 | PyObject *args; |
| 1010 | { |
| 1011 | char *s = PyString_AS_STRING(self), *s_new; |
| 1012 | int i, n = PyString_GET_SIZE(self); |
| 1013 | PyObject *new; |
| 1014 | |
| 1015 | if (!PyArg_ParseTuple(args, "")) |
| 1016 | return NULL; |
| 1017 | new = PyString_FromStringAndSize(NULL, n); |
| 1018 | if (new == NULL) |
| 1019 | return NULL; |
| 1020 | s_new = PyString_AsString(new); |
| 1021 | for (i = 0; i < n; i++) { |
| 1022 | int c = Py_CHARMASK(*s++); |
| 1023 | if (isupper(c)) { |
| 1024 | *s_new = tolower(c); |
| 1025 | } else |
| 1026 | *s_new = c; |
| 1027 | s_new++; |
| 1028 | } |
| 1029 | return new; |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | static char upper__doc__[] = |
| 1034 | "S.upper() -> string\n\ |
| 1035 | \n\ |
| 1036 | Return a copy of the string S converted to uppercase."; |
| 1037 | |
| 1038 | static PyObject * |
| 1039 | string_upper(self, args) |
| 1040 | PyStringObject *self; |
| 1041 | PyObject *args; |
| 1042 | { |
| 1043 | char *s = PyString_AS_STRING(self), *s_new; |
| 1044 | int i, n = PyString_GET_SIZE(self); |
| 1045 | PyObject *new; |
| 1046 | |
| 1047 | if (!PyArg_ParseTuple(args, "")) |
| 1048 | return NULL; |
| 1049 | new = PyString_FromStringAndSize(NULL, n); |
| 1050 | if (new == NULL) |
| 1051 | return NULL; |
| 1052 | s_new = PyString_AsString(new); |
| 1053 | for (i = 0; i < n; i++) { |
| 1054 | int c = Py_CHARMASK(*s++); |
| 1055 | if (islower(c)) { |
| 1056 | *s_new = toupper(c); |
| 1057 | } else |
| 1058 | *s_new = c; |
| 1059 | s_new++; |
| 1060 | } |
| 1061 | return new; |
| 1062 | } |
| 1063 | |
| 1064 | |
| 1065 | static char capitalize__doc__[] = |
| 1066 | "S.capitalize() -> string\n\ |
| 1067 | \n\ |
| 1068 | Return a copy of the string S with only its first character\n\ |
| 1069 | capitalized."; |
| 1070 | |
| 1071 | static PyObject * |
| 1072 | string_capitalize(self, args) |
| 1073 | PyStringObject *self; |
| 1074 | PyObject *args; |
| 1075 | { |
| 1076 | char *s = PyString_AS_STRING(self), *s_new; |
| 1077 | int i, n = PyString_GET_SIZE(self); |
| 1078 | PyObject *new; |
| 1079 | |
| 1080 | if (!PyArg_ParseTuple(args, "")) |
| 1081 | return NULL; |
| 1082 | new = PyString_FromStringAndSize(NULL, n); |
| 1083 | if (new == NULL) |
| 1084 | return NULL; |
| 1085 | s_new = PyString_AsString(new); |
| 1086 | if (0 < n) { |
| 1087 | int c = Py_CHARMASK(*s++); |
| 1088 | if (islower(c)) |
| 1089 | *s_new = toupper(c); |
| 1090 | else |
| 1091 | *s_new = c; |
| 1092 | s_new++; |
| 1093 | } |
| 1094 | for (i = 1; i < n; i++) { |
| 1095 | int c = Py_CHARMASK(*s++); |
| 1096 | if (isupper(c)) |
| 1097 | *s_new = tolower(c); |
| 1098 | else |
| 1099 | *s_new = c; |
| 1100 | s_new++; |
| 1101 | } |
| 1102 | return new; |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | static char count__doc__[] = |
| 1107 | "S.count(sub[, start[, end]]) -> int\n\ |
| 1108 | \n\ |
| 1109 | Return the number of occurrences of substring sub in string\n\ |
| 1110 | S[start:end]. Optional arguments start and end are\n\ |
| 1111 | interpreted as in slice notation."; |
| 1112 | |
| 1113 | static PyObject * |
| 1114 | string_count(self, args) |
| 1115 | PyStringObject *self; |
| 1116 | PyObject *args; |
| 1117 | { |
| 1118 | char *s = PyString_AS_STRING(self), *sub; |
| 1119 | int len = PyString_GET_SIZE(self), n; |
| 1120 | int i = 0, last = INT_MAX; |
| 1121 | int m, r; |
| 1122 | |
| 1123 | if (!PyArg_ParseTuple(args, "t#|ii", &sub, &n, &i, &last)) |
| 1124 | return NULL; |
| 1125 | if (last > len) |
| 1126 | last = len; |
| 1127 | if (last < 0) |
| 1128 | last += len; |
| 1129 | if (last < 0) |
| 1130 | last = 0; |
| 1131 | if (i < 0) |
| 1132 | i += len; |
| 1133 | if (i < 0) |
| 1134 | i = 0; |
| 1135 | m = last + 1 - n; |
| 1136 | if (n == 0) |
| 1137 | return PyInt_FromLong((long) (m-i)); |
| 1138 | |
| 1139 | r = 0; |
| 1140 | while (i < m) { |
| 1141 | if (!memcmp(s+i, sub, n)) { |
| 1142 | r++; |
| 1143 | i += n; |
| 1144 | } else { |
| 1145 | i++; |
| 1146 | } |
| 1147 | } |
| 1148 | return PyInt_FromLong((long) r); |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | static char swapcase__doc__[] = |
| 1153 | "S.swapcase() -> string\n\ |
| 1154 | \n\ |
| 1155 | Return a copy of the string S with upper case characters\n\ |
| 1156 | converted to lowercase and vice versa."; |
| 1157 | |
| 1158 | static PyObject * |
| 1159 | string_swapcase(self, args) |
| 1160 | PyStringObject *self; |
| 1161 | PyObject *args; |
| 1162 | { |
| 1163 | char *s = PyString_AS_STRING(self), *s_new; |
| 1164 | int i, n = PyString_GET_SIZE(self); |
| 1165 | PyObject *new; |
| 1166 | |
| 1167 | if (!PyArg_ParseTuple(args, "")) |
| 1168 | return NULL; |
| 1169 | new = PyString_FromStringAndSize(NULL, n); |
| 1170 | if (new == NULL) |
| 1171 | return NULL; |
| 1172 | s_new = PyString_AsString(new); |
| 1173 | for (i = 0; i < n; i++) { |
| 1174 | int c = Py_CHARMASK(*s++); |
| 1175 | if (islower(c)) { |
| 1176 | *s_new = toupper(c); |
| 1177 | } |
| 1178 | else if (isupper(c)) { |
| 1179 | *s_new = tolower(c); |
| 1180 | } |
| 1181 | else |
| 1182 | *s_new = c; |
| 1183 | s_new++; |
| 1184 | } |
| 1185 | return new; |
| 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | static char translate__doc__[] = |
| 1190 | "S.translate(table [,deletechars]) -> string\n\ |
| 1191 | \n\ |
| 1192 | Return a copy of the string S, where all characters occurring\n\ |
| 1193 | in the optional argument deletechars are removed, and the\n\ |
| 1194 | remaining characters have been mapped through the given\n\ |
| 1195 | translation table, which must be a string of length 256."; |
| 1196 | |
| 1197 | static PyObject * |
| 1198 | string_translate(self, args) |
| 1199 | PyStringObject *self; |
| 1200 | PyObject *args; |
| 1201 | { |
| 1202 | register char *input, *table, *output; |
| 1203 | register int i, c, changed = 0; |
| 1204 | PyObject *input_obj = (PyObject*)self; |
| 1205 | char *table1, *output_start, *del_table=NULL; |
| 1206 | int inlen, tablen, dellen = 0; |
| 1207 | PyObject *result; |
| 1208 | int trans_table[256]; |
| 1209 | |
| 1210 | if (!PyArg_ParseTuple(args, "t#|t#", |
| 1211 | &table1, &tablen, &del_table, &dellen)) |
| 1212 | return NULL; |
| 1213 | if (tablen != 256) { |
| 1214 | PyErr_SetString(PyExc_ValueError, |
| 1215 | "translation table must be 256 characters long"); |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | |
| 1219 | table = table1; |
| 1220 | inlen = PyString_Size(input_obj); |
| 1221 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 1222 | if (result == NULL) |
| 1223 | return NULL; |
| 1224 | output_start = output = PyString_AsString(result); |
| 1225 | input = PyString_AsString(input_obj); |
| 1226 | |
| 1227 | if (dellen == 0) { |
| 1228 | /* If no deletions are required, use faster code */ |
| 1229 | for (i = inlen; --i >= 0; ) { |
| 1230 | c = Py_CHARMASK(*input++); |
| 1231 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1232 | changed = 1; |
| 1233 | } |
| 1234 | if (changed) |
| 1235 | return result; |
| 1236 | Py_DECREF(result); |
| 1237 | Py_INCREF(input_obj); |
| 1238 | return input_obj; |
| 1239 | } |
| 1240 | |
| 1241 | for (i = 0; i < 256; i++) |
| 1242 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1243 | |
| 1244 | for (i = 0; i < dellen; i++) |
| 1245 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 1246 | |
| 1247 | for (i = inlen; --i >= 0; ) { |
| 1248 | c = Py_CHARMASK(*input++); |
| 1249 | if (trans_table[c] != -1) |
| 1250 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1251 | continue; |
| 1252 | changed = 1; |
| 1253 | } |
| 1254 | if (!changed) { |
| 1255 | Py_DECREF(result); |
| 1256 | Py_INCREF(input_obj); |
| 1257 | return input_obj; |
| 1258 | } |
| 1259 | /* Fix the size of the resulting string */ |
| 1260 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 1261 | return NULL; |
| 1262 | return result; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 1267 | |
| 1268 | /* |
| 1269 | mymemfind |
| 1270 | |
| 1271 | strstr replacement for arbitrary blocks of memory. |
| 1272 | |
| 1273 | Locates the first occurance in the memory pointed to by MEM of the |
| 1274 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 1275 | found, or -1 if not found. If len of PAT is greater than length of |
| 1276 | MEM, the function returns -1. |
| 1277 | */ |
| 1278 | static int |
| 1279 | mymemfind(mem, len, pat, pat_len) |
| 1280 | char *mem; |
| 1281 | int len; |
| 1282 | char *pat; |
| 1283 | int pat_len; |
| 1284 | { |
| 1285 | register int ii; |
| 1286 | |
| 1287 | /* pattern can not occur in the last pat_len-1 chars */ |
| 1288 | len -= pat_len; |
| 1289 | |
| 1290 | for (ii = 0; ii <= len; ii++) { |
| 1291 | if (mem[ii] == pat[0] && |
| 1292 | (pat_len == 1 || |
| 1293 | memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) { |
| 1294 | return ii; |
| 1295 | } |
| 1296 | } |
| 1297 | return -1; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | mymemcnt |
| 1302 | |
| 1303 | Return the number of distinct times PAT is found in MEM. |
| 1304 | meaning mem=1111 and pat==11 returns 2. |
| 1305 | mem=11111 and pat==11 also return 2. |
| 1306 | */ |
| 1307 | static int |
| 1308 | mymemcnt(mem, len, pat, pat_len) |
| 1309 | char *mem; |
| 1310 | int len; |
| 1311 | char *pat; |
| 1312 | int pat_len; |
| 1313 | { |
| 1314 | register int offset = 0; |
| 1315 | int nfound = 0; |
| 1316 | |
| 1317 | while (len >= 0) { |
| 1318 | offset = mymemfind(mem, len, pat, pat_len); |
| 1319 | if (offset == -1) |
| 1320 | break; |
| 1321 | mem += offset + pat_len; |
| 1322 | len -= offset + pat_len; |
| 1323 | nfound++; |
| 1324 | } |
| 1325 | return nfound; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | mymemreplace |
| 1330 | |
| 1331 | Return a string in which all occurences of PAT in memory STR are |
| 1332 | replaced with SUB. |
| 1333 | |
| 1334 | If length of PAT is less than length of STR or there are no occurences |
| 1335 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 1336 | string is allocated here and returned. |
| 1337 | |
| 1338 | on return, out_len is: |
| 1339 | the length of output string, or |
| 1340 | -1 if the input string is returned, or |
| 1341 | unchanged if an error occurs (no memory). |
| 1342 | |
| 1343 | return value is: |
| 1344 | the new string allocated locally, or |
| 1345 | NULL if an error occurred. |
| 1346 | */ |
| 1347 | static char * |
| 1348 | mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len) |
| 1349 | char *str; |
| 1350 | int len; /* input string */ |
| 1351 | char *pat; |
| 1352 | int pat_len; /* pattern string to find */ |
| 1353 | char *sub; |
| 1354 | int sub_len; /* substitution string */ |
| 1355 | int count; /* number of replacements, 0 == all */ |
| 1356 | int *out_len; |
| 1357 | |
| 1358 | { |
| 1359 | char *out_s; |
| 1360 | char *new_s; |
| 1361 | int nfound, offset, new_len; |
| 1362 | |
| 1363 | if (len == 0 || pat_len > len) |
| 1364 | goto return_same; |
| 1365 | |
| 1366 | /* find length of output string */ |
| 1367 | nfound = mymemcnt(str, len, pat, pat_len); |
| 1368 | if (count > 0) |
| 1369 | nfound = nfound > count ? count : nfound; |
| 1370 | if (nfound == 0) |
| 1371 | goto return_same; |
| 1372 | new_len = len + nfound*(sub_len - pat_len); |
| 1373 | |
| 1374 | new_s = (char *)malloc(new_len); |
| 1375 | if (new_s == NULL) return NULL; |
| 1376 | |
| 1377 | *out_len = new_len; |
| 1378 | out_s = new_s; |
| 1379 | |
| 1380 | while (len > 0) { |
| 1381 | /* find index of next instance of pattern */ |
| 1382 | offset = mymemfind(str, len, pat, pat_len); |
| 1383 | /* if not found, break out of loop */ |
| 1384 | if (offset == -1) break; |
| 1385 | |
| 1386 | /* copy non matching part of input string */ |
| 1387 | memcpy(new_s, str, offset); /* copy part of str before pat */ |
| 1388 | str += offset + pat_len; /* move str past pattern */ |
| 1389 | len -= offset + pat_len; /* reduce length of str remaining */ |
| 1390 | |
| 1391 | /* copy substitute into the output string */ |
| 1392 | new_s += offset; /* move new_s to dest for sub string */ |
| 1393 | memcpy(new_s, sub, sub_len); /* copy substring into new_s */ |
| 1394 | new_s += sub_len; /* offset new_s past sub string */ |
| 1395 | |
| 1396 | /* break when we've done count replacements */ |
| 1397 | if (--count == 0) break; |
| 1398 | } |
| 1399 | /* copy any remaining values into output string */ |
| 1400 | if (len > 0) |
| 1401 | memcpy(new_s, str, len); |
| 1402 | return out_s; |
| 1403 | |
| 1404 | return_same: |
| 1405 | *out_len = -1; |
| 1406 | return str; |
| 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | static char replace__doc__[] = |
| 1411 | "S.replace (old, new[, maxsplit]) -> string\n\ |
| 1412 | \n\ |
| 1413 | Return a copy of string S with all occurrences of substring\n\ |
| 1414 | old replaced by new. If the optional argument maxsplit is\n\ |
| 1415 | given, only the first maxsplit occurrences are replaced."; |
| 1416 | |
| 1417 | static PyObject * |
| 1418 | string_replace(self, args) |
| 1419 | PyStringObject *self; |
| 1420 | PyObject *args; |
| 1421 | { |
| 1422 | char *str = PyString_AS_STRING(self), *pat,*sub,*new_s; |
| 1423 | int len = PyString_GET_SIZE(self), pat_len,sub_len,out_len; |
| 1424 | int count = 0; |
| 1425 | PyObject *new; |
| 1426 | |
| 1427 | if (!PyArg_ParseTuple(args, "t#t#|i", |
| 1428 | &pat, &pat_len, &sub, &sub_len, &count)) |
| 1429 | return NULL; |
| 1430 | if (pat_len <= 0) { |
| 1431 | PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
| 1432 | return NULL; |
| 1433 | } |
| 1434 | new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len); |
| 1435 | if (new_s == NULL) { |
| 1436 | PyErr_NoMemory(); |
| 1437 | return NULL; |
| 1438 | } |
| 1439 | if (out_len == -1) { |
| 1440 | /* we're returning another reference to self */ |
| 1441 | new = (PyObject*)self; |
| 1442 | Py_INCREF(new); |
| 1443 | } |
| 1444 | else { |
| 1445 | new = PyString_FromStringAndSize(new_s, out_len); |
| 1446 | free(new_s); |
| 1447 | } |
| 1448 | return new; |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | static char startswith__doc__[] = |
| 1453 | "S.startswith(prefix[, start[, end]]) -> int\n\ |
| 1454 | \n\ |
| 1455 | Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ |
| 1456 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1457 | comparing S at that position."; |
| 1458 | |
| 1459 | static PyObject * |
| 1460 | string_startswith(self, args) |
| 1461 | PyStringObject *self; |
| 1462 | PyObject *args; |
| 1463 | { |
| 1464 | char* str = PyString_AS_STRING(self); |
| 1465 | int len = PyString_GET_SIZE(self); |
| 1466 | char* prefix; |
| 1467 | int plen; |
| 1468 | int start = 0; |
| 1469 | int end = -1; |
| 1470 | |
| 1471 | if (!PyArg_ParseTuple(args, "t#|ii", &prefix, &plen, &start, &end)) |
| 1472 | return NULL; |
| 1473 | |
| 1474 | /* adopt Java semantics for index out of range. it is legal for |
| 1475 | * offset to be == plen, but this only returns true if prefix is |
| 1476 | * the empty string. |
| 1477 | */ |
| 1478 | if (start < 0 || start+plen > len) |
| 1479 | return PyInt_FromLong(0); |
| 1480 | |
| 1481 | if (!memcmp(str+start, prefix, plen)) { |
| 1482 | /* did the match end after the specified end? */ |
| 1483 | if (end < 0) |
| 1484 | return PyInt_FromLong(1); |
| 1485 | else if (end - start < plen) |
| 1486 | return PyInt_FromLong(0); |
| 1487 | else |
| 1488 | return PyInt_FromLong(1); |
| 1489 | } |
| 1490 | else return PyInt_FromLong(0); |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | static char endswith__doc__[] = |
| 1495 | "S.endswith(suffix[, start[, end]]) -> int\n\ |
| 1496 | \n\ |
| 1497 | Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ |
| 1498 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1499 | comparing S at that position."; |
| 1500 | |
| 1501 | static PyObject * |
| 1502 | string_endswith(self, args) |
| 1503 | PyStringObject *self; |
| 1504 | PyObject *args; |
| 1505 | { |
| 1506 | char* str = PyString_AS_STRING(self); |
| 1507 | int len = PyString_GET_SIZE(self); |
| 1508 | char* suffix; |
| 1509 | int plen; |
| 1510 | int start = 0; |
| 1511 | int end = -1; |
| 1512 | int lower, upper; |
| 1513 | |
| 1514 | if (!PyArg_ParseTuple(args, "t#|ii", &suffix, &plen, &start, &end)) |
| 1515 | return NULL; |
| 1516 | |
| 1517 | if (start < 0 || start > len || plen > len) |
| 1518 | return PyInt_FromLong(0); |
| 1519 | |
| 1520 | upper = (end >= 0 && end <= len) ? end : len; |
| 1521 | lower = (upper - plen) > start ? (upper - plen) : start; |
| 1522 | |
| 1523 | if (upper-lower >= plen && !memcmp(str+lower, suffix, plen)) |
| 1524 | return PyInt_FromLong(1); |
| 1525 | else return PyInt_FromLong(0); |
| 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | static PyMethodDef |
| 1531 | string_methods[] = { |
| 1532 | /* counterparts of the obsolete stropmodule functions */ |
| 1533 | {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__}, |
| 1534 | {"count", (PyCFunction)string_count, 1, count__doc__}, |
| 1535 | {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__}, |
| 1536 | {"find", (PyCFunction)string_find, 1, find__doc__}, |
| 1537 | {"index", (PyCFunction)string_index, 1, index__doc__}, |
| 1538 | {"join", (PyCFunction)string_join, 1, join__doc__}, |
| 1539 | {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__}, |
| 1540 | {"lower", (PyCFunction)string_lower, 1, lower__doc__}, |
| 1541 | /* maketrans */ |
| 1542 | {"replace", (PyCFunction)string_replace, 1, replace__doc__}, |
| 1543 | {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__}, |
| 1544 | {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__}, |
| 1545 | {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__}, |
| 1546 | {"split", (PyCFunction)string_split, 1, split__doc__}, |
| 1547 | {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__}, |
| 1548 | {"strip", (PyCFunction)string_strip, 1, strip__doc__}, |
| 1549 | {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__}, |
| 1550 | {"translate", (PyCFunction)string_translate, 1, strip__doc__}, |
| 1551 | {"upper", (PyCFunction)string_upper, 1, upper__doc__}, |
| 1552 | /* TBD */ |
| 1553 | /* {"ljust" (PyCFunction)string_ljust, 1, ljust__doc__}, */ |
| 1554 | /* {"rjust" (PyCFunction)string_rjust, 1, rjust__doc__}, */ |
| 1555 | /* {"center" (PyCFunction)string_center, 1, center__doc__}, */ |
| 1556 | /* {"zfill" (PyCFunction)string_zfill, 1, zfill__doc__}, */ |
| 1557 | /* {"expandtabs" (PyCFunction)string_expandtabs, 1, ljust__doc__}, */ |
| 1558 | /* {"capwords" (PyCFunction)string_capwords, 1, capwords__doc__}, */ |
| 1559 | {NULL, NULL} /* sentinel */ |
| 1560 | }; |
| 1561 | |
| 1562 | static PyObject * |
| 1563 | string_getattr(s, name) |
| 1564 | PyStringObject *s; |
| 1565 | char *name; |
| 1566 | { |
| 1567 | return Py_FindMethod(string_methods, (PyObject*)s, name); |
| 1568 | } |
| 1569 | |
| 1570 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1571 | PyTypeObject PyString_Type = { |
| 1572 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1573 | 0, |
| 1574 | "string", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1575 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1576 | sizeof(char), |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1577 | (destructor)string_dealloc, /*tp_dealloc*/ |
| 1578 | (printfunc)string_print, /*tp_print*/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1579 | (getattrfunc)string_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1580 | 0, /*tp_setattr*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1581 | (cmpfunc)string_compare, /*tp_compare*/ |
| 1582 | (reprfunc)string_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1583 | 0, /*tp_as_number*/ |
| 1584 | &string_as_sequence, /*tp_as_sequence*/ |
| 1585 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1586 | (hashfunc)string_hash, /*tp_hash*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 1587 | 0, /*tp_call*/ |
| 1588 | 0, /*tp_str*/ |
| 1589 | 0, /*tp_getattro*/ |
| 1590 | 0, /*tp_setattro*/ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1591 | &string_as_buffer, /*tp_as_buffer*/ |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1592 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 1593 | 0, /*tp_doc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1594 | }; |
| 1595 | |
| 1596 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1597 | PyString_Concat(pv, w) |
| 1598 | register PyObject **pv; |
| 1599 | register PyObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1600 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1601 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1602 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1603 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1604 | if (w == NULL || !PyString_Check(*pv)) { |
| 1605 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1606 | *pv = NULL; |
| 1607 | return; |
| 1608 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1609 | v = string_concat((PyStringObject *) *pv, w); |
| 1610 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1611 | *pv = v; |
| 1612 | } |
| 1613 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1614 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1615 | PyString_ConcatAndDel(pv, w) |
| 1616 | register PyObject **pv; |
| 1617 | register PyObject *w; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1618 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1619 | PyString_Concat(pv, w); |
| 1620 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1624 | /* The following function breaks the notion that strings are immutable: |
| 1625 | it changes the size of a string. We get away with this only if there |
| 1626 | is only one module referencing the object. You can also think of it |
| 1627 | as creating a new string object and destroying the old one, only |
| 1628 | more efficiently. In any case, don't use this if the string may |
| 1629 | already be known to some other part of the code... */ |
| 1630 | |
| 1631 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1632 | _PyString_Resize(pv, newsize) |
| 1633 | PyObject **pv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1634 | int newsize; |
| 1635 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1636 | register PyObject *v; |
| 1637 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1638 | v = *pv; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1639 | if (!PyString_Check(v) || v->ob_refcnt != 1) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1640 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1641 | Py_DECREF(v); |
| 1642 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1643 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1644 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1645 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 1646 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 1647 | --_Py_RefTotal; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1648 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1649 | _Py_ForgetReference(v); |
| 1650 | *pv = (PyObject *) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1651 | realloc((char *)v, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1652 | sizeof(PyStringObject) + newsize * sizeof(char)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1653 | if (*pv == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1654 | PyMem_DEL(v); |
| 1655 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1656 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1657 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1658 | _Py_NewReference(*pv); |
| 1659 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1660 | sv->ob_size = newsize; |
| 1661 | sv->ob_sval[newsize] = '\0'; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1664 | |
| 1665 | /* Helpers for formatstring */ |
| 1666 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1667 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1668 | getnextarg(args, arglen, p_argidx) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1669 | PyObject *args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1670 | int arglen; |
| 1671 | int *p_argidx; |
| 1672 | { |
| 1673 | int argidx = *p_argidx; |
| 1674 | if (argidx < arglen) { |
| 1675 | (*p_argidx)++; |
| 1676 | if (arglen < 0) |
| 1677 | return args; |
| 1678 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1679 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1680 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1681 | PyErr_SetString(PyExc_TypeError, |
| 1682 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1683 | return NULL; |
| 1684 | } |
| 1685 | |
| 1686 | #define F_LJUST (1<<0) |
| 1687 | #define F_SIGN (1<<1) |
| 1688 | #define F_BLANK (1<<2) |
| 1689 | #define F_ALT (1<<3) |
| 1690 | #define F_ZERO (1<<4) |
| 1691 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1692 | static int |
| 1693 | formatfloat(buf, flags, prec, type, v) |
| 1694 | char *buf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1695 | int flags; |
| 1696 | int prec; |
| 1697 | int type; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1698 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1699 | { |
| 1700 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1701 | double x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1702 | if (!PyArg_Parse(v, "d;float argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1703 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1704 | if (prec < 0) |
| 1705 | prec = 6; |
| 1706 | if (prec > 50) |
| 1707 | prec = 50; /* Arbitrary limitation */ |
| 1708 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 1709 | type = 'g'; |
| 1710 | sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type); |
| 1711 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1712 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1715 | static int |
| 1716 | formatint(buf, flags, prec, type, v) |
| 1717 | char *buf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1718 | int flags; |
| 1719 | int prec; |
| 1720 | int type; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1721 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1722 | { |
| 1723 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1724 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1725 | if (!PyArg_Parse(v, "l;int argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1726 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1727 | if (prec < 0) |
| 1728 | prec = 1; |
| 1729 | sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type); |
| 1730 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1731 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1734 | static int |
| 1735 | formatchar(buf, v) |
| 1736 | char *buf; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1737 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1738 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1739 | if (PyString_Check(v)) { |
| 1740 | 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] | 1741 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1742 | } |
| 1743 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1744 | 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] | 1745 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1746 | } |
| 1747 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1748 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1751 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1752 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ |
| 1753 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1754 | PyObject * |
| 1755 | PyString_Format(format, args) |
| 1756 | PyObject *format; |
| 1757 | PyObject *args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1758 | { |
| 1759 | char *fmt, *res; |
| 1760 | int fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1761 | int args_owned = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1762 | PyObject *result; |
| 1763 | PyObject *dict = NULL; |
| 1764 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 1765 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1766 | return NULL; |
| 1767 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1768 | fmt = PyString_AsString(format); |
| 1769 | fmtcnt = PyString_Size(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1770 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1771 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1772 | if (result == NULL) |
| 1773 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1774 | res = PyString_AsString(result); |
| 1775 | if (PyTuple_Check(args)) { |
| 1776 | arglen = PyTuple_Size(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1777 | argidx = 0; |
| 1778 | } |
| 1779 | else { |
| 1780 | arglen = -1; |
| 1781 | argidx = -2; |
| 1782 | } |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1783 | if (args->ob_type->tp_as_mapping) |
| 1784 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1785 | while (--fmtcnt >= 0) { |
| 1786 | if (*fmt != '%') { |
| 1787 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1788 | rescnt = fmtcnt + 100; |
| 1789 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1790 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1791 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1792 | res = PyString_AsString(result) |
| 1793 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1794 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1795 | } |
| 1796 | *res++ = *fmt++; |
| 1797 | } |
| 1798 | else { |
| 1799 | /* Got a format specifier */ |
| 1800 | int flags = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1801 | int width = -1; |
| 1802 | int prec = -1; |
| 1803 | int size = 0; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 1804 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1805 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1806 | PyObject *v = NULL; |
| 1807 | PyObject *temp = NULL; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1808 | char *buf; |
| 1809 | int sign; |
| 1810 | int len; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1811 | char tmpbuf[120]; /* For format{float,int,char}() */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1812 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1813 | if (*fmt == '(') { |
| 1814 | char *keystart; |
| 1815 | int keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1816 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1817 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1818 | |
| 1819 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1820 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1821 | "format requires a mapping"); |
| 1822 | goto error; |
| 1823 | } |
| 1824 | ++fmt; |
| 1825 | --fmtcnt; |
| 1826 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1827 | /* Skip over balanced parentheses */ |
| 1828 | while (pcount > 0 && --fmtcnt >= 0) { |
| 1829 | if (*fmt == ')') |
| 1830 | --pcount; |
| 1831 | else if (*fmt == '(') |
| 1832 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1833 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1834 | } |
| 1835 | keylen = fmt - keystart - 1; |
| 1836 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1837 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1838 | "incomplete format key"); |
| 1839 | goto error; |
| 1840 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1841 | key = PyString_FromStringAndSize(keystart, |
| 1842 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1843 | if (key == NULL) |
| 1844 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1845 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1846 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1847 | args_owned = 0; |
| 1848 | } |
| 1849 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1850 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1851 | if (args == NULL) { |
| 1852 | goto error; |
| 1853 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1854 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1855 | arglen = -1; |
| 1856 | argidx = -2; |
| 1857 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1858 | while (--fmtcnt >= 0) { |
| 1859 | switch (c = *fmt++) { |
| 1860 | case '-': flags |= F_LJUST; continue; |
| 1861 | case '+': flags |= F_SIGN; continue; |
| 1862 | case ' ': flags |= F_BLANK; continue; |
| 1863 | case '#': flags |= F_ALT; continue; |
| 1864 | case '0': flags |= F_ZERO; continue; |
| 1865 | } |
| 1866 | break; |
| 1867 | } |
| 1868 | if (c == '*') { |
| 1869 | v = getnextarg(args, arglen, &argidx); |
| 1870 | if (v == NULL) |
| 1871 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1872 | if (!PyInt_Check(v)) { |
| 1873 | PyErr_SetString(PyExc_TypeError, |
| 1874 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1875 | goto error; |
| 1876 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1877 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 1878 | if (width < 0) { |
| 1879 | flags |= F_LJUST; |
| 1880 | width = -width; |
| 1881 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1882 | if (--fmtcnt >= 0) |
| 1883 | c = *fmt++; |
| 1884 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1885 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1886 | width = c - '0'; |
| 1887 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1888 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1889 | if (!isdigit(c)) |
| 1890 | break; |
| 1891 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1892 | PyErr_SetString( |
| 1893 | PyExc_ValueError, |
| 1894 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1895 | goto error; |
| 1896 | } |
| 1897 | width = width*10 + (c - '0'); |
| 1898 | } |
| 1899 | } |
| 1900 | if (c == '.') { |
| 1901 | prec = 0; |
| 1902 | if (--fmtcnt >= 0) |
| 1903 | c = *fmt++; |
| 1904 | if (c == '*') { |
| 1905 | v = getnextarg(args, arglen, &argidx); |
| 1906 | if (v == NULL) |
| 1907 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1908 | if (!PyInt_Check(v)) { |
| 1909 | PyErr_SetString( |
| 1910 | PyExc_TypeError, |
| 1911 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1912 | goto error; |
| 1913 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1914 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1915 | if (prec < 0) |
| 1916 | prec = 0; |
| 1917 | if (--fmtcnt >= 0) |
| 1918 | c = *fmt++; |
| 1919 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1920 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1921 | prec = c - '0'; |
| 1922 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1923 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1924 | if (!isdigit(c)) |
| 1925 | break; |
| 1926 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1927 | PyErr_SetString( |
| 1928 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1929 | "prec too big"); |
| 1930 | goto error; |
| 1931 | } |
| 1932 | prec = prec*10 + (c - '0'); |
| 1933 | } |
| 1934 | } |
| 1935 | } /* prec */ |
| 1936 | if (fmtcnt >= 0) { |
| 1937 | if (c == 'h' || c == 'l' || c == 'L') { |
| 1938 | size = c; |
| 1939 | if (--fmtcnt >= 0) |
| 1940 | c = *fmt++; |
| 1941 | } |
| 1942 | } |
| 1943 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1944 | PyErr_SetString(PyExc_ValueError, |
| 1945 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1946 | goto error; |
| 1947 | } |
| 1948 | if (c != '%') { |
| 1949 | v = getnextarg(args, arglen, &argidx); |
| 1950 | if (v == NULL) |
| 1951 | goto error; |
| 1952 | } |
| 1953 | sign = 0; |
| 1954 | fill = ' '; |
| 1955 | switch (c) { |
| 1956 | case '%': |
| 1957 | buf = "%"; |
| 1958 | len = 1; |
| 1959 | break; |
| 1960 | case 's': |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1961 | temp = PyObject_Str(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1962 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1963 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 1964 | if (!PyString_Check(temp)) { |
| 1965 | PyErr_SetString(PyExc_TypeError, |
| 1966 | "%s argument has non-string str()"); |
| 1967 | goto error; |
| 1968 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1969 | buf = PyString_AsString(temp); |
| 1970 | len = PyString_Size(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1971 | if (prec >= 0 && len > prec) |
| 1972 | len = prec; |
| 1973 | break; |
| 1974 | case 'i': |
| 1975 | case 'd': |
| 1976 | case 'u': |
| 1977 | case 'o': |
| 1978 | case 'x': |
| 1979 | case 'X': |
| 1980 | if (c == 'i') |
| 1981 | c = 'd'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1982 | buf = tmpbuf; |
| 1983 | len = formatint(buf, flags, prec, c, v); |
| 1984 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1985 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1986 | sign = (c == 'd'); |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 1987 | if (flags&F_ZERO) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1988 | fill = '0'; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 1989 | if ((flags&F_ALT) && |
| 1990 | (c == 'x' || c == 'X') && |
| 1991 | buf[0] == '0' && buf[1] == c) { |
| 1992 | *res++ = *buf++; |
| 1993 | *res++ = *buf++; |
| 1994 | rescnt -= 2; |
| 1995 | len -= 2; |
| 1996 | width -= 2; |
| 1997 | if (width < 0) |
| 1998 | width = 0; |
| 1999 | } |
| 2000 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2001 | break; |
| 2002 | case 'e': |
| 2003 | case 'E': |
| 2004 | case 'f': |
| 2005 | case 'g': |
| 2006 | case 'G': |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2007 | buf = tmpbuf; |
| 2008 | len = formatfloat(buf, flags, prec, c, v); |
| 2009 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2010 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2011 | sign = 1; |
| 2012 | if (flags&F_ZERO) |
| 2013 | fill = '0'; |
| 2014 | break; |
| 2015 | case 'c': |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2016 | buf = tmpbuf; |
| 2017 | len = formatchar(buf, v); |
| 2018 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2019 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2020 | break; |
| 2021 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2022 | PyErr_Format(PyExc_ValueError, |
| 2023 | "unsupported format character '%c' (0x%x)", |
| 2024 | c, c); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2025 | goto error; |
| 2026 | } |
| 2027 | if (sign) { |
| 2028 | if (*buf == '-' || *buf == '+') { |
| 2029 | sign = *buf++; |
| 2030 | len--; |
| 2031 | } |
| 2032 | else if (flags & F_SIGN) |
| 2033 | sign = '+'; |
| 2034 | else if (flags & F_BLANK) |
| 2035 | sign = ' '; |
| 2036 | else |
| 2037 | sign = '\0'; |
| 2038 | } |
| 2039 | if (width < len) |
| 2040 | width = len; |
| 2041 | if (rescnt < width + (sign != '\0')) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2042 | reslen -= rescnt; |
| 2043 | rescnt = width + fmtcnt + 100; |
| 2044 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2045 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2046 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2047 | res = PyString_AsString(result) |
| 2048 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2049 | } |
| 2050 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 2051 | if (fill != ' ') |
| 2052 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2053 | rescnt--; |
| 2054 | if (width > len) |
| 2055 | width--; |
| 2056 | } |
| 2057 | if (width > len && !(flags&F_LJUST)) { |
| 2058 | do { |
| 2059 | --rescnt; |
| 2060 | *res++ = fill; |
| 2061 | } while (--width > len); |
| 2062 | } |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 2063 | if (sign && fill == ' ') |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 2064 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2065 | memcpy(res, buf, len); |
| 2066 | res += len; |
| 2067 | rescnt -= len; |
| 2068 | while (--width >= len) { |
| 2069 | --rescnt; |
| 2070 | *res++ = ' '; |
| 2071 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2072 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2073 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2074 | "not all arguments converted"); |
| 2075 | goto error; |
| 2076 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2077 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2078 | } /* '%' */ |
| 2079 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 2080 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2081 | PyErr_SetString(PyExc_TypeError, |
| 2082 | "not all arguments converted"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2083 | goto error; |
| 2084 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2085 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2086 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2087 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2088 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2089 | return result; |
| 2090 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2091 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2092 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2093 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2094 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2095 | return NULL; |
| 2096 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2097 | |
| 2098 | |
| 2099 | #ifdef INTERN_STRINGS |
| 2100 | |
| 2101 | static PyObject *interned; |
| 2102 | |
| 2103 | void |
| 2104 | PyString_InternInPlace(p) |
| 2105 | PyObject **p; |
| 2106 | { |
| 2107 | register PyStringObject *s = (PyStringObject *)(*p); |
| 2108 | PyObject *t; |
| 2109 | if (s == NULL || !PyString_Check(s)) |
| 2110 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
| 2111 | if ((t = s->ob_sinterned) != NULL) { |
| 2112 | if (t == (PyObject *)s) |
| 2113 | return; |
| 2114 | Py_INCREF(t); |
| 2115 | *p = t; |
| 2116 | Py_DECREF(s); |
| 2117 | return; |
| 2118 | } |
| 2119 | if (interned == NULL) { |
| 2120 | interned = PyDict_New(); |
| 2121 | if (interned == NULL) |
| 2122 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2123 | } |
| 2124 | if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { |
| 2125 | Py_INCREF(t); |
| 2126 | *p = s->ob_sinterned = t; |
| 2127 | Py_DECREF(s); |
| 2128 | return; |
| 2129 | } |
| 2130 | t = (PyObject *)s; |
| 2131 | if (PyDict_SetItem(interned, t, t) == 0) { |
| 2132 | s->ob_sinterned = t; |
| 2133 | return; |
| 2134 | } |
| 2135 | PyErr_Clear(); |
| 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | PyObject * |
| 2140 | PyString_InternFromString(cp) |
| 2141 | const char *cp; |
| 2142 | { |
| 2143 | PyObject *s = PyString_FromString(cp); |
| 2144 | if (s == NULL) |
| 2145 | return NULL; |
| 2146 | PyString_InternInPlace(&s); |
| 2147 | return s; |
| 2148 | } |
| 2149 | |
| 2150 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2151 | |
| 2152 | void |
| 2153 | PyString_Fini() |
| 2154 | { |
| 2155 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2156 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 2157 | Py_XDECREF(characters[i]); |
| 2158 | characters[i] = NULL; |
| 2159 | } |
| 2160 | #ifndef DONT_SHARE_SHORT_STRINGS |
| 2161 | Py_XDECREF(nullstring); |
| 2162 | nullstring = NULL; |
| 2163 | #endif |
Guido van Rossum | 971a7aa | 1997-08-05 02:15:12 +0000 | [diff] [blame] | 2164 | #ifdef INTERN_STRINGS |
| 2165 | if (interned) { |
| 2166 | int pos, changed; |
| 2167 | PyObject *key, *value; |
| 2168 | do { |
| 2169 | changed = 0; |
| 2170 | pos = 0; |
| 2171 | while (PyDict_Next(interned, &pos, &key, &value)) { |
| 2172 | if (key->ob_refcnt == 2 && key == value) { |
| 2173 | PyDict_DelItem(interned, key); |
| 2174 | changed = 1; |
| 2175 | } |
| 2176 | } |
| 2177 | } while (changed); |
| 2178 | } |
| 2179 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2180 | } |