diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-26 01:42:18 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-26 01:42:18 +1000 |
commit | 09d26d43b5262f480fd55eab9980eff06a71edbb (patch) | |
tree | 1e53765861cd966d204782aeefd15b1a67b3266f /source/core/StarDirectives.cpp | |
parent | 13a74602bd4c46149da9949d448387a40b8ebd1c (diff) |
Add string view variant of Star::String and use it
it's 1:30 AM AGAIN !! !!!!!
This might have broken the inventory icons of custom hats a little, need to look into that
Diffstat (limited to 'source/core/StarDirectives.cpp')
-rw-r--r-- | source/core/StarDirectives.cpp | 162 |
1 files changed, 97 insertions, 65 deletions
diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp index c7c35fc..fd4ee46 100644 --- a/source/core/StarDirectives.cpp +++ b/source/core/StarDirectives.cpp @@ -1,92 +1,125 @@ +#include "StarDirectives.hpp" #include "StarImage.hpp" #include "StarImageProcessing.hpp" -#include "StarDirectives.hpp" #include "StarXXHash.hpp" #include "StarLogging.hpp" namespace Star { -Directives::Entry::Entry(ImageOperation&& newOperation, String&& newString) { +Directives::Entry::Entry(ImageOperation&& newOperation, size_t strBegin, size_t strLength) { operation = move(newOperation); - string = move(newString); + begin = strBegin; + length = strLength; } -Directives::Entry::Entry(ImageOperation const& newOperation, String const& newString) { +Directives::Entry::Entry(ImageOperation const& newOperation, size_t strBegin, size_t strLength) { operation = newOperation; - string = newString; + begin = strBegin; + length = strLength; } Directives::Entry::Entry(Entry const& other) { operation = other.operation; - string = other.string; + begin = other.begin; + length = other.length; } -Directives::Directives() : hash(0) {} -Directives::Directives(String const& directives) : hash(0) { - string = directives; - parse(string); +StringView Directives::Entry::string(Shared const& parent) const { + StringView result(parent.string); + result = result.substr(begin, length); + return result; } -Directives::Directives(String&& directives) : hash(0) { - string = move(directives); - parse(string); +bool Directives::Shared::empty() const { + return entries.empty(); } -Directives::Directives(const char* directives) : hash(0), string(directives) { - parse(string); +Directives::Shared::Shared(List<Entry>&& givenEntries, String&& givenString) { + entries = move(givenEntries); + string = move(givenString); + hash = XXH3_64bits(string.utf8Ptr(), string.utf8Size()); } -Directives::Directives(List<Entry>&& newEntries) { - entries = std::make_shared<List<Entry> const>(move(newEntries)); - String newString; - string = move(buildString(newString)); - hash = XXH3_64bits(string.utf8Ptr(), string.utf8Size()); +Directives::Directives() {} +Directives::Directives(String const& directives) { + parse(String(directives)); +} + +Directives::Directives(String&& directives) { + parse(move(directives)); } -void Directives::parse(String const& directives) { +Directives::Directives(const char* directives) { + parse(directives); +} + +void Directives::parse(String&& directives) { if (directives.empty()) return; List<Entry> newList; - StringList split = directives.split('?'); - newList.reserve(split.size()); - for (String& str : split) { - if (!str.empty()) { + + StringView(directives).forEachSplitView("?", [&](StringView split, size_t beg, size_t end) { + if (!split.empty()) { try { - ImageOperation operation = imageOperationFromString(str); - newList.emplace_back(move(operation), move(str)); - } catch (StarException const& e) { - newList.emplace_back(ErrorImageOperation{ std::current_exception() }, move(str)); + 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 (newList.empty()) return; - entries = std::make_shared<List<Entry> const>(move(newList)); - hash = XXH3_64bits(directives.utf8Ptr(), directives.utf8Size()); - //if (directives.utf8Size() > 1000) - // Logger::logf(LogLevel::Debug, "Directives: Parsed %u character long string", directives.utf8Size()); + shared = std::make_shared<Shared const>(move(newList), move(directives)); +} + +String Directives::string() const { + if (!shared) + return ""; + else + return shared->string; +} + +String const* Directives::stringPtr() const { + if (!shared) + return nullptr; + else + return &shared->string; } -String& Directives::buildString(String& out) const { - if (entries) { - for (auto& entry : *entries) { - out += "?"; - out += entry.string; + +String Directives::buildString() const { + String built; + if (shared) { + for (auto& entry : shared->entries) { + built += "?"; + built += entry.string(*shared); } } + return built; +} + +String& Directives::addToString(String& out) const { + if (!empty()) + out += shared->string; return out; } -String Directives::toString() const { - return string; +size_t Directives::hash() const { + return shared ? shared->hash : 0; +} + +size_t Directives::size() const { + return shared ? shared->entries.size() : 0; } inline bool Directives::empty() const { - return !entries || entries->empty(); + return !shared || shared->empty(); } inline Directives::operator bool() const { @@ -98,13 +131,16 @@ DataStream& operator>>(DataStream& ds, Directives& directives) { String string; ds.read(string); - directives.parse(string); + directives.parse(move(string)); return ds; } DataStream& operator<<(DataStream& ds, Directives const& directives) { - ds.write(directives.toString()); + if (directives) + ds.write(directives.shared->string); + else + ds.write(String()); return ds; } @@ -117,7 +153,7 @@ DirectivesGroup::DirectivesGroup(String const& directives) : m_count(0) { Directives parsed(directives); if (parsed) { m_directives.emplace_back(move(parsed)); - m_count = m_directives.back().entries->size(); + m_count = m_directives.back().size(); } } DirectivesGroup::DirectivesGroup(String&& directives) : m_count(0) { @@ -129,7 +165,7 @@ DirectivesGroup::DirectivesGroup(String&& directives) : m_count(0) { Directives parsed(move(directives)); if (parsed) { m_directives.emplace_back(move(parsed)); - m_count = m_directives.back().entries->size(); + m_count = m_directives.back().size(); } } @@ -153,14 +189,7 @@ bool DirectivesGroup::compare(DirectivesGroup const& other) const { void DirectivesGroup::append(Directives const& directives) { m_directives.emplace_back(directives); - if (directives.entries) - m_count += m_directives.back().entries->size(); -} - -void DirectivesGroup::append(List<Directives::Entry>&& entries) { - size_t size = entries.size(); - m_directives.emplace_back(move(entries)); - m_count += size; + m_count += m_directives.back().size(); } void DirectivesGroup::clear() { @@ -181,23 +210,24 @@ inline String DirectivesGroup::toString() const { void DirectivesGroup::addToString(String& string) const { for (auto& directives : m_directives) - string += directives.string; + if (directives.shared) + string += directives.shared->string; } void DirectivesGroup::forEach(DirectivesCallback callback) const { for (auto& directives : m_directives) { - if (directives.entries) { - for (auto& entry : *directives.entries) - callback(entry); + if (directives.shared) { + for (auto& entry : directives.shared->entries) + callback(entry, directives); } } } bool DirectivesGroup::forEachAbortable(AbortableDirectivesCallback callback) const { for (auto& directives : m_directives) { - if (directives.entries) { - for (auto& entry : *directives.entries) { - if (!callback(entry)) + if (directives.shared) { + for (auto& entry : directives.shared->entries) { + if (!callback(entry, directives)) return false; } } @@ -213,7 +243,7 @@ inline Image DirectivesGroup::applyNewImage(Image const& image) const { } void DirectivesGroup::applyExistingImage(Image& image) const { - forEach([&](auto const& entry) { + forEach([&](auto const& entry, Directives const& directives) { if (auto error = entry.operation.ptr<ErrorImageOperation>()) std::rethrow_exception(error->exception); else @@ -223,8 +253,10 @@ void DirectivesGroup::applyExistingImage(Image& image) const { inline size_t DirectivesGroup::hash() const { XXHash3 hasher; - for (auto& directives : m_directives) - hasher.push((const char*)&directives.hash, sizeof(directives.hash)); + for (auto& directives : m_directives) { + size_t hash = directives.hash(); + hasher.push((const char*)&hash, sizeof(hash)); + } return hasher.digest(); } @@ -245,7 +277,7 @@ DataStream& operator>>(DataStream& ds, DirectivesGroup& directivesGroup) { String string; ds.read(string); - directivesGroup = move(DirectivesGroup(move(string))); + directivesGroup = DirectivesGroup(move(string)); return ds; } |