From e18396648238f99693dd9e1b75848fb3f367facf Mon Sep 17 00:00:00 2001 From: str4d Date: Wed, 18 Jan 2012 01:46:05 +0000 Subject: [PATCH] Simplify the HTML escape/unescape functions to use static arrays --- core/java/src/net/i2p/data/DataHelper.java | 26 +++++++--------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 95809ed46..02cb951d4 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -1468,6 +1468,9 @@ public class DataHelper { return rv; } + private static final String escapeChars[] = {"&", "\"", "<", ">"}; + private static final String escapeCodes[] = {"&", """, "<", ">"}; + /** * Escape a string for inclusion in HTML * @param unescaped the unescaped string, may be null @@ -1475,15 +1478,9 @@ public class DataHelper { */ public static String escapeHTML(String unescaped) { if (unescaped == null) return null; - Map map = new HashMap(); - map.put("\"","""); - map.put("<","<"); - map.put(">",">"); - String escaped = unescaped.replaceAll("&","&"); - for (Map.Entry entry : map.entrySet()) { - String k = entry.getKey(); - String v = entry.getValue(); - escaped = escaped.replaceAll(k, v); + String escaped = unescaped; + for (int i = 0; i < escapeChars.length; i++) { + escaped = escaped.replaceAll(escapeChars[i], escapeCodes[i]); } return escaped; } @@ -1495,16 +1492,9 @@ public class DataHelper { */ public static String unescapeHTML(String escaped) { if (escaped == null) return null; - Map map = new HashMap(); - map.put("&","&"); - map.put(""","\""); - map.put("<","<"); - map.put(">",">"); String unescaped = escaped; - for (Map.Entry entry : map.entrySet()) { - String k = entry.getKey(); - String v = entry.getValue(); - unescaped = unescaped.replaceAll(k, v); + for (int i = 0; i < escapeChars.length; i++) { + unescaped = unescaped.replaceAll(escapeCodes[i], escapeChars[i]); } return unescaped; }