From a4c2f0778a2d1bc34b123ca0c292b30b3317a3a1 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 6 Apr 2024 19:30:01 +0100 Subject: [PATCH] Codechange: Use range-for to iterate keycode-to-name lookups. (#12429) Replaces C-style looping. --- src/hotkeys.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 2260e456ea..50c25579da 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -26,12 +26,12 @@ static std::vector *_hotkey_lists = nullptr; /** String representation of a keycode */ struct KeycodeNames { - const char *name; ///< Name of the keycode + const std::string_view name; ///< Name of the keycode WindowKeyCodes keycode; ///< The keycode }; /** Array of non-standard keycodes that can be used in the hotkeys config file. */ -static const KeycodeNames _keycode_to_name[] = { +static const std::initializer_list _keycode_to_name = { {"SHIFT", WKC_SHIFT}, {"CTRL", WKC_CTRL}, {"ALT", WKC_ALT}, @@ -100,9 +100,9 @@ static uint16_t ParseCode(const char *start, const char *end) while (start < end && *start == ' ') start++; while (end > start && *end == ' ') end--; std::string_view str{start, (size_t)(end - start)}; - for (uint i = 0; i < lengthof(_keycode_to_name); i++) { - if (StrEqualsIgnoreCase(str, _keycode_to_name[i].name)) { - return _keycode_to_name[i].keycode; + for (const auto &kn : _keycode_to_name) { + if (StrEqualsIgnoreCase(str, kn.name)) { + return kn.keycode; } } if (end - start == 1) { @@ -193,9 +193,9 @@ static std::string KeycodeToString(uint16_t keycode) if (!str.empty()) str += "+"; keycode = keycode & ~WKC_SPECIAL_KEYS; - for (uint i = 0; i < lengthof(_keycode_to_name); i++) { - if (_keycode_to_name[i].keycode == keycode) { - str += _keycode_to_name[i].name; + for (const auto &kn : _keycode_to_name) { + if (kn.keycode == keycode) { + str += kn.name; return str; } }