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; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 928 | |
Barry Warsaw | 153a27c | 1999-12-15 02:22:52 +0000 | [diff] [blame] | 929 | if (!PyArg_ParseTuple(args, "")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 930 | return NULL; |
| 931 | |
| 932 | i = 0; |
| 933 | if (striptype != RIGHTSTRIP) { |
| 934 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 935 | i++; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | j = len; |
| 940 | if (striptype != LEFTSTRIP) { |
| 941 | do { |
| 942 | j--; |
| 943 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 944 | j++; |
| 945 | } |
| 946 | |
| 947 | if (i == 0 && j == len) { |
| 948 | Py_INCREF(self); |
| 949 | return (PyObject*)self; |
| 950 | } |
| 951 | else |
| 952 | return PyString_FromStringAndSize(s+i, j-i); |
| 953 | } |
| 954 | |
| 955 | |
| 956 | static char strip__doc__[] = |
| 957 | "S.strip() -> string\n\ |
| 958 | \n\ |
| 959 | Return a copy of the string S with leading and trailing\n\ |
| 960 | whitespace removed."; |
| 961 | |
| 962 | static PyObject * |
| 963 | string_strip(self, args) |
| 964 | PyStringObject *self; |
| 965 | PyObject *args; |
| 966 | { |
| 967 | return do_strip(self, args, BOTHSTRIP); |
| 968 | } |
| 969 | |
| 970 | |
| 971 | static char lstrip__doc__[] = |
| 972 | "S.lstrip() -> string\n\ |
| 973 | \n\ |
| 974 | Return a copy of the string S with leading whitespace removed."; |
| 975 | |
| 976 | static PyObject * |
| 977 | string_lstrip(self, args) |
| 978 | PyStringObject *self; |
| 979 | PyObject *args; |
| 980 | { |
| 981 | return do_strip(self, args, LEFTSTRIP); |
| 982 | } |
| 983 | |
| 984 | |
| 985 | static char rstrip__doc__[] = |
| 986 | "S.rstrip() -> string\n\ |
| 987 | \n\ |
| 988 | Return a copy of the string S with trailing whitespace removed."; |
| 989 | |
| 990 | static PyObject * |
| 991 | string_rstrip(self, args) |
| 992 | PyStringObject *self; |
| 993 | PyObject *args; |
| 994 | { |
| 995 | return do_strip(self, args, RIGHTSTRIP); |
| 996 | } |
| 997 | |
| 998 | |
| 999 | static char lower__doc__[] = |
| 1000 | "S.lower() -> string\n\ |
| 1001 | \n\ |
| 1002 | Return a copy of the string S converted to lowercase."; |
| 1003 | |
| 1004 | static PyObject * |
| 1005 | string_lower(self, args) |
| 1006 | PyStringObject *self; |
| 1007 | PyObject *args; |
| 1008 | { |
| 1009 | char *s = PyString_AS_STRING(self), *s_new; |
| 1010 | int i, n = PyString_GET_SIZE(self); |
| 1011 | PyObject *new; |
| 1012 | |
| 1013 | if (!PyArg_ParseTuple(args, "")) |
| 1014 | return NULL; |
| 1015 | new = PyString_FromStringAndSize(NULL, n); |
| 1016 | if (new == NULL) |
| 1017 | return NULL; |
| 1018 | s_new = PyString_AsString(new); |
| 1019 | for (i = 0; i < n; i++) { |
| 1020 | int c = Py_CHARMASK(*s++); |
| 1021 | if (isupper(c)) { |
| 1022 | *s_new = tolower(c); |
| 1023 | } else |
| 1024 | *s_new = c; |
| 1025 | s_new++; |
| 1026 | } |
| 1027 | return new; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | static char upper__doc__[] = |
| 1032 | "S.upper() -> string\n\ |
| 1033 | \n\ |
| 1034 | Return a copy of the string S converted to uppercase."; |
| 1035 | |
| 1036 | static PyObject * |
| 1037 | string_upper(self, args) |
| 1038 | PyStringObject *self; |
| 1039 | PyObject *args; |
| 1040 | { |
| 1041 | char *s = PyString_AS_STRING(self), *s_new; |
| 1042 | int i, n = PyString_GET_SIZE(self); |
| 1043 | PyObject *new; |
| 1044 | |
| 1045 | if (!PyArg_ParseTuple(args, "")) |
| 1046 | return NULL; |
| 1047 | new = PyString_FromStringAndSize(NULL, n); |
| 1048 | if (new == NULL) |
| 1049 | return NULL; |
| 1050 | s_new = PyString_AsString(new); |
| 1051 | for (i = 0; i < n; i++) { |
| 1052 | int c = Py_CHARMASK(*s++); |
| 1053 | if (islower(c)) { |
| 1054 | *s_new = toupper(c); |
| 1055 | } else |
| 1056 | *s_new = c; |
| 1057 | s_new++; |
| 1058 | } |
| 1059 | return new; |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | static char capitalize__doc__[] = |
| 1064 | "S.capitalize() -> string\n\ |
| 1065 | \n\ |
| 1066 | Return a copy of the string S with only its first character\n\ |
| 1067 | capitalized."; |
| 1068 | |
| 1069 | static PyObject * |
| 1070 | string_capitalize(self, args) |
| 1071 | PyStringObject *self; |
| 1072 | PyObject *args; |
| 1073 | { |
| 1074 | char *s = PyString_AS_STRING(self), *s_new; |
| 1075 | int i, n = PyString_GET_SIZE(self); |
| 1076 | PyObject *new; |
| 1077 | |
| 1078 | if (!PyArg_ParseTuple(args, "")) |
| 1079 | return NULL; |
| 1080 | new = PyString_FromStringAndSize(NULL, n); |
| 1081 | if (new == NULL) |
| 1082 | return NULL; |
| 1083 | s_new = PyString_AsString(new); |
| 1084 | if (0 < n) { |
| 1085 | int c = Py_CHARMASK(*s++); |
| 1086 | if (islower(c)) |
| 1087 | *s_new = toupper(c); |
| 1088 | else |
| 1089 | *s_new = c; |
| 1090 | s_new++; |
| 1091 | } |
| 1092 | for (i = 1; i < n; i++) { |
| 1093 | int c = Py_CHARMASK(*s++); |
| 1094 | if (isupper(c)) |
| 1095 | *s_new = tolower(c); |
| 1096 | else |
| 1097 | *s_new = c; |
| 1098 | s_new++; |
| 1099 | } |
| 1100 | return new; |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | static char count__doc__[] = |
| 1105 | "S.count(sub[, start[, end]]) -> int\n\ |
| 1106 | \n\ |
| 1107 | Return the number of occurrences of substring sub in string\n\ |
| 1108 | S[start:end]. Optional arguments start and end are\n\ |
| 1109 | interpreted as in slice notation."; |
| 1110 | |
| 1111 | static PyObject * |
| 1112 | string_count(self, args) |
| 1113 | PyStringObject *self; |
| 1114 | PyObject *args; |
| 1115 | { |
| 1116 | char *s = PyString_AS_STRING(self), *sub; |
| 1117 | int len = PyString_GET_SIZE(self), n; |
| 1118 | int i = 0, last = INT_MAX; |
| 1119 | int m, r; |
| 1120 | |
| 1121 | if (!PyArg_ParseTuple(args, "t#|ii", &sub, &n, &i, &last)) |
| 1122 | return NULL; |
| 1123 | if (last > len) |
| 1124 | last = len; |
| 1125 | if (last < 0) |
| 1126 | last += len; |
| 1127 | if (last < 0) |
| 1128 | last = 0; |
| 1129 | if (i < 0) |
| 1130 | i += len; |
| 1131 | if (i < 0) |
| 1132 | i = 0; |
| 1133 | m = last + 1 - n; |
| 1134 | if (n == 0) |
| 1135 | return PyInt_FromLong((long) (m-i)); |
| 1136 | |
| 1137 | r = 0; |
| 1138 | while (i < m) { |
| 1139 | if (!memcmp(s+i, sub, n)) { |
| 1140 | r++; |
| 1141 | i += n; |
| 1142 | } else { |
| 1143 | i++; |
| 1144 | } |
| 1145 | } |
| 1146 | return PyInt_FromLong((long) r); |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | static char swapcase__doc__[] = |
| 1151 | "S.swapcase() -> string\n\ |
| 1152 | \n\ |
| 1153 | Return a copy of the string S with upper case characters\n\ |
| 1154 | converted to lowercase and vice versa."; |
| 1155 | |
| 1156 | static PyObject * |
| 1157 | string_swapcase(self, args) |
| 1158 | PyStringObject *self; |
| 1159 | PyObject *args; |
| 1160 | { |
| 1161 | char *s = PyString_AS_STRING(self), *s_new; |
| 1162 | int i, n = PyString_GET_SIZE(self); |
| 1163 | PyObject *new; |
| 1164 | |
| 1165 | if (!PyArg_ParseTuple(args, "")) |
| 1166 | return NULL; |
| 1167 | new = PyString_FromStringAndSize(NULL, n); |
| 1168 | if (new == NULL) |
| 1169 | return NULL; |
| 1170 | s_new = PyString_AsString(new); |
| 1171 | for (i = 0; i < n; i++) { |
| 1172 | int c = Py_CHARMASK(*s++); |
| 1173 | if (islower(c)) { |
| 1174 | *s_new = toupper(c); |
| 1175 | } |
| 1176 | else if (isupper(c)) { |
| 1177 | *s_new = tolower(c); |
| 1178 | } |
| 1179 | else |
| 1180 | *s_new = c; |
| 1181 | s_new++; |
| 1182 | } |
| 1183 | return new; |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | static char translate__doc__[] = |
| 1188 | "S.translate(table [,deletechars]) -> string\n\ |
| 1189 | \n\ |
| 1190 | Return a copy of the string S, where all characters occurring\n\ |
| 1191 | in the optional argument deletechars are removed, and the\n\ |
| 1192 | remaining characters have been mapped through the given\n\ |
| 1193 | translation table, which must be a string of length 256."; |
| 1194 | |
| 1195 | static PyObject * |
| 1196 | string_translate(self, args) |
| 1197 | PyStringObject *self; |
| 1198 | PyObject *args; |
| 1199 | { |
| 1200 | register char *input, *table, *output; |
| 1201 | register int i, c, changed = 0; |
| 1202 | PyObject *input_obj = (PyObject*)self; |
| 1203 | char *table1, *output_start, *del_table=NULL; |
| 1204 | int inlen, tablen, dellen = 0; |
| 1205 | PyObject *result; |
| 1206 | int trans_table[256]; |
| 1207 | |
| 1208 | if (!PyArg_ParseTuple(args, "t#|t#", |
| 1209 | &table1, &tablen, &del_table, &dellen)) |
| 1210 | return NULL; |
| 1211 | if (tablen != 256) { |
| 1212 | PyErr_SetString(PyExc_ValueError, |
| 1213 | "translation table must be 256 characters long"); |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | |
| 1217 | table = table1; |
| 1218 | inlen = PyString_Size(input_obj); |
| 1219 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 1220 | if (result == NULL) |
| 1221 | return NULL; |
| 1222 | output_start = output = PyString_AsString(result); |
| 1223 | input = PyString_AsString(input_obj); |
| 1224 | |
| 1225 | if (dellen == 0) { |
| 1226 | /* If no deletions are required, use faster code */ |
| 1227 | for (i = inlen; --i >= 0; ) { |
| 1228 | c = Py_CHARMASK(*input++); |
| 1229 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1230 | changed = 1; |
| 1231 | } |
| 1232 | if (changed) |
| 1233 | return result; |
| 1234 | Py_DECREF(result); |
| 1235 | Py_INCREF(input_obj); |
| 1236 | return input_obj; |
| 1237 | } |
| 1238 | |
| 1239 | for (i = 0; i < 256; i++) |
| 1240 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1241 | |
| 1242 | for (i = 0; i < dellen; i++) |
| 1243 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 1244 | |
| 1245 | for (i = inlen; --i >= 0; ) { |
| 1246 | c = Py_CHARMASK(*input++); |
| 1247 | if (trans_table[c] != -1) |
| 1248 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1249 | continue; |
| 1250 | changed = 1; |
| 1251 | } |
| 1252 | if (!changed) { |
| 1253 | Py_DECREF(result); |
| 1254 | Py_INCREF(input_obj); |
| 1255 | return input_obj; |
| 1256 | } |
| 1257 | /* Fix the size of the resulting string */ |
| 1258 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 1259 | return NULL; |
| 1260 | return result; |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 1265 | |
| 1266 | /* |
| 1267 | mymemfind |
| 1268 | |
| 1269 | strstr replacement for arbitrary blocks of memory. |
| 1270 | |
| 1271 | Locates the first occurance in the memory pointed to by MEM of the |
| 1272 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 1273 | found, or -1 if not found. If len of PAT is greater than length of |
| 1274 | MEM, the function returns -1. |
| 1275 | */ |
| 1276 | static int |
| 1277 | mymemfind(mem, len, pat, pat_len) |
| 1278 | char *mem; |
| 1279 | int len; |
| 1280 | char *pat; |
| 1281 | int pat_len; |
| 1282 | { |
| 1283 | register int ii; |
| 1284 | |
| 1285 | /* pattern can not occur in the last pat_len-1 chars */ |
| 1286 | len -= pat_len; |
| 1287 | |
| 1288 | for (ii = 0; ii <= len; ii++) { |
| 1289 | if (mem[ii] == pat[0] && |
| 1290 | (pat_len == 1 || |
| 1291 | memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) { |
| 1292 | return ii; |
| 1293 | } |
| 1294 | } |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | mymemcnt |
| 1300 | |
| 1301 | Return the number of distinct times PAT is found in MEM. |
| 1302 | meaning mem=1111 and pat==11 returns 2. |
| 1303 | mem=11111 and pat==11 also return 2. |
| 1304 | */ |
| 1305 | static int |
| 1306 | mymemcnt(mem, len, pat, pat_len) |
| 1307 | char *mem; |
| 1308 | int len; |
| 1309 | char *pat; |
| 1310 | int pat_len; |
| 1311 | { |
| 1312 | register int offset = 0; |
| 1313 | int nfound = 0; |
| 1314 | |
| 1315 | while (len >= 0) { |
| 1316 | offset = mymemfind(mem, len, pat, pat_len); |
| 1317 | if (offset == -1) |
| 1318 | break; |
| 1319 | mem += offset + pat_len; |
| 1320 | len -= offset + pat_len; |
| 1321 | nfound++; |
| 1322 | } |
| 1323 | return nfound; |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | mymemreplace |
| 1328 | |
| 1329 | Return a string in which all occurences of PAT in memory STR are |
| 1330 | replaced with SUB. |
| 1331 | |
| 1332 | If length of PAT is less than length of STR or there are no occurences |
| 1333 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 1334 | string is allocated here and returned. |
| 1335 | |
| 1336 | on return, out_len is: |
| 1337 | the length of output string, or |
| 1338 | -1 if the input string is returned, or |
| 1339 | unchanged if an error occurs (no memory). |
| 1340 | |
| 1341 | return value is: |
| 1342 | the new string allocated locally, or |
| 1343 | NULL if an error occurred. |
| 1344 | */ |
| 1345 | static char * |
| 1346 | mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len) |
| 1347 | char *str; |
| 1348 | int len; /* input string */ |
| 1349 | char *pat; |
| 1350 | int pat_len; /* pattern string to find */ |
| 1351 | char *sub; |
| 1352 | int sub_len; /* substitution string */ |
| 1353 | int count; /* number of replacements, 0 == all */ |
| 1354 | int *out_len; |
| 1355 | |
| 1356 | { |
| 1357 | char *out_s; |
| 1358 | char *new_s; |
| 1359 | int nfound, offset, new_len; |
| 1360 | |
| 1361 | if (len == 0 || pat_len > len) |
| 1362 | goto return_same; |
| 1363 | |
| 1364 | /* find length of output string */ |
| 1365 | nfound = mymemcnt(str, len, pat, pat_len); |
| 1366 | if (count > 0) |
| 1367 | nfound = nfound > count ? count : nfound; |
| 1368 | if (nfound == 0) |
| 1369 | goto return_same; |
| 1370 | new_len = len + nfound*(sub_len - pat_len); |
| 1371 | |
| 1372 | new_s = (char *)malloc(new_len); |
| 1373 | if (new_s == NULL) return NULL; |
| 1374 | |
| 1375 | *out_len = new_len; |
| 1376 | out_s = new_s; |
| 1377 | |
| 1378 | while (len > 0) { |
| 1379 | /* find index of next instance of pattern */ |
| 1380 | offset = mymemfind(str, len, pat, pat_len); |
| 1381 | /* if not found, break out of loop */ |
| 1382 | if (offset == -1) break; |
| 1383 | |
| 1384 | /* copy non matching part of input string */ |
| 1385 | memcpy(new_s, str, offset); /* copy part of str before pat */ |
| 1386 | str += offset + pat_len; /* move str past pattern */ |
| 1387 | len -= offset + pat_len; /* reduce length of str remaining */ |
| 1388 | |
| 1389 | /* copy substitute into the output string */ |
| 1390 | new_s += offset; /* move new_s to dest for sub string */ |
| 1391 | memcpy(new_s, sub, sub_len); /* copy substring into new_s */ |
| 1392 | new_s += sub_len; /* offset new_s past sub string */ |
| 1393 | |
| 1394 | /* break when we've done count replacements */ |
| 1395 | if (--count == 0) break; |
| 1396 | } |
| 1397 | /* copy any remaining values into output string */ |
| 1398 | if (len > 0) |
| 1399 | memcpy(new_s, str, len); |
| 1400 | return out_s; |
| 1401 | |
| 1402 | return_same: |
| 1403 | *out_len = -1; |
| 1404 | return str; |
| 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | static char replace__doc__[] = |
| 1409 | "S.replace (old, new[, maxsplit]) -> string\n\ |
| 1410 | \n\ |
| 1411 | Return a copy of string S with all occurrences of substring\n\ |
| 1412 | old replaced by new. If the optional argument maxsplit is\n\ |
| 1413 | given, only the first maxsplit occurrences are replaced."; |
| 1414 | |
| 1415 | static PyObject * |
| 1416 | string_replace(self, args) |
| 1417 | PyStringObject *self; |
| 1418 | PyObject *args; |
| 1419 | { |
| 1420 | char *str = PyString_AS_STRING(self), *pat,*sub,*new_s; |
| 1421 | int len = PyString_GET_SIZE(self), pat_len,sub_len,out_len; |
| 1422 | int count = 0; |
| 1423 | PyObject *new; |
| 1424 | |
| 1425 | if (!PyArg_ParseTuple(args, "t#t#|i", |
| 1426 | &pat, &pat_len, &sub, &sub_len, &count)) |
| 1427 | return NULL; |
| 1428 | if (pat_len <= 0) { |
| 1429 | PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
| 1430 | return NULL; |
| 1431 | } |
| 1432 | new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len); |
| 1433 | if (new_s == NULL) { |
| 1434 | PyErr_NoMemory(); |
| 1435 | return NULL; |
| 1436 | } |
| 1437 | if (out_len == -1) { |
| 1438 | /* we're returning another reference to self */ |
| 1439 | new = (PyObject*)self; |
| 1440 | Py_INCREF(new); |
| 1441 | } |
| 1442 | else { |
| 1443 | new = PyString_FromStringAndSize(new_s, out_len); |
| 1444 | free(new_s); |
| 1445 | } |
| 1446 | return new; |
| 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | static char startswith__doc__[] = |
| 1451 | "S.startswith(prefix[, start[, end]]) -> int\n\ |
| 1452 | \n\ |
| 1453 | Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ |
| 1454 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1455 | comparing S at that position."; |
| 1456 | |
| 1457 | static PyObject * |
| 1458 | string_startswith(self, args) |
| 1459 | PyStringObject *self; |
| 1460 | PyObject *args; |
| 1461 | { |
| 1462 | char* str = PyString_AS_STRING(self); |
| 1463 | int len = PyString_GET_SIZE(self); |
| 1464 | char* prefix; |
| 1465 | int plen; |
| 1466 | int start = 0; |
| 1467 | int end = -1; |
| 1468 | |
| 1469 | if (!PyArg_ParseTuple(args, "t#|ii", &prefix, &plen, &start, &end)) |
| 1470 | return NULL; |
| 1471 | |
| 1472 | /* adopt Java semantics for index out of range. it is legal for |
| 1473 | * offset to be == plen, but this only returns true if prefix is |
| 1474 | * the empty string. |
| 1475 | */ |
| 1476 | if (start < 0 || start+plen > len) |
| 1477 | return PyInt_FromLong(0); |
| 1478 | |
| 1479 | if (!memcmp(str+start, prefix, plen)) { |
| 1480 | /* did the match end after the specified end? */ |
| 1481 | if (end < 0) |
| 1482 | return PyInt_FromLong(1); |
| 1483 | else if (end - start < plen) |
| 1484 | return PyInt_FromLong(0); |
| 1485 | else |
| 1486 | return PyInt_FromLong(1); |
| 1487 | } |
| 1488 | else return PyInt_FromLong(0); |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | static char endswith__doc__[] = |
| 1493 | "S.endswith(suffix[, start[, end]]) -> int\n\ |
| 1494 | \n\ |
| 1495 | Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ |
| 1496 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1497 | comparing S at that position."; |
| 1498 | |
| 1499 | static PyObject * |
| 1500 | string_endswith(self, args) |
| 1501 | PyStringObject *self; |
| 1502 | PyObject *args; |
| 1503 | { |
| 1504 | char* str = PyString_AS_STRING(self); |
| 1505 | int len = PyString_GET_SIZE(self); |
| 1506 | char* suffix; |
| 1507 | int plen; |
| 1508 | int start = 0; |
| 1509 | int end = -1; |
| 1510 | int lower, upper; |
| 1511 | |
| 1512 | if (!PyArg_ParseTuple(args, "t#|ii", &suffix, &plen, &start, &end)) |
| 1513 | return NULL; |
| 1514 | |
| 1515 | if (start < 0 || start > len || plen > len) |
| 1516 | return PyInt_FromLong(0); |
| 1517 | |
| 1518 | upper = (end >= 0 && end <= len) ? end : len; |
| 1519 | lower = (upper - plen) > start ? (upper - plen) : start; |
| 1520 | |
| 1521 | if (upper-lower >= plen && !memcmp(str+lower, suffix, plen)) |
| 1522 | return PyInt_FromLong(1); |
| 1523 | else return PyInt_FromLong(0); |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | static PyMethodDef |
| 1529 | string_methods[] = { |
| 1530 | /* counterparts of the obsolete stropmodule functions */ |
| 1531 | {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__}, |
| 1532 | {"count", (PyCFunction)string_count, 1, count__doc__}, |
| 1533 | {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__}, |
| 1534 | {"find", (PyCFunction)string_find, 1, find__doc__}, |
| 1535 | {"index", (PyCFunction)string_index, 1, index__doc__}, |
| 1536 | {"join", (PyCFunction)string_join, 1, join__doc__}, |
| 1537 | {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__}, |
| 1538 | {"lower", (PyCFunction)string_lower, 1, lower__doc__}, |
| 1539 | /* maketrans */ |
| 1540 | {"replace", (PyCFunction)string_replace, 1, replace__doc__}, |
| 1541 | {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__}, |
| 1542 | {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__}, |
| 1543 | {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__}, |
| 1544 | {"split", (PyCFunction)string_split, 1, split__doc__}, |
| 1545 | {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__}, |
| 1546 | {"strip", (PyCFunction)string_strip, 1, strip__doc__}, |
| 1547 | {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__}, |
| 1548 | {"translate", (PyCFunction)string_translate, 1, strip__doc__}, |
| 1549 | {"upper", (PyCFunction)string_upper, 1, upper__doc__}, |
| 1550 | /* TBD */ |
| 1551 | /* {"ljust" (PyCFunction)string_ljust, 1, ljust__doc__}, */ |
| 1552 | /* {"rjust" (PyCFunction)string_rjust, 1, rjust__doc__}, */ |
| 1553 | /* {"center" (PyCFunction)string_center, 1, center__doc__}, */ |
| 1554 | /* {"zfill" (PyCFunction)string_zfill, 1, zfill__doc__}, */ |
| 1555 | /* {"expandtabs" (PyCFunction)string_expandtabs, 1, ljust__doc__}, */ |
| 1556 | /* {"capwords" (PyCFunction)string_capwords, 1, capwords__doc__}, */ |
| 1557 | {NULL, NULL} /* sentinel */ |
| 1558 | }; |
| 1559 | |
| 1560 | static PyObject * |
| 1561 | string_getattr(s, name) |
| 1562 | PyStringObject *s; |
| 1563 | char *name; |
| 1564 | { |
| 1565 | return Py_FindMethod(string_methods, (PyObject*)s, name); |
| 1566 | } |
| 1567 | |
| 1568 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1569 | PyTypeObject PyString_Type = { |
| 1570 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1571 | 0, |
| 1572 | "string", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1573 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1574 | sizeof(char), |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1575 | (destructor)string_dealloc, /*tp_dealloc*/ |
| 1576 | (printfunc)string_print, /*tp_print*/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1577 | (getattrfunc)string_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1578 | 0, /*tp_setattr*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1579 | (cmpfunc)string_compare, /*tp_compare*/ |
| 1580 | (reprfunc)string_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1581 | 0, /*tp_as_number*/ |
| 1582 | &string_as_sequence, /*tp_as_sequence*/ |
| 1583 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1584 | (hashfunc)string_hash, /*tp_hash*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 1585 | 0, /*tp_call*/ |
| 1586 | 0, /*tp_str*/ |
| 1587 | 0, /*tp_getattro*/ |
| 1588 | 0, /*tp_setattro*/ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1589 | &string_as_buffer, /*tp_as_buffer*/ |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1590 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 1591 | 0, /*tp_doc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1592 | }; |
| 1593 | |
| 1594 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1595 | PyString_Concat(pv, w) |
| 1596 | register PyObject **pv; |
| 1597 | register PyObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1598 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1599 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1600 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1601 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1602 | if (w == NULL || !PyString_Check(*pv)) { |
| 1603 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1604 | *pv = NULL; |
| 1605 | return; |
| 1606 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1607 | v = string_concat((PyStringObject *) *pv, w); |
| 1608 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1609 | *pv = v; |
| 1610 | } |
| 1611 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1612 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1613 | PyString_ConcatAndDel(pv, w) |
| 1614 | register PyObject **pv; |
| 1615 | register PyObject *w; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1616 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1617 | PyString_Concat(pv, w); |
| 1618 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1622 | /* The following function breaks the notion that strings are immutable: |
| 1623 | it changes the size of a string. We get away with this only if there |
| 1624 | is only one module referencing the object. You can also think of it |
| 1625 | as creating a new string object and destroying the old one, only |
| 1626 | more efficiently. In any case, don't use this if the string may |
| 1627 | already be known to some other part of the code... */ |
| 1628 | |
| 1629 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1630 | _PyString_Resize(pv, newsize) |
| 1631 | PyObject **pv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1632 | int newsize; |
| 1633 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1634 | register PyObject *v; |
| 1635 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1636 | v = *pv; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1637 | if (!PyString_Check(v) || v->ob_refcnt != 1) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1638 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1639 | Py_DECREF(v); |
| 1640 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1641 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1642 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1643 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 1644 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 1645 | --_Py_RefTotal; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1646 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1647 | _Py_ForgetReference(v); |
| 1648 | *pv = (PyObject *) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1649 | realloc((char *)v, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1650 | sizeof(PyStringObject) + newsize * sizeof(char)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1651 | if (*pv == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1652 | PyMem_DEL(v); |
| 1653 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1654 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1655 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1656 | _Py_NewReference(*pv); |
| 1657 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 1658 | sv->ob_size = newsize; |
| 1659 | sv->ob_sval[newsize] = '\0'; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1660 | return 0; |
| 1661 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1662 | |
| 1663 | /* Helpers for formatstring */ |
| 1664 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1665 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1666 | getnextarg(args, arglen, p_argidx) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1667 | PyObject *args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1668 | int arglen; |
| 1669 | int *p_argidx; |
| 1670 | { |
| 1671 | int argidx = *p_argidx; |
| 1672 | if (argidx < arglen) { |
| 1673 | (*p_argidx)++; |
| 1674 | if (arglen < 0) |
| 1675 | return args; |
| 1676 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1677 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1678 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1679 | PyErr_SetString(PyExc_TypeError, |
| 1680 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1681 | return NULL; |
| 1682 | } |
| 1683 | |
| 1684 | #define F_LJUST (1<<0) |
| 1685 | #define F_SIGN (1<<1) |
| 1686 | #define F_BLANK (1<<2) |
| 1687 | #define F_ALT (1<<3) |
| 1688 | #define F_ZERO (1<<4) |
| 1689 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1690 | static int |
| 1691 | formatfloat(buf, flags, prec, type, v) |
| 1692 | char *buf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1693 | int flags; |
| 1694 | int prec; |
| 1695 | int type; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1696 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1697 | { |
| 1698 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1699 | double x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1700 | if (!PyArg_Parse(v, "d;float argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1701 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1702 | if (prec < 0) |
| 1703 | prec = 6; |
| 1704 | if (prec > 50) |
| 1705 | prec = 50; /* Arbitrary limitation */ |
| 1706 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 1707 | type = 'g'; |
| 1708 | sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type); |
| 1709 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1710 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1713 | static int |
| 1714 | formatint(buf, flags, prec, type, v) |
| 1715 | char *buf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1716 | int flags; |
| 1717 | int prec; |
| 1718 | int type; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1719 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1720 | { |
| 1721 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1722 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1723 | if (!PyArg_Parse(v, "l;int argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1724 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1725 | if (prec < 0) |
| 1726 | prec = 1; |
| 1727 | sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type); |
| 1728 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1729 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1732 | static int |
| 1733 | formatchar(buf, v) |
| 1734 | char *buf; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1735 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1736 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1737 | if (PyString_Check(v)) { |
| 1738 | 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] | 1739 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1740 | } |
| 1741 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1742 | 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] | 1743 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1744 | } |
| 1745 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1746 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1749 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1750 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ |
| 1751 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1752 | PyObject * |
| 1753 | PyString_Format(format, args) |
| 1754 | PyObject *format; |
| 1755 | PyObject *args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1756 | { |
| 1757 | char *fmt, *res; |
| 1758 | int fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1759 | int args_owned = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1760 | PyObject *result; |
| 1761 | PyObject *dict = NULL; |
| 1762 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 1763 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1764 | return NULL; |
| 1765 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1766 | fmt = PyString_AsString(format); |
| 1767 | fmtcnt = PyString_Size(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1768 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1769 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1770 | if (result == NULL) |
| 1771 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1772 | res = PyString_AsString(result); |
| 1773 | if (PyTuple_Check(args)) { |
| 1774 | arglen = PyTuple_Size(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1775 | argidx = 0; |
| 1776 | } |
| 1777 | else { |
| 1778 | arglen = -1; |
| 1779 | argidx = -2; |
| 1780 | } |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1781 | if (args->ob_type->tp_as_mapping) |
| 1782 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1783 | while (--fmtcnt >= 0) { |
| 1784 | if (*fmt != '%') { |
| 1785 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1786 | rescnt = fmtcnt + 100; |
| 1787 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1788 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1789 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1790 | res = PyString_AsString(result) |
| 1791 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1792 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1793 | } |
| 1794 | *res++ = *fmt++; |
| 1795 | } |
| 1796 | else { |
| 1797 | /* Got a format specifier */ |
| 1798 | int flags = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1799 | int width = -1; |
| 1800 | int prec = -1; |
| 1801 | int size = 0; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 1802 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1803 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1804 | PyObject *v = NULL; |
| 1805 | PyObject *temp = NULL; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1806 | char *buf; |
| 1807 | int sign; |
| 1808 | int len; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1809 | char tmpbuf[120]; /* For format{float,int,char}() */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1810 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1811 | if (*fmt == '(') { |
| 1812 | char *keystart; |
| 1813 | int keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1814 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1815 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1816 | |
| 1817 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1818 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1819 | "format requires a mapping"); |
| 1820 | goto error; |
| 1821 | } |
| 1822 | ++fmt; |
| 1823 | --fmtcnt; |
| 1824 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1825 | /* Skip over balanced parentheses */ |
| 1826 | while (pcount > 0 && --fmtcnt >= 0) { |
| 1827 | if (*fmt == ')') |
| 1828 | --pcount; |
| 1829 | else if (*fmt == '(') |
| 1830 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1831 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1832 | } |
| 1833 | keylen = fmt - keystart - 1; |
| 1834 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1835 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1836 | "incomplete format key"); |
| 1837 | goto error; |
| 1838 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1839 | key = PyString_FromStringAndSize(keystart, |
| 1840 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1841 | if (key == NULL) |
| 1842 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1843 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1844 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1845 | args_owned = 0; |
| 1846 | } |
| 1847 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1848 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1849 | if (args == NULL) { |
| 1850 | goto error; |
| 1851 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 1852 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1853 | arglen = -1; |
| 1854 | argidx = -2; |
| 1855 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1856 | while (--fmtcnt >= 0) { |
| 1857 | switch (c = *fmt++) { |
| 1858 | case '-': flags |= F_LJUST; continue; |
| 1859 | case '+': flags |= F_SIGN; continue; |
| 1860 | case ' ': flags |= F_BLANK; continue; |
| 1861 | case '#': flags |= F_ALT; continue; |
| 1862 | case '0': flags |= F_ZERO; continue; |
| 1863 | } |
| 1864 | break; |
| 1865 | } |
| 1866 | if (c == '*') { |
| 1867 | v = getnextarg(args, arglen, &argidx); |
| 1868 | if (v == NULL) |
| 1869 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1870 | if (!PyInt_Check(v)) { |
| 1871 | PyErr_SetString(PyExc_TypeError, |
| 1872 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1873 | goto error; |
| 1874 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1875 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 1876 | if (width < 0) { |
| 1877 | flags |= F_LJUST; |
| 1878 | width = -width; |
| 1879 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1880 | if (--fmtcnt >= 0) |
| 1881 | c = *fmt++; |
| 1882 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1883 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1884 | width = c - '0'; |
| 1885 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1886 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1887 | if (!isdigit(c)) |
| 1888 | break; |
| 1889 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1890 | PyErr_SetString( |
| 1891 | PyExc_ValueError, |
| 1892 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1893 | goto error; |
| 1894 | } |
| 1895 | width = width*10 + (c - '0'); |
| 1896 | } |
| 1897 | } |
| 1898 | if (c == '.') { |
| 1899 | prec = 0; |
| 1900 | if (--fmtcnt >= 0) |
| 1901 | c = *fmt++; |
| 1902 | if (c == '*') { |
| 1903 | v = getnextarg(args, arglen, &argidx); |
| 1904 | if (v == NULL) |
| 1905 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1906 | if (!PyInt_Check(v)) { |
| 1907 | PyErr_SetString( |
| 1908 | PyExc_TypeError, |
| 1909 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1910 | goto error; |
| 1911 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1912 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1913 | if (prec < 0) |
| 1914 | prec = 0; |
| 1915 | if (--fmtcnt >= 0) |
| 1916 | c = *fmt++; |
| 1917 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1918 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1919 | prec = c - '0'; |
| 1920 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1921 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1922 | if (!isdigit(c)) |
| 1923 | break; |
| 1924 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1925 | PyErr_SetString( |
| 1926 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1927 | "prec too big"); |
| 1928 | goto error; |
| 1929 | } |
| 1930 | prec = prec*10 + (c - '0'); |
| 1931 | } |
| 1932 | } |
| 1933 | } /* prec */ |
| 1934 | if (fmtcnt >= 0) { |
| 1935 | if (c == 'h' || c == 'l' || c == 'L') { |
| 1936 | size = c; |
| 1937 | if (--fmtcnt >= 0) |
| 1938 | c = *fmt++; |
| 1939 | } |
| 1940 | } |
| 1941 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1942 | PyErr_SetString(PyExc_ValueError, |
| 1943 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1944 | goto error; |
| 1945 | } |
| 1946 | if (c != '%') { |
| 1947 | v = getnextarg(args, arglen, &argidx); |
| 1948 | if (v == NULL) |
| 1949 | goto error; |
| 1950 | } |
| 1951 | sign = 0; |
| 1952 | fill = ' '; |
| 1953 | switch (c) { |
| 1954 | case '%': |
| 1955 | buf = "%"; |
| 1956 | len = 1; |
| 1957 | break; |
| 1958 | case 's': |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1959 | temp = PyObject_Str(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1960 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1961 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 1962 | if (!PyString_Check(temp)) { |
| 1963 | PyErr_SetString(PyExc_TypeError, |
| 1964 | "%s argument has non-string str()"); |
| 1965 | goto error; |
| 1966 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1967 | buf = PyString_AsString(temp); |
| 1968 | len = PyString_Size(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1969 | if (prec >= 0 && len > prec) |
| 1970 | len = prec; |
| 1971 | break; |
| 1972 | case 'i': |
| 1973 | case 'd': |
| 1974 | case 'u': |
| 1975 | case 'o': |
| 1976 | case 'x': |
| 1977 | case 'X': |
| 1978 | if (c == 'i') |
| 1979 | c = 'd'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 1980 | buf = tmpbuf; |
| 1981 | len = formatint(buf, flags, prec, c, v); |
| 1982 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1983 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1984 | sign = (c == 'd'); |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 1985 | if (flags&F_ZERO) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1986 | fill = '0'; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 1987 | if ((flags&F_ALT) && |
| 1988 | (c == 'x' || c == 'X') && |
| 1989 | buf[0] == '0' && buf[1] == c) { |
| 1990 | *res++ = *buf++; |
| 1991 | *res++ = *buf++; |
| 1992 | rescnt -= 2; |
| 1993 | len -= 2; |
| 1994 | width -= 2; |
| 1995 | if (width < 0) |
| 1996 | width = 0; |
| 1997 | } |
| 1998 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 1999 | break; |
| 2000 | case 'e': |
| 2001 | case 'E': |
| 2002 | case 'f': |
| 2003 | case 'g': |
| 2004 | case 'G': |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2005 | buf = tmpbuf; |
| 2006 | len = formatfloat(buf, flags, prec, c, v); |
| 2007 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2008 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2009 | sign = 1; |
| 2010 | if (flags&F_ZERO) |
| 2011 | fill = '0'; |
| 2012 | break; |
| 2013 | case 'c': |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2014 | buf = tmpbuf; |
| 2015 | len = formatchar(buf, v); |
| 2016 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2017 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2018 | break; |
| 2019 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2020 | PyErr_Format(PyExc_ValueError, |
| 2021 | "unsupported format character '%c' (0x%x)", |
| 2022 | c, c); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2023 | goto error; |
| 2024 | } |
| 2025 | if (sign) { |
| 2026 | if (*buf == '-' || *buf == '+') { |
| 2027 | sign = *buf++; |
| 2028 | len--; |
| 2029 | } |
| 2030 | else if (flags & F_SIGN) |
| 2031 | sign = '+'; |
| 2032 | else if (flags & F_BLANK) |
| 2033 | sign = ' '; |
| 2034 | else |
| 2035 | sign = '\0'; |
| 2036 | } |
| 2037 | if (width < len) |
| 2038 | width = len; |
| 2039 | if (rescnt < width + (sign != '\0')) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2040 | reslen -= rescnt; |
| 2041 | rescnt = width + fmtcnt + 100; |
| 2042 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2043 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2044 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2045 | res = PyString_AsString(result) |
| 2046 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2047 | } |
| 2048 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 2049 | if (fill != ' ') |
| 2050 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2051 | rescnt--; |
| 2052 | if (width > len) |
| 2053 | width--; |
| 2054 | } |
| 2055 | if (width > len && !(flags&F_LJUST)) { |
| 2056 | do { |
| 2057 | --rescnt; |
| 2058 | *res++ = fill; |
| 2059 | } while (--width > len); |
| 2060 | } |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 2061 | if (sign && fill == ' ') |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 2062 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2063 | memcpy(res, buf, len); |
| 2064 | res += len; |
| 2065 | rescnt -= len; |
| 2066 | while (--width >= len) { |
| 2067 | --rescnt; |
| 2068 | *res++ = ' '; |
| 2069 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2070 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2071 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2072 | "not all arguments converted"); |
| 2073 | goto error; |
| 2074 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2075 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2076 | } /* '%' */ |
| 2077 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 2078 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2079 | PyErr_SetString(PyExc_TypeError, |
| 2080 | "not all arguments converted"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2081 | goto error; |
| 2082 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2083 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2084 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2085 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2086 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2087 | return result; |
| 2088 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2089 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2090 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2091 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2092 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2093 | return NULL; |
| 2094 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2095 | |
| 2096 | |
| 2097 | #ifdef INTERN_STRINGS |
| 2098 | |
| 2099 | static PyObject *interned; |
| 2100 | |
| 2101 | void |
| 2102 | PyString_InternInPlace(p) |
| 2103 | PyObject **p; |
| 2104 | { |
| 2105 | register PyStringObject *s = (PyStringObject *)(*p); |
| 2106 | PyObject *t; |
| 2107 | if (s == NULL || !PyString_Check(s)) |
| 2108 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
| 2109 | if ((t = s->ob_sinterned) != NULL) { |
| 2110 | if (t == (PyObject *)s) |
| 2111 | return; |
| 2112 | Py_INCREF(t); |
| 2113 | *p = t; |
| 2114 | Py_DECREF(s); |
| 2115 | return; |
| 2116 | } |
| 2117 | if (interned == NULL) { |
| 2118 | interned = PyDict_New(); |
| 2119 | if (interned == NULL) |
| 2120 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2121 | } |
| 2122 | if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { |
| 2123 | Py_INCREF(t); |
| 2124 | *p = s->ob_sinterned = t; |
| 2125 | Py_DECREF(s); |
| 2126 | return; |
| 2127 | } |
| 2128 | t = (PyObject *)s; |
| 2129 | if (PyDict_SetItem(interned, t, t) == 0) { |
| 2130 | s->ob_sinterned = t; |
| 2131 | return; |
| 2132 | } |
| 2133 | PyErr_Clear(); |
| 2134 | } |
| 2135 | |
| 2136 | |
| 2137 | PyObject * |
| 2138 | PyString_InternFromString(cp) |
| 2139 | const char *cp; |
| 2140 | { |
| 2141 | PyObject *s = PyString_FromString(cp); |
| 2142 | if (s == NULL) |
| 2143 | return NULL; |
| 2144 | PyString_InternInPlace(&s); |
| 2145 | return s; |
| 2146 | } |
| 2147 | |
| 2148 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2149 | |
| 2150 | void |
| 2151 | PyString_Fini() |
| 2152 | { |
| 2153 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2154 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 2155 | Py_XDECREF(characters[i]); |
| 2156 | characters[i] = NULL; |
| 2157 | } |
| 2158 | #ifndef DONT_SHARE_SHORT_STRINGS |
| 2159 | Py_XDECREF(nullstring); |
| 2160 | nullstring = NULL; |
| 2161 | #endif |
Guido van Rossum | 971a7aa | 1997-08-05 02:15:12 +0000 | [diff] [blame] | 2162 | #ifdef INTERN_STRINGS |
| 2163 | if (interned) { |
| 2164 | int pos, changed; |
| 2165 | PyObject *key, *value; |
| 2166 | do { |
| 2167 | changed = 0; |
| 2168 | pos = 0; |
| 2169 | while (PyDict_Next(interned, &pos, &key, &value)) { |
| 2170 | if (key->ob_refcnt == 2 && key == value) { |
| 2171 | PyDict_DelItem(interned, key); |
| 2172 | changed = 1; |
| 2173 | } |
| 2174 | } |
| 2175 | } while (changed); |
| 2176 | } |
| 2177 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2178 | } |