2016-04-14 11:00:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-14 11:00:23 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-14 11:00:23 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
enum EntityInTextType {
|
2016-04-29 12:00:48 +00:00
|
|
|
EntityInTextInvalid = 0,
|
|
|
|
|
2016-04-14 11:00:23 +00:00
|
|
|
EntityInTextUrl,
|
|
|
|
EntityInTextCustomUrl,
|
|
|
|
EntityInTextEmail,
|
|
|
|
EntityInTextHashtag,
|
2018-02-06 07:24:55 +00:00
|
|
|
EntityInTextCashtag,
|
2016-04-14 11:00:23 +00:00
|
|
|
EntityInTextMention,
|
2016-04-29 12:00:48 +00:00
|
|
|
EntityInTextMentionName,
|
2016-04-14 11:00:23 +00:00
|
|
|
EntityInTextBotCommand,
|
|
|
|
|
|
|
|
EntityInTextBold,
|
|
|
|
EntityInTextItalic,
|
|
|
|
EntityInTextCode, // inline
|
|
|
|
EntityInTextPre, // block
|
|
|
|
};
|
2016-04-29 12:00:48 +00:00
|
|
|
|
|
|
|
class EntityInText;
|
|
|
|
using EntitiesInText = QList<EntityInText>;
|
|
|
|
|
|
|
|
class EntityInText {
|
|
|
|
public:
|
|
|
|
EntityInText(EntityInTextType type, int offset, int length, const QString &data = QString())
|
|
|
|
: _type(type)
|
|
|
|
, _offset(offset)
|
|
|
|
, _length(length)
|
|
|
|
, _data(data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityInTextType type() const {
|
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
int offset() const {
|
|
|
|
return _offset;
|
|
|
|
}
|
|
|
|
int length() const {
|
|
|
|
return _length;
|
|
|
|
}
|
|
|
|
QString data() const {
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void extendToLeft(int extent) {
|
|
|
|
_offset -= extent;
|
|
|
|
_length += extent;
|
2016-04-14 11:00:23 +00:00
|
|
|
}
|
2016-04-29 12:00:48 +00:00
|
|
|
void shrinkFromRight(int shrink) {
|
|
|
|
_length -= shrink;
|
|
|
|
}
|
|
|
|
void shiftLeft(int shift) {
|
|
|
|
_offset -= shift;
|
|
|
|
if (_offset < 0) {
|
|
|
|
_length += _offset;
|
|
|
|
_offset = 0;
|
|
|
|
if (_length < 0) {
|
|
|
|
_length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 17:33:48 +00:00
|
|
|
void shiftRight(int shift) {
|
|
|
|
_offset += shift;
|
|
|
|
}
|
2016-04-29 12:00:48 +00:00
|
|
|
void updateTextEnd(int textEnd) {
|
|
|
|
if (_offset > textEnd) {
|
|
|
|
_offset = textEnd;
|
|
|
|
_length = 0;
|
|
|
|
} else if (_offset + _length > textEnd) {
|
|
|
|
_length = textEnd - _offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int firstMonospaceOffset(const EntitiesInText &entities, int textLength) {
|
|
|
|
int result = textLength;
|
|
|
|
for_const (auto &entity, entities) {
|
|
|
|
if (entity.type() == EntityInTextPre || entity.type() == EntityInTextCode) {
|
|
|
|
accumulate_min(result, entity.offset());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const {
|
|
|
|
return type() != EntityInTextInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EntityInTextType _type;
|
|
|
|
int _offset, _length;
|
|
|
|
QString _data;
|
|
|
|
|
2016-04-14 11:00:23 +00:00
|
|
|
};
|
2016-05-06 17:33:48 +00:00
|
|
|
|
|
|
|
struct TextWithEntities {
|
|
|
|
QString text;
|
|
|
|
EntitiesInText entities;
|
2017-07-06 13:44:11 +00:00
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return text.isEmpty();
|
|
|
|
}
|
2016-05-06 17:33:48 +00:00
|
|
|
};
|
2016-04-14 11:00:23 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
TextParseMultiline = 0x001,
|
|
|
|
TextParseLinks = 0x002,
|
|
|
|
TextParseRichText = 0x004,
|
|
|
|
TextParseMentions = 0x008,
|
|
|
|
TextParseHashtags = 0x010,
|
|
|
|
TextParseBotCommands = 0x020,
|
2017-07-06 13:44:11 +00:00
|
|
|
TextParseMarkdown = 0x040,
|
2016-04-14 11:00:23 +00:00
|
|
|
|
|
|
|
TextTwitterMentions = 0x100,
|
|
|
|
TextTwitterHashtags = 0x200,
|
|
|
|
TextInstagramMentions = 0x400,
|
|
|
|
TextInstagramHashtags = 0x800,
|
|
|
|
};
|
|
|
|
|
2018-01-13 12:45:11 +00:00
|
|
|
struct TextWithTags {
|
|
|
|
struct Tag {
|
2018-05-31 18:28:37 +00:00
|
|
|
int offset = 0;
|
|
|
|
int length = 0;
|
2018-01-13 12:45:11 +00:00
|
|
|
QString id;
|
|
|
|
};
|
|
|
|
using Tags = QVector<Tag>;
|
|
|
|
|
|
|
|
QString text;
|
|
|
|
Tags tags;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const TextWithTags::Tag &a, const TextWithTags::Tag &b) {
|
|
|
|
return (a.offset == b.offset) && (a.length == b.length) && (a.id == b.id);
|
|
|
|
}
|
|
|
|
inline bool operator!=(const TextWithTags::Tag &a, const TextWithTags::Tag &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const TextWithTags &a, const TextWithTags &b) {
|
|
|
|
return (a.text == b.text) && (a.tags == b.tags);
|
|
|
|
}
|
|
|
|
inline bool operator!=(const TextWithTags &a, const TextWithTags &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
// Parsing helpers.
|
|
|
|
|
|
|
|
namespace TextUtilities {
|
|
|
|
|
|
|
|
bool IsValidProtocol(const QString &protocol);
|
|
|
|
bool IsValidTopDomain(const QString &domain);
|
|
|
|
|
|
|
|
const QRegularExpression &RegExpDomain();
|
|
|
|
const QRegularExpression &RegExpDomainExplicit();
|
|
|
|
const QRegularExpression &RegExpMailNameAtEnd();
|
|
|
|
const QRegularExpression &RegExpHashtag();
|
2018-03-06 15:29:45 +00:00
|
|
|
const QRegularExpression &RegExpHashtagExclude();
|
2017-07-06 11:37:42 +00:00
|
|
|
const QRegularExpression &RegExpMention();
|
|
|
|
const QRegularExpression &RegExpBotCommand();
|
2018-05-22 19:09:13 +00:00
|
|
|
QString MarkdownBoldGoodBefore();
|
|
|
|
QString MarkdownBoldBadAfter();
|
|
|
|
QString MarkdownItalicGoodBefore();
|
|
|
|
QString MarkdownItalicBadAfter();
|
|
|
|
QString MarkdownCodeGoodBefore();
|
|
|
|
QString MarkdownCodeBadAfter();
|
|
|
|
QString MarkdownPreGoodBefore();
|
|
|
|
QString MarkdownPreBadAfter();
|
2017-07-06 11:37:42 +00:00
|
|
|
|
|
|
|
inline void Append(TextWithEntities &to, TextWithEntities &&append) {
|
|
|
|
auto entitiesShiftRight = to.text.size();
|
|
|
|
for (auto &entity : append.entities) {
|
|
|
|
entity.shiftRight(entitiesShiftRight);
|
|
|
|
}
|
|
|
|
to.text += append.text;
|
|
|
|
to.entities += append.entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Text preprocess.
|
|
|
|
QString Clean(const QString &text);
|
|
|
|
QString EscapeForRichParsing(const QString &text);
|
|
|
|
QString SingleLine(const QString &text);
|
|
|
|
QString RemoveAccents(const QString &text);
|
|
|
|
QStringList PrepareSearchWords(const QString &query, const QRegularExpression *SplitterOverride = nullptr);
|
|
|
|
bool CutPart(TextWithEntities &sending, TextWithEntities &left, int limit);
|
|
|
|
|
|
|
|
struct MentionNameFields {
|
|
|
|
MentionNameFields(int32 userId = 0, uint64 accessHash = 0) : userId(userId), accessHash(accessHash) {
|
|
|
|
}
|
|
|
|
int32 userId = 0;
|
|
|
|
uint64 accessHash = 0;
|
|
|
|
};
|
|
|
|
inline MentionNameFields MentionNameDataToFields(const QString &data) {
|
2016-04-30 17:04:14 +00:00
|
|
|
auto components = data.split('.');
|
|
|
|
if (!components.isEmpty()) {
|
2017-07-06 11:37:42 +00:00
|
|
|
return { components.at(0).toInt(), (components.size() > 1) ? components.at(1).toULongLong() : 0 };
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
2017-07-06 11:37:42 +00:00
|
|
|
return MentionNameFields {};
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
inline QString MentionNameDataFromFields(const MentionNameFields &fields) {
|
|
|
|
auto result = QString::number(fields.userId);
|
|
|
|
if (fields.accessHash) {
|
|
|
|
result += '.' + QString::number(fields.accessHash);
|
|
|
|
}
|
|
|
|
return result;
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
EntitiesInText EntitiesFromMTP(const QVector<MTPMessageEntity> &entities);
|
|
|
|
enum class ConvertOption {
|
|
|
|
WithLocal,
|
|
|
|
SkipLocal,
|
|
|
|
};
|
2017-07-06 13:44:11 +00:00
|
|
|
MTPVector<MTPMessageEntity> EntitiesToMTP(const EntitiesInText &entities, ConvertOption option = ConvertOption::WithLocal);
|
2016-04-14 11:00:23 +00:00
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
// New entities are added to the ones that are already in result.
|
2017-07-06 13:44:11 +00:00
|
|
|
// Changes text if (flags & TextParseMarkdown).
|
2018-01-26 15:40:11 +00:00
|
|
|
TextWithEntities ParseEntities(const QString &text, int32 flags);
|
2017-07-06 11:37:42 +00:00
|
|
|
void ParseEntities(TextWithEntities &result, int32 flags, bool rich = false);
|
2016-04-14 11:00:23 +00:00
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
void PrepareForSending(TextWithEntities &result, int32 flags);
|
|
|
|
void Trim(TextWithEntities &result);
|
2016-04-14 11:00:23 +00:00
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
enum class PrepareTextOption {
|
|
|
|
IgnoreLinks,
|
|
|
|
CheckLinks,
|
|
|
|
};
|
|
|
|
inline QString PrepareForSending(const QString &text, PrepareTextOption option = PrepareTextOption::IgnoreLinks) {
|
|
|
|
auto result = TextWithEntities { text };
|
|
|
|
auto prepareFlags = (option == PrepareTextOption::CheckLinks) ? (TextParseLinks | TextParseMentions | TextParseHashtags | TextParseBotCommands) : 0;
|
|
|
|
PrepareForSending(result, prepareFlags);
|
|
|
|
return result.text;
|
2016-04-14 11:00:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
// Replace bad symbols with space and remove '\r'.
|
|
|
|
void ApplyServerCleaning(TextWithEntities &result);
|
|
|
|
|
2018-05-20 17:42:30 +00:00
|
|
|
QByteArray SerializeTags(const TextWithTags::Tags &tags);
|
|
|
|
TextWithTags::Tags DeserializeTags(QByteArray data, int textLength);
|
|
|
|
QString TagsMimeType();
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
} // namespace TextUtilities
|
2017-06-20 16:03:18 +00:00
|
|
|
|
|
|
|
namespace Lang {
|
|
|
|
|
|
|
|
template <typename ResultString>
|
|
|
|
struct StartReplacements;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct StartReplacements<TextWithEntities> {
|
|
|
|
static inline TextWithEntities Call(QString &&langString) {
|
|
|
|
return { std::move(langString), EntitiesInText() };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ResultString>
|
|
|
|
struct ReplaceTag;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ReplaceTag<TextWithEntities> {
|
|
|
|
static TextWithEntities Call(TextWithEntities &&original, ushort tag, const TextWithEntities &replacement);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
} // namespace Lang
|