From cf7a7796891da30742fd0503ef17779969cd31b0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 1 Jun 2018 01:01:24 +0300 Subject: [PATCH] Fix mixed markup sending. --- .../SourceFiles/ui/widgets/input_fields.cpp | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/ui/widgets/input_fields.cpp b/Telegram/SourceFiles/ui/widgets/input_fields.cpp index dc590c4db..df445e09c 100644 --- a/Telegram/SourceFiles/ui/widgets/input_fields.cpp +++ b/Telegram/SourceFiles/ui/widgets/input_fields.cpp @@ -2284,10 +2284,22 @@ TextWithTags InputField::getTextWithAppliedMarkdown() const { auto result = TextWithTags(); result.text.reserve(originalText.size()); result.tags.reserve(originalTags.size() + _lastMarkdownTags.size()); - auto from = 0; auto removed = 0; auto originalTag = originalTags.begin(); const auto originalTagsEnd = originalTags.end(); + const auto addOriginalTagsUpTill = [&](int offset) { + while (originalTag != originalTagsEnd + && originalTag->offset + originalTag->length <= offset) { + result.tags.push_back(*originalTag++); + result.tags.back().offset -= removed; + } + }; + auto from = 0; + const auto addOriginalTextUpTill = [&](int offset) { + if (offset > from) { + result.text.append(originalText.midRef(from, offset - from)); + } + }; auto link = links.begin(); const auto linksEnd = links.end(); for (const auto &tag : _lastMarkdownTags) { @@ -2299,11 +2311,7 @@ TextWithTags InputField::getTextWithAppliedMarkdown() const { if (entityLength <= 0) { continue; } - while (originalTag != originalTagsEnd - && originalTag->offset + originalTag->length <= tag.start) { - result.tags.push_back(*originalTag++); - result.tags.back().offset -= removed; - } + addOriginalTagsUpTill(tag.start); if (originalTag != originalTagsEnd && originalTag->offset < tag.start + tag.length) { continue; @@ -2318,9 +2326,7 @@ TextWithTags InputField::getTextWithAppliedMarkdown() const { || link->offset() < tag.start)) { continue; } - if (tag.start > from) { - result.text.append(originalText.midRef(from, tag.start - from)); - } + addOriginalTextUpTill(tag.start); result.tags.push_back(TextWithTags::Tag{ int(result.text.size()), entityLength, @@ -2330,9 +2336,8 @@ TextWithTags InputField::getTextWithAppliedMarkdown() const { from = tag.start + tag.length; removed += 2 * tagLength; } - if (originalText.size() > from) { - result.text.append(originalText.midRef(from)); - } + addOriginalTagsUpTill(originalText.size()); + addOriginalTextUpTill(originalText.size()); return result; }