2019-09-17 07:51:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "lang/lang_text_entity.h"
|
|
|
|
|
|
|
|
#include "lang/lang_tag.h"
|
|
|
|
|
|
|
|
namespace Lang {
|
|
|
|
|
|
|
|
TextWithEntities ReplaceTag<TextWithEntities>::Call(TextWithEntities &&original, ushort tag, const TextWithEntities &replacement) {
|
|
|
|
auto replacementPosition = FindTagReplacementPosition(original.text, tag);
|
|
|
|
if (replacementPosition < 0) {
|
|
|
|
return std::move(original);
|
|
|
|
}
|
2022-11-02 12:39:13 +00:00
|
|
|
return Replace(std::move(original), replacement, replacementPosition);
|
|
|
|
}
|
2019-09-17 07:51:02 +00:00
|
|
|
|
2022-11-02 12:39:13 +00:00
|
|
|
TextWithEntities ReplaceTag<TextWithEntities>::Replace(TextWithEntities &&original, const TextWithEntities &replacement, int start) {
|
2019-09-17 07:51:02 +00:00
|
|
|
auto result = TextWithEntities();
|
2022-11-02 12:39:13 +00:00
|
|
|
result.text = ReplaceTag<QString>::Replace(std::move(original.text), replacement.text, start);
|
2019-09-17 07:51:02 +00:00
|
|
|
auto originalEntitiesCount = original.entities.size();
|
|
|
|
auto replacementEntitiesCount = replacement.entities.size();
|
|
|
|
if (originalEntitiesCount != 0 || replacementEntitiesCount != 0) {
|
|
|
|
result.entities.reserve(originalEntitiesCount + replacementEntitiesCount);
|
|
|
|
|
2022-11-02 12:39:13 +00:00
|
|
|
auto replacementEnd = start + int(replacement.text.size());
|
2019-09-17 07:51:02 +00:00
|
|
|
auto replacementEntity = replacement.entities.cbegin();
|
2022-11-02 12:39:13 +00:00
|
|
|
auto addReplacementEntitiesUntil = [&](int untilPosition) {
|
2019-09-17 07:51:02 +00:00
|
|
|
while (replacementEntity != replacement.entities.cend()) {
|
2022-11-02 12:39:13 +00:00
|
|
|
auto newOffset = start + replacementEntity->offset();
|
2019-09-17 07:51:02 +00:00
|
|
|
if (newOffset >= untilPosition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto newEnd = newOffset + replacementEntity->length();
|
2022-11-02 12:39:13 +00:00
|
|
|
newOffset = std::clamp(newOffset, start, replacementEnd);
|
|
|
|
newEnd = std::clamp(newEnd, start, replacementEnd);
|
2019-09-17 07:51:02 +00:00
|
|
|
if (auto newLength = newEnd - newOffset) {
|
|
|
|
result.entities.push_back({ replacementEntity->type(), newOffset, newLength, replacementEntity->data() });
|
|
|
|
}
|
|
|
|
++replacementEntity;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &entity : std::as_const(original.entities)) {
|
|
|
|
// Transform the entity by the replacement.
|
|
|
|
auto offset = entity.offset();
|
|
|
|
auto end = offset + entity.length();
|
2022-11-02 12:39:13 +00:00
|
|
|
if (offset > start) {
|
2019-09-17 07:51:02 +00:00
|
|
|
offset = offset + replacement.text.size() - kTagReplacementSize;
|
|
|
|
}
|
2022-11-02 12:39:13 +00:00
|
|
|
if (end > start) {
|
2019-09-17 07:51:02 +00:00
|
|
|
end = end + replacement.text.size() - kTagReplacementSize;
|
|
|
|
}
|
2021-10-19 13:00:21 +00:00
|
|
|
offset = std::clamp(offset, 0, int(result.text.size()));
|
|
|
|
end = std::clamp(end, 0, int(result.text.size()));
|
2019-09-17 07:51:02 +00:00
|
|
|
|
|
|
|
// Add all replacement entities that start before the current original entity.
|
|
|
|
addReplacementEntitiesUntil(offset);
|
|
|
|
|
|
|
|
// Add a modified original entity.
|
|
|
|
if (auto length = end - offset) {
|
|
|
|
result.entities.push_back({ entity.type(), offset, length, entity.data() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add the remaining replacement entities.
|
|
|
|
addReplacementEntitiesUntil(result.text.size());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Lang
|