]> granicus.if.org Git - nethack/commitdiff
followup to PR #744 (really this time)
authorPatR <rankin@nethack.org>
Sun, 24 Apr 2022 08:01:00 +0000 (01:01 -0700)
committerPatR <rankin@nethack.org>
Sun, 24 Apr 2022 08:01:00 +0000 (01:01 -0700)
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 "<class> of typename" unless it contains " of ".

src/sp_lev.c

index c31fcae6b67f9b67a03c8dd933d783ffbcc8b4f1..34cb45341cd524fbc3da6d2b533d2dc796f42f27 100644 (file)
@@ -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;
         }