diff options
author | Guido van Rossum <guido@python.org> | 1991-04-04 10:48:33 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1991-04-04 10:48:33 (GMT) |
commit | daa8bb334d77f6d25a4b6dfb1c02f11423011e50 (patch) | |
tree | 586f882e9f6bd39ae6ab25cb0eb2b011df61605b /Objects/stringobject.c | |
parent | e6c67a72637a9cb9166d702b02cff39877ac19bb (diff) | |
download | cpython-daa8bb334d77f6d25a4b6dfb1c02f11423011e50.zip cpython-daa8bb334d77f6d25a4b6dfb1c02f11423011e50.tar.gz cpython-daa8bb334d77f6d25a4b6dfb1c02f11423011e50.tar.bz2 |
Optimized single-character strings gotten from s[i].
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 9b11f92..68f5c3a 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -241,28 +241,38 @@ stringslice(a, i, j) return newsizedstringobject(a->ob_sval + i, (int) (j-i)); } +#ifdef __STDC__ +#include <limits.h> +#else +#ifndef UCHAR_MAX +#define UCHAR_MAX 255 +#endif +#endif + +static object *characters[UCHAR_MAX + 1]; + static object * stringitem(a, i) stringobject *a; register int i; { - /* This is optimized since this is a common operation! */ - - register stringobject *op; + int c; + object *v; if (i < 0 || i >= a->ob_size) { err_setstr(IndexError, "string index out of range"); return NULL; } - op = (stringobject *) - malloc(sizeof(stringobject) + sizeof(char)); - if (op == NULL) - return err_nomem(); - NEWREF(op); - op->ob_type = &Stringtype; - op->ob_size = 1; - op->ob_sval[0] = a->ob_sval[i]; - op->ob_sval[1] = '\0'; - return (object *) op; + c = a->ob_sval[i] & UCHAR_MAX; + v = characters[c]; + if (v == NULL) { + v = newsizedstringobject((char *)NULL, 1); + if (v == NULL) + return NULL; + characters[c] = v; + ((stringobject *)v)->ob_sval[0] = c; + } + INCREF(v); + return v; } static int |