OpenTTD Source 20250529-master-g10c159a79f
string_consumer.hpp File Reference

Parse strings. More...

#include <charconv>
#include "format.hpp"

Go to the source code of this file.

Data Structures

class  StringConsumer
 Parse data from a string / buffer. More...
 

Functions

template<class T = uint32_t>
static std::optional< T > ParseInteger (std::string_view arg, int base=10, bool clamp=false)
 Change a string into its number representation.
 

Detailed Description

Parse strings.

Definition in file string_consumer.hpp.

Function Documentation

◆ ParseInteger()

template<class T = uint32_t>
static std::optional< T > ParseInteger ( std::string_view  arg,
int  base = 10,
bool  clamp = false 
)
inlinestatic

Change a string into its number representation.

Supports decimal and hexadecimal numbers. Accepts leading and trailing whitespace. Trailing junk is an error.

Parameters
argThe string to be converted.
baseThe base for parsing the number, defaults to only decimal numbers. Use 0 to also allow hexadecimal.
clampIf the value is a valid number, but out of range for T, return the maximum representable value. Negative values for unsigned results are still treated as invalid.
Returns
The number, or std::nullopt if it could not be parsed.

Definition at line 917 of file string_consumer.hpp.

References StringConsumer::SkipUntilCharNotIn(), and StringConsumer::WHITESPACE_NO_NEWLINE.

Referenced by ConScrollToTile(), SelectCompanyManagerFaceWindow::OnQueryTextFinished(), IndustryViewWindow::OnQueryTextFinished(), SpriteAlignerWindow::OnQueryTextFinished(), OrdersWindow::OnQueryTextFinished(), CustomCurrencyWindow::OnQueryTextFinished(), ScenarioEditorToolbarWindow::OnQueryTextFinished(), FoundTownWindow::OnQueryTextFinished(), openttd_main(), and ParseType().