improve regex, implement pattern cache

This commit is contained in:
M. Dietrich 2014-09-03 15:39:46 +02:00
parent a7b3aa343e
commit cb3ba4045e
1 changed files with 23 additions and 15 deletions

View File

@ -545,24 +545,32 @@ public class UIHelper {
return getContactPicture(account.getJid(), size, context, false); 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) { public static String transformAsciiEmoticons(String body) {
if (body != null) { if (body != null) {
for (String[] r: new String[][]{ // see https://de.wikipedia.org/wiki/Unicodeblock_Smileys // 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
{":-?D", " 😃 ", }, for (Object[] r: new Object[][]{
{":-?[Ppb]", " 😋 ", }, {armorRegex(":-?\\)"), armorReplacement("😃"), },
{"8-?\\)", " 😎 ", }, {armorRegex(";-?\\)"), armorReplacement("😉"), },
{":-?\\|", " 😐 ", }, {armorRegex(":-?D"), armorReplacement("😀"), },
{":-?[/\\\\]", " 😕 ", }, {armorRegex(":-?[Ppb]"), armorReplacement("😋"), },
{":-?\\*", " 😗 ", }, {armorRegex("8-?\\)"), armorReplacement("😎"), },
{":-?[0Oo]", " 😮 ", }, {armorRegex(":-?\\|"), armorReplacement("😐"), },
{":-?\\(", " 😞 ", }, {armorRegex(":-?[/\\\\]"), armorReplacement("😕"), },
{"\\^\\^", " 😁 ", }, {armorRegex(":-?\\*"), armorReplacement("😗"), },
{armorRegex(":-?[0Oo]"), armorReplacement("😮"), },
{armorRegex(":-?\\("), armorReplacement("😞"), },
{armorRegex("\\^\\^"), armorReplacement("😁"), },
}) { }) {
String p = r[0]; Pattern pattern = (Pattern)r[0];
p = "(^" + p + "$|^" + p + "\\s+|\\s+" + p + "\\s+|\\s+" + p + "$)"; String replacement = (String)r[1];
body = body.replaceAll(p, r[1]); body = pattern.matcher(body).replaceAll(replacement);
} }
body = body.trim(); body = body.trim();
} }