| 6 |
| it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 |
| 10 | It is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 |
| 15 | In addition, as a special exception, the copyright holders give permission |
| 16 | to link the code of portions of this program with the OpenSSL library. |
| 17 |
| 18 | Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE |
| 19 | Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org |
| 20 | */ |
| 21 |
| 22 | #include "emoji_suggestions.h" |
| 23 |
| 24 | #include <jni.h> |
| 25 | #include <algorithm> |
| 26 | #include "emoji_suggestions_data.h" |
| 27 |
| 28 | checksum Crc32Table[256]; |
| 29 | class Crc32Initializer { |
| 30 | public: |
| 31 | Crc32Initializer() { |
| 32 | checksum poly = 0x04C11DB7U; |
| 33 | for (auto i = 0; i != 256; ++i) { |
| 34 | Crc32Table[i] = reflect(i, 8) << 24; |
| 35 | for (auto j = 0; j != 8; ++j) { |
| 36 | Crc32Table[i] = (Crc32Table[i] << 1) ^ (Crc32Table[i] & (1 << 31) ? poly : 0); |
| 37 | } |
| 38 | Crc32Table[i] = reflect(Crc32Table[i], 32); |
| 39 | } |
| 40 | } |
| 41 |
| 42 | private: |
| 43 | checksum reflect(checksum val, char ch) { |
| 44 | checksum result = 0; |
| 45 | for (int i = 1; i < (ch + 1); ++i) { |
| 46 | if (val & 1) { |
| 47 | result |= 1 << (ch - i); |
| 48 | } |
| 49 | val >>= 1; |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 |
| 54 | }; |
| 55 |
| 56 | checksum countChecksum(const void *data, std::size_t size) { |
| 57 | static Crc32Initializer InitTable; |
| 58 |
| 59 | auto buffer = static_cast<const unsigned char*>(data); |
| 60 | auto result = checksum(0xFFFFFFFFU); |
| 61 | for (auto i = std::size_t(0); i != size; ++i) { |
| 62 | result = (result >> 8) ^ Crc32Table[(result & 0xFFU) ^ buffer[i]]; |
| 63 | } |
| 64 | return (result ^ 0xFFFFFFFFU); |
| 65 | } |
| 66 |
| 67 | class string_span { |
| 68 | public: |
| 69 | string_span() = default; |
| 70 | string_span(const utf16string *data, std::size_t size) : begin_(data), size_(size) { |
| 71 | } |
| 72 | string_span(const std::vector<utf16string> &data) : begin_(data.data()), size_(data.size()) { |
| 73 | } |
| 74 | string_span(const string_span &other) = default; |
| 75 | string_span &operator=(const string_span &other) = default; |
| 76 |
| 77 | const utf16string *begin() const { |
| 78 | return begin_; |
| 79 | } |
| 80 | const utf16string *end() const { |
| 81 | return begin_ + size_; |
| 82 | } |
| 83 | std::size_t size() const { |
| 84 | return size_; |
| 85 | } |
| 86 |
| 87 | string_span subspan(std::size_t offset, std::size_t size) { |
| 88 | return string_span(begin_ + offset, size); |
| 89 | } |
| 90 |
| 91 | private: |
| 92 | const utf16string *begin_ = nullptr; |
| 93 | std::size_t size_ = 0; |
| 94 |
| 95 | }; |
| 96 |
| 97 | bool IsNumber(utf16char ch) { |
| 98 | return (ch >= '0' && ch <= '9'); |
| 99 | } |
| 100 |
| 101 | bool IsLetterOrNumber(utf16char ch) { |
| 102 | return (ch >= 'a' && ch <= 'z') || IsNumber(ch); |
| 103 | } |
| 104 |
| 105 | using Replacement = Replacement; |
| 106 |
| 107 | class Completer { |
| 108 | public: |
| 109 | Completer(utf16string query); |
| 110 |
| 111 | std::vector<Suggestion> resolve(); |
| 112 |
| 113 | private: |
| 114 | struct Result { |
| 115 | const Replacement *replacement; |
| 116 | int wordsUsed; |
| 117 | }; |
| 118 |
| 119 | static std::vector<utf16char> NormalizeQuery(utf16string query); |
| 120 | void addResult(const Replacement *replacement); |
| 121 | bool isDuplicateOfLastResult(const Replacement *replacement) const; |
| 122 | bool isBetterThanLastResult(const Replacement *replacement) const; |
| 123 | void processInitialList(); |
| 124 | void filterInitialList(); |
| 125 | void initWordsTracking(); |
| 126 | bool matchQueryForCurrentItem(); |
| 127 | bool matchQueryTailStartingFrom(int position); |
| 128 | string_span findWordsStartingWith(utf16char ch); |
| 129 | int findEqualCharsCount(int position, const utf16string *word); |
| 130 | std::vector<Suggestion> prepareResult(); |
| 131 | bool startsWithQuery(utf16string word); |
| 132 | bool isExactMatch(utf16string replacement); |
| 133 |
| 134 | std::vector<Result> _result; |
| 135 |
| 136 | utf16string _initialQuery; |
| 137 | const std::vector<utf16char> _query; |
| 138 | const utf16char *_queryBegin = nullptr; |
| 139 | int _querySize = 0; |
| 140 |
| 141 | const std::vector<const Replacement*> *_initialList = nullptr; |
| 142 |
| 143 | string_span _currentItemWords; |
| 144 | int _currentItemWordsUsedCount = 0; |
| 145 |
| 146 | class UsedWordGuard { |
| 147 | public: |
| 148 | UsedWordGuard(std::vector<small> &map, int index); |
| 149 | UsedWordGuard(const UsedWordGuard &other) = delete; |
| 150 | UsedWordGuard(UsedWordGuard &&other); |
| 151 | UsedWordGuard &operator=(const UsedWordGuard &other) = delete; |
| 152 | UsedWordGuard &operator=(UsedWordGuard &&other) = delete; |
| 153 | explicit operator bool() const; |
| 154 | ~UsedWordGuard(); |
| 155 |
| 156 | private: |
| 157 | std::vector<small> &_map; |
| 158 | int _index = 0; |
| 159 | bool _guarded = false; |
| 160 |
| 161 | }; |
| 162 | std::vector<small> _currentItemWordsUsedMap; |
| 163 |
| 164 | }; |
| 165 |
| 166 | Completer::UsedWordGuard::UsedWordGuard(std::vector<small> &map, int index) : _map(map), _index(index) { |
| 167 | if (!_map[_index]) { |
| 168 | _guarded = _map[_index] = 1; |
| 169 | } |
| 170 | } |
| 171 |
| 172 | Completer::UsedWordGuard::UsedWordGuard(UsedWordGuard &&other) : _map(other._map), _index(other._index), _guarded(other._guarded) { |
| 173 | other._guarded = 0; |
| 174 | } |
| 175 |
| 176 | Completer::UsedWordGuard::operator bool() const { |
| 177 | return _guarded; |
| 178 | } |
| 179 |
| 180 | Completer::UsedWordGuard::~UsedWordGuard() { |
| 181 | if (_guarded) { |
| 182 | _map[_index] = 0; |
| 183 | } |
| 184 | } |
| 185 |
| 186 | Completer::Completer(utf16string query) : _initialQuery(query), _query(NormalizeQuery(query)) { |
| 187 | } |
| 188 |
| 189 | // Remove all non-letters-or-numbers. |
| 190 | // Leave '-' and '+' only if they're followed by a number or |
| 191 | // at the end of the query (so it is possibly followed by a number). |
| 192 | std::vector<utf16char> Completer::NormalizeQuery(utf16string query) { |
| 193 | auto result = std::vector<utf16char>(); |
| 194 | result.reserve(query.size()); |
| 195 | auto copyFrom = query.data(); |
| 196 | auto e = copyFrom + query.size(); |
| 197 | auto copyTo = result.data(); |
| 198 | for (auto i = query.data(); i != e; ++i) { |
| 199 | if (IsLetterOrNumber(*i)) { |
| 200 | continue; |
| 201 | } else if (*i == '-' || *i == '+') { |
| 202 | if (i + 1 == e || IsNumber(*(i + 1))) { |
| 203 | continue; |
| 204 | } |
| 205 | } |
| 206 | if (i > copyFrom) { |
| 207 | result.resize(result.size() + (i - copyFrom)); |
| 208 | memcpy(copyTo, copyFrom, (i - copyFrom) * sizeof(utf16char)); |
| 209 | copyTo += (i - copyFrom); |
| 210 | } |
| 211 | copyFrom = i + 1; |
| 212 | } |
| 213 | if (e > copyFrom) { |
| 214 | result.resize(result.size() + (e - copyFrom)); |
| 215 | memcpy(copyTo, copyFrom, (e - copyFrom) * sizeof(utf16char)); |
| 216 | copyTo += (e - copyFrom); |
| 217 | } |
| 218 | return result; |
| 219 | } |
| 220 |
| 221 | std::vector<Suggestion> Completer::resolve() { |
| 222 | _queryBegin = _query.data(); |
| 223 | _querySize = _query.size(); |
| 224 | if (!_querySize) { |
| 225 | return std::vector<Suggestion>(); |
| 226 | } |
| 227 | _initialList = GetReplacements(*_queryBegin); |
| 228 | if (!_initialList) { |
| 229 | return std::vector<Suggestion>(); |
| 230 | } |
| 231 | _result.reserve(_initialList->size()); |
| 232 | processInitialList(); |
| 233 | return prepareResult(); |
| 234 | } |
| 235 |
| 236 | bool Completer::isDuplicateOfLastResult(const Replacement *item) const { |
| 237 | if (_result.empty()) { |
| 238 | return false; |
| 239 | } |
| 240 | return (_result.back().replacement->emoji == item->emoji); |
| 241 | } |
| 242 |
| 243 | bool Completer::isBetterThanLastResult(const Replacement *item) const { |
| 244 | auto &last = _result.back(); |
| 245 | if (_currentItemWordsUsedCount < last.wordsUsed) { |
| 246 | return true; |
| 247 | } |
| 248 |
| 249 | auto firstCharOfQuery = _query[0]; |
| 250 | auto firstCharAfterColonLast = last.replacement->replacement[1]; |
| 251 | auto firstCharAfterColonCurrent = item->replacement[1]; |
| 252 | auto goodLast = (firstCharAfterColonLast == firstCharOfQuery); |
| 253 | auto goodCurrent = (firstCharAfterColonCurrent == firstCharOfQuery); |
| 254 | return !goodLast && goodCurrent; |
| 255 | } |
| 256 |
| 257 | void Completer::addResult(const Replacement *item) { |
| 258 | if (!isDuplicateOfLastResult(item)) { |
| 259 | _result.push_back({ item, _currentItemWordsUsedCount }); |
| 260 | } else if (isBetterThanLastResult(item)) { |
| 261 | _result.back() = { item, _currentItemWordsUsedCount }; |
| 262 | } |
| 263 | } |
| 264 |
| 265 | void Completer::processInitialList() { |
| 266 | if (_querySize > 1) { |
| 267 | filterInitialList(); |
| 268 | } else { |
| 269 | _currentItemWordsUsedCount = 1; |
| 270 | for (auto item : *_initialList) { |
| 271 | addResult(item); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 |
| 276 | void Completer::initWordsTracking() { |
| 277 | auto maxWordsCount = 0; |
| 278 | for (auto item : *_initialList) { |
| 279 | auto wordsCount = item->words.size(); |
| 280 | if (maxWordsCount < wordsCount) { |
| 281 | maxWordsCount = wordsCount; |
| 282 | } |
| 283 | } |
| 284 | _currentItemWordsUsedMap = std::vector<small>(maxWordsCount, 0); |
| 285 | } |
| 286 |
| 287 | void Completer::filterInitialList() { |
| 288 | initWordsTracking(); |
| 289 | for (auto item : *_initialList) { |
| 290 | _currentItemWords = string_span(item->words); |
| 291 | _currentItemWordsUsedCount = 1; |
| 292 | if (matchQueryForCurrentItem()) { |
| 293 | addResult(item); |
| 294 | } |
| 295 | _currentItemWordsUsedCount = 0; |
| 296 | } |
| 297 | } |
| 298 |
| 299 | bool Completer::matchQueryForCurrentItem() { |
| 300 | if (_currentItemWords.size() < 2) { |
| 301 | return startsWithQuery(*_currentItemWords.begin()); |
| 302 | } |
| 303 | return matchQueryTailStartingFrom(0); |
| 304 | } |
| 305 |
| 306 | bool Completer::startsWithQuery(utf16string word) { |
| 307 | if (word.size() < _query.size()) { |
| 308 | return false; |
| 309 | } |
| 310 | for (auto i = std::size_t(0), size = _query.size(); i != size; ++i) { |
| 311 | if (word[i] != _query[i]) { |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 |
| 318 | bool Completer::isExactMatch(utf16string replacement) { |
| 319 | if (replacement.size() != _initialQuery.size() + 1) { |
| 320 | return false; |
| 321 | } |
| 322 | for (auto i = std::size_t(0), size = _initialQuery.size(); i != size; ++i) { |
| 323 | if (replacement[i] != _initialQuery[i]) { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 |
| 330 | bool Completer::matchQueryTailStartingFrom(int position) { |
| 331 | auto charsLeftToMatch = (_querySize - position); |
| 332 | if (!charsLeftToMatch) { |
| 333 | return true; |
| 334 | } |
| 335 |
| 336 | auto firstCharToMatch = *(_queryBegin + position); |
| 337 | auto foundWords = findWordsStartingWith(firstCharToMatch); |
| 338 |
| 339 | for (auto word = foundWords.begin(), foundWordsEnd = word + foundWords.size(); word != foundWordsEnd; ++word) { |
| 340 | auto wordIndex = word - _currentItemWords.begin(); |
| 341 | if (auto guard = UsedWordGuard(_currentItemWordsUsedMap, wordIndex)) { |
| 342 | ++_currentItemWordsUsedCount; |
| 343 | auto equalCharsCount = findEqualCharsCount(position, word); |
| 344 | for (auto check = equalCharsCount; check != 0; --check) { |
| 345 | if (matchQueryTailStartingFrom(position + check)) { |
| 346 | return true; |
| 347 | } |
| 348 | } |
| 349 | --_currentItemWordsUsedCount; |
| 350 | } |
| 351 | } |
| 352 | return false; |
| 353 | } |
| 354 |
| 355 | int Completer::findEqualCharsCount(int position, const utf16string *word) { |
| 356 | auto charsLeft = (_querySize - position); |
| 357 | auto wordBegin = word->data(); |
| 358 | auto wordSize = word->size(); |
| 359 | auto possibleEqualCharsCount = (charsLeft > wordSize ? wordSize : charsLeft); |
| 360 | for (auto equalTill = 1; equalTill != possibleEqualCharsCount; ++equalTill) { |
| 361 | auto wordCh = *(wordBegin + equalTill); |
| 362 | auto queryCh = *(_queryBegin + position + equalTill); |
| 363 | if (wordCh != queryCh) { |
| 364 | return equalTill; |
| 365 | } |
| 366 | } |
| 367 | return possibleEqualCharsCount; |
| 368 | } |
| 369 |
| 370 | std::vector<Suggestion> Completer::prepareResult() { |
| 371 | auto firstCharOfQuery = _query[0]; |
| 372 | std::stable_partition(_result.begin(), _result.end(), [firstCharOfQuery](Result &result) { |
| 373 | auto firstCharAfterColon = result.replacement->replacement[1]; |
| 374 | return (firstCharAfterColon == firstCharOfQuery); |
| 375 | }); |
| 376 | std::stable_partition(_result.begin(), _result.end(), [](Result &result) { |
| 377 | return (result.wordsUsed < 2); |
| 378 | }); |
| 379 | std::stable_partition(_result.begin(), _result.end(), [](Result &result) { |
| 380 | return (result.wordsUsed < 3); |
| 381 | }); |
| 382 | std::stable_partition(_result.begin(), _result.end(), [this](Result &result) { |
| 383 | return isExactMatch(result.replacement->replacement); |
| 384 | }); |
| 385 |
| 386 | auto result = std::vector<Suggestion>(); |
| 387 | result.reserve(_result.size()); |
| 388 | for (auto &item : _result) { |
| 389 | result.emplace_back(item.replacement->emoji, item.replacement->replacement, item.replacement->replacement); |
| 390 | } |
| 391 | return result; |
| 392 | } |
| 393 |
| 394 | string_span Completer::findWordsStartingWith(utf16char ch) { |
| 395 | auto begin = std::lower_bound(_currentItemWords.begin(), _currentItemWords.end(), ch, [](utf16string word, utf16char ch) { |
| 396 | return word[0] < ch; |
| 397 | }); |
| 398 | auto end = std::upper_bound(_currentItemWords.begin(), _currentItemWords.end(), ch, [](utf16char ch, utf16string word) { |
| 399 | return ch < word[0]; |
| 400 | }); |
| 401 | return _currentItemWords.subspan(begin - _currentItemWords.begin(), end - begin); |
| 402 | } |
| 403 |
| 404 | std::vector<Suggestion> GetSuggestions(utf16string query) { |
| 405 | return Completer(query).resolve(); |
| 406 | } |
| 407 |
| 408 | int GetSuggestionMaxLength() { |
| 409 | return kReplacementMaxLength; |
| 410 | } |
| 411 |
| 412 | jclass jclass_Suggestion = nullptr; |
| 413 | jmethodID jclass_Suggestion_constructor; |
| 414 |
| 415 | extern "C" { |
| 416 |
| 417 | jobjectArray |
| 418 | Java_org_telegram_messenger_Emoji_getSuggestion(JNIEnv *env, jobject object, jstring query) { |
| 419 | const jchar *raw = env->GetStringChars(query, 0); |
| 420 | jsize len = env->GetStringLength(query); |
| 421 | std::vector<Suggestion> suggestions = GetSuggestions(utf16string(raw, len)); |
| 422 | env->ReleaseStringChars(query, raw); |
| 423 |
| 424 | if (suggestions.empty()) { |
| 425 | return nullptr; |
| 426 | } |
| 427 |
| 428 | if (jclass_Suggestion == nullptr) { |
| 429 | jclass_Suggestion = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/messenger/EmojiSuggestion")); |
| 430 | jclass_Suggestion_constructor = env->GetMethodID(jclass_Suggestion, "<init>", |
| 431 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 432 | } |
| 433 |
| 434 | jobjectArray result = env->NewObjectArray(suggestions.size(), jclass_Suggestion, nullptr); |
| 435 | size_t size = suggestions.size(); |
| 436 | for (size_t a = 0; a < size; a++) { |
| 437 | Suggestion &suggestion = suggestions[a]; |
| 438 | utf16string emoji = suggestion.emoji(); |
| 439 | utf16string label = suggestion.label(); |
| 440 | jstring emojiStr = env->NewString(emoji.data(), emoji.size()); |
| 441 | jstring labelStr = env->NewString(label.data(), label.size()); |
| 442 | object = env->NewObject(jclass_Suggestion, jclass_Suggestion_constructor, emojiStr, labelStr); |
| 443 | env->SetObjectArrayElement(result, a, object); |
| 444 | env->DeleteLocalRef(object); |
| 445 | env->DeleteLocalRef(emojiStr); |
| 446 | env->DeleteLocalRef(labelStr); |
| 447 | } |
| 448 |
| 449 | return result; |
| 450 | } |
| 451 |
| 452 | } |
| 453 |