diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-26 14:40:14 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-26 14:40:14 +1000 |
commit | ed3d5dffc099b1c5bcb14451fffc63de1b6ac634 (patch) | |
tree | a65f78b8cfcfe475faccc568efb1975f4433c87e /source/core/StarString.hpp | |
parent | 79c5012033935b54d6ef94b2830aed12f92811c1 (diff) |
slight NetworkedAnimator drawables optimization
Diffstat (limited to 'source/core/StarString.hpp')
-rw-r--r-- | source/core/StarString.hpp | 51 |
1 files changed, 51 insertions, 0 deletions
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 <typename Lookup> String lookupTags(Lookup&& lookup) const; + // StringView variant + template <typename Lookup> + Maybe<String> maybeLookupTagsView(Lookup&& lookup) const; + + template <typename Lookup> + 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 <typename Lookup> +Maybe<String> String::maybeLookupTagsView(Lookup&& lookup) const { + List<std::string_view> 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<String>(); + + 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 <typename Lookup> +String String::lookupTagsView(Lookup&& lookup) const { + auto result = maybeLookupTagsView(lookup); + return result ? move(result.take()) : String(); +} + template <typename MapType> String String::replaceTags(MapType const& tags, bool replaceWithDefault, String defaultValue) const { return lookupTags([&](String const& key) -> String { |