Codechange: Make StringToContentType() clearer. (#12566)

Decouples string to ContentType mapping from position within enum.

Slightly less efficient, but removes lengthof, array indices, and casting.
This commit is contained in:
Peter Nelson 2024-04-24 21:26:31 +01:00 committed by GitHub
parent 5e689ce25e
commit e20f48799e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 3 deletions

View File

@ -2049,9 +2049,16 @@ DEF_CONSOLE_CMD(ConNetworkAuthorizedKey)
/** Resolve a string to a content type. */
static ContentType StringToContentType(const char *str)
{
static const char * const inv_lookup[] = { "", "base", "newgrf", "ai", "ailib", "scenario", "heightmap" };
for (uint i = 1 /* there is no type 0 */; i < lengthof(inv_lookup); i++) {
if (StrEqualsIgnoreCase(str, inv_lookup[i])) return (ContentType)i;
static const std::initializer_list<std::pair<std::string_view, ContentType>> content_types = {
{"base", CONTENT_TYPE_BASE_GRAPHICS},
{"newgrf", CONTENT_TYPE_NEWGRF},
{"ai", CONTENT_TYPE_AI},
{"ailib", CONTENT_TYPE_AI_LIBRARY},
{"scenario", CONTENT_TYPE_SCENARIO},
{"heightmap", CONTENT_TYPE_HEIGHTMAP},
};
for (const auto &ct : content_types) {
if (StrEqualsIgnoreCase(str, ct.first)) return ct.second;
}
return CONTENT_TYPE_END;
}