-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
1,357 additions
and
846 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "stdafx.h" | ||
|
||
#include "ParsingUtils.hpp" | ||
|
||
ParseIncludeResult ParseInclude(pstr string, pcstr& out_include_name) | ||
{ | ||
VERIFY(string); | ||
|
||
// Skip any whitespace characters | ||
string = ParseAllSpaces(string); | ||
|
||
// Check for #include | ||
static constexpr pcstr IncludeTag = "#include"; | ||
if (std::strncmp(string, IncludeTag, 8) != 0) | ||
return ParseIncludeResult::NoInclude; | ||
|
||
string += 8; | ||
|
||
// Skip any whitespace characters | ||
string = ParseAllSpaces(string); | ||
|
||
// Check that after the tag there is a quote | ||
if (*string != '\"') | ||
return ParseIncludeResult::Error; | ||
|
||
// Mark the start of the include name | ||
++string; | ||
out_include_name = string; | ||
|
||
string = ParseUntil(string, '\"'); | ||
|
||
// Check for unterminated or empty include name | ||
if (*string == '\0' || out_include_name == string) | ||
return ParseIncludeResult::Error; | ||
|
||
// Check for unreasonably long include names | ||
const size_t size = string - out_include_name; | ||
if (size > 1024) | ||
return ParseIncludeResult::Error; | ||
|
||
// NOTE(Andre): Yes this might look scary but it's perfectly fine. Since the include name is already in the string | ||
// we are parsing and its not used afterwards we simply replace the closing quote with a null byte and we have a | ||
// valid c-string pointed to by 'out_include_name' and safe ourselves the need to copy the string. | ||
*string = '\0'; | ||
|
||
return ParseIncludeResult::Success; | ||
} | ||
|
||
pcstr ParseAllSpaces(pcstr string) | ||
{ | ||
VERIFY(string); | ||
|
||
while (*string != '\0' && std::isspace(*string)) | ||
++string; | ||
|
||
return string; | ||
} | ||
|
||
pstr ParseAllSpaces(pstr string) { return const_cast<pstr>(ParseAllSpaces(reinterpret_cast<pcstr>(string))); } | ||
|
||
pcstr ParseUntil(pcstr string, const char character) | ||
{ | ||
VERIFY(string); | ||
|
||
while (*string != '\0' && *string != character) | ||
++string; | ||
|
||
return string; | ||
} | ||
|
||
pstr ParseUntil(pstr string, const char character) | ||
{ | ||
return const_cast<pstr>(ParseUntil(reinterpret_cast<pcstr>(string), character)); | ||
} | ||
|
||
void StringCopyLowercase(pstr destination, pcstr src, std::size_t size) | ||
{ | ||
VERIFY(destination); | ||
VERIFY(src); | ||
|
||
for (std::size_t i = 0; *src != '\0' && i < size; ++i) | ||
{ | ||
*destination = std::tolower(*src); | ||
++src; | ||
++destination; | ||
} | ||
|
||
// Ensure the string is null-terminated | ||
*destination = '\0'; | ||
} | ||
|
||
void StringCopyLowercase(pstr destination, shared_str src, std::size_t size) | ||
{ | ||
StringCopyLowercase(destination, *src, size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "xr_types.h" | ||
#include "xrstring.h" | ||
|
||
enum class ParseIncludeResult | ||
{ | ||
Success, /// There is a valid #include and 'out_include_name' contains the filename | ||
Error, /// There is a #include but there is some problem | ||
NoInclude, /// There is no #include on this line | ||
}; | ||
|
||
// Given a string of the form: '#include "filename"' we try to parse filename into 'out_include_name' | ||
// Note that the file name is parsed inplace to avoid copying the string | ||
ParseIncludeResult ParseInclude(pstr string, pcstr& out_include_name); | ||
|
||
// Starting from the beginning of the string skips all characters for which 'std::isspace' is 'true'. | ||
// Returns the first position where 'std::isspace' is 'false'. | ||
pcstr ParseAllSpaces(pcstr string); | ||
|
||
pstr ParseAllSpaces(pstr string); | ||
|
||
// Starting from the begging of the string skips all characters until 'character' is found | ||
// or until the end of the string is reached. | ||
// Returns the first position where 'character' is found or the end of string if 'character' is not found | ||
pcstr ParseUntil(pcstr string, const char character); | ||
|
||
pstr ParseUntil(pstr string, const char character); | ||
|
||
// Copies 'size' characters from 'src' to 'destination' and converts it to lowercase | ||
void StringCopyLowercase(pstr destination, pcstr src, std::size_t size); | ||
|
||
void StringCopyLowercase(pstr destination, shared_str src, std::size_t size); |
Oops, something went wrong.