Handle abnormal directives prefixes

This commit is contained in:
Kae 2023-06-26 15:34:41 +10:00
parent ed3d5dffc0
commit ec9a138e1a
2 changed files with 26 additions and 9 deletions

View File

@ -34,9 +34,10 @@ bool Directives::Shared::empty() const {
return entries.empty();
}
Directives::Shared::Shared(List<Entry>&& givenEntries, String&& givenString) {
Directives::Shared::Shared(List<Entry>&& givenEntries, String&& givenString, StringView givenPrefix) {
entries = move(givenEntries);
string = move(givenString);
prefix = givenPrefix;
hash = XXH3_64bits(string.utf8Ptr(), string.utf8Size());
}
@ -58,23 +59,27 @@ void Directives::parse(String&& directives) {
return;
List<Entry> newList;
StringView(directives).forEachSplitView("?", [&](StringView split, size_t beg, size_t end) {
StringView view(directives);
StringView prefix = "";
view.forEachSplitView("?", [&](StringView split, size_t beg, size_t end) {
if (!split.empty()) {
try {
ImageOperation operation = imageOperationFromString(split);
newList.emplace_back(move(operation), beg, end);
}
catch (StarException const& e) {
newList.emplace_back(ErrorImageOperation{ std::current_exception() }, beg, end);
if (beg == 0)
prefix = split;
else
newList.emplace_back(ErrorImageOperation{ std::current_exception() }, beg, end);
}
}
});
if (newList.empty())
if (newList.empty() && !prefix.empty())
return;
shared = std::make_shared<Shared const>(move(newList), move(directives));
shared = std::make_shared<Shared const>(move(newList), move(directives), prefix);
}
String Directives::string() const {
@ -84,6 +89,13 @@ String Directives::string() const {
return shared->string;
}
StringView Directives::prefix() const {
if (!shared)
return "";
else
return shared->prefix;
}
String const* Directives::stringPtr() const {
if (!shared)
return nullptr;
@ -93,15 +105,18 @@ String const* Directives::stringPtr() const {
String Directives::buildString() const {
String built;
if (shared) {
String built = shared->prefix;
for (auto& entry : shared->entries) {
built += "?";
built += entry.string(*shared);
}
return built;
}
return built;
return String();
}
String& Directives::addToString(String& out) const {

View File

@ -30,10 +30,11 @@ public:
struct Shared {
List<Entry> entries;
String string;
StringView prefix;
size_t hash = 0;
bool empty() const;
Shared(List<Entry>&& givenEntries, String&& givenString);
Shared(List<Entry>&& givenEntries, String&& givenString, StringView givenPrefix);
};
Directives();
@ -43,6 +44,7 @@ public:
void parse(String&& directives);
String string() const;
StringView prefix() const;
String const* stringPtr() const;
String buildString() const;
String& addToString(String& out) const;