From: Jakub Skoczen Date: Fri, 22 Jul 2011 12:59:39 +0000 (+0200) Subject: Rewrite inefficient string functions X-Git-Tag: v1.8~6 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=f079ba53623fc80782044f8f06488d19a3948b27;p=cql-java-moved-to-github.git Rewrite inefficient string functions --- diff --git a/src/main/java/org/z3950/zing/cql/CQLTermNode.java b/src/main/java/org/z3950/zing/cql/CQLTermNode.java index b45abdc..5966700 100644 --- a/src/main/java/org/z3950/zing/cql/CQLTermNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLTermNode.java @@ -173,7 +173,7 @@ public class CQLTermNode extends CQLNode { String attr, s = ""; for (int i = 0; i < attrs.size(); i++) { attr = (String) attrs.get(i); - s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " "; + s += "@attr " + attr.replace(" ", " @attr ") + " "; } String text = term; @@ -201,7 +201,7 @@ public class CQLTermNode extends CQLNode { str.indexOf('/') != -1 || str.indexOf('(') != -1 || str.indexOf(')') != -1) { - str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"'; + str = '"' + str.replace("\"", "\\\"") + '"'; } return str; diff --git a/src/main/java/org/z3950/zing/cql/Utils.java b/src/main/java/org/z3950/zing/cql/Utils.java index 6777e46..9224d36 100644 --- a/src/main/java/org/z3950/zing/cql/Utils.java +++ b/src/main/java/org/z3950/zing/cql/Utils.java @@ -22,30 +22,25 @@ class Utils { // s/&/&/g; // s//>/g; - // This is hideously inefficient, but I just don't see a better - // way using the standard JAVA library. - // static String xq(String str) { - str = replaceString(str, "&", "&"); - str = replaceString(str, "<", "<"); - str = replaceString(str, ">", ">"); - return str; - } - - // I can't _believe_ I have to write this by hand in 2002 ... - static String replaceString(String str, String from, String to) { - StringBuffer sb = new StringBuffer(); - int ix; // index of next `from' - int offset = 0; // index of previous `from' + length(from) - - while ((ix = str.indexOf(from, offset)) != -1) { - sb.append(str.substring(offset, ix)); - sb.append(to); - offset = ix + from.length(); - } - - // End of string: append last bit and we're done - sb.append(str.substring(offset)); + StringBuilder sb = new StringBuilder(); + for(int i = 0; i': + sb.append(">"); + break; + case '&': + sb.append("&"); + break; + default: + sb.append(c); + } + } return sb.toString(); } + }