OpenTTD Source: StringConsumer Class Reference

OpenTTD Source 15.0-RC2

Public Types | Public Member Functions | Static Public Attributes | Static Private Member Functions | Private Attributes
StringConsumer Class Reference

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

`#include <string_consumer.hpp>`

## Public Types
enum | SeparatorUsage { READ_ALL_SEPARATORS , READ_ONE_SEPARATOR , KEEP_SEPARATOR , SKIP_ONE_SEPARATOR , SKIP_ALL_SEPARATORS }
| Treatment of separator characters. More...
using | size_type = std::string_view::size_type

## Public Member Functions
| StringConsumer (std::string_view src)
| Construct parser with data from string.
| StringConsumer (const std::string &src)
| Construct parser with data from string.
| StringConsumer (std::span< const char > src)
| Construct parser with data from span.
bool | AnyBytesLeft () const noexcept
| Check whether any bytes left to read.
size_type | GetBytesLeft () const noexcept
| Get number of bytes left to read.
bool | AnyBytesRead () const noexcept
| Check whether any bytes were already read.
size_type | GetBytesRead () const noexcept
| Get number of already read bytes.
std::string_view | GetOrigData () const noexcept
| Get the original data, as passed to the constructor.
std::string_view | GetReadData () const noexcept
| Get already read data.
std::string_view | GetLeftData () const noexcept
| Get data left to read.
void | SkipAll ()
| Discard all remaining data.
std::optional< uint8_t > | PeekUint8 () const
| Peek binary uint8.
std::optional< uint8_t > | TryReadUint8 ()
| Try to read binary uint8, and then advance reader.
uint8_t | ReadUint8 (uint8_t def=0)
| Read binary uint8, and advance reader.
void | SkipUint8 ()
| Skip binary uint8.
std::optional< int8_t > | PeekSint8 () const
| Peek binary int8.
std::optional< int8_t > | TryReadSint8 ()
| Try to read binary int8, and then advance reader.
int8_t | ReadSint8 (int8_t def=0)
| Read binary int8, and advance reader.
void | SkipSint8 ()
| Skip binary int8.
std::optional< uint16_t > | PeekUint16LE () const
| Peek binary uint16 using little endian.
std::optional< uint16_t > | TryReadUint16LE ()
| Try to read binary uint16, and then advance reader.
uint16_t | ReadUint16LE (uint16_t def=0)
| Read binary uint16 using little endian, and advance reader.
void | SkipUint16LE ()
| Skip binary uint16, and advance reader.
std::optional< int16_t > | PeekSint16LE () const
| Peek binary int16 using little endian.
std::optional< int16_t > | TryReadSint16LE ()
| Try to read binary int16, and then advance reader.
int16_t | ReadSint16LE (int16_t def=0)
| Read binary int16 using little endian, and advance reader.
void | SkipSint16LE ()
| Skip binary int16, and advance reader.
std::optional< uint32_t > | PeekUint32LE () const
| Peek binary uint32 using little endian.
std::optional< uint32_t > | TryReadUint32LE ()
| Try to read binary uint32, and then advance reader.
uint32_t | ReadUint32LE (uint32_t def=0)
| Read binary uint32 using little endian, and advance reader.
void | SkipUint32LE ()
| Skip binary uint32, and advance reader.
std::optional< int32_t > | PeekSint32LE () const
| Peek binary int32 using little endian.
std::optional< int32_t > | TryReadSint32LE ()
| Try to read binary int32, and then advance reader.
int32_t | ReadSint32LE (int32_t def=0)
| Read binary int32 using little endian, and advance reader.
void | SkipSint32LE ()
| Skip binary int32, and advance reader.
std::optional< uint64_t > | PeekUint64LE () const
| Peek binary uint64 using little endian.
std::optional< uint64_t > | TryReadUint64LE ()
| Try to read binary uint64, and then advance reader.
uint64_t | ReadUint64LE (uint64_t def=0)
| Read binary uint64 using little endian, and advance reader.
void | SkipUint64LE ()
| Skip binary uint64, and advance reader.
std::optional< int64_t > | PeekSint64LE () const
| Peek binary int64 using little endian.
std::optional< int64_t > | TryReadSint64LE ()
| Try to read binary int64, and then advance reader.
int64_t | ReadSint64LE (int64_t def=0)
| Read binary int64 using little endian, and advance reader.
void | SkipSint64LE ()
| Skip binary int64, and advance reader.
std::optional< char > | PeekChar () const
| Peek 8-bit character.
std::optional< char > | TryReadChar ()
| Try to read a 8-bit character, and then advance reader.
char | ReadChar (char def='?')
| Read 8-bit character, and advance reader.
void | SkipChar ()
| Skip 8-bit character, and advance reader.
std::pair< size_type, char32_t > | PeekUtf8 () const
| Peek UTF-8 character.
std::optional< char32_t > | TryReadUtf8 ()
| Try to read a UTF-8 character, and then advance reader.
char32_t | ReadUtf8 (char32_t def='?')
| Read UTF-8 character, and advance reader.
void | SkipUtf8 ()
| Skip UTF-8 character, and advance reader.
bool | PeekIf (std::string_view str) const
| Check whether the next data matches 'str'.
bool | ReadIf (std::string_view str)
| Check whether the next data matches 'str', and skip it.
void | SkipIf (std::string_view str)
| If the next data matches 'str', then skip it.
bool | PeekCharIf (char c) const
| Check whether the next 8-bit char matches 'c'.
bool | ReadCharIf (char c)
| Check whether the next 8-bit char matches 'c', and skip it.
void | SkipCharIf (char c)
| If the next data matches the 8-bit char 'c', then skip it.
bool | PeekUtf8If (char32_t c) const
| Check whether the next UTF-8 char matches 'c'.
bool | ReadUtf8If (char32_t c)
| Check whether the next UTF-8 char matches 'c', and skip it.
void | SkipUtf8If (char32_t c)
| If the next data matches the UTF-8 char 'c', then skip it.
std::string_view | Peek (size_type len) const
| Peek the next 'len' bytes.
std::string_view | Read (size_type len)
| Read the next 'len' bytes, and advance reader.
void | Skip (size_type len)
| Discard some bytes.
size_type | Find (std::string_view str) const
| Find first occurrence of 'str'.
size_type | FindChar (char c) const
| Find first occurrence of 8-bit char 'c'.
size_type | FindUtf8 (char32_t c) const
| Find first occurrence of UTF-8 char 'c'.
size_type | FindCharIn (std::string_view chars) const
| Find first occurrence of any 8-bit char in 'chars'.
size_type | FindCharNotIn (std::string_view chars) const
| Find first occurrence of any 8-bit char not in 'chars'.
std::optional< char > | PeekCharIfIn (std::string_view chars) const
| Check whether the next 8-bit char is in 'chars'.
std::optional< char > | ReadCharIfIn (std::string_view chars)
| Read next 8-bit char, check whether it is in 'chars', and advance reader.
void | SkipCharIfIn (std::string_view chars)
| If the next 8-bit char is in 'chars', skip it.
std::optional< char > | PeekCharIfNotIn (std::string_view chars) const
| Check whether the next 8-bit char is not in 'chars'.
std::optional< char > | ReadCharIfNotIn (std::string_view chars)
| Read next 8-bit char, check whether it is not in 'chars', and advance reader.
void | SkipCharIfNotIn (std::string_view chars)
| If the next 8-bit char is not in 'chars', skip it.
std::string_view | PeekUntilCharIn (std::string_view chars) const
| Peek 8-bit chars, while they are not in 'chars', until they are.
std::string_view | ReadUntilCharIn (std::string_view chars)
| Read 8-bit chars, while they are not in 'chars', until they are; and advance reader.
void | SkipUntilCharIn (std::string_view chars)
| Skip 8-bit chars, while they are not in 'chars', until they are.
std::string_view | PeekUntilCharNotIn (std::string_view chars) const
| Peek 8-bit chars, while they are in 'chars', until they are not.
std::string_view | ReadUntilCharNotIn (std::string_view chars)
| Read 8-bit chars, while they are in 'chars', until they are not; and advance reader.
void | SkipUntilCharNotIn (std::string_view chars)
| Skip 8-bit chars, while they are in 'chars', until they are not.
std::string_view | PeekUntil (std::string_view str, SeparatorUsage sep) const
| Peek data until the first occurrence of 'str'.
std::string_view | ReadUntil (std::string_view str, SeparatorUsage sep)
| Read data until the first occurrence of 'str', and advance reader.
void | SkipUntil (std::string_view str, SeparatorUsage sep)
| Skip data until the first occurrence of 'str'.
std::string_view | PeekUntilChar (char c, SeparatorUsage sep) const
| Peek data until the first occurrence of 8-bit char 'c'.
std::string_view | ReadUntilChar (char c, SeparatorUsage sep)
| Read data until the first occurrence of 8-bit char 'c', and advance reader.
void | SkipUntilChar (char c, SeparatorUsage sep)
| Skip data until the first occurrence of 8-bit char 'c'.
std::string_view | PeekUntilUtf8 (char32_t c, SeparatorUsage sep) const
| Peek data until the first occurrence of UTF-8 char 'c'.
std::string_view | ReadUntilUtf8 (char32_t c, SeparatorUsage sep)
| Read data until the first occurrence of UTF-8 char 'c', and advance reader.
void | SkipUntilUtf8 (char32_t c, SeparatorUsage sep)
| Skip data until the first occurrence of UTF-8 char 'c'.
template<class T >
std::pair< size_type, T > | PeekIntegerBase (int base, bool clamp=false) const
| Peek and parse an integer in number 'base'.
template<class T >
std::optional< T > | TryReadIntegerBase (int base, bool clamp=false)
| Try to read and parse an integer in number 'base', and then advance the reader.
template<class T >
T | ReadIntegerBase (int base, T def=0, bool clamp=false)
| Read and parse an integer in number 'base', and advance the reader.
void | SkipIntegerBase (int base)
| Skip an integer in number 'base'.

## Static Public Attributes
static constexpr size_type | npos = std::string_view::npos
| Special value for "end of data".
static const std::string_view | WHITESPACE_NO_NEWLINE = "\t\v\f\r "
| ASCII whitespace characters, excluding new-line.
static const std::string_view | WHITESPACE_OR_NEWLINE = "\t\n\v\f\r "
| ASCII whitespace characters, including new-line.

## Static Private Member Functions
static void | LogError (std::string &&msg)
template<class T >
static std::pair< size_type, T > | ParseIntegerBase (std::string_view src, int base, bool clamp, bool log_errors)

## Private Attributes
std::string_view | src
size_type | position = 0

## Detailed Description

Parse data from a string / buffer.

There are generally four operations for each data type:

- Peek: Check and return validity and value. Do not advance read position.
- TryRead: Check and return validity and value. Advance reader, if valid.
- Read: Check validity, return value or fallback-value. Advance reader, even if value is invalid, to avoid deadlocks/stalling.
- Skip: Discard value. Advance reader, even if value is invalid, to avoid deadlocks/stalling.

Definition at line 27 of file string_consumer.hpp.

## Member Typedef Documentation

## ◆ size_type

using StringConsumer::size_type = std::string_view::size_type

Definition at line 29 of file string_consumer.hpp.

## Member Enumeration Documentation

## ◆ SeparatorUsage

enum StringConsumer::SeparatorUsage

Treatment of separator characters.

Enumerator
READ_ALL_SEPARATORS | Read all consecutive separators, and include them all in the result.
READ_ONE_SEPARATOR | Read one separator, and include it in the result.
KEEP_SEPARATOR | Keep the separator in the data as next value to be read.
SKIP_ONE_SEPARATOR | Read and discard one separator, do not include it in the result.
SKIP_ALL_SEPARATORS | Read and discard all consecutive separators, do not include any in the result.

Definition at line 692 of file string_consumer.hpp.

## Constructor & Destructor Documentation

## ◆ StringConsumer() [1/3]

StringConsumer::StringConsumer | ( | std::string_view | src | ) |
| inlineexplicit

Construct parser with data from string.

Definition at line 57 of file string_consumer.hpp.

## ◆ StringConsumer() [2/3]

StringConsumer::StringConsumer | ( | const std::string & | src | ) |
| inlineexplicit

Construct parser with data from string.

Definition at line 61 of file string_consumer.hpp.

## ◆ StringConsumer() [3/3]

StringConsumer::StringConsumer | ( | std::span< const char > | src | ) |
| inlineexplicit

Construct parser with data from span.

Definition at line 65 of file string_consumer.hpp.

## Member Function Documentation

## ◆ AnyBytesLeft()

bool StringConsumer::AnyBytesLeft | ( |  | ) | const
| inlinenoexcept

Check whether any bytes left to read.

Definition at line 70 of file string_consumer.hpp.

Referenced by DecodeEncodedString(), FixSCCEncoded(), FixSCCEncodedNegative(), GetKeyboardLayout(), HandleNewGRFStringControlCodes(), ParseResolution(), EncodedString::ReplaceParam(), StrMakeValid(), StrValid(), and TranslateTTDPatchCodes().

## ◆ AnyBytesRead()

bool StringConsumer::AnyBytesRead | ( |  | ) | const
| inlinenoexcept

Check whether any bytes were already read.

Definition at line 79 of file string_consumer.hpp.

## ◆ Find()

StringConsumer::size_type StringConsumer::Find | ( | std::string_view | str | ) | const

Find first occurrence of 'str'.

Returns
  Offset from current reader position. 'npos' if no match found.

Definition at line 109 of file string_consumer.cpp.

Referenced by FindChar(), FindUtf8(), and SkipUntil().

## ◆ FindChar()

size_type StringConsumer::FindChar | ( | char | c | ) | const
| inline

Find first occurrence of 8-bit char 'c'.

Returns
  Offset from current reader position. 'npos' if no match found.

Definition at line 554 of file string_consumer.hpp.

References Find().

## ◆ FindCharIn()

StringConsumer::size_type StringConsumer::FindCharIn | ( | std::string_view | chars | ) | const

Find first occurrence of any 8-bit char in 'chars'.

Returns
  Offset from current reader position. 'npos' if no match found.

Definition at line 122 of file string_consumer.cpp.

Referenced by PeekUntilCharIn(), ReadUntilCharIn(), and SkipUntilCharIn().

## ◆ FindCharNotIn()

StringConsumer::size_type StringConsumer::FindCharNotIn | ( | std::string_view | chars | ) | const

Find first occurrence of any 8-bit char not in 'chars'.

Returns
  Offset from current reader position. 'npos' if no match found.

Definition at line 129 of file string_consumer.cpp.

Referenced by PeekUntilCharNotIn(), ReadUntilCharNotIn(), and SkipUntilCharNotIn().

## ◆ FindUtf8()

StringConsumer::size_type StringConsumer::FindUtf8 | ( | char32_t | c | ) | const

Find first occurrence of UTF-8 char 'c'.

Returns
  Offset from current reader position. 'npos' if no match found.

Definition at line 116 of file string_consumer.cpp.

References EncodeUtf8(), and Find().

## ◆ GetBytesLeft()

size_type StringConsumer::GetBytesLeft | ( |  | ) | const
| inlinenoexcept

Get number of bytes left to read.

Definition at line 74 of file string_consumer.hpp.

Referenced by PeekUint16LE(), PeekUint32LE(), PeekUint64LE(), PeekUint8(), and Skip().

## ◆ GetBytesRead()

size_type StringConsumer::GetBytesRead | ( |  | ) | const
| inlinenoexcept

Get number of already read bytes.

Definition at line 83 of file string_consumer.hpp.

## ◆ GetLeftData()

std::string_view StringConsumer::GetLeftData | ( |  | ) | const
| inlinenoexcept

Get data left to read.

Definition at line 96 of file string_consumer.hpp.

Referenced by GetDriverParam().

## ◆ GetOrigData()

std::string_view StringConsumer::GetOrigData | ( |  | ) | const
| inlinenoexcept

Get the original data, as passed to the constructor.

Definition at line 88 of file string_consumer.hpp.

## ◆ GetReadData()

std::string_view StringConsumer::GetReadData | ( |  | ) | const
| inlinenoexcept

Get already read data.

Definition at line 92 of file string_consumer.hpp.

## ◆ LogError()

void StringConsumer::LogError | ( | std::string && | msg | ) |
| staticprivate

Definition at line 29 of file string_consumer.cpp.

## ◆ ParseIntegerBase()

template<class T >

static std::pair< size_type, T > StringConsumer::ParseIntegerBase | ( | std::string_view | src,
|  | int | base,
|  | bool | clamp,
|  | bool | log_errors
| ) |  |
| inlinestaticprivate

Definition at line 800 of file string_consumer.hpp.

## ◆ Peek()

std::string_view StringConsumer::Peek | ( | size_type | len | ) | const

Peek the next 'len' bytes.

Parameters

  len | Bytes to read, 'npos' to read all.

Returns
  Up to 'len' bytes.

Definition at line 86 of file string_consumer.cpp.

Referenced by PeekUntilCharIn(), PeekUntilCharNotIn(), and Read().

## ◆ PeekChar()

std::optional< char > StringConsumer::PeekChar | ( |  | ) | const

Peek 8-bit character.

Returns
  Read char, std::nullopt if not enough data.

Definition at line 73 of file string_consumer.cpp.

References PeekUint8().

Referenced by PeekCharIfIn(), PeekCharIfNotIn(), ReadChar(), and TryReadChar().

## ◆ PeekCharIf()

bool StringConsumer::PeekCharIf | ( | char | c | ) | const
| inline

Check whether the next 8-bit char matches 'c'.

Definition at line 471 of file string_consumer.hpp.

References PeekIf().

Referenced by StrMakeValid().

## ◆ PeekCharIfIn()

std::optional< char > StringConsumer::PeekCharIfIn | ( | std::string_view | chars | ) | const
| inline

Check whether the next 8-bit char is in 'chars'.

Returns
  Matching char, std::nullopt if no match.

Definition at line 579 of file string_consumer.hpp.

References PeekChar().

Referenced by ReadCharIfIn(), and SkipCharIfIn().

## ◆ PeekCharIfNotIn()

std::optional< char > StringConsumer::PeekCharIfNotIn | ( | std::string_view | chars | ) | const
| inline

Check whether the next 8-bit char is not in 'chars'.

Returns
  Non-matching char, std::nullopt if match.

Definition at line 609 of file string_consumer.hpp.

References PeekChar().

Referenced by ReadCharIfNotIn(), and SkipCharIfNotIn().

## ◆ PeekIf()

bool StringConsumer::PeekIf | ( | std::string_view | str | ) | const
| inline

Check whether the next data matches 'str'.

Definition at line 447 of file string_consumer.hpp.

Referenced by PeekCharIf(), ReadIf(), and SkipIf().

## ◆ PeekIntegerBase()

template<class T >

std::pair< size_type, T > StringConsumer::PeekIntegerBase | ( | int | base,
|  | bool | clamp = `false`
| ) |  | const
| inline

Peek and parse an integer in number 'base'.

If 'base == 0', then a prefix '0x' decides between base 16 or base 10.

Parameters

  clamp | If 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
  Length of string match, and parsed value.

Note
  The parser rejects leading whitespace and unary plus.

Definition at line 861 of file string_consumer.hpp.

## ◆ PeekSint16LE()

std::optional< int16_t > StringConsumer::PeekSint16LE | ( |  | ) | const
| inline

Peek binary int16 using little endian.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 204 of file string_consumer.hpp.

References PeekUint16LE().

Referenced by ReadSint16LE(), and TryReadSint16LE().

## ◆ PeekSint32LE()

std::optional< int32_t > StringConsumer::PeekSint32LE | ( |  | ) | const
| inline

Peek binary int32 using little endian.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 273 of file string_consumer.hpp.

References PeekUint32LE().

Referenced by ReadSint32LE(), and TryReadSint32LE().

## ◆ PeekSint64LE()

std::optional< int64_t > StringConsumer::PeekSint64LE | ( |  | ) | const
| inline

Peek binary int64 using little endian.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 342 of file string_consumer.hpp.

References PeekUint64LE().

Referenced by ReadSint64LE(), and TryReadSint64LE().

## ◆ PeekSint8()

std::optional< int8_t > StringConsumer::PeekSint8 | ( |  | ) | const
| inline

Peek binary int8.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 137 of file string_consumer.hpp.

References PeekUint8().

Referenced by ReadSint8(), and TryReadSint8().

## ◆ PeekUint16LE()

std::optional< uint16_t > StringConsumer::PeekUint16LE | ( |  | ) | const

Peek binary uint16 using little endian.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 44 of file string_consumer.cpp.

References GetBytesLeft().

Referenced by PeekSint16LE(), ReadUint16LE(), and TryReadUint16LE().

## ◆ PeekUint32LE()

std::optional< uint32_t > StringConsumer::PeekUint32LE | ( |  | ) | const

Peek binary uint32 using little endian.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 51 of file string_consumer.cpp.

References GetBytesLeft().

Referenced by ByteReader::PeekDWord(), PeekSint32LE(), ReadUint32LE(), and TryReadUint32LE().

## ◆ PeekUint64LE()

std::optional< uint64_t > StringConsumer::PeekUint64LE | ( |  | ) | const

Peek binary uint64 using little endian.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 60 of file string_consumer.cpp.

References GetBytesLeft().

Referenced by PeekSint64LE(), ReadUint64LE(), and TryReadUint64LE().

## ◆ PeekUint8()

std::optional< uint8_t > StringConsumer::PeekUint8 | ( |  | ) | const

Peek binary uint8.

Returns
  Read integer, std::nullopt if not enough data.

Definition at line 38 of file string_consumer.cpp.

References GetBytesLeft().

Referenced by PeekChar(), PeekSint8(), ReadUint8(), and TryReadUint8().

## ◆ PeekUntil()

std::string_view StringConsumer::PeekUntil | ( | std::string_view | str,
|  | SeparatorUsage | sep
| ) |  | const

Peek data until the first occurrence of 'str'.

Parameters

  str | Separator string.
  sep | Whether to include/exclude 'str' from the result.

Definition at line 136 of file string_consumer.cpp.

References READ_ALL_SEPARATORS, and READ_ONE_SEPARATOR.

Referenced by PeekUntilChar(), PeekUntilUtf8(), and ReadUntil().

## ◆ PeekUntilChar()

std::string_view StringConsumer::PeekUntilChar | ( | char | c,
|  | SeparatorUsage | sep
| ) |  | const
| inline

Peek data until the first occurrence of 8-bit char 'c'.

Parameters

  c | Separator char.
  sep | Whether to include/exclude 'c' from the result.

Definition at line 756 of file string_consumer.hpp.

References PeekUntil().

## ◆ PeekUntilCharIn()

std::string_view StringConsumer::PeekUntilCharIn | ( | std::string_view | chars | ) | const
| inline

Peek 8-bit chars, while they are not in 'chars', until they are.

Returns
  Non-matching chars.

Definition at line 639 of file string_consumer.hpp.

References FindCharIn(), and Peek().

## ◆ PeekUntilCharNotIn()

std::string_view StringConsumer::PeekUntilCharNotIn | ( | std::string_view | chars | ) | const
| inline

Peek 8-bit chars, while they are in 'chars', until they are not.

Returns
  Matching chars.

Definition at line 666 of file string_consumer.hpp.

References FindCharNotIn(), and Peek().

## ◆ PeekUntilUtf8()

std::string_view StringConsumer::PeekUntilUtf8 | ( | char32_t | c,
|  | SeparatorUsage | sep
| ) |  | const

Peek data until the first occurrence of UTF-8 char 'c'.

Parameters

  c | Separator char.
  sep | Whether to include/exclude 'c' from the result.

Definition at line 156 of file string_consumer.cpp.

References EncodeUtf8(), and PeekUntil().

## ◆ PeekUtf8()

std::pair< StringConsumer::size_type, char32_t > StringConsumer::PeekUtf8 | ( |  | ) | const

Peek UTF-8 character.

Returns
  Length and read char, {0, 0} if no valid data.

Definition at line 80 of file string_consumer.cpp.

References DecodeUtf8().

Referenced by PeekUtf8If(), ReadUtf8(), ReadUtf8If(), SkipUtf8(), SkipUtf8If(), and TryReadUtf8().

## ◆ PeekUtf8If()

bool StringConsumer::PeekUtf8If | ( | char32_t | c | ) | const
| inline

Check whether the next UTF-8 char matches 'c'.

Definition at line 493 of file string_consumer.hpp.

References PeekUtf8().

Referenced by DecodeEncodedString().

## ◆ Read()

std::string_view StringConsumer::Read | ( | size_type | len | ) |
| inline

Read the next 'len' bytes, and advance reader.

Parameters

  len | Bytes to read, 'npos' to read all.

Returns
  Up to 'len' bytes.

Definition at line 530 of file string_consumer.hpp.

References npos, Peek(), and Skip().

Referenced by DecodeEncodedString(), FormatString(), FileStringReader::HandlePragma(), ReadUntilCharIn(), ReadUntilCharNotIn(), and EncodedString::ReplaceParam().

## ◆ ReadChar()

char StringConsumer::ReadChar | ( | char | def = `'?'` | ) |
| inline

Read 8-bit character, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read character, 'def' if not enough data.

Definition at line 394 of file string_consumer.hpp.

References PeekChar(), and SkipChar().

## ◆ ReadCharIf()

bool StringConsumer::ReadCharIf | ( | char | c | ) |
| inline

Check whether the next 8-bit char matches 'c', and skip it.

Definition at line 478 of file string_consumer.hpp.

References ReadIf().

Referenced by BadgeClassLoadConfigFeature().

## ◆ ReadCharIfIn()

std::optional< char > StringConsumer::ReadCharIfIn | ( | std::string_view | chars | ) |
| inline

Read next 8-bit char, check whether it is in 'chars', and advance reader.

Returns
  Matching char, std::nullopt if no match.

Definition at line 590 of file string_consumer.hpp.

References PeekCharIfIn(), and Skip().

## ◆ ReadCharIfNotIn()

std::optional< char > StringConsumer::ReadCharIfNotIn | ( | std::string_view | chars | ) |
| inline

Read next 8-bit char, check whether it is not in 'chars', and advance reader.

Returns
  Non-matching char, std::nullopt if match.

Definition at line 620 of file string_consumer.hpp.

References PeekCharIfNotIn(), and Skip().

## ◆ ReadIf()

bool StringConsumer::ReadIf | ( | std::string_view | str | ) |
| inline

Check whether the next data matches 'str', and skip it.

Definition at line 454 of file string_consumer.hpp.

References PeekIf(), and Skip().

Referenced by ParseResolution(), ReadCharIf(), ReadUntil(), SkipIntegerBase(), and SkipUntil().

## ◆ ReadIntegerBase()

template<class T >

T StringConsumer::ReadIntegerBase | ( | int | base,
|  | T | def = `0`,
|  | bool | clamp = `false`
| ) |  |
| inline

Read and parse an integer in number 'base', and advance the reader.

If 'base == 0', then a prefix '0x' decides between base 16 or base 10.

Parameters

  clamp | If 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
  Parsed value, or 'def' if invalid.

Note
  The reader is advanced, even if no valid data was present.
  The parser rejects leading whitespace and unary plus.

Definition at line 891 of file string_consumer.hpp.

References SkipIntegerBase().

Referenced by DecodeEncodedString(), FixSCCEncodedNegative(), StringReader::HandlePragma(), FileStringReader::HandlePragma(), ClientNetworkContentSocketHandler::OnReceiveData(), and EncodedString::ReplaceParam().

## ◆ ReadSint16LE()

int16_t StringConsumer::ReadSint16LE | ( | int16_t | def = `0` | ) |
| inline

Read binary int16 using little endian, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 225 of file string_consumer.hpp.

References PeekSint16LE(), and SkipSint16LE().

## ◆ ReadSint32LE()

int32_t StringConsumer::ReadSint32LE | ( | int32_t | def = `0` | ) |
| inline

Read binary int32 using little endian, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 294 of file string_consumer.hpp.

References PeekSint32LE(), and SkipSint32LE().

## ◆ ReadSint64LE()

int64_t StringConsumer::ReadSint64LE | ( | int64_t | def = `0` | ) |
| inline

Read binary int64 using little endian, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 363 of file string_consumer.hpp.

References PeekSint64LE(), and SkipSint64LE().

## ◆ ReadSint8()

int8_t StringConsumer::ReadSint8 | ( | int8_t | def = `0` | ) |
| inline

Read binary int8, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Definition at line 157 of file string_consumer.hpp.

References PeekSint8(), and SkipSint8().

## ◆ ReadUint16LE()

uint16_t StringConsumer::ReadUint16LE | ( | uint16_t | def = `0` | ) |
| inline

Read binary uint16 using little endian, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 188 of file string_consumer.hpp.

References PeekUint16LE(), and SkipUint16LE().

Referenced by FormatString(), ProcessNewGRFStringControlCode(), and TranslateTTDPatchCodes().

## ◆ ReadUint32LE()

uint32_t StringConsumer::ReadUint32LE | ( | uint32_t | def = `0` | ) |
| inline

Read binary uint32 using little endian, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 257 of file string_consumer.hpp.

References PeekUint32LE(), and SkipUint32LE().

## ◆ ReadUint64LE()

uint64_t StringConsumer::ReadUint64LE | ( | uint64_t | def = `0` | ) |
| inline

Read binary uint64 using little endian, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 326 of file string_consumer.hpp.

References PeekUint64LE(), and SkipUint64LE().

## ◆ ReadUint8()

uint8_t StringConsumer::ReadUint8 | ( | uint8_t | def = `0` | ) |
| inline

Read binary uint8, and advance reader.

Parameters

  def | Default value to return, if not enough data.

Returns
  Read integer, 'def' if not enough data.

Definition at line 122 of file string_consumer.hpp.

References PeekUint8(), and SkipUint8().

Referenced by FormatString(), ProcessNewGRFStringControlCode(), and TranslateTTDPatchCodes().

## ◆ ReadUntil()

std::string_view StringConsumer::ReadUntil | ( | std::string_view | str,
|  | SeparatorUsage | sep
| ) |  |
| inline

Read data until the first occurrence of 'str', and advance reader.

Parameters

  str | Separator string.
  sep | Whether to include/exclude 'str' from the result, and/or skip it.

Definition at line 711 of file string_consumer.hpp.

References PeekUntil(), ReadIf(), Skip(), SKIP_ALL_SEPARATORS, SKIP_ONE_SEPARATOR, and SkipIf().

Referenced by ReadUntilChar(), and ReadUntilUtf8().

## ◆ ReadUntilChar()

std::string_view StringConsumer::ReadUntilChar | ( | char | c,
|  | SeparatorUsage | sep
| ) |  |
| inline

Read data until the first occurrence of 8-bit char 'c', and advance reader.

Parameters

  c | Separator char.
  sep | Whether to include/exclude 'c' from the result, and/or skip it.

Definition at line 765 of file string_consumer.hpp.

References ReadUntil().

Referenced by StringReader::HandlePragma(), FileStringReader::HandlePragma(), ParseCompanyManagerFaceCode(), ParseHotkeys(), ParseKeycode(), and ByteReader::ReadString().

## ◆ ReadUntilCharIn()

std::string_view StringConsumer::ReadUntilCharIn | ( | std::string_view | chars | ) |
| inline

Read 8-bit chars, while they are not in 'chars', until they are; and advance reader.

Returns
  Non-matching chars.

Definition at line 648 of file string_consumer.hpp.

References FindCharIn(), and Read().

## ◆ ReadUntilCharNotIn()

std::string_view StringConsumer::ReadUntilCharNotIn | ( | std::string_view | chars | ) |
| inline

Read 8-bit chars, while they are in 'chars', until they are not; and advance reader.

Returns
  Matching chars.

Definition at line 675 of file string_consumer.hpp.

References FindCharNotIn(), and Read().

## ◆ ReadUntilUtf8()

std::string_view StringConsumer::ReadUntilUtf8 | ( | char32_t | c,
|  | SeparatorUsage | sep
| ) |  |

Read data until the first occurrence of UTF-8 char 'c', and advance reader.

Parameters

  c | Separator char.
  sep | Whether to include/exclude 'c' from the result, and/or skip it.

Definition at line 162 of file string_consumer.cpp.

References EncodeUtf8(), and ReadUntil().

Referenced by DecodeEncodedString(), FixSCCEncodedNegative(), and EncodedString::ReplaceParam().

## ◆ ReadUtf8()

char32_t StringConsumer::ReadUtf8 | ( | char32_t | def = `'?'` | ) |
| inline

Read UTF-8 character, and advance reader.

Parameters

  def | Default value to return, if no valid data.

Returns
  Read char, 'def' if no valid data.

Note
  The reader is advanced, even if no valid data was present.

Definition at line 425 of file string_consumer.hpp.

References PeekUtf8(), and Skip().

Referenced by DecodeEncodedString(), FormatString(), GetKeyboardLayout(), HandleNewGRFStringControlCodes(), ProcessNewGRFStringControlCode(), and EncodedString::ReplaceParam().

## ◆ ReadUtf8If()

bool StringConsumer::ReadUtf8If | ( | char32_t | c | ) |
| inline

Check whether the next UTF-8 char matches 'c', and skip it.

Definition at line 501 of file string_consumer.hpp.

References PeekUtf8(), and Skip().

Referenced by FixSCCEncodedNegative(), FormatString(), EncodedString::ReplaceParam(), and TranslateTTDPatchCodes().

## ◆ Skip()

void StringConsumer::Skip | ( | size_type | len | ) |

Discard some bytes.

Parameters

  len | Number of bytes to skip, 'npos' to skip all.

Definition at line 97 of file string_consumer.cpp.

References GetBytesLeft().

Referenced by ProcessNewGRFStringControlCode(), Read(), ReadCharIfIn(), ReadCharIfNotIn(), ReadIf(), ReadUntil(), ReadUtf8(), ReadUtf8If(), SkipChar(), SkipCharIfIn(), SkipCharIfNotIn(), SkipIf(), SkipSint16LE(), SkipSint32LE(), SkipSint64LE(), SkipSint8(), SkipUint16LE(), SkipUint32LE(), SkipUint64LE(), SkipUint8(), SkipUntil(), SkipUntilCharIn(), SkipUntilCharNotIn(), SkipUtf8(), SkipUtf8If(), StrMakeValid(), and TryReadUtf8().

## ◆ SkipAll()

void StringConsumer::SkipAll | ( |  | ) |
| inline

Discard all remaining data.

Definition at line 101 of file string_consumer.hpp.

Referenced by DecodeEncodedString().

## ◆ SkipChar()

void StringConsumer::SkipChar | ( |  | ) |
| inline

Skip 8-bit character, and advance reader.

Definition at line 402 of file string_consumer.hpp.

References Skip().

Referenced by ReadChar(), and TryReadChar().

## ◆ SkipCharIf()

void StringConsumer::SkipCharIf | ( | char | c | ) |
| inline

If the next data matches the 8-bit char 'c', then skip it.

Definition at line 485 of file string_consumer.hpp.

References SkipIf().

## ◆ SkipCharIfIn()

void StringConsumer::SkipCharIfIn | ( | std::string_view | chars | ) |
| inline

If the next 8-bit char is in 'chars', skip it.

Definition at line 599 of file string_consumer.hpp.

References PeekCharIfIn(), and Skip().

## ◆ SkipCharIfNotIn()

void StringConsumer::SkipCharIfNotIn | ( | std::string_view | chars | ) |
| inline

If the next 8-bit char is not in 'chars', skip it.

Definition at line 629 of file string_consumer.hpp.

References PeekCharIfNotIn(), and Skip().

## ◆ SkipIf()

void StringConsumer::SkipIf | ( | std::string_view | str | ) |
| inline

If the next data matches 'str', then skip it.

Definition at line 463 of file string_consumer.hpp.

References PeekIf(), and Skip().

Referenced by ReadUntil(), SkipCharIf(), SkipIntegerBase(), and SkipUntil().

## ◆ SkipIntegerBase()

void StringConsumer::SkipIntegerBase | ( | int | base | ) |

Skip an integer in number 'base'.

If 'base == 0', then a prefix '0x' decides between base 16 or base 10.

Note
  The reader is advanced, even if no valid data was present.
  The parser rejects leading whitespace and unary plus.

Definition at line 174 of file string_consumer.cpp.

References ReadIf(), SkipIf(), and SkipUntilCharNotIn().

Referenced by ReadIntegerBase(), and TryReadIntegerBase().

## ◆ SkipSint16LE()

void StringConsumer::SkipSint16LE | ( |  | ) |
| inline

Skip binary int16, and advance reader.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 235 of file string_consumer.hpp.

References Skip().

Referenced by ReadSint16LE(), and TryReadSint16LE().

## ◆ SkipSint32LE()

void StringConsumer::SkipSint32LE | ( |  | ) |
| inline

Skip binary int32, and advance reader.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 304 of file string_consumer.hpp.

References Skip().

Referenced by ReadSint32LE(), and TryReadSint32LE().

## ◆ SkipSint64LE()

void StringConsumer::SkipSint64LE | ( |  | ) |
| inline

Skip binary int64, and advance reader.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 373 of file string_consumer.hpp.

References Skip().

Referenced by ReadSint64LE(), and TryReadSint64LE().

## ◆ SkipSint8()

void StringConsumer::SkipSint8 | ( |  | ) |
| inline

Skip binary int8.

Definition at line 166 of file string_consumer.hpp.

References Skip().

Referenced by ReadSint8(), and TryReadSint8().

## ◆ SkipUint16LE()

void StringConsumer::SkipUint16LE | ( |  | ) |
| inline

Skip binary uint16, and advance reader.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 198 of file string_consumer.hpp.

References Skip().

Referenced by ProcessNewGRFStringControlCode(), ReadUint16LE(), and TryReadUint16LE().

## ◆ SkipUint32LE()

void StringConsumer::SkipUint32LE | ( |  | ) |
| inline

Skip binary uint32, and advance reader.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 267 of file string_consumer.hpp.

References Skip().

Referenced by ReadUint32LE(), and TryReadUint32LE().

## ◆ SkipUint64LE()

void StringConsumer::SkipUint64LE | ( |  | ) |
| inline

Skip binary uint64, and advance reader.

Note
  The reader is advanced, even if not enough data was present.

Definition at line 336 of file string_consumer.hpp.

References Skip().

Referenced by ReadUint64LE(), and TryReadUint64LE().

## ◆ SkipUint8()

void StringConsumer::SkipUint8 | ( |  | ) |
| inline

Skip binary uint8.

Definition at line 131 of file string_consumer.hpp.

References Skip().

Referenced by ProcessNewGRFStringControlCode(), ReadUint8(), TranslateTTDPatchCodes(), and TryReadUint8().

## ◆ SkipUntil()

void StringConsumer::SkipUntil | ( | std::string_view | str,
|  | SeparatorUsage | sep
| ) |  |
| inline

Skip data until the first occurrence of 'str'.

Parameters

  str | Separator string.
  sep | Whether to also skip 'str'.

Definition at line 733 of file string_consumer.hpp.

References Find(), READ_ALL_SEPARATORS, READ_ONE_SEPARATOR, ReadIf(), Skip(), SKIP_ALL_SEPARATORS, SKIP_ONE_SEPARATOR, and SkipIf().

Referenced by SkipUntilChar(), and SkipUntilUtf8().

## ◆ SkipUntilChar()

void StringConsumer::SkipUntilChar | ( | char | c,
|  | SeparatorUsage | sep
| ) |  |
| inline

Skip data until the first occurrence of 8-bit char 'c'.

Parameters

  c | Separator char.
  sep | Whether to also skip 'c'.

Definition at line 774 of file string_consumer.hpp.

References SkipUntil().

## ◆ SkipUntilCharIn()

void StringConsumer::SkipUntilCharIn | ( | std::string_view | chars | ) |
| inline

Skip 8-bit chars, while they are not in 'chars', until they are.

Definition at line 656 of file string_consumer.hpp.

References FindCharIn(), and Skip().

## ◆ SkipUntilCharNotIn()

void StringConsumer::SkipUntilCharNotIn | ( | std::string_view | chars | ) |
| inline

Skip 8-bit chars, while they are in 'chars', until they are not.

Definition at line 683 of file string_consumer.hpp.

References FindCharNotIn(), and Skip().

Referenced by LookupManyOfMany(), ParseInteger(), ParseIntList(), and SkipIntegerBase().

## ◆ SkipUntilUtf8()

void StringConsumer::SkipUntilUtf8 | ( | char32_t | c,
|  | SeparatorUsage | sep
| ) |  |

Skip data until the first occurrence of UTF-8 char 'c'.

Parameters

  c | Separator char.
  sep | Whether to also skip 'c'.

Definition at line 168 of file string_consumer.cpp.

References EncodeUtf8(), and SkipUntil().

## ◆ SkipUtf8()

void StringConsumer::SkipUtf8 | ( |  | ) |
| inline

Skip UTF-8 character, and advance reader.

Note
  The reader is advanced, even if no valid data was present.
  This behaves different to Utf8View::iterator. Here we do not skip overlong encodings, because we want to allow binary data to follow UTF-8 data.

Definition at line 438 of file string_consumer.hpp.

References PeekUtf8(), and Skip().

Referenced by RemapNewGRFStringControlCode().

## ◆ SkipUtf8If()

void StringConsumer::SkipUtf8If | ( | char32_t | c | ) |
| inline

If the next data matches the UTF-8 char 'c', then skip it.

Definition at line 511 of file string_consumer.hpp.

References PeekUtf8(), and Skip().

Referenced by DecodeEncodedString().

## ◆ TryReadChar()

std::optional< char > StringConsumer::TryReadChar | ( |  | ) |
| inline

Try to read a 8-bit character, and then advance reader.

Definition at line 383 of file string_consumer.hpp.

References PeekChar(), and SkipChar().

## ◆ TryReadIntegerBase()

template<class T >

std::optional< T > StringConsumer::TryReadIntegerBase | ( | int | base,
|  | bool | clamp = `false`
| ) |  |
| inline

Try to read and parse an integer in number 'base', and then advance the reader.

If 'base == 0', then a prefix '0x' decides between base 16 or base 10.

Parameters

  clamp | If 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
  Parsed value, if valid.

Note
  The parser rejects leading whitespace and unary plus.

Definition at line 874 of file string_consumer.hpp.

References SkipIntegerBase().

Referenced by BadgeClassLoadConfigFeature(), FileList::FindItem(), FixSCCEncodedNegative(), ParseResolution(), OneOfManySettingDesc::ParseSingleValue(), IntSettingDesc::ParseValue(), EncodedString::ReplaceParam(), and SetDebugString().

## ◆ TryReadSint16LE()

std::optional< int16_t > StringConsumer::TryReadSint16LE | ( |  | ) |
| inline

Try to read binary int16, and then advance reader.

Definition at line 213 of file string_consumer.hpp.

References PeekSint16LE(), and SkipSint16LE().

## ◆ TryReadSint32LE()

std::optional< int32_t > StringConsumer::TryReadSint32LE | ( |  | ) |
| inline

Try to read binary int32, and then advance reader.

Definition at line 282 of file string_consumer.hpp.

References PeekSint32LE(), and SkipSint32LE().

## ◆ TryReadSint64LE()

std::optional< int64_t > StringConsumer::TryReadSint64LE | ( |  | ) |
| inline

Try to read binary int64, and then advance reader.

Definition at line 351 of file string_consumer.hpp.

References PeekSint64LE(), and SkipSint64LE().

## ◆ TryReadSint8()

std::optional< int8_t > StringConsumer::TryReadSint8 | ( |  | ) |
| inline

Try to read binary int8, and then advance reader.

Definition at line 146 of file string_consumer.hpp.

References PeekSint8(), and SkipSint8().

## ◆ TryReadUint16LE()

std::optional< uint16_t > StringConsumer::TryReadUint16LE | ( |  | ) |
| inline

Try to read binary uint16, and then advance reader.

Definition at line 176 of file string_consumer.hpp.

References PeekUint16LE(), and SkipUint16LE().

Referenced by ByteReader::ReadWord().

## ◆ TryReadUint32LE()

std::optional< uint32_t > StringConsumer::TryReadUint32LE | ( |  | ) |
| inline

Try to read binary uint32, and then advance reader.

Definition at line 245 of file string_consumer.hpp.

References PeekUint32LE(), and SkipUint32LE().

Referenced by ByteReader::ReadDWord().

## ◆ TryReadUint64LE()

std::optional< uint64_t > StringConsumer::TryReadUint64LE | ( |  | ) |
| inline

Try to read binary uint64, and then advance reader.

Definition at line 314 of file string_consumer.hpp.

References PeekUint64LE(), and SkipUint64LE().

## ◆ TryReadUint8()

std::optional< uint8_t > StringConsumer::TryReadUint8 | ( |  | ) |
| inline

Try to read binary uint8, and then advance reader.

Definition at line 111 of file string_consumer.hpp.

References PeekUint8(), and SkipUint8().

Referenced by ByteReader::ReadByte().

## ◆ TryReadUtf8()

std::optional< char32_t > StringConsumer::TryReadUtf8 | ( |  | ) |
| inline

Try to read a UTF-8 character, and then advance reader.

Definition at line 412 of file string_consumer.hpp.

References PeekUtf8(), and Skip().

Referenced by FixSCCEncoded(), IConsoleCmdExec(), StrMakeValid(), StrValid(), and TranslateTTDPatchCodes().

## Field Documentation

## ◆ npos

constexpr size_type StringConsumer::npos = std::string_view::npos
| staticconstexpr

Special value for "end of data".

Definition at line 34 of file string_consumer.hpp.

Referenced by DecodeEncodedString(), FileStringReader::HandlePragma(), ParseCompanyManagerFaceCode(), Read(), and EncodedString::ReplaceParam().

## ◆ position

size_type StringConsumer::position = 0
| private

Definition at line 49 of file string_consumer.hpp.

## ◆ src

std::string_view StringConsumer::src
| private

Definition at line 48 of file string_consumer.hpp.

## ◆ WHITESPACE_NO_NEWLINE

const std::string_view StringConsumer::WHITESPACE_NO_NEWLINE = "\t\v\f\r "
| static

ASCII whitespace characters, excluding new-line.

Usable in FindChar(In|NotIn), (Peek|Read|Skip)(If|Until)Char(In|NotIn)

Definition at line 40 of file string_consumer.hpp.

Referenced by ParseCode(), ParseInteger(), ParseIntList(), and StrTrimInPlace().

## ◆ WHITESPACE_OR_NEWLINE

const std::string_view StringConsumer::WHITESPACE_OR_NEWLINE = "\t\n\v\f\r "
| static

ASCII whitespace characters, including new-line.

Usable in FindChar(In|NotIn), (Peek|Read|Skip)(If|Until)Char(In|NotIn)

Definition at line 45 of file string_consumer.hpp.

Referenced by IniLoadFile::LoadFromDisk(), StringReader::ParseFile(), and ReadRawLanguageStrings().

The documentation for this class was generated from the following files:

- src/core/string_consumer.hpp
- src/core/string_consumer.cpp