diff options
Diffstat (limited to 'src/main/java/de/pixart/messenger/utils')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/Emoticons.java | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/Emoticons.java b/src/main/java/de/pixart/messenger/utils/Emoticons.java index 8cfddf8a4..08ee9db99 100644 --- a/src/main/java/de/pixart/messenger/utils/Emoticons.java +++ b/src/main/java/de/pixart/messenger/utils/Emoticons.java @@ -29,8 +29,11 @@ package de.pixart.messenger.utils; +import android.util.LruCache; + import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.regex.Pattern; @@ -70,12 +73,16 @@ public class Emoticons { ENCLOSED_ALPHANUMERIC_SUPPLEMENT, ENCLOSED_IDEOGRAPHIC_SUPPLEMENT, MISC_TECHNICAL); + + private static final int MAX_EMOIJS = 42; private static final int ZWJ = 0x200D; private static final int VARIATION_16 = 0xFE0F; private static final int COMBINING_ENCLOSING_KEYCAP = 0x20E3; private static final int BLACK_FLAG = 0x1F3F4; private static final UnicodeRange FITZPATRICK = new UnicodeRange(0x1F3FB, 0x1F3FF); + private static final LruCache<CharSequence,Pattern> CACHE = new LruCache<>(256); + private static List<Symbol> parse(String input) { List<Symbol> symbols = new ArrayList<>(); Builder builder = new Builder(); @@ -98,16 +105,33 @@ public class Emoticons { return symbols; } - public static Pattern generatePattern(CharSequence input) { - final StringBuilder pattern = new StringBuilder(); + public static Pattern getEmojiPattern(CharSequence input) { + Pattern pattern = CACHE.get(input); + if (pattern == null) { + pattern = generatePattern(input); + CACHE.put(input, pattern); + } + return pattern; + } + + private static Pattern generatePattern(CharSequence input) { + final HashSet<String> emojis = new HashSet<>(); + int i = 0; for (Symbol symbol : parse(input.toString())) { if (symbol instanceof Emoji) { - if (pattern.length() != 0) { - pattern.append('|'); + emojis.add(symbol.toString()); + if (++i >= MAX_EMOIJS) { + return Pattern.compile(""); } - pattern.append(Pattern.quote(symbol.toString())); } } + final StringBuilder pattern = new StringBuilder(); + for (String emoji : emojis) { + if (pattern.length() != 0) { + pattern.append('|'); + } + pattern.append(Pattern.quote(emoji)); + } return Pattern.compile(pattern.toString()); } |