Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6a6bdc

Browse files
committed
Optimized stringitem.
1 parent bf10973 commit b6a6bdc

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

Objects/stringobject.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,23 @@ stringitem(a, i)
246246
stringobject *a;
247247
register int i;
248248
{
249+
/* This is optimized since this is a common operation! */
250+
251+
register stringobject *op;
249252
if (i < 0 || i >= a->ob_size) {
250253
err_setstr(IndexError, "string index out of range");
251254
return NULL;
252255
}
253-
return stringslice(a, i, i+1);
256+
op = (stringobject *)
257+
malloc(sizeof(stringobject) + sizeof(char));
258+
if (op == NULL)
259+
return err_nomem();
260+
NEWREF(op);
261+
op->ob_type = &Stringtype;
262+
op->ob_size = 1;
263+
op->ob_sval[0] = a->ob_sval[i];
264+
op->ob_sval[1] = '\0';
265+
return (object *) op;
254266
}
255267

256268
static int

0 commit comments

Comments
 (0)