diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 08a6b286d3..a92e8f618b 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -71,18 +71,6 @@ static IntervalTimer _scheduled_monthly_timer = {{TimerGameCa IConsoleCmdExec(fmt::format("exec {}", filename)); }}; - -/** - * Change a string into its number representation. Supports decimal and hexadecimal numbers. - * @param arg The string to be converted. - * @param base The base for parsing the number, defaults to only decimal numbers. Use 0 to also allow hexadecimal. - * @return The number, or std::nullopt when it could not be parsed. - */ -static std::optional ParseInteger(std::string_view arg, int base = 10) -{ - return StringConsumer{arg}.TryReadIntegerBase(base); -} - /** * Parse an integer using #ParseInteger and convert it to the requested type. * @param arg The string to be converted. diff --git a/src/core/string_consumer.hpp b/src/core/string_consumer.hpp index 6e314d5489..720b37c7a3 100644 --- a/src/core/string_consumer.hpp +++ b/src/core/string_consumer.hpp @@ -892,4 +892,23 @@ public: void SkipIntegerBase(int base); }; +/** + * Change a string into its number representation. + * Supports decimal and hexadecimal numbers. + * Accepts leading and trailing whitespace. Trailing junk is an error. + * @param arg The string to be converted. + * @param base The base for parsing the number, defaults to only decimal numbers. Use 0 to also allow hexadecimal. + * @return The number, or std::nullopt if it could not be parsed. + */ +template +static inline std::optional ParseInteger(std::string_view arg, int base = 10) +{ + StringConsumer consumer{arg}; + consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE); + auto result = consumer.TryReadIntegerBase(base); + consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE); + if (consumer.AnyBytesLeft()) return std::nullopt; + return result; +} + #endif /* STRING_CONSUMER_HPP */