List of usage examples for java.lang StringBuffer charAt
@Override public synchronized char charAt(int index)
From source file:Comments.java
public static String removeComment(String input) { StringBuffer sb = new StringBuffer(input); char NQ = ' ', quote = NQ; int len = sb.length(); for (int j = 0, lineno = 1; j < len; j++) { if (sb.charAt(j) == '\n') ++lineno;/*from www . j av a2s . c o m*/ if (quote != NQ) { if (sb.charAt(j) == quote) { quote = NQ; } else if (sb.charAt(j) == '\\') { j++; //fix for "123\\\r\n123" if (sb.charAt(j) == '\r') j++; // if(sb.charAt(j) == '\n') j++; } else if (sb.charAt(j) == '\n') { throw new IllegalStateException("Unterminated string at line " + lineno); } } else if (sb.charAt(j) == '/' && j + 1 < len && (sb.charAt(j + 1) == '*' || sb.charAt(j + 1) == '/')) { int l = j; boolean eol = sb.charAt(++j) == '/'; while (++j < len) { if (sb.charAt(j) == '\n') ++lineno; if (eol) { if (sb.charAt(j) == '\n') { sb.delete(l, sb.charAt(j - 1) == '\r' ? j - 1 : j); len = sb.length(); j = l; break; } } else if (sb.charAt(j) == '*' && j + 1 < len && sb.charAt(j + 1) == '/') { sb.delete(l, j + 2); len = sb.length(); j = l; break; } } } else if (sb.charAt(j) == '\'' || sb.charAt(j) == '"') { quote = sb.charAt(j); } else if (sb.charAt(j) == '/') { // regex boolean regex = false; for (int k = j;;) { if (--k < 0) { regex = true; break; } char ck = sb.charAt(k); if (!Character.isWhitespace(ck)) { regex = ck == '(' || ck == ',' || ck == '=' || ck == ':' || ck == '?' || ck == '{' || ck == '[' || ck == ';' || ck == '!' || ck == '&' || ck == '|' || ck == '^' || (ck == 'n' && k > 4 && "return".equals(sb.substring(k - 5, k + 1))) || (ck == 'e' && k > 2 && "case".equals(sb.substring(k - 3, k + 1))); break; } } if (regex) { while (++j < len && sb.charAt(j) != '/') { if (sb.charAt(j) == '\\') j++; else if (sb.charAt(j) == '\n') { throw new IllegalStateException("Unterminated regex at line " + lineno); } } } } } return sb.toString(); }
From source file:net.jforum.util.URLNormalizer.java
/** * //from w w w.j ava2 s . com * @param url the url to normalize * @param limit do not process more than <code>limit + 1</code> chars * @param friendlyTruncate If <code>true</code>, will try to not cut a word if * more than <code>limit</code> chars were processed. It will stop in the next * special char * @return the normalized url */ public static String normalize(String url, int limit, boolean friendlyTruncate) { char[] chars = url.toCharArray(); StringBuffer sb = new StringBuffer(url.length()); for (int i = 0; i < chars.length; i++) { if (i <= limit || (friendlyTruncate && i > limit && sb.charAt(sb.length() - 1) != '_')) { if (Character.isSpaceChar(chars[i]) || chars[i] == '-') { if (friendlyTruncate && i > limit) { break; } if (i > 0 && sb.charAt(sb.length() - 1) != '_') { sb.append('_'); } } if (Character.isLetterOrDigit(chars[i])) { sb.append(chars[i]); } else if (friendlyTruncate && i > limit) { break; } } } return sb.toString().toLowerCase(); }
From source file:org.yestech.lib.util.DecoderUtil.java
private static void uriDecode(final StringBuffer buffer, final int offset, final int length) { int index = offset; int count = length; for (; count > 0; count--, index++) { final char ch = buffer.charAt(index); if (ch != '%') { continue; }//from ww w . j a v a 2 s . co m if (count < 3) { throw new RuntimeException( "invalid-escape-sequence.error: " + buffer.substring(index, index + count)); } // Decode int dig1 = Character.digit(buffer.charAt(index + 1), 16); int dig2 = Character.digit(buffer.charAt(index + 2), 16); if (dig1 == -1 || dig2 == -1) { throw new RuntimeException("invalid-escape-sequence.error " + buffer.substring(index, index + 3)); } char value = (char) (dig1 << 4 | dig2); // Replace buffer.setCharAt(index, value); buffer.delete(index + 1, index + 3); count -= 2; } }
From source file:org.ops4j.pax.web.service.internal.WelcomeFilesFilter.java
private static String addPaths(final String path1, final String path2) { if (path1 == null || path1.length() == 0) { if (path1 != null && path2 == null) { return path1; }/* w w w . j a v a 2 s . c om*/ return path2; } if (path2 == null || path2.length() == 0) { return path1; } int split = path1.indexOf(';'); if (split < 0) { split = path1.indexOf('?'); } if (split == 0) { return path2 + path1; } if (split < 0) { split = path1.length(); } StringBuffer buf = new StringBuffer(path1.length() + path2.length() + 2); buf.append(path1); if (buf.charAt(split - 1) == '/') { if (path2.startsWith("/")) { buf.deleteCharAt(split - 1); buf.insert(split - 1, path2); } else { buf.insert(split, path2); } } else { if (path2.startsWith("/")) { buf.insert(split, path2); } else { buf.insert(split, '/'); buf.insert(split + 1, path2); } } return buf.toString(); }
From source file:com.linuxbox.enkive.docsearch.indri.IndriQueryComposer.java
/** * Sanitize search term by only allowing letters, digits, and a small subset * of symbols./*from w w w. j av a 2 s . c o m*/ * * @param buffer */ protected static void sanitizeStringBuffer(StringBuffer buffer) { for (int i = buffer.length() - 1; i >= 0; i--) { Character c = buffer.charAt(i); if (!Character.isLetterOrDigit(c) && !allowableSymbols.contains(c)) { buffer.deleteCharAt(i); } } }
From source file:utils.hashing.similarity.java
public static int britoshteinPercentage(final StringBuffer c1, final char[] c2) { // get the smallest of the two arrays to compare final int size = Math.min(c1.length(), c2.length); int points = 0; int pointer = 0; // compare each case for (int i = 0; i < size; i++) { if (c1.charAt(pointer) == c2[i]) { points++;//from www. jav a 2s . c om pointer++; } } // get the percentual value return (points * 100) / size; }
From source file:org.eclim.plugin.jdt.command.junit.JUnitUtils.java
private static void replaceIllegalCharacters(StringBuffer buffer) { char character = 0; for (int index = buffer.length() - 1; index >= 0; index--) { character = buffer.charAt(index); if (Character.isWhitespace(character)) { buffer.deleteCharAt(index);/*w w w . j av a2 s . co m*/ } else if (character == '<') { buffer.replace(index, index + 1, OF_TAG); } else if (character == '?') { buffer.replace(index, index + 1, QUESTION_MARK_TAG); // Skipping this for now so we don't rely on sun packages. /*}else if (!Character.isJavaIdentifierPart(character)) { // Check for surrogates if (!UTF16.isSurrogate(character)) { buffer.deleteCharAt(index); }*/ } } }
From source file:org.kuali.rice.edl.impl.components.WorkflowDocumentState.java
public static void addActions(Document dom, Element documentState, List actions) { Element actionsPossible = EDLXmlUtils.getOrCreateChildElement(documentState, "actionsPossible", true); Iterator it = actions.iterator(); while (it.hasNext()) { String action = it.next().toString(); Element actionElement = dom.createElement(action); // if we use string.xsl we can avoid doing this here // (unless for some reason we decide we want different titles) if (!Character.isUpperCase(action.charAt(0))) { StringBuffer sb = new StringBuffer(action); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); action = sb.toString();// w ww. j a v a 2 s . c om } actionElement.setAttribute("title", action); actionsPossible.appendChild(actionElement); } Element annotatable = EDLXmlUtils.getOrCreateChildElement(documentState, "annotatable", true); annotatable.appendChild(dom.createTextNode(String.valueOf(isAnnotatable(actions)))); }
From source file:SystemIDResolver.java
/** * Replace spaces with "%20" and backslashes with forward slashes in * the input string to generate a well-formed URI string. * * @param str The input string//w w w .jav a2 s . c o m * @return The string after conversion */ private static String replaceChars(String str) { StringBuffer buf = new StringBuffer(str); int length = buf.length(); for (int i = 0; i < length; i++) { char currentChar = buf.charAt(i); // Replace space with "%20" if (currentChar == ' ') { buf.setCharAt(i, '%'); buf.insert(i + 1, "20"); length = length + 2; i = i + 2; } // Replace backslash with forward slash else if (currentChar == '\\') { buf.setCharAt(i, '/'); } } return buf.toString(); }
From source file:Main.java
private static boolean isEncodeable(StringBuffer p_src, int index) { // white space boolean blank = false; boolean lt = false; String sub = null;/*from ww w . j av a 2s . c om*/ for (int i = index - 1; i >= 0; i--) { char c = p_src.charAt(i); String cstr = "" + c; if ("".equals(cstr.trim())) { blank = true; } else if (c == '<') { sub = p_src.substring(i, index); lt = true; break; } // node value, encode else if (c == '>') { return true; } } // node name, do not encode if (lt && !blank) { return false; } // comments, encode if (sub.startsWith("<!--")) { return true; } // cdata, encode if (sub.startsWith("<![CDATA[")) { return true; } // dtd or version, do not encode if (sub.startsWith("<!") || sub.startsWith("<?")) { return false; } // determine attribute int subLen = sub.length(); boolean eq = false; boolean singleQuote = false; boolean doubleQuote = false; for (int i = 1; i < subLen; i++) { char c = sub.charAt(i); if (c == '=' && !singleQuote && !doubleQuote) { eq = true; } if (c == '\'' && eq && !doubleQuote) { singleQuote = !singleQuote; if (!singleQuote) { eq = false; } } if (c == '"' && eq && !singleQuote) { doubleQuote = !doubleQuote; if (!doubleQuote) { eq = false; } } } if (singleQuote || doubleQuote) { return true; } else { return false; } }