MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ParseKeycode

Function ParseKeycode

src/hotkeys.cpp:119–139  ·  view source on GitHub ↗

* Parse a string representation of a keycode. * @param keystr Input. * @return A valid keycode or std::nullopt. */

Source from the content-addressed store, hash-verified

117 * @return A valid keycode or std::nullopt.
118 */
119static std::optional<uint16_t> ParseKeycode(std::string_view keystr)
120{
121 if (keystr.empty()) return std::nullopt;
122 StringConsumer consumer{keystr};
123 uint16_t keycode = 0;
124 while (consumer.AnyBytesLeft()) {
125 auto cur = consumer.ReadUntilChar('+', StringConsumer::SKIP_ONE_SEPARATOR);
126 auto code = ParseCode(cur);
127 if (!code.has_value()) return std::nullopt;
128 if (*code & WKC_SPECIAL_KEYS) {
129 /* Some completely wrong keycode we don't support. */
130 if (*code & ~WKC_SPECIAL_KEYS) return std::nullopt;
131 keycode |= *code;
132 } else {
133 /* Ignore the code if it has more then 1 letter. */
134 if (keycode & ~WKC_SPECIAL_KEYS) return std::nullopt;
135 keycode |= *code;
136 }
137 }
138 return keycode;
139}
140
141/**
142 * Parse a string to the keycodes it represents

Callers 1

ParseHotkeysFunction · 0.85

Calls 4

ParseCodeFunction · 0.85
AnyBytesLeftMethod · 0.80
ReadUntilCharMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected