Optimized single-character strings gotten from s[i].

This commit is contained in:
Guido van Rossum 1991-04-04 10:48:33 +00:00
parent e6c67a7263
commit daa8bb334d
1 changed files with 23 additions and 13 deletions

View File

@ -241,28 +241,38 @@ stringslice(a, i, j)
return newsizedstringobject(a->ob_sval + i, (int) (j-i)); 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 * static object *
stringitem(a, i) stringitem(a, i)
stringobject *a; stringobject *a;
register int i; register int i;
{ {
/* This is optimized since this is a common operation! */ int c;
object *v;
register stringobject *op;
if (i < 0 || i >= a->ob_size) { if (i < 0 || i >= a->ob_size) {
err_setstr(IndexError, "string index out of range"); err_setstr(IndexError, "string index out of range");
return NULL; return NULL;
} }
op = (stringobject *) c = a->ob_sval[i] & UCHAR_MAX;
malloc(sizeof(stringobject) + sizeof(char)); v = characters[c];
if (op == NULL) if (v == NULL) {
return err_nomem(); v = newsizedstringobject((char *)NULL, 1);
NEWREF(op); if (v == NULL)
op->ob_type = &Stringtype; return NULL;
op->ob_size = 1; characters[c] = v;
op->ob_sval[0] = a->ob_sval[i]; ((stringobject *)v)->ob_sval[0] = c;
op->ob_sval[1] = '\0'; }
return (object *) op; INCREF(v);
return v;
} }
static int static int