17#include "../string_func.h"
19#if defined(STRGEN) || defined(SETTINGSGEN)
20#include "../error_func.h"
25#include "../safeguards.h"
30 void StringConsumer::LogError(std::string &&msg)
32#if defined(STRGEN) || defined(SETTINGSGEN)
42 return static_cast<uint8_t
>(this->src[this->position]);
48 return static_cast<uint8_t
>(this->src[this->position]) |
49 static_cast<uint8_t
>(this->src[this->position + 1]) << 8;
55 return static_cast<uint8_t
>(this->src[this->position]) |
56 static_cast<uint8_t
>(this->src[this->position + 1]) << 8 |
57 static_cast<uint8_t
>(this->src[this->position + 2]) << 16 |
58 static_cast<uint8_t
>(this->src[this->position + 3]) << 24;
64 return static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position])) |
65 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 1])) << 8 |
66 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 2])) << 16 |
67 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 3])) << 24 |
68 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 4])) << 32 |
69 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 5])) << 40 |
70 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 6])) << 48 |
71 static_cast<uint64_t
>(
static_cast<uint8_t
>(this->src[this->position + 7])) << 56;
77 if (!result.has_value())
return {};
78 return static_cast<char>(*result);
83 auto buf = this->src.substr(this->position);
89 auto buf = this->src.substr(this->position);
90 if (len == std::string_view::npos) {
92 }
else if (len > buf.size()) {
95 return buf.substr(0, len);
100 if (len == std::string_view::npos) {
101 this->position = this->src.size();
102 }
else if (size_type max_len =
GetBytesLeft(); len > max_len) {
103 LogError(fmt::format(
"Source buffer too short: {} > {}", len, max_len));
104 this->position = this->src.size();
106 this->position += len;
112 assert(!str.empty());
113 auto buf = this->src.substr(this->position);
114 return buf.find(str);
120 return this->
Find({data, len});
125 assert(!chars.empty());
126 auto buf = this->src.substr(this->position);
127 return buf.find_first_of(chars);
132 assert(!chars.empty());
133 auto buf = this->src.substr(this->position);
134 return buf.find_first_not_of(chars);
139 assert(!str.empty());
140 auto buf = this->src.substr(this->position);
141 auto len = buf.find(str);
142 if (len != std::string_view::npos) {
145 if (buf.compare(len, str.size(), str) == 0) len += str.size();
148 while (buf.compare(len, str.size(), str) == 0) len += str.size();
154 return buf.substr(0, len);
Functions related to bit mathematics.
std::string_view Peek(size_type len) const
Peek the next 'len' bytes.
SeparatorUsage
Treatment of separator characters.
@ 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.
std::optional< uint32_t > PeekUint32LE() const
Peek binary uint32 using little endian.
size_type GetBytesLeft() const noexcept
Get number of bytes left to read.
std::string_view PeekUntil(std::string_view str, SeparatorUsage sep) const
Peek data until the first occurrence of 'str'.
static const std::string_view WHITESPACE_OR_NEWLINE
ASCII whitespace characters, including new-line.
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'.
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
std::optional< char > PeekChar() const
Peek 8-bit character.
bool ReadIf(std::string_view str)
Check whether the next data matches 'str', and skip it.
std::string_view PeekUntilUtf8(char32_t c, SeparatorUsage sep) const
Peek data until the first occurrence of UTF-8 char 'c'.
std::optional< uint64_t > PeekUint64LE() const
Peek binary uint64 using little endian.
void SkipUntilCharNotIn(std::string_view chars)
Skip 8-bit chars, while they are in 'chars', until they are not.
size_type FindCharNotIn(std::string_view chars) const
Find first occurence of any 8-bit char not in 'chars'.
void SkipIf(std::string_view str)
If the next data matches 'str', then skip it.
std::optional< uint16_t > PeekUint16LE() const
Peek binary uint16 using little endian.
void SkipIntegerBase(int base)
Skip an integer in number 'base'.
void Skip(size_type len)
Discard some bytes.
size_type Find(std::string_view str) const
Find first occurence of 'str'.
std::optional< uint8_t > PeekUint8() const
Peek binary uint8.
void SkipUntilUtf8(char32_t c, SeparatorUsage sep)
Skip 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.
std::pair< size_type, char32_t > PeekUtf8() const
Peek UTF-8 character.
size_type FindCharIn(std::string_view chars) const
Find first occurence of any 8-bit char in 'chars'.
size_type FindUtf8(char32_t c) const
Find first occurence of UTF-8 char 'c'.
std::pair< size_t, char32_t > DecodeUtf8(std::string_view buf)
Decode a character from UTF-8.
std::pair< char[4], size_t > EncodeUtf8(char32_t c)
Encode a character to UTF-8.
void DebugPrint(std::string_view category, int level, std::string &&message)
Internal function for outputting the debug line.
void FatalErrorI(const std::string &str)
Error handling for fatal non-user errors.
Compose strings from textual and binary data.
Handling of UTF-8 encoded data.