6#include <BufferStream.h>
31 return c ==
'\n' || c ==
'\r';
35 return std::all_of(str.begin(), str.end(), [](
char c) { return isNewLine(c); });
39 return c ==
' ' || c ==
'\a' || c ==
'\f' || c ==
'\t' || c ==
'\v' ||
isNewLine(c);
43 return std::all_of(str.begin(), str.end(), [](
char c) { return isWhitespace(c); });
47 return std::isdigit(c);
51 return std::all_of(str.begin(), str.end(), [](
char c) { return isNumber(c); });
57 for (
const auto& [normal, special] : escapeSequences) {
58 specialSequences[special] = normal;
62 for (
int i = 0; i < str.length(); i++) {
63 if (specialSequences.contains(str[i])) {
65 out += specialSequences[str[i]];
75 for (
int i = 0; i < str.length(); i++) {
76 if (!escapeSequences.empty() && str[i] ==
'\\' && i < str.length() - 1) {
78 if (escapeSequences.contains(n)) {
79 out += escapeSequences.at(n);
93 stream.seek(-1, std::ios::cur);
97 while (!
isNewLine(stream.read<
char>())) {}
101 while (!std::ranges::equal(stream.read_span<
char>(multiLineCommentEnd.length()), multiLineCommentEnd)) {
102 stream.seek(-
static_cast<int64_t
>(multiLineCommentEnd.length() - 1), std::ios::cur);
118 if (!singleLineCommentStart.empty()) {
119 if (std::ranges::equal(stream.read_span<
char>(singleLineCommentStart.length()), singleLineCommentStart)) {
124 stream.seek(-
static_cast<int64_t
>(singleLineCommentStart.length()), std::ios::cur);
127 if (!multiLineCommentStart.empty()) {
128 if (std::ranges::equal(stream.read_span<
char>(multiLineCommentStart.length()), multiLineCommentStart)) {
133 stream.seek(-
static_cast<int64_t
>(multiLineCommentStart.length()), std::ios::cur);
138 if (stream.peek<
char>() != c) {
146 const auto startSpan = backing.tell();
148 bool stopAtWhitespace =
true;
149 char c = stream.read<
char>();
150 if (start.find(c) != std::string_view::npos) {
151 stopAtWhitespace =
false;
156 for (c = stream.read<
char>(); (stopAtWhitespace && !
isWhitespace(c)) || (!stopAtWhitespace && end.find(c) == std::string_view::npos); c = stream.read<
char>()) {
157 if (!escapeSequences.empty() && c ==
'\\') {
158 auto n = stream.read<
char>();
162 if (escapeSequences.contains(n)) {
163 backing << escapeSequences.at(n);
164 }
else if (!stopAtWhitespace && end.find(n) != std::string_view::npos) {
175 return {
reinterpret_cast<const char*
>(backing.data()) + startSpan, backing.tell() - 1 - startSpan};
183 const auto startSpan = backing.tell();
185 for (
char c = stream.read<
char>(); !
isWhitespace(c) && end.find(c) == std::string_view::npos; c = stream.read<
char>()) {
186 if (!escapeSequences.empty() && c ==
'\\') {
187 auto n = stream.read<
char>();
188 if (escapeSequences.contains(n)) {
189 backing << escapeSequences.at(n);
190 }
else if (
isWhitespace(n) || end.find(n) != std::string_view::npos) {
201 return {
reinterpret_cast<const char*
>(backing.data()) + startSpan, backing.tell() - 1 - startSpan};
std::string_view readStringToBuffer(BufferStream &stream, BufferStream &backing, std::string_view start=DEFAULT_STRING_START, std::string_view end=DEFAULT_STRING_END, const EscapeSequenceMap &escapeSequences=DEFAULT_ESCAPE_SEQUENCES)
Read a string starting at the current stream position.
std::string_view readUnquotedStringToBuffer(BufferStream &stream, BufferStream &backing, const EscapeSequenceMap &escapeSequences=DEFAULT_ESCAPE_SEQUENCES)
Read a string starting at the current stream position.
std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert special characters like \n to escaped special characters like \\n.
std::string convertEscapesToSpecialChars(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert escaped special characters like \\n to special characters like \n.
const EscapeSequenceMap NO_ESCAPE_SEQUENCES
std::unordered_map< char, char > EscapeSequenceMap
void eatMultiLineComment(BufferStream &stream, std::string_view multiLineCommentEnd=DEFAULT_MULTI_LINE_COMMENT_END)
If a multi line comment is detected, eat its contents.
bool isNewLine(char c)
If a char is a newline character.
void eatSingleLineComment(BufferStream &stream)
If a single line comment is detected, eat its contents.
void eatWhitespaceAndSingleLineComments(BufferStream &stream, std::string_view singleLineCommentStart=DEFAULT_SINGLE_LINE_COMMENT_START)
Eat all whitespace and single line comments after the current stream position.
void eatWhitespace(BufferStream &stream)
Eat all whitespace after the current stream position.
const EscapeSequenceMap DEFAULT_ESCAPE_SEQUENCES
bool tryToEatChar(BufferStream &stream, char c)
If the given char exists at the current position, skip over it.
void eatWhitespaceAndComments(BufferStream &stream, std::string_view singleLineCommentStart=DEFAULT_SINGLE_LINE_COMMENT_START, std::string_view multiLineCommentStart=DEFAULT_MULTI_LINE_COMMENT_START)
Eat all whitespace and comments after the current stream position.
bool isNumber(char c)
If a char is a numerical character (0-9).
void eatWhitespaceAndMultiLineComments(BufferStream &stream, std::string_view multiLineCommentStart=DEFAULT_MULTI_LINE_COMMENT_START)
Eat all whitespace and multi line comments after the current stream position.
bool isWhitespace(char c)
If a char is a whitespace character.