mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use range-for to iterate keycode-to-name lookups. (#12429)
Replaces C-style looping.pull/12439/head
parent
b905209421
commit
a4c2f0778a
|
@ -26,12 +26,12 @@ static std::vector<HotkeyList*> *_hotkey_lists = nullptr;
|
||||||
|
|
||||||
/** String representation of a keycode */
|
/** String representation of a keycode */
|
||||||
struct KeycodeNames {
|
struct KeycodeNames {
|
||||||
const char *name; ///< Name of the keycode
|
const std::string_view name; ///< Name of the keycode
|
||||||
WindowKeyCodes keycode; ///< The keycode
|
WindowKeyCodes keycode; ///< The keycode
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Array of non-standard keycodes that can be used in the hotkeys config file. */
|
/** 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<KeycodeNames> _keycode_to_name = {
|
||||||
{"SHIFT", WKC_SHIFT},
|
{"SHIFT", WKC_SHIFT},
|
||||||
{"CTRL", WKC_CTRL},
|
{"CTRL", WKC_CTRL},
|
||||||
{"ALT", WKC_ALT},
|
{"ALT", WKC_ALT},
|
||||||
|
@ -100,9 +100,9 @@ static uint16_t ParseCode(const char *start, const char *end)
|
||||||
while (start < end && *start == ' ') start++;
|
while (start < end && *start == ' ') start++;
|
||||||
while (end > start && *end == ' ') end--;
|
while (end > start && *end == ' ') end--;
|
||||||
std::string_view str{start, (size_t)(end - start)};
|
std::string_view str{start, (size_t)(end - start)};
|
||||||
for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
|
for (const auto &kn : _keycode_to_name) {
|
||||||
if (StrEqualsIgnoreCase(str, _keycode_to_name[i].name)) {
|
if (StrEqualsIgnoreCase(str, kn.name)) {
|
||||||
return _keycode_to_name[i].keycode;
|
return kn.keycode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (end - start == 1) {
|
if (end - start == 1) {
|
||||||
|
@ -193,9 +193,9 @@ static std::string KeycodeToString(uint16_t keycode)
|
||||||
if (!str.empty()) str += "+";
|
if (!str.empty()) str += "+";
|
||||||
keycode = keycode & ~WKC_SPECIAL_KEYS;
|
keycode = keycode & ~WKC_SPECIAL_KEYS;
|
||||||
|
|
||||||
for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
|
for (const auto &kn : _keycode_to_name) {
|
||||||
if (_keycode_to_name[i].keycode == keycode) {
|
if (kn.keycode == keycode) {
|
||||||
str += _keycode_to_name[i].name;
|
str += kn.name;
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue