diff options
author | Daniel Gultsch <daniel@gultsch.de> | 2014-09-04 10:16:28 +0200 |
---|---|---|
committer | Daniel Gultsch <daniel@gultsch.de> | 2014-09-04 10:16:28 +0200 |
commit | 50f90d37b8a873a523da9bfcafdd3984451f9549 (patch) | |
tree | 817f0030df7c8676c80aaf93a2376c0a7c79f3a6 /src | |
parent | 6b9219c5f4b81172edafae31b705fec3ef56b3f6 (diff) | |
parent | 0a2c73f2469fe359162b5c973f86ea2e409272ac (diff) |
Merge pull request #417 from emdete/unicode_emoticons
improve regex, implement pattern cache
Diffstat (limited to 'src')
-rw-r--r-- | src/eu/siacs/conversations/utils/UIHelper.java | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 41724596..896bdb7d 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -544,24 +544,40 @@ public class UIHelper { return getContactPicture(account.getJid(), size, context, false); } } + + private final static class EmoticonPattern { + Pattern pattern; + String replacement; + EmoticonPattern(String ascii, int unicode) { + this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii + "(?=(\\s|$))"); + this.replacement = new String(new int[]{unicode, }, 0, 1); + } + String replaceAll(String body) { + return pattern.matcher(body).replaceAll(replacement); + } + } + + private static final EmoticonPattern[] patterns = new EmoticonPattern[] { + new EmoticonPattern(":-?D", 0x1f600), + new EmoticonPattern("\\^\\^", 0x1f601), + new EmoticonPattern(":'D", 0x1f602), + new EmoticonPattern("\\]-?D", 0x1f608), + new EmoticonPattern(";-?\\)", 0x1f609), + new EmoticonPattern(":-?\\)", 0x1f60a), + new EmoticonPattern("[B8]-?\\)", 0x1f60e), + new EmoticonPattern(":-?\\|", 0x1f610), + new EmoticonPattern(":-?[/\\\\]", 0x1f615), + new EmoticonPattern(":-?\\*", 0x1f617), + new EmoticonPattern(":-?[Ppb]", 0x1f61b), + new EmoticonPattern(":-?\\(", 0x1f61e), + new EmoticonPattern(":-?[0Oo]", 0x1f62e), + new EmoticonPattern("\\\\o/", 0x1F631), + }; + public static String transformAsciiEmoticons(String body) { if (body != null) { - for (String[] r: new String[][]{ // see https://de.wikipedia.org/wiki/Unicodeblock_Smileys - {":-?\\)", " 😀 ", }, - {";-?\\)", " 😉 ", }, - {":-?D", " 😃 ", }, - {":-?[Ppb]", " 😋 ", }, - {"8-?\\)", " 😎 ", }, - {":-?\\|", " 😐 ", }, - {":-?[/\\\\]", " 😕 ", }, - {":-?\\*", " 😗 ", }, - {":-?[0Oo]", " 😮 ", }, - {":-?\\(", " 😞 ", }, - {"\\^\\^", " 😁 ", }, - }) { - String p = r[0]; - p = "(^" + p + "$|^" + p + "\\s+|\\s+" + p + "\\s+|\\s+" + p + "$)"; - body = body.replaceAll(p, r[1]); + for (EmoticonPattern p: patterns) { + body = p.replaceAll(body); } body = body.trim(); } |