From f886e0d6ce1c993e13499f0233cdb0e849541ac0 Mon Sep 17 00:00:00 2001 From: PatR Date: Sun, 24 Apr 2022 01:01:00 -0700 Subject: [PATCH] followup to PR #744 (really this time) Like the commit message for #743, the followup to it was misIDed as #744. This one is for #744. There's no need to check a name against all the " of typename" unless it contains " of ". --- src/sp_lev.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/sp_lev.c b/src/sp_lev.c index c31fcae6b..34cb45341 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -3268,40 +3268,44 @@ get_table_objclass(lua_State *L) static int find_objtype(lua_State *L, const char *s) { - if (s) { + if (s && *s) { int i; const char *objname; char class = 0; - /* In objects.h, some item classes are defined without prefixes (such - * as "scroll of ") in their names, making some names (such as - * "teleportation") ambiguous. Get the object class if it is - * specified, and only return an object of the matching class. */ - struct { + /* In objects.h, some item classes are defined without prefixes + (such as "scroll of ") in their names, making some names (such + as "teleportation") ambiguous. Get the object class if it is + specified, and only return an object of the matching class. */ + static struct objclasspfx { const char *prefix; char class; } class_prefixes[] = { - {"ring of ", RING_CLASS}, - {"potion of ", POTION_CLASS}, - {"scroll of ", SCROLL_CLASS}, - {"spellbook of ", SPBOOK_CLASS}, - {"wand of ", WAND_CLASS}, - {NULL, 0} + { "ring of ", RING_CLASS }, + { "potion of ", POTION_CLASS }, + { "scroll of ", SCROLL_CLASS }, + { "spellbook of ", SPBOOK_CLASS }, + { "wand of ", WAND_CLASS }, + { NULL, 0 } }; - for (i = 0; class_prefixes[i].prefix; i++) { - const char *p = class_prefixes[i].prefix; - if (!strncmpi(s, p, strlen(p))) { - class = class_prefixes[i].class; - s = s + strlen(p); - break; + + if (strstri(s, " of ")) { + for (i = 0; class_prefixes[i].prefix; i++) { + const char *p = class_prefixes[i].prefix; + + if (!strncmpi(s, p, strlen(p))) { + class = class_prefixes[i].class; + s = s + strlen(p); + break; + } } } /* find by object name */ for (i = 0; i < NUM_OBJECTS; i++) { objname = OBJ_NAME(objects[i]); - if ((!class || class == objects[i].oc_class) && - objname && !strcmpi(s, objname)) + if ((!class || class == objects[i].oc_class) + && objname && !strcmpi(s, objname)) return i; }