From 2a2a97f0f804acd19a3c4102777170d5b6fcee93 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 28 May 2022 20:08:52 +0300 Subject: [PATCH] Refactor ui::tagged_text to use allocated strings instead of string_views --- libs/ui/include/psemek/ui/label.hpp | 12 ++++---- libs/ui/include/psemek/ui/tagged_text.hpp | 10 +++---- libs/ui/source/label.cpp | 10 +++---- libs/ui/source/tagged_text.cpp | 17 +++++------- libs/ui/tests/tagged_text.cpp | 34 +++++++++++------------ 5 files changed, 39 insertions(+), 44 deletions(-) diff --git a/libs/ui/include/psemek/ui/label.hpp b/libs/ui/include/psemek/ui/label.hpp index 489dd681..0f14a2f6 100644 --- a/libs/ui/include/psemek/ui/label.hpp +++ b/libs/ui/include/psemek/ui/label.hpp @@ -102,17 +102,17 @@ namespace psemek::ui struct text_chunk { - std::string_view text; + std::string text; text_style style; // to be OR'd with base style std::optional color; - std::optional link; + std::optional link; }; struct image_chunk { - std::string_view id; + std::string id; std::optional color; - std::optional link; + std::optional link; }; using chunk = std::variant; @@ -139,12 +139,12 @@ namespace psemek::ui std::vector batches; geom::vector size{0.f, 0.f}; - std::vector, std::string_view>> link_bboxes; + std::vector, std::string>> link_bboxes; }; mutable std::optional cached_state_; mutable std::optional cached_state_inf_; - std::optional selected_link_; + std::optional selected_link_; bool mouse_down_ = false; void update_cached_state() const; diff --git a/libs/ui/include/psemek/ui/tagged_text.hpp b/libs/ui/include/psemek/ui/tagged_text.hpp index c049471b..9b3c7e00 100644 --- a/libs/ui/include/psemek/ui/tagged_text.hpp +++ b/libs/ui/include/psemek/ui/tagged_text.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include @@ -16,16 +16,16 @@ namespace psemek::ui { struct opening_tag { - std::string_view type; - std::optional attribute; + std::string type; + std::optional attribute; }; struct closing_tag { - std::string_view type; + std::string type; }; - using token = std::variant; + using token = std::variant; std::vector tokens; diff --git a/libs/ui/source/label.cpp b/libs/ui/source/label.cpp index 1d301ad2..98482675 100644 --- a/libs/ui/source/label.cpp +++ b/libs/ui/source/label.cpp @@ -35,7 +35,7 @@ namespace psemek::ui "link", }; - void check_attribute(std::string_view tag, std::optional const & attribute) + void check_attribute(std::string const & tag, std::optional const & attribute) { if (tag == "bold" || tag == "uline" || tag == "strike") { @@ -65,11 +65,11 @@ namespace psemek::ui auto parse_result = tagged_text::parse(text_); - std::unordered_map>> tags_stack; + std::unordered_map>> tags_stack; for (auto const & token : parse_result.tokens) { - if (auto text = std::get_if(&token)) + if (auto text = std::get_if(&token)) { text_chunk chunk; if (!tags_stack["bold"].empty()) @@ -174,7 +174,7 @@ namespace psemek::ui { if (cached_state_) { - std::optional new_selected_link; + std::optional new_selected_link; auto const p = geom::cast(e.position); for (auto const & b : cached_state_->link_bboxes) { @@ -341,7 +341,7 @@ namespace psemek::ui text_style style; gfx::color_rgba color; gfx::texture_view_2d image; - std::optional link; + std::optional link; }; std::vector items; diff --git a/libs/ui/source/tagged_text.cpp b/libs/ui/source/tagged_text.cpp index 40082bee..0e60e4d1 100644 --- a/libs/ui/source/tagged_text.cpp +++ b/libs/ui/source/tagged_text.cpp @@ -56,12 +56,9 @@ namespace psemek::ui } else { - auto append = [&](std::string_view & target) + auto append = [&](std::string & target) { - if (target.empty()) - target = {current, current + 1}; - else - target = {target.data(), current + 1}; + target.push_back(*current); }; if (in_tag) @@ -83,17 +80,17 @@ namespace psemek::ui ++current; if (current < text.end()) { - result.tokens.push_back(std::string_view{}); - append(std::get(result.tokens.back())); + result.tokens.push_back(std::string{}); + append(std::get(result.tokens.back())); } else --current; } else { - if (result.tokens.empty() || !std::get_if(&result.tokens.back())) - result.tokens.push_back(std::string_view{}); - append(std::get(result.tokens.back())); + if (result.tokens.empty() || !std::get_if(&result.tokens.back())) + result.tokens.push_back(std::string{}); + append(std::get(result.tokens.back())); } } } diff --git a/libs/ui/tests/tagged_text.cpp b/libs/ui/tests/tagged_text.cpp index 5f9c2c94..51207fb8 100644 --- a/libs/ui/tests/tagged_text.cpp +++ b/libs/ui/tests/tagged_text.cpp @@ -14,9 +14,9 @@ static void compare(tagged_text const & result, tagged_text const & expected) for (std::size_t i = 0; i < result.tokens.size(); ++i) { expect_equal(result.tokens[i].index(), expected.tokens[i].index()); - if (auto token = std::get_if(&result.tokens[i])) + if (auto token = std::get_if(&result.tokens[i])) { - expect_equal(*token, std::get(expected.tokens[i])); + expect_equal(*token, std::get(expected.tokens[i])); } else if (auto token = std::get_if(&result.tokens[i])) { @@ -116,14 +116,12 @@ test_case(ui_tagged__text_random) generator rng; - std::vector> string_pool; - - auto random_string = [&rng, &string_pool]() -> std::string const & { - auto & result = string_pool.emplace_back(std::make_unique()); - result->resize(uniform(rng, 1, 10)); - for (char & c : *result) + auto random_string = [&rng]() -> std::string { + std::string result; + result.resize(uniform(rng, 1, 10)); + for (char & c : result) c = uniform(rng, 'a', 'z'); - return *result; + return result; }; std::string text; @@ -133,14 +131,14 @@ test_case(ui_tagged__text_random) { auto roll = uniform(rng, 0, 2); - if (!expected.tokens.empty() && std::get_if(&expected.tokens.back()) && roll == 0) + if (!expected.tokens.empty() && std::get_if(&expected.tokens.back()) && roll == 0) roll = uniform(rng, 1, 2); if (roll == 0) { - auto const & str = random_string(); + auto str = random_string(); text += str; - expected.tokens.push_back(std::string_view{str}); + expected.tokens.push_back(std::move(str)); } else if (roll == 1) { @@ -148,16 +146,16 @@ test_case(ui_tagged__text_random) tagged_text::opening_tag token; - auto const & tag_str = random_string(); + auto tag_str = random_string(); text += "[" + tag_str; - token.type = tag_str; + token.type = std::move(tag_str); if (has_attr) { - auto const & attr_str = random_string(); + auto attr_str = random_string(); text += ":" + attr_str; - token.attribute = std::string_view{attr_str}; + token.attribute = std::move(attr_str); } text += "]"; @@ -168,10 +166,10 @@ test_case(ui_tagged__text_random) { tagged_text::closing_tag token; - auto const & tag_str = random_string(); + auto tag_str = random_string(); text += "[/" + tag_str + "]"; - token.type = tag_str; + token.type = std::move(tag_str); expected.tokens.push_back(token); }