SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
Text.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdexcept>
4#include <string_view>
5#include <unordered_map>
6
7class BufferStream;
8
10
11constexpr std::string_view DEFAULT_SINGLE_LINE_COMMENT_START = "//";
12constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_START = "/*";
13constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_END = "*/";
14constexpr std::string_view DEFAULT_STRING_START = "\"";
15constexpr std::string_view DEFAULT_STRING_END = "\"";
16
17using EscapeSequenceMap = std::unordered_map<char, char>;
20
26[[nodiscard]] bool isNewLine(char c);
27
33[[nodiscard]] bool isNewLine(std::string_view str);
34
40[[nodiscard]] bool isWhitespace(char c);
41
47[[nodiscard]] bool isWhitespace(std::string_view str);
48
54[[nodiscard]] bool isNumber(char c);
55
61[[nodiscard]] bool isNumber(std::string_view str);
62
69[[nodiscard]] std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap& escapeSequences);
70
77[[nodiscard]] std::string convertEscapesToSpecialChars(std::string_view str, const EscapeSequenceMap& escapeSequences);
78
83void eatWhitespace(BufferStream& stream);
84
90void eatSingleLineComment(BufferStream& stream);
91
98void eatMultiLineComment(BufferStream& stream, std::string_view multiLineCommentEnd = DEFAULT_MULTI_LINE_COMMENT_END);
99
105void eatWhitespaceAndSingleLineComments(BufferStream& stream, std::string_view singleLineCommentStart = DEFAULT_SINGLE_LINE_COMMENT_START);
106
112void eatWhitespaceAndMultiLineComments(BufferStream& stream, std::string_view multiLineCommentStart = DEFAULT_MULTI_LINE_COMMENT_START);
113
120void eatWhitespaceAndComments(BufferStream& stream, std::string_view singleLineCommentStart = DEFAULT_SINGLE_LINE_COMMENT_START, std::string_view multiLineCommentStart = DEFAULT_MULTI_LINE_COMMENT_START);
121
128[[nodiscard]] bool tryToEatChar(BufferStream& stream, char c);
129
139std::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);
140
148std::string_view readUnquotedStringToBuffer(BufferStream& stream, BufferStream& backing, const EscapeSequenceMap& escapeSequences = DEFAULT_ESCAPE_SEQUENCES);
149
158std::string_view readUnquotedStringToBuffer(BufferStream& stream, BufferStream& backing, std::string_view end, const EscapeSequenceMap& escapeSequences = DEFAULT_ESCAPE_SEQUENCES);
159
160class syntax_error : public std::runtime_error {
161public:
162 using std::runtime_error::runtime_error;
163};
164
165} // namespace sourcepp::parser::text
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.
Definition: Text.cpp:145
std::string_view readUnquotedStringToBuffer(BufferStream &stream, BufferStream &backing, const EscapeSequenceMap &escapeSequences=DEFAULT_ESCAPE_SEQUENCES)
Read a string starting at the current stream position.
Definition: Text.cpp:178
std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert special characters like \n to escaped special characters like \\n.
Definition: Text.cpp:54
std::string convertEscapesToSpecialChars(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert escaped special characters like \\n to special characters like \n.
Definition: Text.cpp:73
const EscapeSequenceMap NO_ESCAPE_SEQUENCES
Definition: Text.cpp:26
std::unordered_map< char, char > EscapeSequenceMap
Definition: Text.h:17
constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_END
Definition: Text.h:13
void eatMultiLineComment(BufferStream &stream, std::string_view multiLineCommentEnd=DEFAULT_MULTI_LINE_COMMENT_END)
If a multi line comment is detected, eat its contents.
Definition: Text.cpp:100
bool isNewLine(char c)
If a char is a newline character.
Definition: Text.cpp:30
void eatSingleLineComment(BufferStream &stream)
If a single line comment is detected, eat its contents.
Definition: Text.cpp:96
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.
Definition: Text.cpp:106
void eatWhitespace(BufferStream &stream)
Eat all whitespace after the current stream position.
Definition: Text.cpp:91
const EscapeSequenceMap DEFAULT_ESCAPE_SEQUENCES
Definition: Text.cpp:12
constexpr std::string_view DEFAULT_SINGLE_LINE_COMMENT_START
Definition: Text.h:11
bool tryToEatChar(BufferStream &stream, char c)
If the given char exists at the current position, skip over it.
Definition: Text.cpp:137
constexpr std::string_view DEFAULT_STRING_END
Definition: Text.h:15
constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_START
Definition: Text.h:12
constexpr std::string_view DEFAULT_STRING_START
Definition: Text.h:14
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.
Definition: Text.cpp:115
bool isNumber(char c)
If a char is a numerical character (0-9).
Definition: Text.cpp:46
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.
Definition: Text.cpp:110
bool isWhitespace(char c)
If a char is a whitespace character.
Definition: Text.cpp:38