private String escapeWindowsJvmOpt(String jvmOpts) { boolean wasOnBackslash = false; StringBuilder escapedJvmOpt = new StringBuilder(); CharacterIterator it = new StringCharacterIterator(jvmOpts); //argument quoting: // - " must be encoded as \" // - % must be encoded as %% // - pathological case: \" must be encoded as \\\", but other than that, \ MUST NOT be quoted // - other characters (including ') will not be quoted // - use a state machine rather than regexps for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { String repl = Character.toString(ch); if (ch == '%') { repl = "%%"; } else if (ch == '"') { repl = (wasOnBackslash ? '\\' : "") + "\\\""; } wasOnBackslash = ch == '\\'; escapedJvmOpt.append(repl); } return escapedJvmOpt.toString(); }
private boolean valid(String input) { if ("".equals(input)) return true; boolean ret = true; it = new StringCharacterIterator(input); c = it.first(); col = 1; if (!value()) { ret = error("value", 1); } else { skipWhiteSpace(); if (c != CharacterIterator.DONE) { ret = error("end", col); } } return ret; }
private boolean literal(String text) { CharacterIterator ci = new StringCharacterIterator(text); char t = ci.first(); if (c != t) return false; int start = col; boolean ret = true; for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) { if (t != nextCharacter()) { ret = false; break; } } nextCharacter(); if (!ret) error("literal " + text, start); return ret; }
private void string(Object obj) { add('"'); CharacterIterator it = new StringCharacterIterator(obj.toString()); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { if (c == '"') add("\\\""); else if (c == '\\') add("\\\\"); else if (c == '/') add("\\/"); else if (c == '\b') add("\\b"); else if (c == '\f') add("\\f"); else if (c == '\n') add("\\n"); else if (c == '\r') add("\\r"); else if (c == '\t') add("\\t"); else if (Character.isISOControl(c)) { unicode(c); } else { add(c); } } add('"'); }
private static int[] getFlagIdCodeSequence(String id, int base) { int[] buf = new int[id.length() + 2]; int pos = 0; buf[pos++] = base; CharacterIterator it = new StringCharacterIterator(id); for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')) { buf[pos++] = 0xE0000 + ch; } else if (ch >= 'A' && ch <= 'Z') { buf[pos++] = 0xE0020 + ch; } } buf[pos++] = 0xE007F; int[] codePoints = new int[pos]; for (int i = 0; i < pos; i++) { codePoints[i] = buf[i]; } return codePoints; }
public static String escapeForHTML(String s, char c) { if (s == null || s.isEmpty()) { return s; } StringBuilder b = new StringBuilder(); StringCharacterIterator it = new StringCharacterIterator(s); char ch = it.current(); while (ch != CharacterIterator.DONE) { if (ch == '<') { b.append("<"); } else if (ch == '>') { b.append(">"); } else if (ch == '&') { b.append("&"); } else if (ch == '\"') { b.append("""); } else { b.append(ch); } ch = it.next(); } return b.toString(); }
public void TestBug4153072() { BreakIterator iter = BreakIterator.getWordInstance(); String str = "...Hello, World!..."; int begin = 3; int end = str.length() - 3; boolean gotException = false; boolean dummy; iter.setText(new StringCharacterIterator(str, begin, end, begin)); for (int index = -1; index < begin + 1; ++index) { try { dummy = iter.isBoundary(index); if (index < begin) errln("Didn't get exception with offset = " + index + " and begin index = " + begin); } catch (IllegalArgumentException e) { if (index >= begin) errln("Got exception with offset = " + index + " and begin index = " + begin); } } }
/** * Replaces all of the {@link MessageUtil#BUKKIT_COLOUR_CODE}'s with * {@link MessageUtil#ALT_COLOUR_CODE}. This method is usually invoked * when wanting to output a user-friendly string to a configuration file. * * @param s string to be uncoloured. * @return uncoloured string. */ public static String uncolour(String s) { if (s == null || s.isEmpty()) { return null; } StringBuilder builder = new StringBuilder(s.length()); CharacterIterator it = new StringCharacterIterator(s); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { switch (c) { case BUKKIT_COLOUR_CODE: builder.append(ALT_COLOUR_CODE); break; default: builder.append(c); break; } } return builder.toString(); }
/** * addslashes() 函数在指定的预定义字符前添加反斜杠。 做转义处理后,写入数据库就不会因敏感字符('之类)导致sql有问题。 * 此处的转义处理内容是:在字符串中的单引号、双引号、反斜杠之前,都附加上反斜杠 */ public static String addSlashes(String text) { if (text == null || text.equals("")) { return ""; } StringBuffer sb = new StringBuffer(text.length() * 2); StringCharacterIterator iterator = new StringCharacterIterator(text); char character = iterator.current(); while (character != StringCharacterIterator.DONE) { // DONE,字符型,当迭代器已到达文本末尾或开始处时返回的常量。 switch (character) { case '\'': /* 单引号 */ case '"': /* 双引号 */ case '\\': /* 单斜杠 */ sb.append("\\"); /* 单斜杠 */ default: sb.append(character); break; } character = iterator.next(); } return sb.toString(); }
/** * Returns a string that contains all characters of the given string in * reverse order. */ public String reverse( String str ) { if ( str == null ) return null ; char[] newStr = new char[str.length()] ; StringCharacterIterator iterator = new StringCharacterIterator(str) ; int i = 0 ; for(char ch = iterator.last(); ch != CharacterIterator.DONE; ch = iterator.previous()) { newStr[i] = ch ; i++ ; } return new String( newStr ) ; }
StringTokenizer(String value, char delim) { string = value; // Loop on the characters counting the separators and remembering // their positions StringCharacterIterator sci = new StringCharacterIterator(string); char c = sci.first(); while (c != CharacterIterator.DONE) { if (c == delim) { // Remember its position separatorPosition[tokens] = sci.getIndex(); tokens++; // Resize the position array if needed if (tokens >= separatorPosition.length) { int[] copy = new int[separatorPosition.length * 10]; System.arraycopy(separatorPosition, 0, copy, 0, separatorPosition.length); separatorPosition = copy; } } c = sci.next(); } // Add one token: tokens = separatorCount + 1 tokens++; }
private static void processStatement(final StringCharacterIterator iter, final StringBuffer output, final int level) { if (iter.current() != '(') throw new Error("Expected '(' but found '" + iter.current() + "'"); output.append('('); boolean lb = false; boolean sub = false; while (iter.next() != CharacterIterator.DONE) { switch (iter.current()) { case '(': if (!lb) lineBreak(output, level + 1); processStatement(iter, output, level + 1); sub = true; lb = false; break; case ')': if (!lb && sub) lineBreak(output, level); output.append(')'); return; default: output.append(iter.current()); lb = false; } } }
public String normalizeSpaces(String s) { if (s == null) return null; StringBuffer buf = new StringBuffer(); CharacterIterator iter = new StringCharacterIterator(s); boolean inWhitespace = false; // Flag set if we're in a second // consecutive whitespace for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (Character.isWhitespace(c)) { if (!inWhitespace) { buf.append(' '); inWhitespace = true; } } else { inWhitespace = false; buf.append(c); } } return buf.toString(); }
static String mungeModuleName(String bsn) { if (bsn.length() == 0) { return "_"; } CharacterIterator ci = new StringCharacterIterator(bsn); StringBuilder sb = new StringBuilder(); boolean first = true; for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) { if (first) { first = false; if (Character.isJavaIdentifierStart(c)) { sb.append(c); } else sb.append('_'); } else { first = c == '.'; if (first || Character.isJavaIdentifierPart(c)) { sb.append(c); } else { sb.append('_'); } } }; return sb.toString(); }
@Nullable public String getName() { final String text = getNode().getText().trim(); if (text.length() < 2) return null; final char adorn = text.charAt(text.length()-2); final CharacterIterator it = new StringCharacterIterator(text); int finish = 0; for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) { if (ch != adorn) { finish = it.getIndex(); break; } } int start = 0; if (text.charAt(0) == adorn) { for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { if (ch != adorn) { start = it.getIndex() + 1; break; } } } if (finish <= 0 || start < 0) return null; return text.substring(start, finish).trim(); }
private static List<String> findTokens(String data) { List<String> tokens = new ArrayList<String>(); boolean inQuotes = false; StringBuilder currentToken = new StringBuilder(); StringCharacterIterator i = new StringCharacterIterator(data); while (i.current() != StringCharacterIterator.DONE) { char c = i.current(); if (c == '\'') { inQuotes = !inQuotes; } if (!inQuotes && (c == '.' || c == '[' || c == ']')) { if (currentToken.length() > 0) { tokens.add(currentToken.toString()); currentToken.setLength(0); } } else { currentToken.append(c); } i.next(); } tokens.add(currentToken.toString()); return Collections.unmodifiableList(tokens); }
private boolean valid(String input) { if ("".equals(input)) { return true; } boolean ret = true; it = new StringCharacterIterator(input); c = it.first(); col = 1; if (!value()) { ret = false; } else { skipWhiteSpace(); if (c != CharacterIterator.DONE) { ret = false; } } return ret; }
private boolean literal(String text) { CharacterIterator ci = new StringCharacterIterator(text); char t = ci.first(); if (c != t) { return false; } int start = col; boolean ret = true; for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) { if (t != nextCharacter()) { ret = false; break; } } nextCharacter(); if (!ret) { ; } return ret; }
/** * Escape characters for text appearing as XML data, between tags. * * <P>The following characters are replaced with corresponding character entities : * <table border='1' cellpadding='3' cellspacing='0'> * <tr><th> Character </th><th> Encoding </th></tr> * <tr><td> < </td><td> < </td></tr> * <tr><td> > </td><td> > </td></tr> * <tr><td> & </td><td> & </td></tr> * <tr><td> " </td><td> "</td></tr> * <tr><td> ' </td><td> '</td></tr> * </table> * * <P>Note that JSTL's {@code <c:out>} escapes the exact same set of * characters as this method. <span class='highlight'>That is, {@code <c:out>} * is good for escaping to produce valid XML, but not for producing safe * HTML.</span> */ public static String forXML(String aText) { final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(aText); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else if (character == '\"') { result.append("""); } else if (character == '\'') { result.append("'"); } else if (character == '&') { result.append("&"); } else { //the char is not a special one //add it to the result as is result.append(character); } character = iterator.next(); } return result.toString(); }
/** * @param text * @return */ public static String encode(final String text) { final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(text); char character = iterator.current(); String rep = null; while (character != CharacterIterator.DONE) { rep = HTMLTranscoder.MAP.get(character); if (rep == null) { result.append(character); } else { result.append(rep); } character = iterator.next(); } return result.toString(); }
/** * * encodes only newline, <>" and & * * @param text * * @return */ public static String encodeSimple(final String text) { final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(text); char character = iterator.current(); char lastC = 0; String rep = null; while (character != CharacterIterator.DONE) { if (character == '\n' && lastC == '\r') { lastC = character; character = iterator.next(); } rep = HTMLTranscoder.MAP_SIMPLE.get(character); if (rep == null) { result.append(character); } else { result.append(rep); } lastC = character; character = iterator.next(); } return result.toString(); }
/** * Set Tag Text * @param text text * @return Element */ public Element setTagText (String text) { if (text != null && text.length() > 0) { StringCharacterIterator sci = new StringCharacterIterator(text); for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) { int ii = c; if (ii > 255) { setFilterState(true); break; } } } return super.setTagText (text); }
/** Perform the filtering operation. */ public String process(String to_process) { if ( to_process == null || to_process.length() == 0 ) return ""; StringBuffer bs = new StringBuffer(to_process.length() + 50); StringCharacterIterator sci = new StringCharacterIterator(to_process); String tmp = null; for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) { tmp = String.valueOf(c); if (hasAttribute(tmp)) tmp = (String) this.get(tmp); int ii = c; if (ii > 255) tmp = "&#" + ii + ";"; bs.append(tmp); } return(bs.toString()); }
public static boolean isFullyQualifiedClassname(final String classname) { if (classname == null) return false; String[] parts = classname.split("[\\.]"); if (parts.length == 0) return false; for (String part : parts) { CharacterIterator iter = new StringCharacterIterator(part); // Check first character (there should at least be one character for each part) ... char c = iter.first(); if (c == CharacterIterator.DONE) return false; if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c)) return false; c = iter.next(); // Check the remaining characters, if there are any ... while (c != CharacterIterator.DONE) { if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c)) return false; c = iter.next(); } } return true; }
private static String filterXML(String s) { if (s == null) return ""; s = s.trim(); if (s.equals("")) return ""; StringBuffer stringbuffer = new StringBuffer(s.length()); StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s); for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) if (c == '\t' || c == '\n' || c == '\r' || ' ' <= c && c <= '\uD7FF' || '\uE000' <= c && c <= '\uFFFD') stringbuffer.append(c); if (stringbuffer.length() > s.length()) return stringbuffer.toString(); else return s; }
private Object string() { buf.setLength(0); while (c != '"' && c != StringCharacterIterator.DONE) { if (c == '\\') { next(); if (c == 'u') { add(unicode()); } else { Object value = escapes.get(new Character(c)); if (value != null) { add(((Character) value).charValue()); } } } else { add(); } } // unterminated string will terminate automatically next(); return buf.toString(); }
public static String escapeNonCustomRegex(String path) { /* * TODO replace with a regular expression */ StringBuilder sb = new StringBuilder(); boolean inCustomRegion = false; CharacterIterator it = new StringCharacterIterator(path); for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { if (ch == CUSTOM_REGEX_START) { inCustomRegion = true; } else if (ch == CUSTOM_REGEX_END) { inCustomRegion = false; } if (REGEX_SPECIAL_CHARS.contains(ch) && !inCustomRegion) { sb.append('\\'); } sb.append(ch); } return sb.toString(); }
/** * 根据转义列表对字符串进行转义。. * * @param source * 待转义的字符串 * @param escapeCharMap * 转义列表 * @return 转义后的字符串 */ public static String escapeCharacter(String source, HashMap escapeCharMap) { if (source == null || source.length() == 0) return source; if (escapeCharMap.size() == 0) return source; StringBuffer sb = new StringBuffer(); StringCharacterIterator sci = new StringCharacterIterator(source); for (char c = sci.first(); c != StringCharacterIterator.DONE; c = sci.next()) { String character = String.valueOf(c); if (escapeCharMap.containsKey(character)) character = (String) escapeCharMap.get(character); sb.append(character); } return sb.toString(); }
private boolean literal(String text) { CharacterIterator ci = new StringCharacterIterator(text); char t = ci.first(); if (c != t) { return false; } int start = col; boolean ret = true; for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) { if (t != nextCharacter()) { ret = false; break; } } nextCharacter(); if (!ret) { error("literal " + text, start); } return ret; }
private boolean valid(String input) { if ("".equals(input)) { return true; } boolean ret = true; it = new StringCharacterIterator(input); c = it.first(); col = 1; if (!value()) { ret = error("value", 1); } else { skipWhiteSpace(); if (c != CharacterIterator.DONE) { ret = error("end", col); } } return ret; }
private StringBuffer imEscape(String s) { StringBuffer stringbuffer = new StringBuffer(); StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s); for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) { switch (c) { case 34: // '"' case 36: // '$' case 40: // '(' case 41: // ')' case 91: // '[' case 92: // '\\' case 93: // ']' case 123: // '{' case 125: // '}' stringbuffer.append("\\"); break; } stringbuffer.append(c); } stringbuffer.append("\"\0"); return stringbuffer; }
/** * Parse @serializedPath to * * @param serializedPath {@link LapPath} in serialized form, e.g. * /P:0/DC:0/DE:1/S:1 * @return Path object created according to @serializedPath */ public static LapPath parse(String serializedPath) throws ParseException { CharacterIterator iter = new StringCharacterIterator(serializedPath); List<Link> parsedLinks = new LinkedList<Link>(); do { parsedLinks.add(parseLink(iter)); } while (iter.current() != CharacterIterator.DONE); return new LapPath(parsedLinks); }