From ed3d5dffc099b1c5bcb14451fffc63de1b6ac634 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 26 Jun 2023 14:40:14 +1000 Subject: slight NetworkedAnimator drawables optimization --- source/core/StarString.hpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'source/core/StarString.hpp') diff --git a/source/core/StarString.hpp b/source/core/StarString.hpp index ddc7926..5adb0f9 100644 --- a/source/core/StarString.hpp +++ b/source/core/StarString.hpp @@ -212,6 +212,13 @@ public: template String lookupTags(Lookup&& lookup) const; + // StringView variant + template + Maybe maybeLookupTagsView(Lookup&& lookup) const; + + template + String lookupTagsView(Lookup&& lookup) const; + // Replace angle bracket tags in the string with values given by the tags // map. If replaceWithDefault is true, then values that are not found in the // tags map are replace with the default string. If replaceWithDefault is @@ -424,6 +431,50 @@ String String::lookupTags(Lookup&& lookup) const { return move(finalString); } +template +Maybe String::maybeLookupTagsView(Lookup&& lookup) const { + List finalViews = {}; + std::string_view view(utf8()); + + size_t start = 0; + while (true) { + if (start >= view.size()) + break; + + size_t beginTag = view.find_first_of('<', start); + if (beginTag == NPos && !start) + return Maybe(); + + size_t endTag = view.find_first_of('>', beginTag); + if (beginTag != NPos && endTag != NPos) { + finalViews.append(view.substr(start, beginTag - start)); + finalViews.append(lookup(view.substr(beginTag + 1, endTag - beginTag - 1)).takeUtf8()); + start = endTag + 1; + } else { + finalViews.append(view.substr(start)); + break; + } + } + + std::string finalString; + size_t finalSize = 0; + for (auto& view : finalViews) + finalSize += view.size(); + + finalString.reserve(finalSize); + + for (auto& view : finalViews) + finalString += view; + + return move(finalString); +} + +template +String String::lookupTagsView(Lookup&& lookup) const { + auto result = maybeLookupTagsView(lookup); + return result ? move(result.take()) : String(); +} + template String String::replaceTags(MapType const& tags, bool replaceWithDefault, String defaultValue) const { return lookupTags([&](String const& key) -> String { -- cgit v1.2.3