SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
sourcepp::parser::text Namespace Reference

Classes

class  syntax_error
 

Typedefs

using EscapeSequenceMap = std::unordered_map< char, char >
 

Functions

bool isNewLine (char c)
 If a char is a newline character.
 
bool isNewLine (std::string_view str)
 If a string is entirely composed of newline characters.
 
bool isWhitespace (char c)
 If a char is a whitespace character.
 
bool isWhitespace (std::string_view str)
 If a string is entirely composed of whitespace characters.
 
bool isNumber (char c)
 If a char is a numerical character (0-9).
 
bool isNumber (std::string_view str)
 If a string is entirely composed of numerical characters (0-9).
 
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.
 
void eatWhitespace (BufferStream &stream)
 Eat all whitespace after the current stream position.
 
void eatSingleLineComment (BufferStream &stream)
 If a single line comment is detected, eat its contents.
 
void eatMultiLineComment (BufferStream &stream, std::string_view multiLineCommentEnd=DEFAULT_MULTI_LINE_COMMENT_END)
 If a multi 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 eatWhitespaceAndMultiLineComments (BufferStream &stream, std::string_view multiLineCommentStart=DEFAULT_MULTI_LINE_COMMENT_START)
 Eat all whitespace and multi line comments after the current stream position.
 
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 tryToEatChar (BufferStream &stream, char c)
 If the given char exists at the current position, skip over it.
 
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_view readUnquotedStringToBuffer (BufferStream &stream, BufferStream &backing, std::string_view end, const EscapeSequenceMap &escapeSequences=DEFAULT_ESCAPE_SEQUENCES)
 Read a string starting at the current stream position.
 

Variables

constexpr std::string_view DEFAULT_SINGLE_LINE_COMMENT_START = "//"
 
constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_START = "/*"
 
constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_END = "*/"
 
constexpr std::string_view DEFAULT_STRING_START = "\""
 
constexpr std::string_view DEFAULT_STRING_END = "\""
 
const EscapeSequenceMap DEFAULT_ESCAPE_SEQUENCES
 
const EscapeSequenceMap NO_ESCAPE_SEQUENCES = {}
 

Typedef Documentation

◆ EscapeSequenceMap

using sourcepp::parser::text::EscapeSequenceMap = typedef std::unordered_map<char, char>

Definition at line 17 of file Text.h.

Function Documentation

◆ convertEscapesToSpecialChars()

std::string sourcepp::parser::text::convertEscapesToSpecialChars ( std::string_view  str,
const EscapeSequenceMap escapeSequences 
)

Convert escaped special characters like \\n to special characters like \n.

Parameters
strThe string to convert
escapeSequencesThe escapes to convert
Returns
The string with special characters instead of escapes

Definition at line 73 of file Text.cpp.

◆ convertSpecialCharsToEscapes()

std::string sourcepp::parser::text::convertSpecialCharsToEscapes ( std::string_view  str,
const EscapeSequenceMap escapeSequences 
)

Convert special characters like \n to escaped special characters like \\n.

Parameters
strThe string to convert
escapeSequencesThe escapes to convert
Returns
The string without any special characters

Definition at line 54 of file Text.cpp.

◆ eatMultiLineComment()

void sourcepp::parser::text::eatMultiLineComment ( BufferStream &  stream,
std::string_view  multiLineCommentEnd = DEFAULT_MULTI_LINE_COMMENT_END 
)

If a multi line comment is detected, eat its contents.

This function does not handle the detection of multi line comments!

Parameters
streamThe BufferStream to modify.
multiLineCommentEndThe multi line comment ending to search for.

Definition at line 100 of file Text.cpp.

◆ eatSingleLineComment()

void sourcepp::parser::text::eatSingleLineComment ( BufferStream &  stream)

If a single line comment is detected, eat its contents.

This function does not handle the detection of single line comments!

Parameters
streamThe BufferStream to modify.

Definition at line 96 of file Text.cpp.

◆ eatWhitespace()

void sourcepp::parser::text::eatWhitespace ( BufferStream &  stream)

Eat all whitespace after the current stream position.

Parameters
streamThe BufferStream to modify.

Definition at line 91 of file Text.cpp.

◆ eatWhitespaceAndComments()

void sourcepp::parser::text::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.

Parameters
streamThe BufferStream to modify.
singleLineCommentStartThe single line comment start sequence. Leave empty to skip checking for single line comments.
multiLineCommentStartThe multi line comment start sequence. Leave empty to skip checking for multi line comments.

Definition at line 115 of file Text.cpp.

◆ eatWhitespaceAndMultiLineComments()

void sourcepp::parser::text::eatWhitespaceAndMultiLineComments ( BufferStream &  stream,
std::string_view  multiLineCommentStart = DEFAULT_MULTI_LINE_COMMENT_START 
)

Eat all whitespace and multi line comments after the current stream position.

Parameters
streamThe BufferStream to modify.
multiLineCommentStartThe multi line comment start sequence. Leave empty to skip checking for multi line comments.

Definition at line 110 of file Text.cpp.

◆ eatWhitespaceAndSingleLineComments()

void sourcepp::parser::text::eatWhitespaceAndSingleLineComments ( BufferStream &  stream,
std::string_view  singleLineCommentStart = DEFAULT_SINGLE_LINE_COMMENT_START 
)

Eat all whitespace and single line comments after the current stream position.

Parameters
streamThe BufferStream to modify.
singleLineCommentStartThe single line comment start sequence. Leave empty to skip checking for single line comments.

Definition at line 106 of file Text.cpp.

◆ isNewLine() [1/2]

bool sourcepp::parser::text::isNewLine ( char  c)

If a char is a newline character.

Parameters
cThe char.
Returns
The char is a newline character.

Definition at line 30 of file Text.cpp.

◆ isNewLine() [2/2]

bool sourcepp::parser::text::isNewLine ( std::string_view  str)

If a string is entirely composed of newline characters.

Parameters
strThe string.
Returns
The string is entirely composed of newline characters.

Definition at line 34 of file Text.cpp.

◆ isNumber() [1/2]

bool sourcepp::parser::text::isNumber ( char  c)

If a char is a numerical character (0-9).

Parameters
cThe char.
Returns
The char is a numerical character.

Definition at line 46 of file Text.cpp.

◆ isNumber() [2/2]

bool sourcepp::parser::text::isNumber ( std::string_view  str)

If a string is entirely composed of numerical characters (0-9).

Parameters
strThe string.
Returns
The string is entirely composed of numerical characters.

Definition at line 50 of file Text.cpp.

◆ isWhitespace() [1/2]

bool sourcepp::parser::text::isWhitespace ( char  c)

If a char is a whitespace character.

Parameters
cThe char.
Returns
The char is a whitespace character.

Definition at line 38 of file Text.cpp.

◆ isWhitespace() [2/2]

bool sourcepp::parser::text::isWhitespace ( std::string_view  str)

If a string is entirely composed of whitespace characters.

Parameters
strThe string.
Returns
The string is entirely composed of whitespace characters.

Definition at line 42 of file Text.cpp.

◆ readStringToBuffer()

std::string_view sourcepp::parser::text::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.

Parameters
streamThe BufferStream to modify.
backingThe BufferStream to store the string data in.
startThe starting string chars. If any processed char is in this string, the string will be treated as "quoted" and will include spaces.
endThe ending string chars. If any processed char is in this string, and a starting char is detected, the string will terminate.
escapeSequencesCharacters that will be escaped if a backslash is present before them. To disable escapes, pass an empty map.
Returns
A view over the string written to the backing stream.

Definition at line 145 of file Text.cpp.

◆ readUnquotedStringToBuffer() [1/2]

std::string_view sourcepp::parser::text::readUnquotedStringToBuffer ( BufferStream &  stream,
BufferStream &  backing,
const EscapeSequenceMap escapeSequences = DEFAULT_ESCAPE_SEQUENCES 
)

Read a string starting at the current stream position.

Parameters
streamThe BufferStream to modify.
backingThe BufferStream to store the string data in.
escapeSequencesCharacters that will be escaped if a backslash is present before them. To disable escapes, pass an empty map.
Returns
A view over the string written to the backing stream.

Definition at line 178 of file Text.cpp.

◆ readUnquotedStringToBuffer() [2/2]

std::string_view sourcepp::parser::text::readUnquotedStringToBuffer ( BufferStream &  stream,
BufferStream &  backing,
std::string_view  end,
const EscapeSequenceMap escapeSequences = DEFAULT_ESCAPE_SEQUENCES 
)

Read a string starting at the current stream position.

Parameters
streamThe BufferStream to modify.
backingThe BufferStream to store the string data in.
endThe ending string chars. If any processed char is in this string, the string will terminate.
escapeSequencesCharacters that will be escaped if a backslash is present before them. To disable escapes, pass an empty map.
Returns
A view over the string written to the backing stream.

Definition at line 182 of file Text.cpp.

◆ tryToEatChar()

bool sourcepp::parser::text::tryToEatChar ( BufferStream &  stream,
char  c 
)

If the given char exists at the current position, skip over it.

Parameters
streamThe BufferStream to modify
cThe char to compare
Returns
If the char exists at the current position

Definition at line 137 of file Text.cpp.

Variable Documentation

◆ DEFAULT_ESCAPE_SEQUENCES

const EscapeSequenceMap sourcepp::parser::text::DEFAULT_ESCAPE_SEQUENCES
Initial value:
= {
{'\'', '\''},
{'\"', '\"'},
{'?', '?'},
{'\\', '\\'},
{'a', '\a'},
{'b', '\b'},
{'f', '\f'},
{'n', '\n'},
{'r', '\r'},
{'t', '\t'},
{'v', '\v'},
}

Definition at line 12 of file Text.cpp.

◆ DEFAULT_MULTI_LINE_COMMENT_END

constexpr std::string_view sourcepp::parser::text::DEFAULT_MULTI_LINE_COMMENT_END = "*/"
constexpr

Definition at line 13 of file Text.h.

◆ DEFAULT_MULTI_LINE_COMMENT_START

constexpr std::string_view sourcepp::parser::text::DEFAULT_MULTI_LINE_COMMENT_START = "/*"
constexpr

Definition at line 12 of file Text.h.

◆ DEFAULT_SINGLE_LINE_COMMENT_START

constexpr std::string_view sourcepp::parser::text::DEFAULT_SINGLE_LINE_COMMENT_START = "//"
constexpr

Definition at line 11 of file Text.h.

◆ DEFAULT_STRING_END

constexpr std::string_view sourcepp::parser::text::DEFAULT_STRING_END = "\""
constexpr

Definition at line 15 of file Text.h.

◆ DEFAULT_STRING_START

constexpr std::string_view sourcepp::parser::text::DEFAULT_STRING_START = "\""
constexpr

Definition at line 14 of file Text.h.

◆ NO_ESCAPE_SEQUENCES

const EscapeSequenceMap sourcepp::parser::text::NO_ESCAPE_SEQUENCES = {}

Definition at line 26 of file Text.cpp.