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

@ -29,7 +29,7 @@ public:
unsigned long definePos = str.find_last_of('=') + 1;
return str.size() > definePos ? str.substr(definePos) : string();
}
ArgumentAssignable(string name, const span<string_view>& possibleValues) : Argument(move(name)), values() {
ArgumentAssignable(string name, const span<string_view>& possibleValues): Argument(move(name)), values() {
values.reserve(possibleValues.size());
move(possibleValues.begin(), possibleValues.end(), back_inserter(values));
}

View File

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