* 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. */
| 245 | * @return std::optional with a vector of parsed integers. The optional is empty upon an error. |
| 246 | */ |
| 247 | static 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) |
no test coverage detected