[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [onionoo/master] Avoid String.replaceAll() because of memory issues.



commit f660e24be80b2efed1423b9ebf912377ccea49d5
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Mon Oct 27 09:06:30 2014 +0100

    Avoid String.replaceAll() because of memory issues.
    
    From a quick look at the sources it seems that they're using
    String.indexOf() to find patterns rather than regular expressions which
    is what String.replaceAll() uses.
---
 .../java/org/torproject/onionoo/docs/DetailsDocument.java     |    9 +++++----
 src/main/java/org/torproject/onionoo/docs/DetailsStatus.java  |    9 +++++----
 src/main/java/org/torproject/onionoo/docs/DocumentStore.java  |    5 +++--
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java b/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java
index bda9865..7d351ee 100644
--- a/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java
+++ b/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java
@@ -6,6 +6,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.commons.lang.StringUtils;
 
 public class DetailsDocument extends Document {
 
@@ -18,12 +19,12 @@ public class DetailsDocument extends Document {
    * we'll have to do is to change back the '\\' that Gson writes for the
    * '\'. */
   private static String escapeJSON(String s) {
-    return s == null ? null :
-        StringEscapeUtils.escapeJavaScript(s).replaceAll("\\\\'", "'");
+    return StringUtils.replaceEach(StringEscapeUtils.escapeJavaScript(s),
+        new String[] { "\\\\'" }, new String[] { "'" });
   }
   private static String unescapeJSON(String s) {
-    return s == null ? null :
-        StringEscapeUtils.unescapeJavaScript(s.replaceAll("'", "\\'"));
+    return StringEscapeUtils.unescapeJavaScript(StringUtils.replaceEach(s,
+        new String[] { "'" }, new String[] { "\\'" }));
   }
 
   private String nickname;
diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java
index 6fe0d80..6d987df 100644
--- a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java
@@ -6,6 +6,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.commons.lang.StringUtils;
 
 public class DetailsStatus extends Document {
 
@@ -18,12 +19,12 @@ public class DetailsStatus extends Document {
    * we'll have to do is to change back the '\\' that Gson writes for the
    * '\'. */
   private static String escapeJSON(String s) {
-    return s == null ? null :
-        StringEscapeUtils.escapeJavaScript(s).replaceAll("\\\\'", "'");
+    return StringUtils.replaceEach(StringEscapeUtils.escapeJavaScript(s),
+        new String[] { "\\\\'" }, new String[] { "'" });
   }
   private static String unescapeJSON(String s) {
-    return s == null ? null :
-        StringEscapeUtils.unescapeJavaScript(s.replaceAll("'", "\\'"));
+    return StringEscapeUtils.unescapeJavaScript(StringUtils.replaceEach(s,
+        new String[] { "'" }, new String[] { "\\'" }));
   }
 
   private String desc_published;
diff --git a/src/main/java/org/torproject/onionoo/docs/DocumentStore.java b/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
index e3261e4..9a808ac 100644
--- a/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
+++ b/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
@@ -21,6 +21,7 @@ import java.util.Stack;
 import java.util.TreeMap;
 import java.util.TreeSet;
 
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.torproject.onionoo.util.FormattingUtils;
@@ -275,8 +276,8 @@ public class DocumentStore {
        * objects are escaped JSON, e.g., \u00F2.  When Gson serlializes
        * this string, it escapes the \ to \\, hence writes \\u00F2.  We
        * need to undo this and change \\u00F2 back to \u00F2. */
-      documentString = gson.toJson(document).replaceAll("\\\\\\\\u",
-          "\\\\u");
+      documentString = StringUtils.replace(gson.toJson(document),
+          "\\\\\\\\u", "\\\\u");
       /* Existing details statuses don't contain opening and closing curly
        * brackets, so we should remove them from new details statuses,
        * too. */



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits