summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/spl-util.cc
diff options
context:
space:
mode:
authorNeil Moore <neil@s-z.org>2014-07-11 00:11:29 -0400
committerNeil Moore <neil@s-z.org>2014-07-11 00:11:29 -0400
commit9555e25a93d5bb2fa10eefe79a362f5ddb270cde (patch)
tree142351fa48bd5eaad5f00b521a1fc78f610aa574 /crawl-ref/source/spl-util.cc
parent21f9508e466c7a2f130a500bc84fb7dea3c2b24d (diff)
downloadcrawl-ref-9555e25a93d5bb2fa10eefe79a362f5ddb270cde.tar.gz
crawl-ref-9555e25a93d5bb2fa10eefe79a362f5ddb270cde.zip
Improve spell name lookup.
As with the god-name lookup in 0.15-a0-1995-g7e6aeab, prefer names where the substring occurs earlier. The only difference is that here we prefer exact matches to prefixes, so that the order of Blink and Blink Other does not matter when the user searches for "Blink".
Diffstat (limited to 'crawl-ref/source/spl-util.cc')
-rw-r--r--crawl-ref/source/spl-util.cc14
1 files changed, 10 insertions, 4 deletions
diff --git a/crawl-ref/source/spl-util.cc b/crawl-ref/source/spl-util.cc
index b969b0231f..3587a71a69 100644
--- a/crawl-ref/source/spl-util.cc
+++ b/crawl-ref/source/spl-util.cc
@@ -148,21 +148,27 @@ spell_type spell_by_name(string name, bool partial_match)
return SPELL_NO_SPELL;
}
+ // Find the spell with the earliest match for the string.
spell_type spellmatch = SPELL_NO_SPELL;
+ size_t bestpos = string::npos;
for (int i = 0; i < NUM_SPELLS; i++)
{
- spell_type type = static_cast<spell_type>(i);
+ const spell_type type = static_cast<spell_type>(i);
+
if (!is_valid_spell(type))
continue;
- const char *sptitle = spell_title(type);
- const string spell_name = lowercase_string(sptitle);
+ const string spell_name = lowercase_string(spell_title(type));
+ const size_t pos = spell_name.find(name);
- if (spell_name.find(name) != string::npos)
+ if (pos < bestpos)
{
+ // Exact match is better than prefix match.
if (spell_name == name)
return type;
+ // npos is never less than bestpos, so the spec was found.
+ bestpos = pos;
spellmatch = type;
}
}