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

Function ParseIntList

src/settings.cpp:247–272  ·  view source on GitHub ↗

* Parse a string into a vector of uint32s. * @param str the string to be parsed. Each element in the list is separated by a comma or a space character * @return std::optional with a vector of parsed integers. The optional is empty upon an error. */

Source from the content-addressed store, hash-verified

245 * @return std::optional with a vector of parsed integers. The optional is empty upon an error.
246 */
247static std::optional<std::vector<uint32_t>> ParseIntList(std::string_view str)
248{
249 bool comma = false; // do we accept comma?
250 std::vector<uint32_t> result;
251
252 StringConsumer consumer{str};
253 for (;;) {
254 consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE);
255 if (!consumer.AnyBytesLeft()) break;
256 if (comma && consumer.ReadIf(",")) {
257 /* commas are optional, but we only accept one between values */
258 comma = false;
259 continue;
260 }
261 auto v = consumer.TryReadIntegerBase<uint32_t>(10);
262 if (!v.has_value()) return std::nullopt;
263 result.push_back(*v);
264 comma = true;
265 }
266
267 /* If we have read comma but no number after it, fail.
268 * We have read comma when (n != 0) and comma is not allowed */
269 if (!result.empty() && !comma) return std::nullopt;
270
271 return result;
272}
273
274/**
275 * Load parsed string-values into an integer-array (intlist)

Callers 3

LoadIntListFunction · 0.85
GraphicsSetLoadConfigFunction · 0.85
GRFLoadConfigFunction · 0.85

Calls 5

SkipUntilCharNotInMethod · 0.80
AnyBytesLeftMethod · 0.80
ReadIfMethod · 0.80
push_backMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected