commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r617906 - /commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
Date Sat, 02 Feb 2008 22:02:55 GMT
Author: mbenson
Date: Sat Feb  2 14:02:40 2008
New Revision: 617906

URL: http://svn.apache.org/viewvc?rev=617906&view=rev
Log:
refactoring to avoid constructing n intermediate strings

Modified:
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java

Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java?rev=617906&r1=617905&r2=617906&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
(original)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
Sat Feb  2 14:02:40 2008
@@ -624,23 +624,32 @@
      * @return string with any necessary changes made.
      */
     private String escape(String string) {
-        int index = string.indexOf('\'');
-        while (index != -1) {
-            string =
-                string.substring(0, index)
-                    + "'"
-                    + string.substring(index + 1);
-            index = string.indexOf('\'');
+        final char[] c = new char[] { '\'', '"' };
+        final String[] esc = new String[] { "'", """ };
+        StringBuffer sb = null;
+        for (int i = 0; sb == null && i < c.length; i++) {
+            if (string.indexOf(c[i]) >= 0) {
+                sb = new StringBuffer(string);
+            }
         }
-        index = string.indexOf('\"');
-        while (index != -1) {
-            string =
-                string.substring(0, index)
-                    + "&quot;"
-                    + string.substring(index + 1);
-            index = string.indexOf('\"');
+        if (sb == null) {
+            return string;
         }
-        return string;
+        for (int i = 0; i < c.length; i++) {
+            if (string.indexOf(c[i]) < 0) {
+                continue;
+            }
+            int pos = 0;
+            while (pos < sb.length()) {
+                if (sb.charAt(pos) == c[i]) {
+                    sb.replace(pos, pos + 1, esc[i]);
+                    pos += esc[i].length();
+                } else {
+                    pos++;
+                }
+            }
+        }
+        return sb.toString();
     }
 
     /**



Mime
View raw message