Don't find occurrences when closing characters are not found in Target.hpp, make the interpolation strings static const variables and fix typos

This commit is contained in:
Username404 2021-08-31 15:19:44 +02:00
parent bc2f6938e3
commit afc59c7a22
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
2 changed files with 15 additions and 12 deletions

View File

@ -12,17 +12,20 @@
#define INCLUDECOMPONENT(X) virtual void on(const X& parseComponent) {} #define INCLUDECOMPONENT(X) virtual void on(const X& parseComponent) {}
class Target { class Target {
constexpr static const char* const interpolationString = "${";
constexpr static const char* const interpolationCloseString = "}";
protected: protected:
std::stringstream output; std::stringstream output;
INCLUDECOMPONENT(StandardComponents::Define); INCLUDECOMPONENT(StandardComponents::Define);
INCLUDECOMPONENT(StandardComponents::types::String); INCLUDECOMPONENT(StandardComponents::types::String);
void stringInterpolation(string view, const char* openMultiline = "", const char* closeMultiline = "", const char* concatenationCharacters = "+") { void stringInterpolation(string view, const char* openMultiline = "", const char* closeMultiline = "", const char* concatenationCharacters = "+") {
vector<size_t> interpolationVector; vector<size_t> interpolationVector;
const string interpolationString = "${"; unsigned long occurrence_position = view.find(interpolationString);
unsigned long occurence_position = view.find(interpolationString); if (view.find(interpolationCloseString) != string::npos) {
while (occurence_position != string::npos) { while (occurrence_position != string::npos) {
interpolationVector.push_back(occurence_position); interpolationVector.push_back(occurrence_position);
occurence_position = view.find(interpolationString, occurence_position + interpolationString.size()); occurrence_position = view.find(interpolationString, occurrence_position + strlen(interpolationString));
}
} }
unsigned long newLine = view.find('\n'); unsigned long newLine = view.find('\n');
const bool multiline = newLine != string::npos && not !strcmp(openMultiline, "") && not !strcmp(closeMultiline, openMultiline); const bool multiline = newLine != string::npos && not !strcmp(openMultiline, "") && not !strcmp(closeMultiline, openMultiline);
@ -38,21 +41,21 @@ protected:
if (not interpolationVector.empty()) { if (not interpolationVector.empty()) {
unsigned long closingBrace = 0; unsigned long closingBrace = 0;
for (unsigned long i = 0; i < interpolationVector.size(); ++i) { for (unsigned long i = 0; i < interpolationVector.size(); ++i) {
const auto& occurence = interpolationVector[i]; const auto& occurrence = interpolationVector[i];
const bool hasNext = (i + 1) < interpolationVector.size(); const bool hasNext = (i + 1) < interpolationVector.size();
const bool oldClosingBraceIsFar = closingBrace >= occurence; const bool oldClosingBraceIsFar = closingBrace >= occurrence;
if (i > 0) output << concatenationCharacters; if (i > 0) output << concatenationCharacters;
output << openCharacters << view.substr(closingBrace + (i > 0), (occurence - closingBrace) - (i > 0)); output << openCharacters << view.substr(closingBrace + (i > 0), (occurrence - closingBrace) - (i > 0));
closingBrace = view.find_first_of('}', occurence); closingBrace = view.find_first_of(interpolationCloseString, occurrence);
const bool closingBraceIsNPOS = closingBrace == string::npos; const bool closingBraceIsNPOS = closingBrace == string::npos;
const bool wrongClosingBrace = !closingBraceIsNPOS && ((hasNext && closingBrace > interpolationVector[i + 1])); const bool wrongClosingBrace = !closingBraceIsNPOS && ((hasNext && closingBrace > interpolationVector[i + 1]));
if (closingBraceIsNPOS || wrongClosingBrace) { if (closingBraceIsNPOS || wrongClosingBrace) {
output << view.substr(occurence, (hasNext ? interpolationVector[i + 1] - occurence : 0)); output << view.substr(occurrence, (hasNext ? interpolationVector[i + 1] - occurrence : 0));
} }
output << closeCharacters; output << closeCharacters;
if (not closingBraceIsNPOS && not wrongClosingBrace) { if (not closingBraceIsNPOS && not wrongClosingBrace) {
output << concatenationCharacters; output << concatenationCharacters;
output << view.substr(occurence + interpolationString.size(), (closingBrace - occurence) - interpolationString.size()); output << view.substr(occurrence + strlen(interpolationString), (closingBrace - occurrence) - strlen(interpolationString));
} }
} }
} else output << openCharacters << view << closeCharacters << '\n'; } else output << openCharacters << view << closeCharacters << '\n';