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 | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 107 | _Py_NewReference((PyObject *)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 | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 157 | _Py_NewReference((PyObject *)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)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 294 | if (PyUnicode_Check(bb)) |
| 295 | return PyUnicode_Concat((PyObject *)a, bb); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 296 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | /* Optimize cases with empty left or right operand */ |
| 301 | if (a->ob_size == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | Py_INCREF(bb); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | return bb; |
| 304 | } |
| 305 | if (b->ob_size == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 306 | Py_INCREF(a); |
| 307 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | } |
| 309 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | op = (PyStringObject *) |
| 311 | malloc(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 312 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | return PyErr_NoMemory(); |
| 314 | op->ob_type = &PyString_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 315 | op->ob_size = size; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 316 | #ifdef CACHE_HASH |
| 317 | op->ob_shash = -1; |
| 318 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 319 | #ifdef INTERN_STRINGS |
| 320 | op->ob_sinterned = NULL; |
| 321 | #endif |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 322 | _Py_NewReference((PyObject *)op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 323 | memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); |
| 324 | memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); |
| 325 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 326 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 327 | #undef b |
| 328 | } |
| 329 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 330 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 331 | string_repeat(a, n) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | register PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | register int n; |
| 334 | { |
| 335 | register int i; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 336 | register int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | register PyStringObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | if (n < 0) |
| 339 | n = 0; |
| 340 | size = a->ob_size * n; |
| 341 | if (size == a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | Py_INCREF(a); |
| 343 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | op = (PyStringObject *) |
| 346 | malloc(sizeof(PyStringObject) + size * sizeof(char)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 347 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 348 | return PyErr_NoMemory(); |
| 349 | op->ob_type = &PyString_Type; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 350 | op->ob_size = size; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 351 | #ifdef CACHE_HASH |
| 352 | op->ob_shash = -1; |
| 353 | #endif |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 354 | #ifdef INTERN_STRINGS |
| 355 | op->ob_sinterned = NULL; |
| 356 | #endif |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 357 | _Py_NewReference((PyObject *)op); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 358 | for (i = 0; i < size; i += a->ob_size) |
| 359 | memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); |
| 360 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 365 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 367 | string_slice(a, i, j) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 368 | register PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | register int i, j; /* May be negative! */ |
| 370 | { |
| 371 | if (i < 0) |
| 372 | i = 0; |
| 373 | if (j < 0) |
| 374 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 375 | if (j > a->ob_size) |
| 376 | j = a->ob_size; |
| 377 | 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] | 378 | Py_INCREF(a); |
| 379 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 380 | } |
| 381 | if (j < i) |
| 382 | j = i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 383 | return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 386 | static int |
| 387 | string_contains(a, el) |
| 388 | PyObject *a, *el; |
| 389 | { |
| 390 | register char *s, *end; |
| 391 | register char c; |
Guido van Rossum | 96a45ad | 2000-03-13 15:56:08 +0000 | [diff] [blame] | 392 | if (!PyString_Check(el)) |
| 393 | return PyUnicode_Contains(a, el); |
| 394 | if (PyString_Size(el) != 1) { |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 395 | PyErr_SetString(PyExc_TypeError, |
| 396 | "string member test needs char left operand"); |
| 397 | return -1; |
| 398 | } |
| 399 | c = PyString_AsString(el)[0]; |
| 400 | s = PyString_AsString(a); |
| 401 | end = s + PyString_Size(a); |
| 402 | while (s < end) { |
| 403 | if (c == *s++) |
| 404 | return 1; |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 409 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 410 | string_item(a, i) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 411 | PyStringObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 412 | register int i; |
| 413 | { |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 414 | int c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 415 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 416 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 418 | return NULL; |
| 419 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 420 | c = a->ob_sval[i] & UCHAR_MAX; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | v = (PyObject *) characters[c]; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 422 | #ifdef COUNT_ALLOCS |
| 423 | if (v != NULL) |
| 424 | one_strings++; |
| 425 | #endif |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 426 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 427 | v = PyString_FromStringAndSize((char *)NULL, 1); |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 428 | if (v == NULL) |
| 429 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | characters[c] = (PyStringObject *) v; |
| 431 | ((PyStringObject *)v)->ob_sval[0] = c; |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 432 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | Py_INCREF(v); |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 434 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static int |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 438 | string_compare(a, b) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 439 | PyStringObject *a, *b; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 440 | { |
Guido van Rossum | 253919f | 1991-02-13 23:18:39 +0000 | [diff] [blame] | 441 | int len_a = a->ob_size, len_b = b->ob_size; |
| 442 | int min_len = (len_a < len_b) ? len_a : len_b; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 443 | int cmp; |
| 444 | if (min_len > 0) { |
Guido van Rossum | fde7a75 | 1996-10-23 14:19:40 +0000 | [diff] [blame] | 445 | cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 446 | if (cmp == 0) |
| 447 | cmp = memcmp(a->ob_sval, b->ob_sval, min_len); |
| 448 | if (cmp != 0) |
| 449 | return cmp; |
| 450 | } |
Guido van Rossum | 253919f | 1991-02-13 23:18:39 +0000 | [diff] [blame] | 451 | 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] | 452 | } |
| 453 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 454 | static long |
| 455 | string_hash(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 456 | PyStringObject *a; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 457 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 458 | register int len; |
| 459 | register unsigned char *p; |
| 460 | register long x; |
| 461 | |
| 462 | #ifdef CACHE_HASH |
| 463 | if (a->ob_shash != -1) |
| 464 | return a->ob_shash; |
Guido van Rossum | 36b9f79 | 1997-02-14 16:29:22 +0000 | [diff] [blame] | 465 | #ifdef INTERN_STRINGS |
| 466 | if (a->ob_sinterned != NULL) |
| 467 | return (a->ob_shash = |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 468 | ((PyStringObject *)(a->ob_sinterned))->ob_shash); |
Guido van Rossum | 36b9f79 | 1997-02-14 16:29:22 +0000 | [diff] [blame] | 469 | #endif |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 470 | #endif |
| 471 | len = a->ob_size; |
| 472 | p = (unsigned char *) a->ob_sval; |
| 473 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 474 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 475 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 476 | x ^= a->ob_size; |
| 477 | if (x == -1) |
| 478 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 479 | #ifdef CACHE_HASH |
| 480 | a->ob_shash = x; |
| 481 | #endif |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 482 | return x; |
| 483 | } |
| 484 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 485 | static int |
| 486 | string_buffer_getreadbuf(self, index, ptr) |
| 487 | PyStringObject *self; |
| 488 | int index; |
| 489 | const void **ptr; |
| 490 | { |
| 491 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 492 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 493 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 494 | return -1; |
| 495 | } |
| 496 | *ptr = (void *)self->ob_sval; |
| 497 | return self->ob_size; |
| 498 | } |
| 499 | |
| 500 | static int |
| 501 | string_buffer_getwritebuf(self, index, ptr) |
| 502 | PyStringObject *self; |
| 503 | int index; |
| 504 | const void **ptr; |
| 505 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 506 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 507 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | static int |
| 512 | string_buffer_getsegcount(self, lenp) |
| 513 | PyStringObject *self; |
| 514 | int *lenp; |
| 515 | { |
| 516 | if ( lenp ) |
| 517 | *lenp = self->ob_size; |
| 518 | return 1; |
| 519 | } |
| 520 | |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 521 | static int |
| 522 | string_buffer_getcharbuf(self, index, ptr) |
| 523 | PyStringObject *self; |
| 524 | int index; |
| 525 | const char **ptr; |
| 526 | { |
| 527 | if ( index != 0 ) { |
| 528 | PyErr_SetString(PyExc_SystemError, |
| 529 | "accessing non-existent string segment"); |
| 530 | return -1; |
| 531 | } |
| 532 | *ptr = self->ob_sval; |
| 533 | return self->ob_size; |
| 534 | } |
| 535 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 536 | static PySequenceMethods string_as_sequence = { |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 537 | (inquiry)string_length, /*sq_length*/ |
| 538 | (binaryfunc)string_concat, /*sq_concat*/ |
| 539 | (intargfunc)string_repeat, /*sq_repeat*/ |
| 540 | (intargfunc)string_item, /*sq_item*/ |
| 541 | (intintargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 542 | 0, /*sq_ass_item*/ |
| 543 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 544 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 545 | }; |
| 546 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 547 | static PyBufferProcs string_as_buffer = { |
| 548 | (getreadbufferproc)string_buffer_getreadbuf, |
| 549 | (getwritebufferproc)string_buffer_getwritebuf, |
| 550 | (getsegcountproc)string_buffer_getsegcount, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 551 | (getcharbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 554 | |
| 555 | |
| 556 | #define LEFTSTRIP 0 |
| 557 | #define RIGHTSTRIP 1 |
| 558 | #define BOTHSTRIP 2 |
| 559 | |
| 560 | |
| 561 | static PyObject * |
| 562 | split_whitespace(s, len, maxsplit) |
| 563 | char *s; |
| 564 | int len; |
| 565 | int maxsplit; |
| 566 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 567 | int i, j, err; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 568 | PyObject* item; |
| 569 | PyObject *list = PyList_New(0); |
| 570 | |
| 571 | if (list == NULL) |
| 572 | return NULL; |
| 573 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 574 | for (i = j = 0; i < len; ) { |
| 575 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 576 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 577 | j = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 578 | while (i < len && !isspace(Py_CHARMASK(s[i]))) |
| 579 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 580 | if (j < i) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 581 | if (maxsplit-- <= 0) |
| 582 | break; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 583 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 584 | if (item == NULL) |
| 585 | goto finally; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 586 | err = PyList_Append(list, item); |
| 587 | Py_DECREF(item); |
| 588 | if (err < 0) |
| 589 | goto finally; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 590 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 591 | i++; |
| 592 | j = i; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 595 | if (j < len) { |
| 596 | item = PyString_FromStringAndSize(s+j, (int)(len - j)); |
| 597 | if (item == NULL) |
| 598 | goto finally; |
| 599 | err = PyList_Append(list, item); |
| 600 | Py_DECREF(item); |
| 601 | if (err < 0) |
| 602 | goto finally; |
| 603 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 604 | return list; |
| 605 | finally: |
| 606 | Py_DECREF(list); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | static char split__doc__[] = |
| 612 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 613 | \n\ |
| 614 | Return a list of the words in the string S, using sep as the\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 615 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
| 616 | splits are done. If sep is not specified, any whitespace string\n\ |
| 617 | is a separator."; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 618 | |
| 619 | static PyObject * |
| 620 | string_split(self, args) |
| 621 | PyStringObject *self; |
| 622 | PyObject *args; |
| 623 | { |
| 624 | int len = PyString_GET_SIZE(self), n, i, j, err; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 625 | int maxsplit = -1; |
| 626 | const char *s = PyString_AS_STRING(self), *sub; |
| 627 | PyObject *list, *item, *subobj = Py_None; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 629 | if (!PyArg_ParseTuple(args, "|Oi:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 630 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 631 | if (maxsplit < 0) |
| 632 | maxsplit = INT_MAX; |
| 633 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 634 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 635 | if (PyString_Check(subobj)) { |
| 636 | sub = PyString_AS_STRING(subobj); |
| 637 | n = PyString_GET_SIZE(subobj); |
| 638 | } |
| 639 | else if (PyUnicode_Check(subobj)) |
| 640 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
| 641 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 642 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 643 | if (n == 0) { |
| 644 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | list = PyList_New(0); |
| 649 | if (list == NULL) |
| 650 | return NULL; |
| 651 | |
| 652 | i = j = 0; |
| 653 | while (i+n <= len) { |
| 654 | if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 655 | if (maxsplit-- <= 0) |
| 656 | break; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 657 | item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 658 | if (item == NULL) |
| 659 | goto fail; |
| 660 | err = PyList_Append(list, item); |
| 661 | Py_DECREF(item); |
| 662 | if (err < 0) |
| 663 | goto fail; |
| 664 | i = j = i + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 665 | } |
| 666 | else |
| 667 | i++; |
| 668 | } |
| 669 | item = PyString_FromStringAndSize(s+j, (int)(len-j)); |
| 670 | if (item == NULL) |
| 671 | goto fail; |
| 672 | err = PyList_Append(list, item); |
| 673 | Py_DECREF(item); |
| 674 | if (err < 0) |
| 675 | goto fail; |
| 676 | |
| 677 | return list; |
| 678 | |
| 679 | fail: |
| 680 | Py_DECREF(list); |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | static char join__doc__[] = |
| 686 | "S.join(sequence) -> string\n\ |
| 687 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 688 | Return a string which is the concatenation of the strings in the\n\ |
| 689 | sequence. The separator between elements is S."; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 690 | |
| 691 | static PyObject * |
| 692 | string_join(self, args) |
| 693 | PyStringObject *self; |
| 694 | PyObject *args; |
| 695 | { |
| 696 | char *sep = PyString_AS_STRING(self); |
| 697 | int seplen = PyString_GET_SIZE(self); |
| 698 | PyObject *res = NULL; |
| 699 | int reslen = 0; |
| 700 | char *p; |
| 701 | int seqlen = 0; |
| 702 | int sz = 100; |
| 703 | int i, slen; |
| 704 | PyObject *seq; |
| 705 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 706 | if (!PyArg_ParseTuple(args, "O:join", &seq)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 707 | return NULL; |
| 708 | |
| 709 | seqlen = PySequence_Length(seq); |
| 710 | if (seqlen < 0 && PyErr_Occurred()) |
| 711 | return NULL; |
| 712 | |
| 713 | if (seqlen == 1) { |
| 714 | /* Optimization if there's only one item */ |
| 715 | PyObject *item = PySequence_GetItem(seq, 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 716 | if (item == NULL) |
| 717 | return NULL; |
| 718 | if (!PyString_Check(item) && |
| 719 | !PyUnicode_Check(item)) { |
| 720 | PyErr_SetString(PyExc_TypeError, |
| 721 | "first argument must be sequence of strings"); |
| 722 | Py_DECREF(item); |
| 723 | return NULL; |
| 724 | } |
| 725 | return item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 726 | } |
| 727 | if (!(res = PyString_FromStringAndSize((char*)NULL, sz))) |
| 728 | return NULL; |
| 729 | p = PyString_AsString(res); |
| 730 | |
| 731 | /* optimize for lists. all others (tuples and arbitrary sequences) |
| 732 | * just use the abstract interface. |
| 733 | */ |
| 734 | if (PyList_Check(seq)) { |
| 735 | for (i = 0; i < seqlen; i++) { |
| 736 | PyObject *item = PyList_GET_ITEM(seq, i); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 737 | if (!PyString_Check(item)){ |
| 738 | if (PyUnicode_Check(item)) { |
| 739 | Py_DECREF(res); |
| 740 | return PyUnicode_Join( |
| 741 | (PyObject *)self, |
| 742 | seq); |
Barry Warsaw | bf32583 | 2000-03-06 14:52:18 +0000 | [diff] [blame] | 743 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 744 | PyErr_Format(PyExc_TypeError, |
| 745 | "sequence item %i not a string", |
| 746 | i); |
| 747 | goto finally; |
| 748 | } |
| 749 | slen = PyString_GET_SIZE(item); |
| 750 | while (reslen + slen + seplen >= sz) { |
| 751 | if (_PyString_Resize(&res, sz*2)) |
| 752 | goto finally; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 753 | sz *= 2; |
| 754 | p = PyString_AsString(res) + reslen; |
| 755 | } |
| 756 | if (i > 0) { |
| 757 | memcpy(p, sep, seplen); |
| 758 | p += seplen; |
| 759 | reslen += seplen; |
| 760 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 761 | memcpy(p, PyString_AS_STRING(item), slen); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 762 | p += slen; |
| 763 | reslen += slen; |
| 764 | } |
| 765 | } |
| 766 | else { |
| 767 | for (i = 0; i < seqlen; i++) { |
| 768 | PyObject *item = PySequence_GetItem(seq, i); |
Barry Warsaw | bf32583 | 2000-03-06 14:52:18 +0000 | [diff] [blame] | 769 | if (!item) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 770 | goto finally; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 771 | if (!PyString_Check(item)){ |
| 772 | if (PyUnicode_Check(item)) { |
| 773 | Py_DECREF(res); |
| 774 | Py_DECREF(item); |
| 775 | return PyUnicode_Join( |
| 776 | (PyObject *)self, |
| 777 | seq); |
| 778 | } |
| 779 | Py_DECREF(item); |
| 780 | PyErr_Format(PyExc_TypeError, |
| 781 | "sequence item %i not a string", |
| 782 | i); |
Barry Warsaw | bf32583 | 2000-03-06 14:52:18 +0000 | [diff] [blame] | 783 | goto finally; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 784 | } |
| 785 | slen = PyString_GET_SIZE(item); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 786 | while (reslen + slen + seplen >= sz) { |
Barry Warsaw | bf32583 | 2000-03-06 14:52:18 +0000 | [diff] [blame] | 787 | if (_PyString_Resize(&res, sz*2)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 788 | Py_DECREF(item); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 789 | goto finally; |
Barry Warsaw | bf32583 | 2000-03-06 14:52:18 +0000 | [diff] [blame] | 790 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 791 | sz *= 2; |
| 792 | p = PyString_AsString(res) + reslen; |
| 793 | } |
| 794 | if (i > 0) { |
| 795 | memcpy(p, sep, seplen); |
| 796 | p += seplen; |
| 797 | reslen += seplen; |
| 798 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 799 | memcpy(p, PyString_AS_STRING(item), slen); |
| 800 | Py_DECREF(item); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 801 | p += slen; |
| 802 | reslen += slen; |
| 803 | } |
| 804 | } |
| 805 | if (_PyString_Resize(&res, reslen)) |
| 806 | goto finally; |
| 807 | return res; |
| 808 | |
| 809 | finally: |
| 810 | Py_DECREF(res); |
| 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | |
| 815 | |
| 816 | static long |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 817 | string_find_internal(self, args, dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 818 | PyStringObject *self; |
| 819 | PyObject *args; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 820 | int dir; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 821 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 822 | const char *s = PyString_AS_STRING(self), *sub; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 823 | int len = PyString_GET_SIZE(self); |
| 824 | int n, i = 0, last = INT_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 825 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 827 | if (!PyArg_ParseTuple(args, "O|ii:find/rfind/index/rindex", |
| 828 | &subobj, &i, &last)) |
| 829 | return -2; |
| 830 | if (PyString_Check(subobj)) { |
| 831 | sub = PyString_AS_STRING(subobj); |
| 832 | n = PyString_GET_SIZE(subobj); |
| 833 | } |
| 834 | else if (PyUnicode_Check(subobj)) |
| 835 | return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); |
| 836 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 837 | return -2; |
| 838 | |
| 839 | if (last > len) |
| 840 | last = len; |
| 841 | if (last < 0) |
| 842 | last += len; |
| 843 | if (last < 0) |
| 844 | last = 0; |
| 845 | if (i < 0) |
| 846 | i += len; |
| 847 | if (i < 0) |
| 848 | i = 0; |
| 849 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 850 | if (dir > 0) { |
| 851 | if (n == 0 && i <= last) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 852 | return (long)i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 853 | last -= n; |
| 854 | for (; i <= last; ++i) |
| 855 | if (s[i] == sub[0] && |
| 856 | (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0)) |
| 857 | return (long)i; |
| 858 | } |
| 859 | else { |
| 860 | int j; |
| 861 | |
| 862 | if (n == 0 && i <= last) |
| 863 | return (long)last; |
| 864 | for (j = last-n; j >= i; --j) |
| 865 | if (s[j] == sub[0] && |
| 866 | (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0)) |
| 867 | return (long)j; |
| 868 | } |
| 869 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 870 | return -1; |
| 871 | } |
| 872 | |
| 873 | |
| 874 | static char find__doc__[] = |
| 875 | "S.find(sub [,start [,end]]) -> int\n\ |
| 876 | \n\ |
| 877 | Return the lowest index in S where substring sub is found,\n\ |
| 878 | such that sub is contained within s[start,end]. Optional\n\ |
| 879 | arguments start and end are interpreted as in slice notation.\n\ |
| 880 | \n\ |
| 881 | Return -1 on failure."; |
| 882 | |
| 883 | static PyObject * |
| 884 | string_find(self, args) |
| 885 | PyStringObject *self; |
| 886 | PyObject *args; |
| 887 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 888 | long result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 889 | if (result == -2) |
| 890 | return NULL; |
| 891 | return PyInt_FromLong(result); |
| 892 | } |
| 893 | |
| 894 | |
| 895 | static char index__doc__[] = |
| 896 | "S.index(sub [,start [,end]]) -> int\n\ |
| 897 | \n\ |
| 898 | Like S.find() but raise ValueError when the substring is not found."; |
| 899 | |
| 900 | static PyObject * |
| 901 | string_index(self, args) |
| 902 | PyStringObject *self; |
| 903 | PyObject *args; |
| 904 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 905 | long result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 906 | if (result == -2) |
| 907 | return NULL; |
| 908 | if (result == -1) { |
| 909 | PyErr_SetString(PyExc_ValueError, |
| 910 | "substring not found in string.index"); |
| 911 | return NULL; |
| 912 | } |
| 913 | return PyInt_FromLong(result); |
| 914 | } |
| 915 | |
| 916 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 917 | static char rfind__doc__[] = |
| 918 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 919 | \n\ |
| 920 | Return the highest index in S where substring sub is found,\n\ |
| 921 | such that sub is contained within s[start,end]. Optional\n\ |
| 922 | arguments start and end are interpreted as in slice notation.\n\ |
| 923 | \n\ |
| 924 | Return -1 on failure."; |
| 925 | |
| 926 | static PyObject * |
| 927 | string_rfind(self, args) |
| 928 | PyStringObject *self; |
| 929 | PyObject *args; |
| 930 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 931 | long result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 932 | if (result == -2) |
| 933 | return NULL; |
| 934 | return PyInt_FromLong(result); |
| 935 | } |
| 936 | |
| 937 | |
| 938 | static char rindex__doc__[] = |
| 939 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 940 | \n\ |
| 941 | Like S.rfind() but raise ValueError when the substring is not found."; |
| 942 | |
| 943 | static PyObject * |
| 944 | string_rindex(self, args) |
| 945 | PyStringObject *self; |
| 946 | PyObject *args; |
| 947 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 948 | long result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 949 | if (result == -2) |
| 950 | return NULL; |
| 951 | if (result == -1) { |
| 952 | PyErr_SetString(PyExc_ValueError, |
| 953 | "substring not found in string.rindex"); |
| 954 | return NULL; |
| 955 | } |
| 956 | return PyInt_FromLong(result); |
| 957 | } |
| 958 | |
| 959 | |
| 960 | static PyObject * |
| 961 | do_strip(self, args, striptype) |
| 962 | PyStringObject *self; |
| 963 | PyObject *args; |
| 964 | int striptype; |
| 965 | { |
| 966 | char *s = PyString_AS_STRING(self); |
| 967 | int len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 968 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 969 | if (!PyArg_ParseTuple(args, ":strip")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 970 | return NULL; |
| 971 | |
| 972 | i = 0; |
| 973 | if (striptype != RIGHTSTRIP) { |
| 974 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 975 | i++; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | j = len; |
| 980 | if (striptype != LEFTSTRIP) { |
| 981 | do { |
| 982 | j--; |
| 983 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 984 | j++; |
| 985 | } |
| 986 | |
| 987 | if (i == 0 && j == len) { |
| 988 | Py_INCREF(self); |
| 989 | return (PyObject*)self; |
| 990 | } |
| 991 | else |
| 992 | return PyString_FromStringAndSize(s+i, j-i); |
| 993 | } |
| 994 | |
| 995 | |
| 996 | static char strip__doc__[] = |
| 997 | "S.strip() -> string\n\ |
| 998 | \n\ |
| 999 | Return a copy of the string S with leading and trailing\n\ |
| 1000 | whitespace removed."; |
| 1001 | |
| 1002 | static PyObject * |
| 1003 | string_strip(self, args) |
| 1004 | PyStringObject *self; |
| 1005 | PyObject *args; |
| 1006 | { |
| 1007 | return do_strip(self, args, BOTHSTRIP); |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | static char lstrip__doc__[] = |
| 1012 | "S.lstrip() -> string\n\ |
| 1013 | \n\ |
| 1014 | Return a copy of the string S with leading whitespace removed."; |
| 1015 | |
| 1016 | static PyObject * |
| 1017 | string_lstrip(self, args) |
| 1018 | PyStringObject *self; |
| 1019 | PyObject *args; |
| 1020 | { |
| 1021 | return do_strip(self, args, LEFTSTRIP); |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | static char rstrip__doc__[] = |
| 1026 | "S.rstrip() -> string\n\ |
| 1027 | \n\ |
| 1028 | Return a copy of the string S with trailing whitespace removed."; |
| 1029 | |
| 1030 | static PyObject * |
| 1031 | string_rstrip(self, args) |
| 1032 | PyStringObject *self; |
| 1033 | PyObject *args; |
| 1034 | { |
| 1035 | return do_strip(self, args, RIGHTSTRIP); |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | static char lower__doc__[] = |
| 1040 | "S.lower() -> string\n\ |
| 1041 | \n\ |
| 1042 | Return a copy of the string S converted to lowercase."; |
| 1043 | |
| 1044 | static PyObject * |
| 1045 | string_lower(self, args) |
| 1046 | PyStringObject *self; |
| 1047 | PyObject *args; |
| 1048 | { |
| 1049 | char *s = PyString_AS_STRING(self), *s_new; |
| 1050 | int i, n = PyString_GET_SIZE(self); |
| 1051 | PyObject *new; |
| 1052 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1053 | if (!PyArg_ParseTuple(args, ":lower")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1054 | return NULL; |
| 1055 | new = PyString_FromStringAndSize(NULL, n); |
| 1056 | if (new == NULL) |
| 1057 | return NULL; |
| 1058 | s_new = PyString_AsString(new); |
| 1059 | for (i = 0; i < n; i++) { |
| 1060 | int c = Py_CHARMASK(*s++); |
| 1061 | if (isupper(c)) { |
| 1062 | *s_new = tolower(c); |
| 1063 | } else |
| 1064 | *s_new = c; |
| 1065 | s_new++; |
| 1066 | } |
| 1067 | return new; |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | static char upper__doc__[] = |
| 1072 | "S.upper() -> string\n\ |
| 1073 | \n\ |
| 1074 | Return a copy of the string S converted to uppercase."; |
| 1075 | |
| 1076 | static PyObject * |
| 1077 | string_upper(self, args) |
| 1078 | PyStringObject *self; |
| 1079 | PyObject *args; |
| 1080 | { |
| 1081 | char *s = PyString_AS_STRING(self), *s_new; |
| 1082 | int i, n = PyString_GET_SIZE(self); |
| 1083 | PyObject *new; |
| 1084 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1085 | if (!PyArg_ParseTuple(args, ":upper")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1086 | return NULL; |
| 1087 | new = PyString_FromStringAndSize(NULL, n); |
| 1088 | if (new == NULL) |
| 1089 | return NULL; |
| 1090 | s_new = PyString_AsString(new); |
| 1091 | for (i = 0; i < n; i++) { |
| 1092 | int c = Py_CHARMASK(*s++); |
| 1093 | if (islower(c)) { |
| 1094 | *s_new = toupper(c); |
| 1095 | } else |
| 1096 | *s_new = c; |
| 1097 | s_new++; |
| 1098 | } |
| 1099 | return new; |
| 1100 | } |
| 1101 | |
| 1102 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1103 | static char title__doc__[] = |
| 1104 | "S.title() -> string\n\ |
| 1105 | \n\ |
| 1106 | Return a titlecased version of S, i.e. words start with uppercase\n\ |
| 1107 | characters, all remaining cased characters have lowercase."; |
| 1108 | |
| 1109 | static PyObject* |
| 1110 | string_title(PyUnicodeObject *self, PyObject *args) |
| 1111 | { |
| 1112 | char *s = PyString_AS_STRING(self), *s_new; |
| 1113 | int i, n = PyString_GET_SIZE(self); |
| 1114 | int previous_is_cased = 0; |
| 1115 | PyObject *new; |
| 1116 | |
| 1117 | if (!PyArg_ParseTuple(args, ":title")) |
| 1118 | return NULL; |
| 1119 | new = PyString_FromStringAndSize(NULL, n); |
| 1120 | if (new == NULL) |
| 1121 | return NULL; |
| 1122 | s_new = PyString_AsString(new); |
| 1123 | for (i = 0; i < n; i++) { |
| 1124 | int c = Py_CHARMASK(*s++); |
| 1125 | if (islower(c)) { |
| 1126 | if (!previous_is_cased) |
| 1127 | c = toupper(c); |
| 1128 | previous_is_cased = 1; |
| 1129 | } else if (isupper(c)) { |
| 1130 | if (previous_is_cased) |
| 1131 | c = tolower(c); |
| 1132 | previous_is_cased = 1; |
| 1133 | } else |
| 1134 | previous_is_cased = 0; |
| 1135 | *s_new++ = c; |
| 1136 | } |
| 1137 | return new; |
| 1138 | } |
| 1139 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1140 | static char capitalize__doc__[] = |
| 1141 | "S.capitalize() -> string\n\ |
| 1142 | \n\ |
| 1143 | Return a copy of the string S with only its first character\n\ |
| 1144 | capitalized."; |
| 1145 | |
| 1146 | static PyObject * |
| 1147 | string_capitalize(self, args) |
| 1148 | PyStringObject *self; |
| 1149 | PyObject *args; |
| 1150 | { |
| 1151 | char *s = PyString_AS_STRING(self), *s_new; |
| 1152 | int i, n = PyString_GET_SIZE(self); |
| 1153 | PyObject *new; |
| 1154 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1155 | if (!PyArg_ParseTuple(args, ":capitalize")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1156 | return NULL; |
| 1157 | new = PyString_FromStringAndSize(NULL, n); |
| 1158 | if (new == NULL) |
| 1159 | return NULL; |
| 1160 | s_new = PyString_AsString(new); |
| 1161 | if (0 < n) { |
| 1162 | int c = Py_CHARMASK(*s++); |
| 1163 | if (islower(c)) |
| 1164 | *s_new = toupper(c); |
| 1165 | else |
| 1166 | *s_new = c; |
| 1167 | s_new++; |
| 1168 | } |
| 1169 | for (i = 1; i < n; i++) { |
| 1170 | int c = Py_CHARMASK(*s++); |
| 1171 | if (isupper(c)) |
| 1172 | *s_new = tolower(c); |
| 1173 | else |
| 1174 | *s_new = c; |
| 1175 | s_new++; |
| 1176 | } |
| 1177 | return new; |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | static char count__doc__[] = |
| 1182 | "S.count(sub[, start[, end]]) -> int\n\ |
| 1183 | \n\ |
| 1184 | Return the number of occurrences of substring sub in string\n\ |
| 1185 | S[start:end]. Optional arguments start and end are\n\ |
| 1186 | interpreted as in slice notation."; |
| 1187 | |
| 1188 | static PyObject * |
| 1189 | string_count(self, args) |
| 1190 | PyStringObject *self; |
| 1191 | PyObject *args; |
| 1192 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1193 | const char *s = PyString_AS_STRING(self), *sub; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1194 | int len = PyString_GET_SIZE(self), n; |
| 1195 | int i = 0, last = INT_MAX; |
| 1196 | int m, r; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1197 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1198 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1199 | if (!PyArg_ParseTuple(args, "O|ii:count", &subobj, &i, &last)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1200 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1201 | if (PyString_Check(subobj)) { |
| 1202 | sub = PyString_AS_STRING(subobj); |
| 1203 | n = PyString_GET_SIZE(subobj); |
| 1204 | } |
| 1205 | else if (PyUnicode_Check(subobj)) |
| 1206 | return PyInt_FromLong( |
| 1207 | PyUnicode_Count((PyObject *)self, subobj, i, last)); |
| 1208 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1209 | return NULL; |
| 1210 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1211 | if (last > len) |
| 1212 | last = len; |
| 1213 | if (last < 0) |
| 1214 | last += len; |
| 1215 | if (last < 0) |
| 1216 | last = 0; |
| 1217 | if (i < 0) |
| 1218 | i += len; |
| 1219 | if (i < 0) |
| 1220 | i = 0; |
| 1221 | m = last + 1 - n; |
| 1222 | if (n == 0) |
| 1223 | return PyInt_FromLong((long) (m-i)); |
| 1224 | |
| 1225 | r = 0; |
| 1226 | while (i < m) { |
| 1227 | if (!memcmp(s+i, sub, n)) { |
| 1228 | r++; |
| 1229 | i += n; |
| 1230 | } else { |
| 1231 | i++; |
| 1232 | } |
| 1233 | } |
| 1234 | return PyInt_FromLong((long) r); |
| 1235 | } |
| 1236 | |
| 1237 | |
| 1238 | static char swapcase__doc__[] = |
| 1239 | "S.swapcase() -> string\n\ |
| 1240 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1241 | Return a copy of the string S with uppercase characters\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1242 | converted to lowercase and vice versa."; |
| 1243 | |
| 1244 | static PyObject * |
| 1245 | string_swapcase(self, args) |
| 1246 | PyStringObject *self; |
| 1247 | PyObject *args; |
| 1248 | { |
| 1249 | char *s = PyString_AS_STRING(self), *s_new; |
| 1250 | int i, n = PyString_GET_SIZE(self); |
| 1251 | PyObject *new; |
| 1252 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1253 | if (!PyArg_ParseTuple(args, ":swapcase")) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1254 | return NULL; |
| 1255 | new = PyString_FromStringAndSize(NULL, n); |
| 1256 | if (new == NULL) |
| 1257 | return NULL; |
| 1258 | s_new = PyString_AsString(new); |
| 1259 | for (i = 0; i < n; i++) { |
| 1260 | int c = Py_CHARMASK(*s++); |
| 1261 | if (islower(c)) { |
| 1262 | *s_new = toupper(c); |
| 1263 | } |
| 1264 | else if (isupper(c)) { |
| 1265 | *s_new = tolower(c); |
| 1266 | } |
| 1267 | else |
| 1268 | *s_new = c; |
| 1269 | s_new++; |
| 1270 | } |
| 1271 | return new; |
| 1272 | } |
| 1273 | |
| 1274 | |
| 1275 | static char translate__doc__[] = |
| 1276 | "S.translate(table [,deletechars]) -> string\n\ |
| 1277 | \n\ |
| 1278 | Return a copy of the string S, where all characters occurring\n\ |
| 1279 | in the optional argument deletechars are removed, and the\n\ |
| 1280 | remaining characters have been mapped through the given\n\ |
| 1281 | translation table, which must be a string of length 256."; |
| 1282 | |
| 1283 | static PyObject * |
| 1284 | string_translate(self, args) |
| 1285 | PyStringObject *self; |
| 1286 | PyObject *args; |
| 1287 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1288 | register char *input, *output; |
| 1289 | register const char *table; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1290 | register int i, c, changed = 0; |
| 1291 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1292 | const char *table1, *output_start, *del_table=NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1293 | int inlen, tablen, dellen = 0; |
| 1294 | PyObject *result; |
| 1295 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1296 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1298 | if (!PyArg_ParseTuple(args, "O|O:translate", |
| 1299 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1300 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (PyString_Check(tableobj)) { |
| 1303 | table1 = PyString_AS_STRING(tableobj); |
| 1304 | tablen = PyString_GET_SIZE(tableobj); |
| 1305 | } |
| 1306 | else if (PyUnicode_Check(tableobj)) { |
| 1307 | /* Unicode .translate() does not support the deletechars |
| 1308 | parameter; instead a mapping to None will cause characters |
| 1309 | to be deleted. */ |
| 1310 | if (delobj != NULL) { |
| 1311 | PyErr_SetString(PyExc_TypeError, |
| 1312 | "deletions are implemented differently for unicode"); |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 1316 | } |
| 1317 | else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1318 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (delobj != NULL) { |
| 1321 | if (PyString_Check(delobj)) { |
| 1322 | del_table = PyString_AS_STRING(delobj); |
| 1323 | dellen = PyString_GET_SIZE(delobj); |
| 1324 | } |
| 1325 | else if (PyUnicode_Check(delobj)) { |
| 1326 | PyErr_SetString(PyExc_TypeError, |
| 1327 | "deletions are implemented differently for unicode"); |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 1331 | return NULL; |
| 1332 | |
| 1333 | if (tablen != 256) { |
| 1334 | PyErr_SetString(PyExc_ValueError, |
| 1335 | "translation table must be 256 characters long"); |
| 1336 | return NULL; |
| 1337 | } |
| 1338 | } |
| 1339 | else { |
| 1340 | del_table = NULL; |
| 1341 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | table = table1; |
| 1345 | inlen = PyString_Size(input_obj); |
| 1346 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 1347 | if (result == NULL) |
| 1348 | return NULL; |
| 1349 | output_start = output = PyString_AsString(result); |
| 1350 | input = PyString_AsString(input_obj); |
| 1351 | |
| 1352 | if (dellen == 0) { |
| 1353 | /* If no deletions are required, use faster code */ |
| 1354 | for (i = inlen; --i >= 0; ) { |
| 1355 | c = Py_CHARMASK(*input++); |
| 1356 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1357 | changed = 1; |
| 1358 | } |
| 1359 | if (changed) |
| 1360 | return result; |
| 1361 | Py_DECREF(result); |
| 1362 | Py_INCREF(input_obj); |
| 1363 | return input_obj; |
| 1364 | } |
| 1365 | |
| 1366 | for (i = 0; i < 256; i++) |
| 1367 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1368 | |
| 1369 | for (i = 0; i < dellen; i++) |
| 1370 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 1371 | |
| 1372 | for (i = inlen; --i >= 0; ) { |
| 1373 | c = Py_CHARMASK(*input++); |
| 1374 | if (trans_table[c] != -1) |
| 1375 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1376 | continue; |
| 1377 | changed = 1; |
| 1378 | } |
| 1379 | if (!changed) { |
| 1380 | Py_DECREF(result); |
| 1381 | Py_INCREF(input_obj); |
| 1382 | return input_obj; |
| 1383 | } |
| 1384 | /* Fix the size of the resulting string */ |
| 1385 | if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
| 1386 | return NULL; |
| 1387 | return result; |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 1392 | |
| 1393 | /* |
| 1394 | mymemfind |
| 1395 | |
| 1396 | strstr replacement for arbitrary blocks of memory. |
| 1397 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1398 | Locates the first occurrence in the memory pointed to by MEM of the |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1399 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 1400 | found, or -1 if not found. If len of PAT is greater than length of |
| 1401 | MEM, the function returns -1. |
| 1402 | */ |
| 1403 | static int |
| 1404 | mymemfind(mem, len, pat, pat_len) |
| 1405 | char *mem; |
| 1406 | int len; |
| 1407 | char *pat; |
| 1408 | int pat_len; |
| 1409 | { |
| 1410 | register int ii; |
| 1411 | |
| 1412 | /* pattern can not occur in the last pat_len-1 chars */ |
| 1413 | len -= pat_len; |
| 1414 | |
| 1415 | for (ii = 0; ii <= len; ii++) { |
| 1416 | if (mem[ii] == pat[0] && |
| 1417 | (pat_len == 1 || |
| 1418 | memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) { |
| 1419 | return ii; |
| 1420 | } |
| 1421 | } |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | mymemcnt |
| 1427 | |
| 1428 | Return the number of distinct times PAT is found in MEM. |
| 1429 | meaning mem=1111 and pat==11 returns 2. |
| 1430 | mem=11111 and pat==11 also return 2. |
| 1431 | */ |
| 1432 | static int |
| 1433 | mymemcnt(mem, len, pat, pat_len) |
| 1434 | char *mem; |
| 1435 | int len; |
| 1436 | char *pat; |
| 1437 | int pat_len; |
| 1438 | { |
| 1439 | register int offset = 0; |
| 1440 | int nfound = 0; |
| 1441 | |
| 1442 | while (len >= 0) { |
| 1443 | offset = mymemfind(mem, len, pat, pat_len); |
| 1444 | if (offset == -1) |
| 1445 | break; |
| 1446 | mem += offset + pat_len; |
| 1447 | len -= offset + pat_len; |
| 1448 | nfound++; |
| 1449 | } |
| 1450 | return nfound; |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | mymemreplace |
| 1455 | |
| 1456 | Return a string in which all occurences of PAT in memory STR are |
| 1457 | replaced with SUB. |
| 1458 | |
| 1459 | If length of PAT is less than length of STR or there are no occurences |
| 1460 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 1461 | string is allocated here and returned. |
| 1462 | |
| 1463 | on return, out_len is: |
| 1464 | the length of output string, or |
| 1465 | -1 if the input string is returned, or |
| 1466 | unchanged if an error occurs (no memory). |
| 1467 | |
| 1468 | return value is: |
| 1469 | the new string allocated locally, or |
| 1470 | NULL if an error occurred. |
| 1471 | */ |
| 1472 | static char * |
| 1473 | mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len) |
| 1474 | char *str; |
| 1475 | int len; /* input string */ |
| 1476 | char *pat; |
| 1477 | int pat_len; /* pattern string to find */ |
| 1478 | char *sub; |
| 1479 | int sub_len; /* substitution string */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1480 | int count; /* number of replacements */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1481 | int *out_len; |
| 1482 | |
| 1483 | { |
| 1484 | char *out_s; |
| 1485 | char *new_s; |
| 1486 | int nfound, offset, new_len; |
| 1487 | |
| 1488 | if (len == 0 || pat_len > len) |
| 1489 | goto return_same; |
| 1490 | |
| 1491 | /* find length of output string */ |
| 1492 | nfound = mymemcnt(str, len, pat, pat_len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1493 | if (count < 0) |
| 1494 | count = INT_MAX; |
| 1495 | else if (nfound > count) |
| 1496 | nfound = count; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1497 | if (nfound == 0) |
| 1498 | goto return_same; |
| 1499 | new_len = len + nfound*(sub_len - pat_len); |
| 1500 | |
| 1501 | new_s = (char *)malloc(new_len); |
| 1502 | if (new_s == NULL) return NULL; |
| 1503 | |
| 1504 | *out_len = new_len; |
| 1505 | out_s = new_s; |
| 1506 | |
| 1507 | while (len > 0) { |
| 1508 | /* find index of next instance of pattern */ |
| 1509 | offset = mymemfind(str, len, pat, pat_len); |
| 1510 | /* if not found, break out of loop */ |
| 1511 | if (offset == -1) break; |
| 1512 | |
| 1513 | /* copy non matching part of input string */ |
| 1514 | memcpy(new_s, str, offset); /* copy part of str before pat */ |
| 1515 | str += offset + pat_len; /* move str past pattern */ |
| 1516 | len -= offset + pat_len; /* reduce length of str remaining */ |
| 1517 | |
| 1518 | /* copy substitute into the output string */ |
| 1519 | new_s += offset; /* move new_s to dest for sub string */ |
| 1520 | memcpy(new_s, sub, sub_len); /* copy substring into new_s */ |
| 1521 | new_s += sub_len; /* offset new_s past sub string */ |
| 1522 | |
| 1523 | /* break when we've done count replacements */ |
| 1524 | if (--count == 0) break; |
| 1525 | } |
| 1526 | /* copy any remaining values into output string */ |
| 1527 | if (len > 0) |
| 1528 | memcpy(new_s, str, len); |
| 1529 | return out_s; |
| 1530 | |
| 1531 | return_same: |
| 1532 | *out_len = -1; |
| 1533 | return str; |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | static char replace__doc__[] = |
| 1538 | "S.replace (old, new[, maxsplit]) -> string\n\ |
| 1539 | \n\ |
| 1540 | Return a copy of string S with all occurrences of substring\n\ |
| 1541 | old replaced by new. If the optional argument maxsplit is\n\ |
| 1542 | given, only the first maxsplit occurrences are replaced."; |
| 1543 | |
| 1544 | static PyObject * |
| 1545 | string_replace(self, args) |
| 1546 | PyStringObject *self; |
| 1547 | PyObject *args; |
| 1548 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1549 | const char *str = PyString_AS_STRING(self), *sub, *repl; |
| 1550 | char *new_s; |
| 1551 | int len = PyString_GET_SIZE(self), sub_len, repl_len, out_len; |
| 1552 | int count = -1; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1553 | PyObject *new; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1554 | PyObject *subobj, *replobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1555 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1556 | if (!PyArg_ParseTuple(args, "OO|i:replace", |
| 1557 | &subobj, &replobj, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1558 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1559 | |
| 1560 | if (PyString_Check(subobj)) { |
| 1561 | sub = PyString_AS_STRING(subobj); |
| 1562 | sub_len = PyString_GET_SIZE(subobj); |
| 1563 | } |
| 1564 | else if (PyUnicode_Check(subobj)) |
| 1565 | return PyUnicode_Replace((PyObject *)self, |
| 1566 | subobj, replobj, count); |
| 1567 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
| 1568 | return NULL; |
| 1569 | |
| 1570 | if (PyString_Check(replobj)) { |
| 1571 | repl = PyString_AS_STRING(replobj); |
| 1572 | repl_len = PyString_GET_SIZE(replobj); |
| 1573 | } |
| 1574 | else if (PyUnicode_Check(replobj)) |
| 1575 | return PyUnicode_Replace((PyObject *)self, |
| 1576 | subobj, replobj, count); |
| 1577 | else if (PyObject_AsCharBuffer(replobj, &repl, &repl_len)) |
| 1578 | return NULL; |
| 1579 | |
Guido van Rossum | 96a45ad | 2000-03-13 15:56:08 +0000 | [diff] [blame] | 1580 | if (sub_len <= 0) { |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 1581 | PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1582 | return NULL; |
| 1583 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1584 | new_s = mymemreplace(str,len,sub,sub_len,repl,repl_len,count,&out_len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1585 | if (new_s == NULL) { |
| 1586 | PyErr_NoMemory(); |
| 1587 | return NULL; |
| 1588 | } |
| 1589 | if (out_len == -1) { |
| 1590 | /* we're returning another reference to self */ |
| 1591 | new = (PyObject*)self; |
| 1592 | Py_INCREF(new); |
| 1593 | } |
| 1594 | else { |
| 1595 | new = PyString_FromStringAndSize(new_s, out_len); |
| 1596 | free(new_s); |
| 1597 | } |
| 1598 | return new; |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | static char startswith__doc__[] = |
| 1603 | "S.startswith(prefix[, start[, end]]) -> int\n\ |
| 1604 | \n\ |
| 1605 | Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ |
| 1606 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1607 | comparing S at that position."; |
| 1608 | |
| 1609 | static PyObject * |
| 1610 | string_startswith(self, args) |
| 1611 | PyStringObject *self; |
| 1612 | PyObject *args; |
| 1613 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1614 | const char* str = PyString_AS_STRING(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1615 | int len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1616 | const char* prefix; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1617 | int plen; |
| 1618 | int start = 0; |
| 1619 | int end = -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1620 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1621 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1622 | if (!PyArg_ParseTuple(args, "O|ii:startswith", &subobj, &start, &end)) |
| 1623 | return NULL; |
| 1624 | if (PyString_Check(subobj)) { |
| 1625 | prefix = PyString_AS_STRING(subobj); |
| 1626 | plen = PyString_GET_SIZE(subobj); |
| 1627 | } |
| 1628 | else if (PyUnicode_Check(subobj)) |
| 1629 | return PyInt_FromLong( |
| 1630 | PyUnicode_Tailmatch((PyObject *)self, |
| 1631 | subobj, start, end, -1)); |
| 1632 | else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1633 | return NULL; |
| 1634 | |
| 1635 | /* adopt Java semantics for index out of range. it is legal for |
| 1636 | * offset to be == plen, but this only returns true if prefix is |
| 1637 | * the empty string. |
| 1638 | */ |
| 1639 | if (start < 0 || start+plen > len) |
| 1640 | return PyInt_FromLong(0); |
| 1641 | |
| 1642 | if (!memcmp(str+start, prefix, plen)) { |
| 1643 | /* did the match end after the specified end? */ |
| 1644 | if (end < 0) |
| 1645 | return PyInt_FromLong(1); |
| 1646 | else if (end - start < plen) |
| 1647 | return PyInt_FromLong(0); |
| 1648 | else |
| 1649 | return PyInt_FromLong(1); |
| 1650 | } |
| 1651 | else return PyInt_FromLong(0); |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | static char endswith__doc__[] = |
| 1656 | "S.endswith(suffix[, start[, end]]) -> int\n\ |
| 1657 | \n\ |
| 1658 | Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ |
| 1659 | optional start, test S beginning at that position. With optional end, stop\n\ |
| 1660 | comparing S at that position."; |
| 1661 | |
| 1662 | static PyObject * |
| 1663 | string_endswith(self, args) |
| 1664 | PyStringObject *self; |
| 1665 | PyObject *args; |
| 1666 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1667 | const char* str = PyString_AS_STRING(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1668 | int len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1669 | const char* suffix; |
| 1670 | int slen; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1671 | int start = 0; |
| 1672 | int end = -1; |
| 1673 | int lower, upper; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1674 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1675 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1676 | if (!PyArg_ParseTuple(args, "O|ii:endswith", &subobj, &start, &end)) |
| 1677 | return NULL; |
| 1678 | if (PyString_Check(subobj)) { |
| 1679 | suffix = PyString_AS_STRING(subobj); |
| 1680 | slen = PyString_GET_SIZE(subobj); |
| 1681 | } |
| 1682 | else if (PyUnicode_Check(subobj)) |
| 1683 | return PyInt_FromLong( |
| 1684 | PyUnicode_Tailmatch((PyObject *)self, |
| 1685 | subobj, start, end, +1)); |
| 1686 | else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1687 | return NULL; |
| 1688 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1689 | if (start < 0 || start > len || slen > len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1690 | return PyInt_FromLong(0); |
| 1691 | |
| 1692 | upper = (end >= 0 && end <= len) ? end : len; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1693 | lower = (upper - slen) > start ? (upper - slen) : start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1695 | if (upper-lower >= slen && !memcmp(str+lower, suffix, slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1696 | return PyInt_FromLong(1); |
| 1697 | else return PyInt_FromLong(0); |
| 1698 | } |
| 1699 | |
| 1700 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1701 | static char expandtabs__doc__[] = |
| 1702 | "S.expandtabs([tabsize]) -> string\n\ |
| 1703 | \n\ |
| 1704 | Return a copy of S where all tab characters are expanded using spaces.\n\ |
| 1705 | If tabsize is not given, a tab size of 8 characters is assumed."; |
| 1706 | |
| 1707 | static PyObject* |
| 1708 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 1709 | { |
| 1710 | const char *e, *p; |
| 1711 | char *q; |
| 1712 | int i, j; |
| 1713 | PyObject *u; |
| 1714 | int tabsize = 8; |
| 1715 | |
| 1716 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 1717 | return NULL; |
| 1718 | |
| 1719 | /* First pass: determine size of ouput string */ |
| 1720 | i = j = 0; |
| 1721 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 1722 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 1723 | if (*p == '\t') { |
| 1724 | if (tabsize > 0) |
| 1725 | j += tabsize - (j % tabsize); |
| 1726 | } |
| 1727 | else { |
| 1728 | j++; |
| 1729 | if (*p == '\n' || *p == '\r') { |
| 1730 | i += j; |
| 1731 | j = 0; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | /* Second pass: create output string and fill it */ |
| 1736 | u = PyString_FromStringAndSize(NULL, i + j); |
| 1737 | if (!u) |
| 1738 | return NULL; |
| 1739 | |
| 1740 | j = 0; |
| 1741 | q = PyString_AS_STRING(u); |
| 1742 | |
| 1743 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 1744 | if (*p == '\t') { |
| 1745 | if (tabsize > 0) { |
| 1746 | i = tabsize - (j % tabsize); |
| 1747 | j += i; |
| 1748 | while (i--) |
| 1749 | *q++ = ' '; |
| 1750 | } |
| 1751 | } |
| 1752 | else { |
| 1753 | j++; |
| 1754 | *q++ = *p; |
| 1755 | if (*p == '\n' || *p == '\r') |
| 1756 | j = 0; |
| 1757 | } |
| 1758 | |
| 1759 | return u; |
| 1760 | } |
| 1761 | |
| 1762 | static |
| 1763 | PyObject *pad(PyStringObject *self, |
| 1764 | int left, |
| 1765 | int right, |
| 1766 | char fill) |
| 1767 | { |
| 1768 | PyObject *u; |
| 1769 | |
| 1770 | if (left < 0) |
| 1771 | left = 0; |
| 1772 | if (right < 0) |
| 1773 | right = 0; |
| 1774 | |
| 1775 | if (left == 0 && right == 0) { |
| 1776 | Py_INCREF(self); |
| 1777 | return (PyObject *)self; |
| 1778 | } |
| 1779 | |
| 1780 | u = PyString_FromStringAndSize(NULL, |
| 1781 | left + PyString_GET_SIZE(self) + right); |
| 1782 | if (u) { |
| 1783 | if (left) |
| 1784 | memset(PyString_AS_STRING(u), fill, left); |
| 1785 | memcpy(PyString_AS_STRING(u) + left, |
| 1786 | PyString_AS_STRING(self), |
| 1787 | PyString_GET_SIZE(self)); |
| 1788 | if (right) |
| 1789 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 1790 | fill, right); |
| 1791 | } |
| 1792 | |
| 1793 | return u; |
| 1794 | } |
| 1795 | |
| 1796 | static char ljust__doc__[] = |
| 1797 | "S.ljust(width) -> string\n\ |
| 1798 | \n\ |
| 1799 | Return S left justified in a string of length width. Padding is\n\ |
| 1800 | done using spaces."; |
| 1801 | |
| 1802 | static PyObject * |
| 1803 | string_ljust(PyStringObject *self, PyObject *args) |
| 1804 | { |
| 1805 | int width; |
| 1806 | if (!PyArg_ParseTuple(args, "i:ljust", &width)) |
| 1807 | return NULL; |
| 1808 | |
| 1809 | if (PyString_GET_SIZE(self) >= width) { |
| 1810 | Py_INCREF(self); |
| 1811 | return (PyObject*) self; |
| 1812 | } |
| 1813 | |
| 1814 | return pad(self, 0, width - PyString_GET_SIZE(self), ' '); |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | static char rjust__doc__[] = |
| 1819 | "S.rjust(width) -> string\n\ |
| 1820 | \n\ |
| 1821 | Return S right justified in a string of length width. Padding is\n\ |
| 1822 | done using spaces."; |
| 1823 | |
| 1824 | static PyObject * |
| 1825 | string_rjust(PyStringObject *self, PyObject *args) |
| 1826 | { |
| 1827 | int width; |
| 1828 | if (!PyArg_ParseTuple(args, "i:rjust", &width)) |
| 1829 | return NULL; |
| 1830 | |
| 1831 | if (PyString_GET_SIZE(self) >= width) { |
| 1832 | Py_INCREF(self); |
| 1833 | return (PyObject*) self; |
| 1834 | } |
| 1835 | |
| 1836 | return pad(self, width - PyString_GET_SIZE(self), 0, ' '); |
| 1837 | } |
| 1838 | |
| 1839 | |
| 1840 | static char center__doc__[] = |
| 1841 | "S.center(width) -> string\n\ |
| 1842 | \n\ |
| 1843 | Return S centered in a string of length width. Padding is done\n\ |
| 1844 | using spaces."; |
| 1845 | |
| 1846 | static PyObject * |
| 1847 | string_center(PyStringObject *self, PyObject *args) |
| 1848 | { |
| 1849 | int marg, left; |
| 1850 | int width; |
| 1851 | |
| 1852 | if (!PyArg_ParseTuple(args, "i:center", &width)) |
| 1853 | return NULL; |
| 1854 | |
| 1855 | if (PyString_GET_SIZE(self) >= width) { |
| 1856 | Py_INCREF(self); |
| 1857 | return (PyObject*) self; |
| 1858 | } |
| 1859 | |
| 1860 | marg = width - PyString_GET_SIZE(self); |
| 1861 | left = marg / 2 + (marg & width & 1); |
| 1862 | |
| 1863 | return pad(self, left, marg - left, ' '); |
| 1864 | } |
| 1865 | |
| 1866 | #if 0 |
| 1867 | static char zfill__doc__[] = |
| 1868 | "S.zfill(width) -> string\n\ |
| 1869 | \n\ |
| 1870 | Pad a numeric string x with zeros on the left, to fill a field\n\ |
| 1871 | of the specified width. The string x is never truncated."; |
| 1872 | |
| 1873 | static PyObject * |
| 1874 | string_zfill(PyStringObject *self, PyObject *args) |
| 1875 | { |
| 1876 | int fill; |
| 1877 | PyObject *u; |
| 1878 | char *str; |
| 1879 | |
| 1880 | int width; |
| 1881 | if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
| 1882 | return NULL; |
| 1883 | |
| 1884 | if (PyString_GET_SIZE(self) >= width) { |
| 1885 | Py_INCREF(self); |
| 1886 | return (PyObject*) self; |
| 1887 | } |
| 1888 | |
| 1889 | fill = width - PyString_GET_SIZE(self); |
| 1890 | |
| 1891 | u = pad(self, fill, 0, '0'); |
| 1892 | if (u == NULL) |
| 1893 | return NULL; |
| 1894 | |
| 1895 | str = PyString_AS_STRING(u); |
| 1896 | if (str[fill] == '+' || str[fill] == '-') { |
| 1897 | /* move sign to beginning of string */ |
| 1898 | str[0] = str[fill]; |
| 1899 | str[fill] = '0'; |
| 1900 | } |
| 1901 | |
| 1902 | return u; |
| 1903 | } |
| 1904 | #endif |
| 1905 | |
| 1906 | static char isspace__doc__[] = |
| 1907 | "S.isspace() -> int\n\ |
| 1908 | \n\ |
| 1909 | Return 1 if there are only whitespace characters in S,\n\ |
| 1910 | 0 otherwise."; |
| 1911 | |
| 1912 | static PyObject* |
| 1913 | string_isspace(PyStringObject *self, PyObject *args) |
| 1914 | { |
| 1915 | register const char *p = PyString_AS_STRING(self); |
| 1916 | register const char *e; |
| 1917 | |
| 1918 | if (!PyArg_NoArgs(args)) |
| 1919 | return NULL; |
| 1920 | |
| 1921 | /* Shortcut for single character strings */ |
| 1922 | if (PyString_GET_SIZE(self) == 1 && |
| 1923 | isspace(*p)) |
| 1924 | return PyInt_FromLong(1); |
| 1925 | |
| 1926 | e = p + PyString_GET_SIZE(self); |
| 1927 | for (; p < e; p++) { |
| 1928 | if (!isspace(*p)) |
| 1929 | return PyInt_FromLong(0); |
| 1930 | } |
| 1931 | return PyInt_FromLong(1); |
| 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | static char isdigit__doc__[] = |
| 1936 | "S.isdigit() -> int\n\ |
| 1937 | \n\ |
| 1938 | Return 1 if there are only digit characters in S,\n\ |
| 1939 | 0 otherwise."; |
| 1940 | |
| 1941 | static PyObject* |
| 1942 | string_isdigit(PyStringObject *self, PyObject *args) |
| 1943 | { |
| 1944 | register const char *p = PyString_AS_STRING(self); |
| 1945 | register const char *e; |
| 1946 | |
| 1947 | if (!PyArg_NoArgs(args)) |
| 1948 | return NULL; |
| 1949 | |
| 1950 | /* Shortcut for single character strings */ |
| 1951 | if (PyString_GET_SIZE(self) == 1 && |
| 1952 | isdigit(*p)) |
| 1953 | return PyInt_FromLong(1); |
| 1954 | |
| 1955 | e = p + PyString_GET_SIZE(self); |
| 1956 | for (; p < e; p++) { |
| 1957 | if (!isdigit(*p)) |
| 1958 | return PyInt_FromLong(0); |
| 1959 | } |
| 1960 | return PyInt_FromLong(1); |
| 1961 | } |
| 1962 | |
| 1963 | |
| 1964 | static char islower__doc__[] = |
| 1965 | "S.islower() -> int\n\ |
| 1966 | \n\ |
| 1967 | Return 1 if all cased characters in S are lowercase and there is\n\ |
| 1968 | at least one cased character in S, 0 otherwise."; |
| 1969 | |
| 1970 | static PyObject* |
| 1971 | string_islower(PyStringObject *self, PyObject *args) |
| 1972 | { |
| 1973 | register const char *p = PyString_AS_STRING(self); |
| 1974 | register const char *e; |
| 1975 | int cased; |
| 1976 | |
| 1977 | if (!PyArg_NoArgs(args)) |
| 1978 | return NULL; |
| 1979 | |
| 1980 | /* Shortcut for single character strings */ |
| 1981 | if (PyString_GET_SIZE(self) == 1) |
| 1982 | return PyInt_FromLong(islower(*p) != 0); |
| 1983 | |
| 1984 | e = p + PyString_GET_SIZE(self); |
| 1985 | cased = 0; |
| 1986 | for (; p < e; p++) { |
| 1987 | if (isupper(*p)) |
| 1988 | return PyInt_FromLong(0); |
| 1989 | else if (!cased && islower(*p)) |
| 1990 | cased = 1; |
| 1991 | } |
| 1992 | return PyInt_FromLong(cased); |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | static char isupper__doc__[] = |
| 1997 | "S.isupper() -> int\n\ |
| 1998 | \n\ |
| 1999 | Return 1 if all cased characters in S are uppercase and there is\n\ |
| 2000 | at least one cased character in S, 0 otherwise."; |
| 2001 | |
| 2002 | static PyObject* |
| 2003 | string_isupper(PyStringObject *self, PyObject *args) |
| 2004 | { |
| 2005 | register const char *p = PyString_AS_STRING(self); |
| 2006 | register const char *e; |
| 2007 | int cased; |
| 2008 | |
| 2009 | if (!PyArg_NoArgs(args)) |
| 2010 | return NULL; |
| 2011 | |
| 2012 | /* Shortcut for single character strings */ |
| 2013 | if (PyString_GET_SIZE(self) == 1) |
| 2014 | return PyInt_FromLong(isupper(*p) != 0); |
| 2015 | |
| 2016 | e = p + PyString_GET_SIZE(self); |
| 2017 | cased = 0; |
| 2018 | for (; p < e; p++) { |
| 2019 | if (islower(*p)) |
| 2020 | return PyInt_FromLong(0); |
| 2021 | else if (!cased && isupper(*p)) |
| 2022 | cased = 1; |
| 2023 | } |
| 2024 | return PyInt_FromLong(cased); |
| 2025 | } |
| 2026 | |
| 2027 | |
| 2028 | static char istitle__doc__[] = |
| 2029 | "S.istitle() -> int\n\ |
| 2030 | \n\ |
| 2031 | Return 1 if S is a titlecased string, i.e. uppercase characters\n\ |
| 2032 | may only follow uncased characters and lowercase characters only cased\n\ |
| 2033 | ones. Return 0 otherwise."; |
| 2034 | |
| 2035 | static PyObject* |
| 2036 | string_istitle(PyStringObject *self, PyObject *args) |
| 2037 | { |
| 2038 | register const char *p = PyString_AS_STRING(self); |
| 2039 | register const char *e; |
| 2040 | int cased, previous_is_cased; |
| 2041 | |
| 2042 | if (!PyArg_NoArgs(args)) |
| 2043 | return NULL; |
| 2044 | |
| 2045 | /* Shortcut for single character strings */ |
| 2046 | if (PyString_GET_SIZE(self) == 1) |
| 2047 | return PyInt_FromLong(isupper(*p) != 0); |
| 2048 | |
| 2049 | e = p + PyString_GET_SIZE(self); |
| 2050 | cased = 0; |
| 2051 | previous_is_cased = 0; |
| 2052 | for (; p < e; p++) { |
| 2053 | register const char ch = *p; |
| 2054 | |
| 2055 | if (isupper(ch)) { |
| 2056 | if (previous_is_cased) |
| 2057 | return PyInt_FromLong(0); |
| 2058 | previous_is_cased = 1; |
| 2059 | cased = 1; |
| 2060 | } |
| 2061 | else if (islower(ch)) { |
| 2062 | if (!previous_is_cased) |
| 2063 | return PyInt_FromLong(0); |
| 2064 | previous_is_cased = 1; |
| 2065 | cased = 1; |
| 2066 | } |
| 2067 | else |
| 2068 | previous_is_cased = 0; |
| 2069 | } |
| 2070 | return PyInt_FromLong(cased); |
| 2071 | } |
| 2072 | |
| 2073 | |
| 2074 | static char splitlines__doc__[] = |
| 2075 | "S.splitlines([maxsplit]]) -> list of strings\n\ |
| 2076 | \n\ |
| 2077 | Return a list of the lines in S, breaking at line boundaries.\n\ |
| 2078 | If maxsplit is given, at most maxsplit are done. Line breaks are not\n\ |
| 2079 | included in the resulting list."; |
| 2080 | |
| 2081 | #define SPLIT_APPEND(data, left, right) \ |
| 2082 | str = PyString_FromStringAndSize(data + left, right - left); \ |
| 2083 | if (!str) \ |
| 2084 | goto onError; \ |
| 2085 | if (PyList_Append(list, str)) { \ |
| 2086 | Py_DECREF(str); \ |
| 2087 | goto onError; \ |
| 2088 | } \ |
| 2089 | else \ |
| 2090 | Py_DECREF(str); |
| 2091 | |
| 2092 | static PyObject* |
| 2093 | string_splitlines(PyStringObject *self, PyObject *args) |
| 2094 | { |
| 2095 | int maxcount = -1; |
| 2096 | register int i; |
| 2097 | register int j; |
| 2098 | int len; |
| 2099 | PyObject *list; |
| 2100 | PyObject *str; |
| 2101 | char *data; |
| 2102 | |
| 2103 | if (!PyArg_ParseTuple(args, "|i:splitlines", &maxcount)) |
| 2104 | return NULL; |
| 2105 | |
| 2106 | data = PyString_AS_STRING(self); |
| 2107 | len = PyString_GET_SIZE(self); |
| 2108 | |
| 2109 | if (maxcount < 0) |
| 2110 | maxcount = INT_MAX; |
| 2111 | |
| 2112 | list = PyList_New(0); |
| 2113 | if (!list) |
| 2114 | goto onError; |
| 2115 | |
| 2116 | for (i = j = 0; i < len; ) { |
| 2117 | /* Find a line and append it */ |
| 2118 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 2119 | i++; |
| 2120 | if (maxcount-- <= 0) |
| 2121 | break; |
| 2122 | SPLIT_APPEND(data, j, i); |
| 2123 | |
| 2124 | /* Skip the line break reading CRLF as one line break */ |
| 2125 | if (i < len) { |
| 2126 | if (data[i] == '\r' && i + 1 < len && |
| 2127 | data[i+1] == '\n') |
| 2128 | i += 2; |
| 2129 | else |
| 2130 | i++; |
| 2131 | } |
| 2132 | j = i; |
| 2133 | } |
| 2134 | if (j < len) { |
| 2135 | SPLIT_APPEND(data, j, len); |
| 2136 | } |
| 2137 | |
| 2138 | return list; |
| 2139 | |
| 2140 | onError: |
| 2141 | Py_DECREF(list); |
| 2142 | return NULL; |
| 2143 | } |
| 2144 | |
| 2145 | #undef SPLIT_APPEND |
| 2146 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2147 | |
| 2148 | static PyMethodDef |
| 2149 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2150 | /* Counterparts of the obsolete stropmodule functions; except |
| 2151 | string.maketrans(). */ |
| 2152 | {"join", (PyCFunction)string_join, 1, join__doc__}, |
| 2153 | {"split", (PyCFunction)string_split, 1, split__doc__}, |
| 2154 | {"lower", (PyCFunction)string_lower, 1, lower__doc__}, |
| 2155 | {"upper", (PyCFunction)string_upper, 1, upper__doc__}, |
| 2156 | {"islower", (PyCFunction)string_islower, 0, islower__doc__}, |
| 2157 | {"isupper", (PyCFunction)string_isupper, 0, isupper__doc__}, |
| 2158 | {"isspace", (PyCFunction)string_isspace, 0, isspace__doc__}, |
| 2159 | {"isdigit", (PyCFunction)string_isdigit, 0, isdigit__doc__}, |
| 2160 | {"istitle", (PyCFunction)string_istitle, 0, istitle__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2161 | {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__}, |
| 2162 | {"count", (PyCFunction)string_count, 1, count__doc__}, |
| 2163 | {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__}, |
| 2164 | {"find", (PyCFunction)string_find, 1, find__doc__}, |
| 2165 | {"index", (PyCFunction)string_index, 1, index__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2166 | {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2167 | {"replace", (PyCFunction)string_replace, 1, replace__doc__}, |
| 2168 | {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__}, |
| 2169 | {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__}, |
| 2170 | {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2171 | {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__}, |
| 2172 | {"strip", (PyCFunction)string_strip, 1, strip__doc__}, |
| 2173 | {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__}, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2174 | {"translate", (PyCFunction)string_translate, 1, translate__doc__}, |
| 2175 | {"title", (PyCFunction)string_title, 1, title__doc__}, |
| 2176 | {"ljust", (PyCFunction)string_ljust, 1, ljust__doc__}, |
| 2177 | {"rjust", (PyCFunction)string_rjust, 1, rjust__doc__}, |
| 2178 | {"center", (PyCFunction)string_center, 1, center__doc__}, |
| 2179 | {"expandtabs", (PyCFunction)string_expandtabs, 1, expandtabs__doc__}, |
| 2180 | {"splitlines", (PyCFunction)string_splitlines, 1, splitlines__doc__}, |
| 2181 | #if 0 |
| 2182 | {"zfill", (PyCFunction)string_zfill, 1, zfill__doc__}, |
| 2183 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2184 | {NULL, NULL} /* sentinel */ |
| 2185 | }; |
| 2186 | |
| 2187 | static PyObject * |
| 2188 | string_getattr(s, name) |
| 2189 | PyStringObject *s; |
| 2190 | char *name; |
| 2191 | { |
| 2192 | return Py_FindMethod(string_methods, (PyObject*)s, name); |
| 2193 | } |
| 2194 | |
| 2195 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2196 | PyTypeObject PyString_Type = { |
| 2197 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2198 | 0, |
| 2199 | "string", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2200 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2201 | sizeof(char), |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2202 | (destructor)string_dealloc, /*tp_dealloc*/ |
| 2203 | (printfunc)string_print, /*tp_print*/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2204 | (getattrfunc)string_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2205 | 0, /*tp_setattr*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2206 | (cmpfunc)string_compare, /*tp_compare*/ |
| 2207 | (reprfunc)string_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2208 | 0, /*tp_as_number*/ |
| 2209 | &string_as_sequence, /*tp_as_sequence*/ |
| 2210 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2211 | (hashfunc)string_hash, /*tp_hash*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2212 | 0, /*tp_call*/ |
| 2213 | 0, /*tp_str*/ |
| 2214 | 0, /*tp_getattro*/ |
| 2215 | 0, /*tp_setattro*/ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2216 | &string_as_buffer, /*tp_as_buffer*/ |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 2217 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2218 | 0, /*tp_doc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2219 | }; |
| 2220 | |
| 2221 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2222 | PyString_Concat(pv, w) |
| 2223 | register PyObject **pv; |
| 2224 | register PyObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2225 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2226 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2227 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2228 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2229 | if (w == NULL || !PyString_Check(*pv)) { |
| 2230 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2231 | *pv = NULL; |
| 2232 | return; |
| 2233 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2234 | v = string_concat((PyStringObject *) *pv, w); |
| 2235 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2236 | *pv = v; |
| 2237 | } |
| 2238 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2239 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2240 | PyString_ConcatAndDel(pv, w) |
| 2241 | register PyObject **pv; |
| 2242 | register PyObject *w; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2243 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2244 | PyString_Concat(pv, w); |
| 2245 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2249 | /* The following function breaks the notion that strings are immutable: |
| 2250 | it changes the size of a string. We get away with this only if there |
| 2251 | is only one module referencing the object. You can also think of it |
| 2252 | as creating a new string object and destroying the old one, only |
| 2253 | more efficiently. In any case, don't use this if the string may |
| 2254 | already be known to some other part of the code... */ |
| 2255 | |
| 2256 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2257 | _PyString_Resize(pv, newsize) |
| 2258 | PyObject **pv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2259 | int newsize; |
| 2260 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2261 | register PyObject *v; |
| 2262 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2263 | v = *pv; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2264 | if (!PyString_Check(v) || v->ob_refcnt != 1) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2265 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2266 | Py_DECREF(v); |
| 2267 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 2268 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2269 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2270 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 2271 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 2272 | --_Py_RefTotal; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2273 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2274 | _Py_ForgetReference(v); |
| 2275 | *pv = (PyObject *) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2276 | realloc((char *)v, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2277 | sizeof(PyStringObject) + newsize * sizeof(char)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2278 | if (*pv == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2279 | PyMem_DEL(v); |
| 2280 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 2281 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2282 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2283 | _Py_NewReference(*pv); |
| 2284 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 2285 | sv->ob_size = newsize; |
| 2286 | sv->ob_sval[newsize] = '\0'; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2287 | return 0; |
| 2288 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2289 | |
| 2290 | /* Helpers for formatstring */ |
| 2291 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2292 | static PyObject * |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2293 | getnextarg(args, arglen, p_argidx) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2294 | PyObject *args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2295 | int arglen; |
| 2296 | int *p_argidx; |
| 2297 | { |
| 2298 | int argidx = *p_argidx; |
| 2299 | if (argidx < arglen) { |
| 2300 | (*p_argidx)++; |
| 2301 | if (arglen < 0) |
| 2302 | return args; |
| 2303 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2304 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2305 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2306 | PyErr_SetString(PyExc_TypeError, |
| 2307 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2308 | return NULL; |
| 2309 | } |
| 2310 | |
| 2311 | #define F_LJUST (1<<0) |
| 2312 | #define F_SIGN (1<<1) |
| 2313 | #define F_BLANK (1<<2) |
| 2314 | #define F_ALT (1<<3) |
| 2315 | #define F_ZERO (1<<4) |
| 2316 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2317 | static int |
| 2318 | formatfloat(buf, flags, prec, type, v) |
| 2319 | char *buf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2320 | int flags; |
| 2321 | int prec; |
| 2322 | int type; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2323 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2324 | { |
| 2325 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2326 | double x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2327 | if (!PyArg_Parse(v, "d;float argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2328 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2329 | if (prec < 0) |
| 2330 | prec = 6; |
| 2331 | if (prec > 50) |
| 2332 | prec = 50; /* Arbitrary limitation */ |
| 2333 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 2334 | type = 'g'; |
| 2335 | sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type); |
| 2336 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2337 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2340 | static int |
| 2341 | formatint(buf, flags, prec, type, v) |
| 2342 | char *buf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2343 | int flags; |
| 2344 | int prec; |
| 2345 | int type; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2346 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2347 | { |
| 2348 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2349 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2350 | if (!PyArg_Parse(v, "l;int argument required", &x)) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2351 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2352 | if (prec < 0) |
| 2353 | prec = 1; |
| 2354 | sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type); |
| 2355 | sprintf(buf, fmt, x); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2356 | return strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2359 | static int |
| 2360 | formatchar(buf, v) |
| 2361 | char *buf; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2362 | PyObject *v; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2363 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2364 | if (PyString_Check(v)) { |
| 2365 | 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] | 2366 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2367 | } |
| 2368 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2369 | 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] | 2370 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2371 | } |
| 2372 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2373 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2376 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2377 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ |
| 2378 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2379 | PyObject * |
| 2380 | PyString_Format(format, args) |
| 2381 | PyObject *format; |
| 2382 | PyObject *args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2383 | { |
| 2384 | char *fmt, *res; |
| 2385 | int fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2386 | int args_owned = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2387 | PyObject *result; |
| 2388 | PyObject *dict = NULL; |
| 2389 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 2390 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2391 | return NULL; |
| 2392 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2393 | fmt = PyString_AsString(format); |
| 2394 | fmtcnt = PyString_Size(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2395 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2396 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2397 | if (result == NULL) |
| 2398 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2399 | res = PyString_AsString(result); |
| 2400 | if (PyTuple_Check(args)) { |
| 2401 | arglen = PyTuple_Size(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2402 | argidx = 0; |
| 2403 | } |
| 2404 | else { |
| 2405 | arglen = -1; |
| 2406 | argidx = -2; |
| 2407 | } |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2408 | if (args->ob_type->tp_as_mapping) |
| 2409 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2410 | while (--fmtcnt >= 0) { |
| 2411 | if (*fmt != '%') { |
| 2412 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2413 | rescnt = fmtcnt + 100; |
| 2414 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2415 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2416 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2417 | res = PyString_AsString(result) |
| 2418 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2419 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2420 | } |
| 2421 | *res++ = *fmt++; |
| 2422 | } |
| 2423 | else { |
| 2424 | /* Got a format specifier */ |
| 2425 | int flags = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2426 | int width = -1; |
| 2427 | int prec = -1; |
| 2428 | int size = 0; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 2429 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2430 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2431 | PyObject *v = NULL; |
| 2432 | PyObject *temp = NULL; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2433 | char *buf; |
| 2434 | int sign; |
| 2435 | int len; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2436 | char tmpbuf[120]; /* For format{float,int,char}() */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 2437 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2438 | if (*fmt == '(') { |
| 2439 | char *keystart; |
| 2440 | int keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2441 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2442 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2443 | |
| 2444 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2445 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2446 | "format requires a mapping"); |
| 2447 | goto error; |
| 2448 | } |
| 2449 | ++fmt; |
| 2450 | --fmtcnt; |
| 2451 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2452 | /* Skip over balanced parentheses */ |
| 2453 | while (pcount > 0 && --fmtcnt >= 0) { |
| 2454 | if (*fmt == ')') |
| 2455 | --pcount; |
| 2456 | else if (*fmt == '(') |
| 2457 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2458 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2459 | } |
| 2460 | keylen = fmt - keystart - 1; |
| 2461 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2462 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2463 | "incomplete format key"); |
| 2464 | goto error; |
| 2465 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2466 | key = PyString_FromStringAndSize(keystart, |
| 2467 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2468 | if (key == NULL) |
| 2469 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2470 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2471 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2472 | args_owned = 0; |
| 2473 | } |
| 2474 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2475 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2476 | if (args == NULL) { |
| 2477 | goto error; |
| 2478 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 2479 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2480 | arglen = -1; |
| 2481 | argidx = -2; |
| 2482 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2483 | while (--fmtcnt >= 0) { |
| 2484 | switch (c = *fmt++) { |
| 2485 | case '-': flags |= F_LJUST; continue; |
| 2486 | case '+': flags |= F_SIGN; continue; |
| 2487 | case ' ': flags |= F_BLANK; continue; |
| 2488 | case '#': flags |= F_ALT; continue; |
| 2489 | case '0': flags |= F_ZERO; continue; |
| 2490 | } |
| 2491 | break; |
| 2492 | } |
| 2493 | if (c == '*') { |
| 2494 | v = getnextarg(args, arglen, &argidx); |
| 2495 | if (v == NULL) |
| 2496 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2497 | if (!PyInt_Check(v)) { |
| 2498 | PyErr_SetString(PyExc_TypeError, |
| 2499 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2500 | goto error; |
| 2501 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2502 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 2503 | if (width < 0) { |
| 2504 | flags |= F_LJUST; |
| 2505 | width = -width; |
| 2506 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2507 | if (--fmtcnt >= 0) |
| 2508 | c = *fmt++; |
| 2509 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2510 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2511 | width = c - '0'; |
| 2512 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2513 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2514 | if (!isdigit(c)) |
| 2515 | break; |
| 2516 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2517 | PyErr_SetString( |
| 2518 | PyExc_ValueError, |
| 2519 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2520 | goto error; |
| 2521 | } |
| 2522 | width = width*10 + (c - '0'); |
| 2523 | } |
| 2524 | } |
| 2525 | if (c == '.') { |
| 2526 | prec = 0; |
| 2527 | if (--fmtcnt >= 0) |
| 2528 | c = *fmt++; |
| 2529 | if (c == '*') { |
| 2530 | v = getnextarg(args, arglen, &argidx); |
| 2531 | if (v == NULL) |
| 2532 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2533 | if (!PyInt_Check(v)) { |
| 2534 | PyErr_SetString( |
| 2535 | PyExc_TypeError, |
| 2536 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2537 | goto error; |
| 2538 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2539 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2540 | if (prec < 0) |
| 2541 | prec = 0; |
| 2542 | if (--fmtcnt >= 0) |
| 2543 | c = *fmt++; |
| 2544 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2545 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2546 | prec = c - '0'; |
| 2547 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2548 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2549 | if (!isdigit(c)) |
| 2550 | break; |
| 2551 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2552 | PyErr_SetString( |
| 2553 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2554 | "prec too big"); |
| 2555 | goto error; |
| 2556 | } |
| 2557 | prec = prec*10 + (c - '0'); |
| 2558 | } |
| 2559 | } |
| 2560 | } /* prec */ |
| 2561 | if (fmtcnt >= 0) { |
| 2562 | if (c == 'h' || c == 'l' || c == 'L') { |
| 2563 | size = c; |
| 2564 | if (--fmtcnt >= 0) |
| 2565 | c = *fmt++; |
| 2566 | } |
| 2567 | } |
| 2568 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2569 | PyErr_SetString(PyExc_ValueError, |
| 2570 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2571 | goto error; |
| 2572 | } |
| 2573 | if (c != '%') { |
| 2574 | v = getnextarg(args, arglen, &argidx); |
| 2575 | if (v == NULL) |
| 2576 | goto error; |
| 2577 | } |
| 2578 | sign = 0; |
| 2579 | fill = ' '; |
| 2580 | switch (c) { |
| 2581 | case '%': |
| 2582 | buf = "%"; |
| 2583 | len = 1; |
| 2584 | break; |
| 2585 | case 's': |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2586 | temp = PyObject_Str(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2587 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2588 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 2589 | if (!PyString_Check(temp)) { |
| 2590 | PyErr_SetString(PyExc_TypeError, |
| 2591 | "%s argument has non-string str()"); |
| 2592 | goto error; |
| 2593 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2594 | buf = PyString_AsString(temp); |
| 2595 | len = PyString_Size(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2596 | if (prec >= 0 && len > prec) |
| 2597 | len = prec; |
| 2598 | break; |
| 2599 | case 'i': |
| 2600 | case 'd': |
| 2601 | case 'u': |
| 2602 | case 'o': |
| 2603 | case 'x': |
| 2604 | case 'X': |
| 2605 | if (c == 'i') |
| 2606 | c = 'd'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2607 | buf = tmpbuf; |
| 2608 | len = formatint(buf, flags, prec, c, v); |
| 2609 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2610 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2611 | sign = (c == 'd'); |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 2612 | if (flags&F_ZERO) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2613 | fill = '0'; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 2614 | if ((flags&F_ALT) && |
| 2615 | (c == 'x' || c == 'X') && |
| 2616 | buf[0] == '0' && buf[1] == c) { |
| 2617 | *res++ = *buf++; |
| 2618 | *res++ = *buf++; |
| 2619 | rescnt -= 2; |
| 2620 | len -= 2; |
| 2621 | width -= 2; |
| 2622 | if (width < 0) |
| 2623 | width = 0; |
| 2624 | } |
| 2625 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2626 | break; |
| 2627 | case 'e': |
| 2628 | case 'E': |
| 2629 | case 'f': |
| 2630 | case 'g': |
| 2631 | case 'G': |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2632 | buf = tmpbuf; |
| 2633 | len = formatfloat(buf, flags, prec, c, v); |
| 2634 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2635 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2636 | sign = 1; |
| 2637 | if (flags&F_ZERO) |
| 2638 | fill = '0'; |
| 2639 | break; |
| 2640 | case 'c': |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 2641 | buf = tmpbuf; |
| 2642 | len = formatchar(buf, v); |
| 2643 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2644 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 2647 | PyErr_Format(PyExc_ValueError, |
| 2648 | "unsupported format character '%c' (0x%x)", |
| 2649 | c, c); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2650 | goto error; |
| 2651 | } |
| 2652 | if (sign) { |
| 2653 | if (*buf == '-' || *buf == '+') { |
| 2654 | sign = *buf++; |
| 2655 | len--; |
| 2656 | } |
| 2657 | else if (flags & F_SIGN) |
| 2658 | sign = '+'; |
| 2659 | else if (flags & F_BLANK) |
| 2660 | sign = ' '; |
| 2661 | else |
| 2662 | sign = '\0'; |
| 2663 | } |
| 2664 | if (width < len) |
| 2665 | width = len; |
| 2666 | if (rescnt < width + (sign != '\0')) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 2667 | reslen -= rescnt; |
| 2668 | rescnt = width + fmtcnt + 100; |
| 2669 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2670 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2671 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2672 | res = PyString_AsString(result) |
| 2673 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2674 | } |
| 2675 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 2676 | if (fill != ' ') |
| 2677 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2678 | rescnt--; |
| 2679 | if (width > len) |
| 2680 | width--; |
| 2681 | } |
| 2682 | if (width > len && !(flags&F_LJUST)) { |
| 2683 | do { |
| 2684 | --rescnt; |
| 2685 | *res++ = fill; |
| 2686 | } while (--width > len); |
| 2687 | } |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 2688 | if (sign && fill == ' ') |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 2689 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2690 | memcpy(res, buf, len); |
| 2691 | res += len; |
| 2692 | rescnt -= len; |
| 2693 | while (--width >= len) { |
| 2694 | --rescnt; |
| 2695 | *res++ = ' '; |
| 2696 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 2697 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2698 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 2699 | "not all arguments converted"); |
| 2700 | goto error; |
| 2701 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2702 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2703 | } /* '%' */ |
| 2704 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 2705 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2706 | PyErr_SetString(PyExc_TypeError, |
| 2707 | "not all arguments converted"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2708 | goto error; |
| 2709 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2710 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2711 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2712 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2713 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2714 | return result; |
| 2715 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2716 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2717 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2718 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 2719 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2720 | return NULL; |
| 2721 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2722 | |
| 2723 | |
| 2724 | #ifdef INTERN_STRINGS |
| 2725 | |
| 2726 | static PyObject *interned; |
| 2727 | |
| 2728 | void |
| 2729 | PyString_InternInPlace(p) |
| 2730 | PyObject **p; |
| 2731 | { |
| 2732 | register PyStringObject *s = (PyStringObject *)(*p); |
| 2733 | PyObject *t; |
| 2734 | if (s == NULL || !PyString_Check(s)) |
| 2735 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
| 2736 | if ((t = s->ob_sinterned) != NULL) { |
| 2737 | if (t == (PyObject *)s) |
| 2738 | return; |
| 2739 | Py_INCREF(t); |
| 2740 | *p = t; |
| 2741 | Py_DECREF(s); |
| 2742 | return; |
| 2743 | } |
| 2744 | if (interned == NULL) { |
| 2745 | interned = PyDict_New(); |
| 2746 | if (interned == NULL) |
| 2747 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 2748 | } |
| 2749 | if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { |
| 2750 | Py_INCREF(t); |
| 2751 | *p = s->ob_sinterned = t; |
| 2752 | Py_DECREF(s); |
| 2753 | return; |
| 2754 | } |
| 2755 | t = (PyObject *)s; |
| 2756 | if (PyDict_SetItem(interned, t, t) == 0) { |
| 2757 | s->ob_sinterned = t; |
| 2758 | return; |
| 2759 | } |
| 2760 | PyErr_Clear(); |
| 2761 | } |
| 2762 | |
| 2763 | |
| 2764 | PyObject * |
| 2765 | PyString_InternFromString(cp) |
| 2766 | const char *cp; |
| 2767 | { |
| 2768 | PyObject *s = PyString_FromString(cp); |
| 2769 | if (s == NULL) |
| 2770 | return NULL; |
| 2771 | PyString_InternInPlace(&s); |
| 2772 | return s; |
| 2773 | } |
| 2774 | |
| 2775 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2776 | |
| 2777 | void |
| 2778 | PyString_Fini() |
| 2779 | { |
| 2780 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2781 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 2782 | Py_XDECREF(characters[i]); |
| 2783 | characters[i] = NULL; |
| 2784 | } |
| 2785 | #ifndef DONT_SHARE_SHORT_STRINGS |
| 2786 | Py_XDECREF(nullstring); |
| 2787 | nullstring = NULL; |
| 2788 | #endif |
Guido van Rossum | 971a7aa | 1997-08-05 02:15:12 +0000 | [diff] [blame] | 2789 | #ifdef INTERN_STRINGS |
| 2790 | if (interned) { |
| 2791 | int pos, changed; |
| 2792 | PyObject *key, *value; |
| 2793 | do { |
| 2794 | changed = 0; |
| 2795 | pos = 0; |
| 2796 | while (PyDict_Next(interned, &pos, &key, &value)) { |
| 2797 | if (key->ob_refcnt == 2 && key == value) { |
| 2798 | PyDict_DelItem(interned, key); |
| 2799 | changed = 1; |
| 2800 | } |
| 2801 | } |
| 2802 | } while (changed); |
| 2803 | } |
| 2804 | #endif |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 2805 | } |