From cb3ba4045e78320d7c670edbd7bc320ebe38e301 Mon Sep 17 00:00:00 2001 From: "M. Dietrich" Date: Wed, 3 Sep 2014 15:39:46 +0200 Subject: improve regex, implement pattern cache --- src/eu/siacs/conversations/utils/UIHelper.java | 38 ++++++++++++++++---------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 49645475..072fe715 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -545,24 +545,32 @@ public class UIHelper { return getContactPicture(account.getJid(), size, context, false); } } + + private static final Pattern armorRegex(String regex) { return Pattern.compile("(^|\\s+)" + regex + "(\\s+|$)"); } + + private static final String armorReplacement(String replacement) { return "$1" + replacement + "$2"; } + 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]", " 😮 ", }, - {":-?\\(", " 😞 ", }, - {"\\^\\^", " 😁 ", }, + // see https://developer.android.com/reference/java/util/regex/Pattern.html + // see http://userguide.icu-project.org/strings/regexp + // see https://de.wikipedia.org/wiki/Unicodeblock_Smileys + for (Object[] r: new Object[][]{ + {armorRegex(":-?\\)"), armorReplacement("😃"), }, + {armorRegex(";-?\\)"), armorReplacement("😉"), }, + {armorRegex(":-?D"), armorReplacement("😀"), }, + {armorRegex(":-?[Ppb]"), armorReplacement("😋"), }, + {armorRegex("8-?\\)"), armorReplacement("😎"), }, + {armorRegex(":-?\\|"), armorReplacement("😐"), }, + {armorRegex(":-?[/\\\\]"), armorReplacement("😕"), }, + {armorRegex(":-?\\*"), armorReplacement("😗"), }, + {armorRegex(":-?[0Oo]"), armorReplacement("😮"), }, + {armorRegex(":-?\\("), armorReplacement("😞"), }, + {armorRegex("\\^\\^"), armorReplacement("😁"), }, }) { - String p = r[0]; - p = "(^" + p + "$|^" + p + "\\s+|\\s+" + p + "\\s+|\\s+" + p + "$)"; - body = body.replaceAll(p, r[1]); + Pattern pattern = (Pattern)r[0]; + String replacement = (String)r[1]; + body = pattern.matcher(body).replaceAll(replacement); } body = body.trim(); } -- cgit v1.2.3 From 584984807e3f9e05c7f556d1181a3301db6a8b60 Mon Sep 17 00:00:00 2001 From: "M. Dietrich" Date: Wed, 3 Sep 2014 16:10:50 +0200 Subject: make pattern static --- src/eu/siacs/conversations/utils/UIHelper.java | 36 ++++++++++++++------------ 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 072fe715..1c1bb893 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -546,28 +546,32 @@ public class UIHelper { } } - private static final Pattern armorRegex(String regex) { return Pattern.compile("(^|\\s+)" + regex + "(\\s+|$)"); } - - private static final String armorReplacement(String replacement) { return "$1" + replacement + "$2"; } + private static final Pattern armorRegex(String regex) { + return Pattern.compile("(^|\\s+)" + regex + "(\\s+|$)"); } + + private static final String armorReplacement(String replacement) { + return "$1" + replacement + "$2"; } + + private static final Object[][] patterns = new Object[][]{ + {armorRegex(":-?\\)"), armorReplacement("😃"), }, + {armorRegex(";-?\\)"), armorReplacement("😉"), }, + {armorRegex(":-?D"), armorReplacement("😀"), }, + {armorRegex(":-?[Ppb]"), armorReplacement("😋"), }, + {armorRegex("8-?\\)"), armorReplacement("😎"), }, + {armorRegex(":-?\\|"), armorReplacement("😐"), }, + {armorRegex(":-?[/\\\\]"), armorReplacement("😕"), }, + {armorRegex(":-?\\*"), armorReplacement("😗"), }, + {armorRegex(":-?[0Oo]"), armorReplacement("😮"), }, + {armorRegex(":-?\\("), armorReplacement("😞"), }, + {armorRegex("\\^\\^"), armorReplacement("😁"), }, + }; public static String transformAsciiEmoticons(String body) { if (body != null) { // see https://developer.android.com/reference/java/util/regex/Pattern.html // see http://userguide.icu-project.org/strings/regexp // see https://de.wikipedia.org/wiki/Unicodeblock_Smileys - for (Object[] r: new Object[][]{ - {armorRegex(":-?\\)"), armorReplacement("😃"), }, - {armorRegex(";-?\\)"), armorReplacement("😉"), }, - {armorRegex(":-?D"), armorReplacement("😀"), }, - {armorRegex(":-?[Ppb]"), armorReplacement("😋"), }, - {armorRegex("8-?\\)"), armorReplacement("😎"), }, - {armorRegex(":-?\\|"), armorReplacement("😐"), }, - {armorRegex(":-?[/\\\\]"), armorReplacement("😕"), }, - {armorRegex(":-?\\*"), armorReplacement("😗"), }, - {armorRegex(":-?[0Oo]"), armorReplacement("😮"), }, - {armorRegex(":-?\\("), armorReplacement("😞"), }, - {armorRegex("\\^\\^"), armorReplacement("😁"), }, - }) { + for (Object[] r: patterns) { Pattern pattern = (Pattern)r[0]; String replacement = (String)r[1]; body = pattern.matcher(body).replaceAll(replacement); -- cgit v1.2.3 From 0a686bc71c1f3888185f292602ff067c50374253 Mon Sep 17 00:00:00 2001 From: "M. Dietrich" Date: Wed, 3 Sep 2014 17:29:13 +0200 Subject: use class, use codepoint --- src/eu/siacs/conversations/utils/UIHelper.java | 51 +++++++++++++------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 1c1bb893..f717c5da 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -546,35 +546,36 @@ public class UIHelper { } } - private static final Pattern armorRegex(String regex) { - return Pattern.compile("(^|\\s+)" + regex + "(\\s+|$)"); } - - private static final String armorReplacement(String replacement) { - return "$1" + replacement + "$2"; } - - private static final Object[][] patterns = new Object[][]{ - {armorRegex(":-?\\)"), armorReplacement("😃"), }, - {armorRegex(";-?\\)"), armorReplacement("😉"), }, - {armorRegex(":-?D"), armorReplacement("😀"), }, - {armorRegex(":-?[Ppb]"), armorReplacement("😋"), }, - {armorRegex("8-?\\)"), armorReplacement("😎"), }, - {armorRegex(":-?\\|"), armorReplacement("😐"), }, - {armorRegex(":-?[/\\\\]"), armorReplacement("😕"), }, - {armorRegex(":-?\\*"), armorReplacement("😗"), }, - {armorRegex(":-?[0Oo]"), armorReplacement("😮"), }, - {armorRegex(":-?\\("), armorReplacement("😞"), }, - {armorRegex("\\^\\^"), armorReplacement("😁"), }, + 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(":-?\\)", 0x1f603), + new EmoticonPattern(";-?\\)", 0x1f609), + new EmoticonPattern(":-?D", 0x1f600), + new EmoticonPattern(":-?[Ppb]", 0x1f60b), + new EmoticonPattern("8-?\\)", 0x1f60e), + new EmoticonPattern(":-?\\|", 0x1f610), + new EmoticonPattern(":-?[/\\\\]", 0x1f615), + new EmoticonPattern(":-?\\*", 0x1f617), + new EmoticonPattern(":-?[0Oo]", 0x1f62e), + new EmoticonPattern(":-?\\(", 0x1f61e), + new EmoticonPattern("\\^\\^", 0x1f601), }; public static String transformAsciiEmoticons(String body) { if (body != null) { - // see https://developer.android.com/reference/java/util/regex/Pattern.html - // see http://userguide.icu-project.org/strings/regexp - // see https://de.wikipedia.org/wiki/Unicodeblock_Smileys - for (Object[] r: patterns) { - Pattern pattern = (Pattern)r[0]; - String replacement = (String)r[1]; - body = pattern.matcher(body).replaceAll(replacement); + for (EmoticonPattern p: patterns) { + body = p.replaceAll(body); } body = body.trim(); } -- cgit v1.2.3 From 0a2c73f2469fe359162b5c973f86ea2e409272ac Mon Sep 17 00:00:00 2001 From: "M. Dietrich" Date: Wed, 3 Sep 2014 17:44:38 +0200 Subject: improve emo mapping --- src/eu/siacs/conversations/utils/UIHelper.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index f717c5da..768bc063 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -559,17 +559,20 @@ public class UIHelper { } private static final EmoticonPattern[] patterns = new EmoticonPattern[] { - new EmoticonPattern(":-?\\)", 0x1f603), - new EmoticonPattern(";-?\\)", 0x1f609), new EmoticonPattern(":-?D", 0x1f600), - new EmoticonPattern(":-?[Ppb]", 0x1f60b), - new EmoticonPattern("8-?\\)", 0x1f60e), + 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(":-?[0Oo]", 0x1f62e), + new EmoticonPattern(":-?[Ppb]", 0x1f61b), new EmoticonPattern(":-?\\(", 0x1f61e), - new EmoticonPattern("\\^\\^", 0x1f601), + new EmoticonPattern(":-?[0Oo]", 0x1f62e), + new EmoticonPattern("\\\\o/", 0x1F631), }; public static String transformAsciiEmoticons(String body) { -- cgit v1.2.3