From e20f48799e5a830eb28b67c24e976b1f37f8b4ae Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 24 Apr 2024 21:26:31 +0100 Subject: [PATCH] Codechange: Make StringToContentType() clearer. (#12566) Decouples string to ContentType mapping from position within enum. Slightly less efficient, but removes lengthof, array indices, and casting. --- src/console_cmds.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 62a8e18c6d..781a93f8cc 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -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> 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; }