Skip to content

Commit e4b5604

Browse files
committed
emoji: Compute separator-plus-query just once per query
This is NFC except for performance: it saves us from having to construct this string again for each emoji name. At this stage there's a (much smaller) performance loss, too: we now compute this string even when we'll never actually use it because the query doesn't contain the separator. But we'll soon start checking for word-aligned matches even for those queries where we'd accept a non-word-aligned match; once we do, we'll need this string for all nonempty queries, for almost all names.
1 parent ab50a5d commit e4b5604

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lib/model/emoji.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,24 @@ class EmojiAutocompleteView extends AutocompleteView<EmojiAutocompleteQuery, Emo
348348
}
349349

350350
class EmojiAutocompleteQuery extends ComposeAutocompleteQuery {
351-
EmojiAutocompleteQuery(super.raw)
352-
: _adjusted = _adjustQuery(raw);
351+
factory EmojiAutocompleteQuery(String raw)
352+
=> EmojiAutocompleteQuery._(raw, _adjustQuery(raw));
353353

354+
EmojiAutocompleteQuery._(super.raw, String adjusted)
355+
: _adjusted = adjusted,
356+
_sepAdjusted = _separator + adjusted;
357+
358+
/// The query string as adjusted for comparing to emoji names,
359+
/// via [_adjustQuery].
354360
final String _adjusted;
355361

362+
/// The concatenation of [_separator] with [_adjusted].
363+
///
364+
/// Useful for finding word-aligned matches in an emoji name.
365+
final String _sepAdjusted;
366+
367+
static const _separator = '_';
368+
356369
static String _adjustQuery(String raw) {
357370
return raw.toLowerCase().replaceAll(' ', '_'); // TODO(#1067) remove diacritics too
358371
}
@@ -375,9 +388,8 @@ class EmojiAutocompleteQuery extends ComposeAutocompleteQuery {
375388
// Compare query_matches_string_in_order in Zulip web:shared/src/typeahead.ts .
376389
bool _nameMatches(String emojiName) {
377390
// TODO(#1067) this assumes emojiName is already lower-case (and no diacritics)
378-
const String separator = '_';
379391

380-
if (!_adjusted.contains(separator)) {
392+
if (!_adjusted.contains(_separator)) {
381393
// If the query is a single token (doesn't contain a separator),
382394
// the match can be anywhere in the string.
383395
return emojiName.contains(_adjusted);
@@ -388,7 +400,7 @@ class EmojiAutocompleteQuery extends ComposeAutocompleteQuery {
388400
// (E.g. for 'ab_cd_ef', query could be 'ab_c' or 'cd_ef',
389401
// but not 'b_cd_ef'.)
390402
return emojiName.startsWith(_adjusted)
391-
|| emojiName.contains(separator + _adjusted);
403+
|| emojiName.contains(_sepAdjusted);
392404
}
393405

394406
@override

0 commit comments

Comments
 (0)