> The simplest optimization would be to create the 'sb' buffer > at least as big as the 'input' string. After all, even if > there were no actual substitutions, the 'sb' would be as big, > and this would avoid StringBuffer doing a lot of > reallocations, especially if 'input' is more than the 16 > characters, the default capacity of a StringBuffer. This > could be accomplished with just something like:
Smart, and doesn't require much effort. Does this save a lot of time? > A more sophisticated optimization would require two loops. > The first would look very much like the existing loop, but no > 'sb' StringBuffer would be created until one of the special > characters was found. The "gamble" is that the string > contains no characters to be escaped, no extra objects would > be created. It would look something like: > > public static String escapeXml(String input) { > StringBuffer sb = null; > int length = input.length(); > int currentPos; > for (int currentPos = 0; currentPos < length; currentPos++) { > char c = input.charAt(currentPos); > if (c == '&') { > sb = new StringBuffer(length+4); > sb.append(input.substring(0,currentPos)); // > copy over the string up until we found this char > sb.append("&"); > break; > } > else if (c == '<') { > > sb = new StringBuffer(length+3); > sb.append(input.substring(0,currentPos)); // > copy over the string up until we found this char > sb.append("<"); > > break; > } > else if (c == '>') { > > sb = new StringBuffer(length+3); > sb.append(input.substring(0,currentPos)); // > copy over the string up until we found this char > sb.append(">"); > > break; > } > else if (c == '"') { > > sb = new StringBuffer(length+5); > sb.append(input.substring(0,currentPos)); // > copy over the string up until we found this char > sb.append("""); > > break; > } > else if (c == '\'') { > > sb = new StringBuffer(length+5); > sb.append(input.substring(0,currentPos)); // > copy over the string up until we found this char > sb.append("'"); > > break; > } > > } > > // If we didn't create a new buffer, then the input > string didn't need any escaping so we win big time > // and we can just return the same string they gave > us (no object creation, no copying). > if ( sb == null ) > return input; > > // Oh well, we did have some escaping to do, so let's > check the rest to see if there are any more to do. > for ( ++currentPos; currentPos < length; ++currentPos) { > char c = input.charAt(currentPos); > if (c == '&') > sb.append("&"); > else if (c == '<') > sb.append("<"); > else if (c == '>') > sb.append(">"); > else if (c == '"') > sb.append("""); > else if (c == '\'') > sb.append("'"); > else > sb.append(c); > } > return sb.toString(); > } Try it simpler: public static String escapeXml(String input) { StringBuffer sb = null; int length = input.length(); int currentPos; //Check if there are any special characters to escape for (currentPos = 0; currentPos < length; currentPos++) { char c = input.charAt(currentPos); if (c == '&' || c == '<' || c == '>' || c == '"' || c == '\'') { sb = new StringBuffer(length); sb.append(input.substring(0,currentPos)); // copy over the string up until we found this char break; } } // If we didn't create a new buffer, then the input string didn't need any escaping so we win big time // and we can just return the same string they gave us (no object creation, no copying). if ( sb == null ) return input; // Oh well, we did have some escaping to do, so let's check the rest to see if there are any more to do. for (; currentPos < length;++currentPos) { char c = input.charAt(currentPos); if (c == '&') sb.append("&"); else if (c == '<') sb.append("<"); else if (c == '>') sb.append(">"); else if (c == '"') sb.append("""); else if (c == '\'') sb.append("'"); else sb.append(c); } return sb.toString(); } Martin --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]