diff options
author | M. Dietrich <mdt@emdete.de> | 2014-09-03 15:39:46 +0200 |
---|---|---|
committer | M. Dietrich <mdt@emdete.de> | 2014-09-03 15:39:46 +0200 |
commit | cb3ba4045e78320d7c670edbd7bc320ebe38e301 (patch) | |
tree | 5c595bc5bf7386bac517d5e5059f666a04809b49 | |
parent | a7b3aa343ea3774d105a09132135fdd7f13fdb3f (diff) |
improve regex, implement pattern cache
-rw-r--r-- | src/eu/siacs/conversations/utils/UIHelper.java | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 49645475b..072fe7157 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(); } |