OpenTTD Source 20250503-master-gf4f05dea33
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.
 
 StringConsumer (const char *src, size_type len)
 Construct parser with data from buffer.
 
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 wheter 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 occurence of 'str'.
 
size_type FindChar (char c) const
 Find first occurence of 8-bit char 'c'.
 
size_type FindUtf8 (char32_t c) const
 Find first occurence of UTF-8 char 'c'.
 
size_type FindCharIn (std::string_view chars) const
 Find first occurence of any 8-bit char in 'chars'.
 
size_type FindCharNotIn (std::string_view chars) const
 Find first occurence 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) const
 Peek and parse an integer in number 'base'.
 
template<class T >
std::optional< T > TryReadIntegerBase (int base)
 Try to read and parse an integer in number 'base', and then advance the reader.
 
template<class T >
ReadIntegerBase (int base, T def=0)
 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 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

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 696 of file string_consumer.hpp.

Constructor & Destructor Documentation

◆ StringConsumer() [1/4]

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

Construct parser with data from string.

Definition at line 57 of file string_consumer.hpp.

◆ StringConsumer() [2/4]

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

Construct parser with data from string.

Definition at line 61 of file string_consumer.hpp.

◆ StringConsumer() [3/4]

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

Construct parser with data from span.

Definition at line 65 of file string_consumer.hpp.

◆ StringConsumer() [4/4]

StringConsumer::StringConsumer ( const char *  src,
size_type  len 
)
inline

Construct parser with data from buffer.

Definition at line 69 of file string_consumer.hpp.

Member Function Documentation

◆ AnyBytesLeft()

bool StringConsumer::AnyBytesLeft ( ) const
inlinenoexcept

◆ AnyBytesRead()

bool StringConsumer::AnyBytesRead ( ) const
inlinenoexcept

Check wheter any bytes were already read.

Definition at line 83 of file string_consumer.hpp.

◆ Find()

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

Find first occurence of 'str'.

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

Definition at line 110 of file string_consumer.cpp.

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

◆ FindChar()

size_type StringConsumer::FindChar ( char  c) const
inline

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

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

Definition at line 558 of file string_consumer.hpp.

References Find().

◆ FindCharIn()

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

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

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

Definition at line 123 of file string_consumer.cpp.

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

◆ FindCharNotIn()

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

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

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

Definition at line 130 of file string_consumer.cpp.

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

◆ FindUtf8()

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

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

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

Definition at line 117 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 78 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 87 of file string_consumer.hpp.

◆ GetLeftData()

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

Get data left to read.

Definition at line 100 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 92 of file string_consumer.hpp.

◆ GetReadData()

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

Get already read data.

Definition at line 96 of file string_consumer.hpp.

◆ LogError()

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

Definition at line 30 of file string_consumer.cpp.

◆ ParseIntegerBase()

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

Definition at line 804 of file string_consumer.hpp.

◆ Peek()

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

Peek the next 'len' bytes.

Parameters
lenBytes to read, 'npos' to read all.
Returns
Up to 'len' bytes.

Definition at line 87 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 74 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 475 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 583 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 613 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 451 of file string_consumer.hpp.

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

◆ PeekIntegerBase()

template<class T >
std::pair< size_type, T > StringConsumer::PeekIntegerBase ( int  base) const
inline

Peek and parse an integer in number 'base'.

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

Returns
Length of string match, and parsed value.
Note
The parser rejects leading whitespace and unary plus.

Definition at line 854 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 208 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 277 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 346 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 141 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 45 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 52 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 61 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 39 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
strSeparator string.
sepWhether to include/exclude 'str' from the result.

Definition at line 137 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
cSeparator char.
sepWhether to include/exclude 'c' from the result.

Definition at line 760 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 643 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 670 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
cSeparator char.
sepWhether to include/exclude 'c' from the result.

Definition at line 157 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 81 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 497 of file string_consumer.hpp.

References PeekUtf8().

◆ Read()

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

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

Parameters
lenBytes to read, 'npos' to read all.
Returns
Up to 'len' bytes.

Definition at line 534 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
defDefault value to return, if not enough data.
Returns
Read character, 'def' if not enough data.

Definition at line 398 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 482 of file string_consumer.hpp.

References ReadIf().

◆ 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 594 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 624 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 458 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,
def = 0 
)
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.

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 880 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
defDefault 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 229 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
defDefault 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 298 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
defDefault 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 367 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
defDefault value to return, if not enough data.
Returns
Read integer, 'def' if not enough data.

Definition at line 161 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
defDefault 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 192 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
defDefault 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 261 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
defDefault 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 330 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
defDefault value to return, if not enough data.
Returns
Read integer, 'def' if not enough data.

Definition at line 126 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
strSeparator string.
sepWhether to include/exclude 'str' from the result, and/or skip it.

Definition at line 715 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
cSeparator char.
sepWhether to include/exclude 'c' from the result, and/or skip it.

Definition at line 769 of file string_consumer.hpp.

References ReadUntil().

Referenced by StringReader::HandlePragma(), FileStringReader::HandlePragma(), 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 652 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 679 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
cSeparator char.
sepWhether to include/exclude 'c' from the result, and/or skip it.

Definition at line 163 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
defDefault 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 429 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 505 of file string_consumer.hpp.

References PeekUtf8(), and Skip().

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

◆ Skip()

◆ SkipAll()

void StringConsumer::SkipAll ( )
inline

Discard all remaining data.

Definition at line 105 of file string_consumer.hpp.

Referenced by DecodeEncodedString().

◆ SkipChar()

void StringConsumer::SkipChar ( )
inline

Skip 8-bit character, and advance reader.

Definition at line 406 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 489 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 603 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 633 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 467 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 175 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 239 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 308 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 377 of file string_consumer.hpp.

References Skip().

Referenced by ReadSint64LE(), and TryReadSint64LE().

◆ SkipSint8()

void StringConsumer::SkipSint8 ( )
inline

Skip binary int8.

Definition at line 170 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 202 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 271 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 340 of file string_consumer.hpp.

References Skip().

Referenced by ReadUint64LE(), and TryReadUint64LE().

◆ SkipUint8()

void StringConsumer::SkipUint8 ( )
inline

Skip binary uint8.

Definition at line 135 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
strSeparator string.
sepWhether to also skip 'str'.

Definition at line 737 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
cSeparator char.
sepWhether to also skip 'c'.

Definition at line 778 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 660 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 687 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
cSeparator char.
sepWhether to also skip 'c'.

Definition at line 169 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 442 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 515 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 387 of file string_consumer.hpp.

References PeekChar(), and SkipChar().

◆ TryReadIntegerBase()

template<class T >
std::optional< T > StringConsumer::TryReadIntegerBase ( int  base)
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.

Returns
Parsed value, if valid.
Note
The parser rejects leading whitespace and unary plus.

Definition at line 865 of file string_consumer.hpp.

References SkipIntegerBase().

Referenced by 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 217 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 286 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 355 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 150 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 180 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 249 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 318 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 115 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 416 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(), 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 ParseInteger(), and ParseIntList().

◆ 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.


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