diff --git a/ui/text/text_utilities.cpp b/ui/text/text_utilities.cpp index 37172fd..715c8ed 100644 --- a/ui/text/text_utilities.cpp +++ b/ui/text/text_utilities.cpp @@ -74,5 +74,48 @@ TextWithEntities RichLangValue(const QString &text) { return result; } +TextWithEntities Mid(const TextWithEntities &text, int position, int n) { + if (n == -1) { + n = int(text.text.size()) - position; + } + const auto midEnd = (position + n); + auto entities = ranges::views::all( + text.entities + ) | ranges::views::filter([&](const EntityInText &entity) { + // Intersects of ranges. + const auto l1 = entity.offset(); + const auto r1 = entity.offset() + entity.length() - 1; + const auto l2 = position; + const auto r2 = midEnd - 1; + return !(l1 > r2 || l2 > r1); + }) | ranges::views::transform([&](const EntityInText &entity) { + if ((entity.offset() == position) && (entity.length() == n)) { + return entity; + } + const auto start = std::max(entity.offset(), position); + const auto end = std::min(entity.offset() + entity.length(), midEnd); + return EntityInText( + entity.type(), + start - position, + end - start, + entity.data()); + }) | ranges::to(); + return { + .text = text.text.mid(position, n), + .entities = std::move(entities), + }; +} + +TextWithEntities Filtered( + const TextWithEntities &text, + const std::vector &types) { + auto result = ranges::views::all( + text.entities + ) | ranges::views::filter([&](const EntityInText &entity) { + return ranges::contains(types, entity.type()); + }) | ranges::to(); + return { .text = text.text, .entities = std::move(result) }; +} + } // namespace Text } // namespace Ui diff --git a/ui/text/text_utilities.h b/ui/text/text_utilities.h index c77c567..b07d0e7 100644 --- a/ui/text/text_utilities.h +++ b/ui/text/text_utilities.h @@ -65,5 +65,13 @@ inline constexpr auto Upper = details::ToUpperType{}; return rpl::map(WithEntities); } +[[nodiscard]] TextWithEntities Mid( + const TextWithEntities &text, + int position, + int n = -1); +[[nodiscard]] TextWithEntities Filtered( + const TextWithEntities &result, + const std::vector &types); + } // namespace Text } // namespace Ui