Skip to content

Commit 3db7aae

Browse files
committed
Add td_api::diffText.
1 parent d4cc165 commit 3db7aae

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ set(TDLIB_SOURCE_PART1
422422
td/telegram/DialogParticipantManager.cpp
423423
td/telegram/DialogPhoto.cpp
424424
td/telegram/DialogSource.cpp
425+
td/telegram/DiffText.cpp
425426
td/telegram/Dimensions.cpp
426427
td/telegram/DisallowedGiftsSettings.cpp
427428
td/telegram/Document.cpp
@@ -792,6 +793,7 @@ set(TDLIB_SOURCE_PART2
792793
td/telegram/DialogParticipantManager.h
793794
td/telegram/DialogPhoto.h
794795
td/telegram/DialogSource.h
796+
td/telegram/DiffText.h
795797
td/telegram/Dimensions.h
796798
td/telegram/DisallowedGiftsSettings.h
797799
td/telegram/Document.h

SplitSource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ function ($matches) use ($needed_std_headers) {
344344
'DialogParticipantFilter' => 'DialogParticipantFilter',
345345
'dialog_participant_manager[_(-](?![.]get[(][)])|DialogParticipantManager' => 'DialogParticipantManager',
346346
'DialogSource' => 'DialogSource',
347+
'DiffText' => 'DiffText',
347348
'DisallowedGiftsSettings' => 'DisallowedGiftsSettings',
348349
'documents_manager[_(-](?![.]get[(][)])|DocumentsManager' => 'DocumentsManager',
349350
'download_manager[_(-](?![.]get[(][)])|DownloadManager[^C]' => 'DownloadManager',

td/generate/scheme/td_api.tl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ textEntities entities:vector<textEntity> = TextEntities;
116116
//-Pre, Code, PreCode, and DateTime entities can't contain other entities. BlockQuote entities can't contain other BlockQuote entities. Bold, Italic, Underline, Strikethrough, and Spoiler entities can contain and can be part of any other entities. All other entities can't contain each other
117117
formattedText text:string entities:vector<textEntity> = FormattedText;
118118

119+
//@description Represents a change of a text @offset Offset of the entity, in UTF-16 code units @length Length of the entity, in UTF-16 code units @type Type of the entity
120+
diffEntity offset:int32 length:int32 type:DiffEntityType = DiffEntity;
121+
122+
//@description A text with some changes highlighted @text The text @entities Entities describing changes in the text. Entities doesn't mutually intersect with each other
123+
diffText text:string entities:vector<diffEntity> = DiffText;
124+
119125

120126
//@description Describes a style that can be used to compose a text @emoji Emoji corresponding to the style @name Name of the style
121127
textCompositionStyle emoji:string name:string = TextCompositionStyle;
@@ -5339,6 +5345,18 @@ textEntityTypeMediaTimestamp media_timestamp:int32 = TextEntityType;
53395345
textEntityTypeDateTime unix_time:int32 formatting_type:DateTimeFormattingType = TextEntityType;
53405346

53415347

5348+
//@class DiffEntityType @description Represents a change of a text
5349+
5350+
//@description Addition of some text
5351+
diffEntityTypeInsert = DiffEntityType;
5352+
5353+
//@description Change of some text @old_text The old text
5354+
diffEntityTypeReplace old_text:string = DiffEntityType;
5355+
5356+
//@description Removal of some text
5357+
diffEntityTypeDelete = DiffEntityType;
5358+
5359+
53425360
//@description A thumbnail to be sent along with a file; must be in JPEG or WEBP format for stickers, and less than 200 KB in size
53435361
//@thumbnail Thumbnail file to send. Sending thumbnails by file_id is currently not supported
53445362
//@width Thumbnail width, usually shouldn't exceed 320. Use 0 if unknown

td/telegram/DiffText.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
#include "td/telegram/DiffText.h"
8+
9+
#include "td/utils/algorithm.h"
10+
#include "td/utils/logging.h"
11+
#include "td/utils/misc.h"
12+
#include "td/utils/utf8.h"
13+
14+
namespace td {
15+
16+
td_api::object_ptr<td_api::DiffEntityType> DiffText::DiffEntity::get_diff_entity_type_object() const {
17+
switch (type_) {
18+
case Type::Insert:
19+
return td_api::make_object<td_api::diffEntityTypeInsert>();
20+
case Type::Replace:
21+
return td_api::make_object<td_api::diffEntityTypeReplace>(old_text_);
22+
case Type::Delete:
23+
return td_api::make_object<td_api::diffEntityTypeDelete>();
24+
default:
25+
UNREACHABLE();
26+
return nullptr;
27+
}
28+
}
29+
30+
td_api::object_ptr<td_api::diffEntity> DiffText::DiffEntity::get_diff_entity_object() const {
31+
return td_api::make_object<td_api::diffEntity>(offset_, length_, get_diff_entity_type_object());
32+
}
33+
34+
bool DiffText::check_entities() const {
35+
// entities must not intersect and must not begin and end in the middle of characters
36+
int32 text_len = narrow_cast<int32>(utf8_utf16_length(text_));
37+
int32 cur_pos = 0;
38+
for (auto &entity : entities_) {
39+
if (entity.offset_ < cur_pos) {
40+
return false;
41+
}
42+
if (entity.length_ < 0 || entity.length_ > text_len - entity.offset_) {
43+
return false;
44+
}
45+
cur_pos = entity.offset_ + entity.length_;
46+
}
47+
return true;
48+
}
49+
50+
DiffText::DiffText(telegram_api::object_ptr<telegram_api::textWithEntities> &&text_with_entities) {
51+
if (text_with_entities == nullptr) {
52+
LOG(ERROR) << "Receive no diff text";
53+
return;
54+
}
55+
56+
text_ = std::move(text_with_entities->text_);
57+
entities_.reserve(text_with_entities->entities_.size());
58+
for (auto &server_entity : text_with_entities->entities_) {
59+
switch (server_entity->get_id()) {
60+
case telegram_api::messageEntityUnknown::ID:
61+
break;
62+
case telegram_api::messageEntityDiffInsert::ID: {
63+
auto entity = static_cast<const telegram_api::messageEntityDiffInsert *>(server_entity.get());
64+
entities_.emplace_back(DiffEntity::Type::Insert, entity->offset_, entity->length_);
65+
break;
66+
}
67+
case telegram_api::messageEntityDiffReplace::ID: {
68+
auto entity = static_cast<const telegram_api::messageEntityDiffReplace *>(server_entity.get());
69+
entities_.emplace_back(DiffEntity::Type::Replace, entity->offset_, entity->length_, entity->old_text_);
70+
break;
71+
}
72+
case telegram_api::messageEntityDiffDelete::ID: {
73+
auto entity = static_cast<const telegram_api::messageEntityDiffDelete *>(server_entity.get());
74+
entities_.emplace_back(DiffEntity::Type::Delete, entity->offset_, entity->length_);
75+
break;
76+
}
77+
default:
78+
LOG(ERROR) << "Receive diff entity " << to_string(server_entity);
79+
}
80+
}
81+
82+
if (!check_entities()) {
83+
LOG(ERROR) << "Receive invalid diff entities in " << to_string(text_with_entities);
84+
entities_.clear();
85+
} else {
86+
td::remove_if(entities_, [](const auto &entity) { return entity.length_ <= 0; });
87+
}
88+
}
89+
90+
td_api::object_ptr<td_api::diffText> DiffText::get_diff_text_object() const {
91+
return td_api::make_object<td_api::diffText>(
92+
text_, transform(entities_, [](const DiffEntity &entity) { return entity.get_diff_entity_object(); }));
93+
}
94+
95+
} // namespace td

td/telegram/DiffText.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
#pragma once
8+
9+
#include "td/telegram/td_api.h"
10+
#include "td/telegram/telegram_api.h"
11+
12+
#include "td/utils/common.h"
13+
14+
namespace td {
15+
16+
class DiffText {
17+
class DiffEntity {
18+
public:
19+
enum class Type : int32 { Insert, Replace, Delete };
20+
Type type_ = Type::Insert;
21+
int32 offset_ = -1;
22+
int32 length_ = -1;
23+
string old_text_;
24+
25+
DiffEntity(Type type, int32 offset, int32 length, string old_text = "")
26+
: type_(type), offset_(offset), length_(length), old_text_(std::move(old_text)) {
27+
}
28+
29+
td_api::object_ptr<td_api::diffEntity> get_diff_entity_object() const;
30+
31+
private:
32+
td_api::object_ptr<td_api::DiffEntityType> get_diff_entity_type_object() const;
33+
};
34+
35+
string text_;
36+
vector<DiffEntity> entities_;
37+
38+
bool check_entities() const;
39+
40+
public:
41+
DiffText() = default;
42+
43+
explicit DiffText(telegram_api::object_ptr<telegram_api::textWithEntities> &&text_with_entities);
44+
45+
td_api::object_ptr<td_api::diffText> get_diff_text_object() const;
46+
};
47+
48+
} // namespace td

0 commit comments

Comments
 (0)